From dbeec0a5de248a7c87a5436d430e2d45fb3c878a Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 10 Nov 2014 16:53:52 +0100 Subject: [PATCH] DATAREST-227 - Renamed WebTestUtils to TestMvcClient. Renmaed WebTestUtils to TestMvcClient, added assertions to constructor and fixed formatting of JavaDoc. Original pull request: #149. --- .../AbstractControllerIntegrationTests.java | 2 +- .../webmvc/AbstractWebIntegrationTests.java | 22 ++--- .../data/rest/webmvc/CommonWebTests.java | 36 +++---- .../RepositoryControllerIntegrationTests.java | 2 +- ...itoryEntityControllerIntegrationTests.java | 2 +- ...itorySearchControllerIntegrationTests.java | 2 +- .../data/rest/webmvc/TestMvcClient.java | 33 ++++--- .../data/rest/webmvc/jpa/JpaWebTests.java | 94 +++++++++---------- .../rest/webmvc/mongodb/MongoWebTests.java | 26 ++--- .../data/rest/webmvc/neo4j/Neo4jWebTests.java | 6 +- .../support/RepositoryLinkBuildUnitTests.java | 4 +- 11 files changed, 119 insertions(+), 110 deletions(-) diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractControllerIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractControllerIntegrationTests.java index c06d90d12..5ba242eef 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractControllerIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractControllerIntegrationTests.java @@ -64,7 +64,7 @@ public abstract class AbstractControllerIntegrationTests { @Before public void initWebInfrastructure() { - WebTestUtils.initWebTest(); + TestMvcClient.initWebTest(); } /** diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java index 7da993978..9b276b00b 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java @@ -51,8 +51,8 @@ import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.JsonPath; /** - * A test harness for hypermedia unit/integration testing. Provides chained operations (like postAndGet) to create - * a new entity and then retrieve it with a single method call. It also provides often-used assertions (like + * A test harness for hypermedia unit/integration testing. Provides chained operations (like postAndGet) to create a new + * entity and then retrieve it with a single method call. It also provides often-used assertions (like * assertJsonPathEquals). * * @author Oliver Gierke @@ -68,15 +68,15 @@ public abstract class AbstractWebIntegrationTests { @Autowired WebApplicationContext context; @Autowired LinkDiscoverers discoverers; - protected WebTestUtils testUtils; + protected TestMvcClient client; protected MockMvc mvc; @Before public void setUp() { - mvc = MockMvcBuilders.webAppContextSetup(context).// - defaultRequest(get("/").accept(WebTestUtils.DEFAULT_MEDIA_TYPE)).build(); - testUtils = new WebTestUtils(mvc, discoverers); + this.mvc = MockMvcBuilders.webAppContextSetup(context).// + defaultRequest(get("/").accept(TestMvcClient.DEFAULT_MEDIA_TYPE)).build(); + this.client = new TestMvcClient(mvc, discoverers); } protected MockHttpServletResponse postAndGet(Link link, Object payload, MediaType mediaType) throws Exception { @@ -94,7 +94,7 @@ public abstract class AbstractWebIntegrationTests { return response; } - return testUtils.request(response.getHeader("Location")); + return client.request(response.getHeader("Location")); } protected MockHttpServletResponse putAndGet(Link link, Object payload, MediaType mediaType) throws Exception { @@ -105,7 +105,7 @@ public abstract class AbstractWebIntegrationTests { andExpect(status().is(both(greaterThanOrEqualTo(200)).and(lessThan(300)))).// andReturn().getResponse(); - return StringUtils.hasText(response.getContentAsString()) ? response : testUtils.request(link); + return StringUtils.hasText(response.getContentAsString()) ? response : client.request(link); } protected MockHttpServletResponse patchAndGet(Link link, Object payload, MediaType mediaType) throws Exception { @@ -117,7 +117,7 @@ public abstract class AbstractWebIntegrationTests { andExpect(status().isNoContent()).// andReturn().getResponse(); - return StringUtils.hasText(response.getContentAsString()) ? response : testUtils.request(href); + return StringUtils.hasText(response.getContentAsString()) ? response : client.request(href); } protected void deleteAndVerify(Link link) throws Exception { @@ -167,7 +167,7 @@ public abstract class AbstractWebIntegrationTests { protected void assertDoesNotHaveLinkWithRel(String rel, MockHttpServletResponse response) throws Exception { String content = response.getContentAsString(); - Link link = testUtils.getDiscoverer(response).findLinkWithRel(rel, content); + Link link = client.getDiscoverer(response).findLinkWithRel(rel, content); assertThat("Expected not to find link with rel " + rel + " but found " + link + "!", link, is(nullValue())); } @@ -222,7 +222,7 @@ public abstract class AbstractWebIntegrationTests { String s = response.getContentAsString(); assertThat("Expected not to find link with rel " + rel + " but found one in " + s, // - testUtils.getDiscoverer(response).findLinkWithRel(rel, s), nullValue()); + client.getDiscoverer(response).findLinkWithRel(rel, s), nullValue()); } }; } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java index 72c805e71..ef6a41446 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java @@ -53,10 +53,10 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { @Test public void exposesRootResource() throws Exception { - ResultActions actions = mvc.perform(get("/").accept(WebTestUtils.DEFAULT_MEDIA_TYPE)).andExpect(status().isOk()); + ResultActions actions = mvc.perform(get("/").accept(TestMvcClient.DEFAULT_MEDIA_TYPE)).andExpect(status().isOk()); for (String rel : expectedRootLinkRels()) { - actions.andExpect(webTestUtils.hasLinkWithRel(rel)); + actions.andExpect(client.hasLinkWithRel(rel)); } } @@ -66,14 +66,14 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { @Test public void exposesSchemasForResourcesExposed() throws Exception { - MockHttpServletResponse response = webTestUtils.request("/"); + MockHttpServletResponse response = client.request("/"); for (String rel : expectedRootLinkRels()) { - Link link = webTestUtils.assertHasLinkWithRel(rel, response); + Link link = client.assertHasLinkWithRel(rel, response); // Resource - webTestUtils.request(link); + client.request(link); // Schema - TODO:Improve by using hypermedia mvc.perform(get(link.expand().getHref() + "/schema").// @@ -110,16 +110,16 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { @Test public void exposesSearchesForRootResources() throws Exception { - MockHttpServletResponse response = webTestUtils.request("/"); + MockHttpServletResponse response = client.request("/"); for (String rel : expectedRootLinkRels()) { - Link link = webTestUtils.assertHasLinkWithRel(rel, response); - String rootResourceRepresentation = webTestUtils.request(link).getContentAsString(); - Link searchLink = webTestUtils.getDiscoverer(response).findLinkWithRel("search", rootResourceRepresentation); + Link link = client.assertHasLinkWithRel(rel, response); + String rootResourceRepresentation = client.request(link).getContentAsString(); + Link searchLink = client.getDiscoverer(response).findLinkWithRel("search", rootResourceRepresentation); if (searchLink != null) { - webTestUtils.request(searchLink); + client.request(searchLink); } } } @@ -130,7 +130,7 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { Map payloads = getPayloadToPost(); assumeFalse(payloads.isEmpty()); - MockHttpServletResponse response = webTestUtils.request("/"); + MockHttpServletResponse response = client.request("/"); for (String rel : expectedRootLinkRels()) { @@ -138,7 +138,7 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { if (payload != null) { - Link link = webTestUtils.assertHasLinkWithRel(rel, response); + Link link = client.assertHasLinkWithRel(rel, response); String target = link.expand().getHref(); MockHttpServletRequestBuilder request = post(target).// @@ -157,12 +157,12 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { @Test public void accessLinkedResources() throws Exception { - MockHttpServletResponse rootResource = webTestUtils.request("/"); + MockHttpServletResponse rootResource = client.request("/"); for (Map.Entry> linked : getRootAndLinkedResources().entrySet()) { - Link resourceLink = webTestUtils.assertHasLinkWithRel(linked.getKey(), rootResource); - MockHttpServletResponse resource = webTestUtils.request(resourceLink); + Link resourceLink = client.assertHasLinkWithRel(linked.getKey(), rootResource); + MockHttpServletResponse resource = client.request(resourceLink); for (String linkedRel : linked.getValue()) { @@ -173,7 +173,7 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { for (Object href : uris) { - webTestUtils.follow(href.toString()). // + client.follow(href.toString()). // andExpect(status().isOk()); } } @@ -188,8 +188,8 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { MediaType ALPS_MEDIA_TYPE = MediaType.valueOf("application/alps+json"); - MockHttpServletResponse response = webTestUtils.request("/"); - Link profileLink = webTestUtils.assertHasLinkWithRel("profile", response); + MockHttpServletResponse response = client.request("/"); + Link profileLink = client.assertHasLinkWithRel("profile", response); mvc.perform(// get(profileLink.expand().getHref()).// diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryControllerIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryControllerIntegrationTests.java index 2ee4ce3fc..4e74f3488 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryControllerIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryControllerIntegrationTests.java @@ -17,7 +17,7 @@ package org.springframework.data.rest.webmvc; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -import static org.springframework.data.rest.webmvc.WebTestUtils.*; +import static org.springframework.data.rest.webmvc.TestMvcClient.*; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java index 7d2e56c87..31b258644 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java @@ -17,7 +17,7 @@ package org.springframework.data.rest.webmvc; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -import static org.springframework.data.rest.webmvc.WebTestUtils.*; +import static org.springframework.data.rest.webmvc.TestMvcClient.*; import static org.springframework.http.HttpMethod.*; import java.util.List; diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java index 96f3e4178..8e8f3a971 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java @@ -17,7 +17,7 @@ package org.springframework.data.rest.webmvc; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -import static org.springframework.data.rest.webmvc.WebTestUtils.*; +import static org.springframework.data.rest.webmvc.TestMvcClient.*; import org.junit.Before; import org.junit.Test; diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/TestMvcClient.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/TestMvcClient.java index 0ecb83456..abd8372df 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/TestMvcClient.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/TestMvcClient.java @@ -35,6 +35,7 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.ResultMatcher; +import org.springframework.util.Assert; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -44,14 +45,24 @@ import org.springframework.web.context.request.ServletRequestAttributes; * @author Oliver Gierke * @author Greg Turnquist */ -public class WebTestUtils { +public class TestMvcClient { public static MediaType DEFAULT_MEDIA_TYPE = org.springframework.hateoas.MediaTypes.HAL_JSON; private final MockMvc mvc; private final LinkDiscoverers discoverers; - public WebTestUtils(MockMvc mvc, LinkDiscoverers discoverers) { + /** + * Creates a new {@link TestMvcClient} for the given {@link MockMvc} and {@link LinkDiscoverers}. + * + * @param mvc must not be {@literal null}. + * @param discoverers must not be {@literal null}. + */ + public TestMvcClient(MockMvc mvc, LinkDiscoverers discoverers) { + + Assert.notNull(mvc, "MockMvc must not be null!"); + Assert.notNull(discoverers, "LinkDiscoverers must not be null!"); + this.mvc = mvc; this.discoverers = discoverers; } @@ -75,8 +86,8 @@ public class WebTestUtils { } /** - * Perform GET [href] with an explicit Accept media type using MockMvc. Verify the requests succeeded - * and also came back as the Accept type. + * Perform GET [href] with an explicit Accept media type using MockMvc. Verify the requests succeeded and also came + * back as the Accept type. * * @param href * @param contentType @@ -91,8 +102,8 @@ public class WebTestUtils { } /** - * Convenience wrapper that first expands the link using URI substitution before requesting with the - * default media type. + * Convenience wrapper that first expands the link using URI substitution before requesting with the default media + * type. * * @param link * @return @@ -103,8 +114,8 @@ public class WebTestUtils { } /** - * Convenience wrapper that first expands the link using URI substitution and then GET [href] using - * an explicit media type + * Convenience wrapper that first expands the link using URI substitution and then GET [href] using an explicit media + * type * * @param link * @param mediaType @@ -161,6 +172,7 @@ public class WebTestUtils { /** * Discover single URI associated with a rel, starting at the root node ("/") + * * @param rel * @return * @throws Exception @@ -210,8 +222,7 @@ public class WebTestUtils { } /** - * For a given servlet response, verify that the provided rel exists in its hypermedia. If so, - * return the URI link. + * For a given servlet response, verify that the provided rel exists in its hypermedia. If so, return the URI link. * * @param rel * @param response @@ -267,6 +278,4 @@ public class WebTestUtils { return linkDiscovererFor; } - - } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java index b9636f967..ebe075c71 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java @@ -125,17 +125,17 @@ public class JpaWebTests extends CommonWebTests { @Test public void accessPersons() throws Exception { - MockHttpServletResponse response = testUtils.request("/people?page=0&size=1"); + MockHttpServletResponse response = client.request("/people?page=0&size=1"); - Link nextLink = testUtils.assertHasLinkWithRel(Link.REL_NEXT, response); + Link nextLink = client.assertHasLinkWithRel(Link.REL_NEXT, response); assertDoesNotHaveLinkWithRel(Link.REL_PREVIOUS, response); - response = testUtils.request(nextLink); - testUtils.assertHasLinkWithRel(Link.REL_PREVIOUS, response); - nextLink = testUtils.assertHasLinkWithRel(Link.REL_NEXT, response); + response = client.request(nextLink); + client.assertHasLinkWithRel(Link.REL_PREVIOUS, response); + nextLink = client.assertHasLinkWithRel(Link.REL_NEXT, response); - response = testUtils.request(nextLink); - testUtils.assertHasLinkWithRel(Link.REL_PREVIOUS, response); + response = client.request(nextLink); + client.assertHasLinkWithRel(Link.REL_PREVIOUS, response); assertDoesNotHaveLinkWithRel(Link.REL_NEXT, response); } @@ -145,13 +145,13 @@ public class JpaWebTests extends CommonWebTests { @Test public void exposesLinkForRelatedResource() throws Exception { - MockHttpServletResponse response = testUtils.request("/"); - Link ordersLink = testUtils.assertHasLinkWithRel("orders", response); + MockHttpServletResponse response = client.request("/"); + Link ordersLink = client.assertHasLinkWithRel("orders", response); - MockHttpServletResponse orders = testUtils.request(ordersLink); + MockHttpServletResponse orders = client.request(ordersLink); Link creatorLink = assertHasContentLinkWithRel("creator", orders); - assertThat(testUtils.request(creatorLink), is(notNullValue())); + assertThat(client.request(creatorLink), is(notNullValue())); } /** @@ -160,10 +160,10 @@ public class JpaWebTests extends CommonWebTests { @Test public void exposesInlinedEntities() throws Exception { - MockHttpServletResponse response = testUtils.request("/"); - Link ordersLink = testUtils.assertHasLinkWithRel("orders", response); + MockHttpServletResponse response = client.request("/"); + Link ordersLink = client.assertHasLinkWithRel("orders", response); - MockHttpServletResponse orders = testUtils.request(ordersLink); + MockHttpServletResponse orders = client.request(ordersLink); assertHasJsonPathValue("$..lineItems", orders); } @@ -185,7 +185,7 @@ public class JpaWebTests extends CommonWebTests { @Test public void createPersonThenVerifyIgnoredAttributesDontExist() throws Exception { - Link peopleLink = testUtils.discoverUnique("people"); + Link peopleLink = client.discoverUnique("people"); ObjectMapper mapper = new ObjectMapper(); Person frodo = new Person("Frodo", "Baggins"); frodo.setAge(77); @@ -208,12 +208,12 @@ public class JpaWebTests extends CommonWebTests { @Test public void createThenPatch() throws Exception { - Link peopleLink = testUtils.discoverUnique("people"); + Link peopleLink = client.discoverUnique("people"); MockHttpServletResponse bilbo = postAndGet(peopleLink, "{ \"firstName\" : \"Bilbo\", \"lastName\" : \"Baggins\" }", MediaType.APPLICATION_JSON); - Link bilboLink = testUtils.assertHasLinkWithRel("self", bilbo); + Link bilboLink = client.assertHasLinkWithRel("self", bilbo); assertThat((String) JsonPath.read(bilbo.getContentAsString(), "$.firstName"), is("Bilbo")); assertThat((String) JsonPath.read(bilbo.getContentAsString(), "$.lastName"), is("Baggins")); @@ -235,13 +235,13 @@ public class JpaWebTests extends CommonWebTests { @Test public void createThenPut() throws Exception { - Link peopleLink = testUtils.discoverUnique("people"); + Link peopleLink = client.discoverUnique("people"); MockHttpServletResponse bilbo = postAndGet(peopleLink,// "{ \"firstName\" : \"Bilbo\", \"lastName\" : \"Baggins\" }",// MediaType.APPLICATION_JSON); - Link bilboLink = testUtils.assertHasLinkWithRel("self", bilbo); + Link bilboLink = client.assertHasLinkWithRel("self", bilbo); assertThat((String) JsonPath.read(bilbo.getContentAsString(), "$.firstName"), equalTo("Bilbo")); assertThat((String) JsonPath.read(bilbo.getContentAsString(), "$.lastName"), equalTo("Baggins")); @@ -376,7 +376,7 @@ public class JpaWebTests extends CommonWebTests { @Test public void propertiesCanHaveNulls() throws Exception { - Link peopleLink = testUtils.discoverUnique("people"); + Link peopleLink = client.discoverUnique("people"); Person frodo = new Person(); frodo.setFirstName("Frodo"); @@ -396,39 +396,39 @@ public class JpaWebTests extends CommonWebTests { @Test public void putShouldWorkDespiteExistingLinks() throws Exception { - Link peopleLink = testUtils.discoverUnique("people"); + Link peopleLink = client.discoverUnique("people"); Person frodo = new Person("Frodo", "Baggins"); String frodoString = mapper.writeValueAsString(frodo); MockHttpServletResponse createdPerson = postAndGet(peopleLink, frodoString, MediaType.APPLICATION_JSON); - Link frodoLink = testUtils.assertHasLinkWithRel("self", createdPerson); + Link frodoLink = client.assertHasLinkWithRel("self", createdPerson); assertJsonPathEquals("$.firstName", "Frodo", createdPerson); String bilboWithFrodosLinks = createdPerson.getContentAsString().replace("Frodo", "Bilbo"); MockHttpServletResponse overwrittenResponse = putAndGet(frodoLink, bilboWithFrodosLinks, MediaType.APPLICATION_JSON); - testUtils.assertHasLinkWithRel("self", overwrittenResponse); + client.assertHasLinkWithRel("self", overwrittenResponse); assertJsonPathEquals("$.firstName", "Bilbo", overwrittenResponse); } private List preparePersonResources(Person primary, Person... persons) throws Exception { - Link peopleLink = testUtils.discoverUnique("people"); + Link peopleLink = client.discoverUnique("people"); List links = new ArrayList(); MockHttpServletResponse primaryResponse = postAndGet(peopleLink, mapper.writeValueAsString(primary), MediaType.APPLICATION_JSON); - links.add(testUtils.assertHasLinkWithRel("siblings", primaryResponse)); + links.add(client.assertHasLinkWithRel("siblings", primaryResponse)); for (Person person : persons) { String payload = mapper.writeValueAsString(person); MockHttpServletResponse response = postAndGet(peopleLink, payload, MediaType.APPLICATION_JSON); - links.add(testUtils.assertHasLinkWithRel(Link.REL_SELF, response)); + links.add(client.assertHasLinkWithRel(Link.REL_SELF, response)); } return links; @@ -440,7 +440,7 @@ public class JpaWebTests extends CommonWebTests { @Test public void doesNotAllowGetToCollectionResourceIfFindAllIsNotExported() throws Exception { - Link link = testUtils.discoverUnique("addresses"); + Link link = client.discoverUnique("addresses"); mvc.perform(get(link.getHref())).// andExpect(status().isMethodNotAllowed()); @@ -452,7 +452,7 @@ public class JpaWebTests extends CommonWebTests { @Test public void doesNotAllowPostToCollectionResourceIfSaveIsNotExported() throws Exception { - Link link = testUtils.discoverUnique("addresses"); + Link link = client.discoverUnique("addresses"); mvc.perform(post(link.getHref()).content("{}").contentType(MediaType.APPLICATION_JSON)).// andExpect(status().isMethodNotAllowed()); @@ -467,9 +467,9 @@ public class JpaWebTests extends CommonWebTests { @Test public void returnsProjectionIfRequested() throws Exception { - Link orders = testUtils.discoverUnique("orders"); + Link orders = client.discoverUnique("orders"); - MockHttpServletResponse response = testUtils.request(orders); + MockHttpServletResponse response = client.request(orders); Link orderLink = assertContentLinkWithRel("self", response, true).expand(); UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(orderLink.getHref()); @@ -497,18 +497,18 @@ public class JpaWebTests extends CommonWebTests { @Test public void onlyLinksShouldAppearWhenExecuteSearchCompact() throws Exception { - Link peopleLink = testUtils.discoverUnique("people"); + Link peopleLink = client.discoverUnique("people"); Person daenerys = new Person("Daenerys", "Targaryen"); String daenerysString = mapper.writeValueAsString(daenerys); MockHttpServletResponse createdPerson = postAndGet(peopleLink, daenerysString, MediaType.APPLICATION_JSON); - Link daenerysLink = testUtils.assertHasLinkWithRel("self", createdPerson); + Link daenerysLink = client.assertHasLinkWithRel("self", createdPerson); assertJsonPathEquals("$.firstName", "Daenerys", createdPerson); - Link searchLink = testUtils.discoverUnique(peopleLink, "search"); - Link byFirstNameLink = testUtils.discoverUnique(searchLink, "findFirstPersonByFirstName"); + Link searchLink = client.discoverUnique(peopleLink, "search"); + Link byFirstNameLink = client.discoverUnique(searchLink, "findFirstPersonByFirstName"); - MockHttpServletResponse response = testUtils.request(byFirstNameLink.expand("Daenerys"), + MockHttpServletResponse response = client.request(byFirstNameLink.expand("Daenerys"), MediaType.parseMediaType("application/x-spring-data-compact+json")); String responseBody = response.getContentAsString(); @@ -526,9 +526,9 @@ public class JpaWebTests extends CommonWebTests { @Test public void rendersExcerptProjectionsCorrectly() throws Exception { - Link authorsLink = testUtils.discoverUnique("authors"); + Link authorsLink = client.discoverUnique("authors"); - MockHttpServletResponse response = testUtils.request(authorsLink); + MockHttpServletResponse response = client.request(authorsLink); String firstAuthorPath = "$._embedded.authors[0]"; // Has main content @@ -542,7 +542,7 @@ public class JpaWebTests extends CommonWebTests { String content = response.getContentAsString(); String href = JsonPath.read(content, firstAuthorPath.concat("._links.self.href")); - testUtils.follow(new Link(href)).andExpect(testUtils.hasLinkWithRel("books")); + client.follow(new Link(href)).andExpect(client.hasLinkWithRel("books")); } /** @@ -551,7 +551,7 @@ public class JpaWebTests extends CommonWebTests { @Test public void returns404WhenTryingToDeleteANonExistingResource() throws Exception { - Link authorsLink = testUtils.discoverUnique("authors"); + Link authorsLink = client.discoverUnique("authors"); mvc.perform(delete(authorsLink.getHref().concat("/{id}"), 4711)).// andExpect(status().isNotFound()); @@ -563,20 +563,20 @@ public class JpaWebTests extends CommonWebTests { @Test public void execturesSearchThatTakesASort() throws Exception { - Link booksLink = testUtils.discoverUnique("books"); - Link searchLink = testUtils.discoverUnique(booksLink, "search"); - Link findBySortedLink = testUtils.discoverUnique(searchLink, "find-by-sorted"); + Link booksLink = client.discoverUnique("books"); + Link searchLink = client.discoverUnique(booksLink, "search"); + Link findBySortedLink = client.discoverUnique(searchLink, "find-by-sorted"); // Assert sort options advertised assertThat(findBySortedLink.isTemplated(), is(true)); assertThat(findBySortedLink.getVariableNames(), contains("sort")); // Assert results returned as specified - testUtils.follow(findBySortedLink.expand("title,desc")).// + client.follow(findBySortedLink.expand("title,desc")).// andExpect(jsonPath("$._embedded.books[0].title").value("Spring Data (Second Edition)")).// andExpect(jsonPath("$._embedded.books[1].title").value("Spring Data")); - testUtils.follow(findBySortedLink.expand("title,asc")).// + client.follow(findBySortedLink.expand("title,asc")).// andExpect(jsonPath("$._embedded.books[0].title").value("Spring Data")).// andExpect(jsonPath("$._embedded.books[1].title").value("Spring Data (Second Edition)")); } @@ -590,7 +590,7 @@ public class JpaWebTests extends CommonWebTests { */ private void assertSiblingNames(Link link, String... siblingNames) throws Exception { - String responseBody = testUtils.request(link).getContentAsString(); + String responseBody = client.request(link).getContentAsString(); List persons = JsonPath.read(responseBody, "$._embedded.people[*].firstName"); assertThat(persons, hasSize(siblingNames.length)); @@ -599,7 +599,7 @@ public class JpaWebTests extends CommonWebTests { private void assertPersonWithNameAndSiblingLink(String name) throws Exception { - MockHttpServletResponse response = testUtils.request(testUtils.discoverUnique("people")); + MockHttpServletResponse response = client.request(client.discoverUnique("people")); String jsonPath = String.format("$._embedded.people[?(@.firstName == '%s')][0]", name); @@ -610,7 +610,7 @@ public class JpaWebTests extends CommonWebTests { // Assert sibling link exposed in resource pointed to Link selfLink = new Link(JsonPath. read(john, "$._links.self.href")); - testUtils.follow(selfLink).// + client.follow(selfLink).// andExpect(status().isOk()).// andExpect(jsonPath("$._links.siblings", is(notNullValue()))); } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java index 7e7e36498..fba1421e1 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java @@ -89,17 +89,17 @@ public class MongoWebTests extends CommonWebTests { @Test public void foo() throws Exception { - Link profileLink = testUtils.discoverUnique("profiles"); - testUtils.follow(profileLink).// + Link profileLink = client.discoverUnique("profiles"); + client.follow(profileLink).// andExpect(jsonPath("$._embedded.profiles").value(hasSize(2))); } @Test public void rendersEmbeddedDocuments() throws Exception { - Link usersLink = testUtils.discoverUnique("users"); - Link userLink = assertHasContentLinkWithRel("self", testUtils.request(usersLink)); - testUtils.follow(userLink).// + Link usersLink = client.discoverUnique("users"); + Link userLink = assertHasContentLinkWithRel("self", client.request(usersLink)); + client.follow(userLink).// andExpect(jsonPath("$.address.zipCode").value(is(notNullValue()))); } @@ -109,22 +109,22 @@ public class MongoWebTests extends CommonWebTests { @Test public void executeQueryMethodWithPrimitiveReturnType() throws Exception { - Link profiles = testUtils.discoverUnique("profiles"); - Link profileSearches = testUtils.discoverUnique(profiles, "search"); - Link countByTypeLink = testUtils.discoverUnique(profileSearches, "countByType"); + Link profiles = client.discoverUnique("profiles"); + Link profileSearches = client.discoverUnique(profiles, "search"); + Link countByTypeLink = client.discoverUnique(profileSearches, "countByType"); assertThat(countByTypeLink.isTemplated(), is(true)); assertThat(countByTypeLink.getVariableNames(), hasItem("type")); - MockHttpServletResponse response = testUtils.request(countByTypeLink.expand("Twitter")); + MockHttpServletResponse response = client.request(countByTypeLink.expand("Twitter")); assertThat(response.getContentAsString(), is("1")); } @Test public void testname() throws Exception { - Link usersLink = testUtils.discoverUnique("users"); - Link userLink = assertHasContentLinkWithRel("self", testUtils.request(usersLink)); + Link usersLink = client.discoverUnique("users"); + Link userLink = assertHasContentLinkWithRel("self", client.request(usersLink)); MockHttpServletResponse response = patchAndGet(userLink, "{\"lastname\" : null, \"address\" : { \"zipCode\" : \"ZIP\"}}", MediaType.APPLICATION_JSON); @@ -136,8 +136,8 @@ public class MongoWebTests extends CommonWebTests { @Test public void testname2() throws Exception { - Link usersLink = testUtils.discoverUnique("users"); - Link userLink = assertHasContentLinkWithRel("self", testUtils.request(usersLink)); + Link usersLink = client.discoverUnique("users"); + Link userLink = assertHasContentLinkWithRel("self", client.request(usersLink)); MockHttpServletResponse response = patchAndGet(userLink, "[{ \"op\": \"replace\", \"path\": \"/address/zipCode\", \"value\": \"ZIP\" }," diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/neo4j/Neo4jWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/neo4j/Neo4jWebTests.java index 38531268e..d207732db 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/neo4j/Neo4jWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/neo4j/Neo4jWebTests.java @@ -86,13 +86,13 @@ public class Neo4jWebTests extends CommonWebTests { public void deletesCustomer() throws Exception { // Lookup customer - Link customers = testUtils.discoverUnique("customers"); - Link customerLink = assertHasContentLinkWithRel("self", testUtils.request(customers)); + Link customers = client.discoverUnique("customers"); + Link customerLink = assertHasContentLinkWithRel("self", client.request(customers)); // Delete customer mvc.perform(delete(customerLink.getHref())); // Assert no customers anymore - assertDoesNotHaveContentLinkWithRel("self", testUtils.request(customers)); + assertDoesNotHaveContentLinkWithRel("self", client.request(customers)); } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryLinkBuildUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryLinkBuildUnitTests.java index 80ff8480a..81963beba 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryLinkBuildUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryLinkBuildUnitTests.java @@ -24,7 +24,7 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.rest.core.mapping.MappingResourceMetadata; import org.springframework.data.rest.core.mapping.ResourceMetadata; import org.springframework.data.rest.webmvc.BaseUri; -import org.springframework.data.rest.webmvc.WebTestUtils; +import org.springframework.data.rest.webmvc.TestMvcClient; import org.springframework.data.rest.webmvc.mongodb.Profile; import org.springframework.hateoas.Link; @@ -43,7 +43,7 @@ public class RepositoryLinkBuildUnitTests { @Test public void usesCurrentRequestsUriBaseForRelativeBaseUri() { - WebTestUtils.initWebTest(); + TestMvcClient.initWebTest(); assertRootUriFor("api", "http://localhost/api/profile"); }