DATAREST-1341 - Further API adaption for Spring HATEOAS 1.0.

This commit is contained in:
Oliver Drotbohm
2019-02-15 11:05:05 +01:00
parent ce321da807
commit 554d6cb27b
44 changed files with 423 additions and 401 deletions

View File

@@ -33,7 +33,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.tests.CommonWebTests;
import org.springframework.data.rest.webmvc.RestMediaTypes;
import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkRelation;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
@@ -106,8 +108,8 @@ public class MongoWebTests extends CommonWebTests {
* @see org.springframework.data.rest.webmvc.AbstractWebIntegrationTests#expectedRootLinkRels()
*/
@Override
protected Iterable<String> expectedRootLinkRels() {
return Arrays.asList("profiles", "users");
protected Iterable<LinkRelation> expectedRootLinkRels() {
return LinkRelation.manyOf("profiles", "users");
}
@Test
@@ -122,7 +124,7 @@ public class MongoWebTests extends CommonWebTests {
public void rendersEmbeddedDocuments() throws Exception {
Link usersLink = client.discoverUnique("users");
Link userLink = assertHasContentLinkWithRel("self", client.request(usersLink));
Link userLink = assertHasContentLinkWithRel(IanaLinkRelations.SELF, client.request(usersLink));
client.follow(userLink).//
andExpect(jsonPath("$.address.zipCode").value(is(notNullValue())));
}
@@ -145,7 +147,7 @@ public class MongoWebTests extends CommonWebTests {
public void testname() throws Exception {
Link usersLink = client.discoverUnique("users");
Link userLink = assertHasContentLinkWithRel("self", client.request(usersLink));
Link userLink = assertHasContentLinkWithRel(IanaLinkRelations.SELF, client.request(usersLink));
MockHttpServletResponse response = patchAndGet(userLink,
"{\"lastname\" : null, \"address\" : { \"zipCode\" : \"ZIP\"}}",
@@ -159,7 +161,7 @@ public class MongoWebTests extends CommonWebTests {
public void testname2() throws Exception {
Link usersLink = client.discoverUnique("users");
Link userLink = assertHasContentLinkWithRel("self", client.request(usersLink));
Link userLink = assertHasContentLinkWithRel(IanaLinkRelations.SELF, client.request(usersLink));
MockHttpServletResponse response = patchAndGet(userLink,
"[{ \"op\": \"replace\", \"path\": \"/address/zipCode\", \"value\": \"ZIP\" },"
@@ -183,7 +185,7 @@ public class MongoWebTests extends CommonWebTests {
String stringReceipt = mapper.writeValueAsString(receipt);
MockHttpServletResponse createdReceipt = postAndGet(receiptLink, stringReceipt, MediaType.APPLICATION_JSON);
Link tacosLink = client.assertHasLinkWithRel("self", createdReceipt);
Link tacosLink = client.assertHasLinkWithRel(IanaLinkRelations.SELF, createdReceipt);
assertJsonPathEquals("$.saleItem", "Springy Tacos", createdReceipt);
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(tacosLink.getHref());
@@ -213,7 +215,7 @@ public class MongoWebTests extends CommonWebTests {
public void putDoesNotRemoveAssociations() throws Exception {
Link usersLink = client.discoverUnique("users");
Link userLink = assertHasContentLinkWithRel("self", client.request(usersLink));
Link userLink = assertHasContentLinkWithRel(IanaLinkRelations.SELF, client.request(usersLink));
Link colleaguesLink = client.assertHasLinkWithRel("colleagues", client.request(userLink));
// Expect a user returned as colleague
@@ -236,7 +238,7 @@ public class MongoWebTests extends CommonWebTests {
public void emptiesAssociationForEmptyUriList() throws Exception {
Link usersLink = client.discoverUnique("users");
Link userLink = assertHasContentLinkWithRel("self", client.request(usersLink));
Link userLink = assertHasContentLinkWithRel(IanaLinkRelations.SELF, client.request(usersLink));
Link colleaguesLink = client.assertHasLinkWithRel("colleagues", client.request(userLink));
putAndGet(colleaguesLink, "", MediaType.parseMediaType("text/uri-list"));
@@ -250,7 +252,7 @@ public class MongoWebTests extends CommonWebTests {
public void updatesMapPropertyCorrectly() throws Exception {
Link profilesLink = client.discoverUnique("profiles");
Link profileLink = assertHasContentLinkWithRel("self", client.request(profilesLink));
Link profileLink = assertHasContentLinkWithRel(IanaLinkRelations.SELF, client.request(profilesLink));
Profile profile = new Profile();
profile.setMetadata(Collections.singletonMap("Key", "Value"));
@@ -272,7 +274,9 @@ public class MongoWebTests extends CommonWebTests {
MockHttpServletResponse response = postAndGet(receiptsLink, mapper.writeValueAsString(receipt),
MediaType.APPLICATION_JSON);
Link receiptLink = client.getDiscoverer(response).findLinkWithRel("self", response.getContentAsString());
Link receiptLink = client.getDiscoverer(response) //
.findLinkWithRel(IanaLinkRelations.SELF, response.getContentAsString()) //
.orElseThrow(() -> new IllegalStateException("Did not find self link!"));
mvc.perform(get(receiptLink.getHref()).header(IF_MODIFIED_SINCE, response.getHeader(LAST_MODIFIED))).//
andExpect(status().isNotModified()).//

View File

@@ -27,7 +27,6 @@ import org.mockito.internal.stubbing.answers.ReturnsArgumentAt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.rest.core.support.DefaultSelfLinkProvider;
import org.springframework.data.rest.core.support.EntityLookup;
import org.springframework.data.rest.tests.AbstractControllerIntegrationTests;
import org.springframework.data.rest.tests.AbstractControllerIntegrationTests.TestConfiguration;
import org.springframework.data.rest.tests.mongodb.MongoDbRepositoryConfig;
@@ -35,6 +34,7 @@ import org.springframework.data.rest.tests.mongodb.User;
import org.springframework.data.rest.webmvc.mapping.Associations;
import org.springframework.data.rest.webmvc.support.Projector;
import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Links;
import org.springframework.test.context.ContextConfiguration;
@@ -58,17 +58,17 @@ public class PersistentEntityResourceAssemblerIntegrationTests extends AbstractC
when(projector.projectExcerpt(any())).thenAnswer(new ReturnsArgumentAt(0));
PersistentEntityResourceAssembler assembler = new PersistentEntityResourceAssembler(entities, projector,
associations, new DefaultSelfLinkProvider(entities, entityLinks, Collections.<EntityLookup<?>> emptyList()));
associations, new DefaultSelfLinkProvider(entities, entityLinks, Collections.emptyList()));
User user = new User();
user.id = BigInteger.valueOf(4711);
PersistentEntityResource resource = assembler.toResource(user);
Links links = new Links(resource.getLinks());
Links links = resource.getLinks();
assertThat(links).hasSize(2);
assertThat(links.getLink("self").orElseThrow(() -> new RuntimeException("Unable to find 'self' link")).getVariables()).isEmpty();
assertThat(links.getLink("user").orElseThrow(() -> new RuntimeException("Unable to find 'user' link")).getVariableNames()).contains("projection");
assertThat(links.getRequiredLink(IanaLinkRelations.SELF).getVariables()).isEmpty();
assertThat(links.getRequiredLink("user").getVariableNames()).contains("projection");
}
}