From e304e14527e6f652fcb2d41d4d5e6fff74fd99d5 Mon Sep 17 00:00:00 2001 From: Greg Turnquist Date: Mon, 15 Sep 2014 14:32:12 -0500 Subject: [PATCH] DATAREST-227 - Broke up AbstractWebIntegrationTest and its subclasses. Decouple the test machinery of this class from the barrage of common test cases used against the various data stores. This way, other test suites that use the same integration approach don't have to be a part of this class hierarchy. Also move utilities and assertion methods into separate utility classes to slim down the class hierarchy. Original pull request: #149. --- .../webmvc/AbstractWebIntegrationTests.java | 275 +----------------- .../data/rest/webmvc/CommonWebTests.java | 201 +++++++++++++ .../data/rest/webmvc/TestMvcClient.java | 272 +++++++++++++++++ .../data/rest/webmvc/WebTestUtils.java | 52 ---- .../rest/webmvc/gemfire/GemfireWebTests.java | 4 +- .../data/rest/webmvc/jpa/JpaWebTests.java | 98 +++---- .../rest/webmvc/mongodb/MongoWebTests.java | 31 +- .../data/rest/webmvc/neo4j/Neo4jWebTests.java | 10 +- 8 files changed, 557 insertions(+), 386 deletions(-) create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/TestMvcClient.java delete mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/WebTestUtils.java 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 c3f7edc44..7da993978 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 @@ -17,26 +17,20 @@ package org.springframework.data.rest.webmvc; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -import static org.junit.Assume.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import java.util.Collections; -import java.util.List; import java.util.Map; -import java.util.Map.Entry; import net.minidev.json.JSONArray; import org.junit.Before; -import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; import org.springframework.hateoas.Link; -import org.springframework.hateoas.LinkDiscoverer; import org.springframework.hateoas.LinkDiscoverers; -import org.springframework.hateoas.MediaTypes; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletResponse; @@ -45,9 +39,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; 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.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.util.LinkedMultiValueMap; @@ -59,6 +51,10 @@ 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 + * assertJsonPathEquals). + * * @author Oliver Gierke * @author Greg Turnquist */ @@ -69,77 +65,18 @@ public abstract class AbstractWebIntegrationTests { private static final String CONTENT_LINK_JSONPATH = "$._embedded.._links.%s.href[0]"; - protected static MediaType DEFAULT_MEDIA_TYPE = org.springframework.hateoas.MediaTypes.HAL_JSON; - @Autowired WebApplicationContext context; @Autowired LinkDiscoverers discoverers; + protected WebTestUtils testUtils; protected MockMvc mvc; @Before public void setUp() { mvc = MockMvcBuilders.webAppContextSetup(context).// - defaultRequest(get("/").accept(DEFAULT_MEDIA_TYPE)).build(); - } - - protected MockHttpServletResponse request(String href, MediaType contentType) throws Exception { - return mvc.perform(get(href).accept(contentType)). // - andExpect(status().isOk()). // - andExpect(content().contentType(contentType)). // - andReturn().getResponse(); - } - - protected MockHttpServletResponse request(Link link) throws Exception { - return request(link.expand().getHref()); - } - - protected MockHttpServletResponse request(Link link, MediaType mediaType) throws Exception { - return request(link.expand().getHref(), mediaType); - } - - protected MockHttpServletResponse request(String href) throws Exception { - return request(href, DEFAULT_MEDIA_TYPE); - } - - protected ResultActions follow(Link link) throws Exception { - return follow(link.expand().getHref()); - } - - protected ResultActions follow(String href) throws Exception { - return mvc.perform(get(href)); - } - - protected List discover(String rel) throws Exception { - return discover(new Link("/"), rel); - } - - protected Link discoverUnique(String rel) throws Exception { - - List discover = discover(rel); - assertThat(discover, hasSize(1)); - return discover.get(0); - } - - protected List discover(Link root, String rel) throws Exception { - - MockHttpServletResponse response = mvc.perform(get(root.expand().getHref()).accept(DEFAULT_MEDIA_TYPE)).// - andExpect(status().isOk()).// - andExpect(hasLinkWithRel(rel)).// - andReturn().getResponse(); - - String s = response.getContentAsString(); - return getDiscoverer(response).findLinksWithRel(rel, s); - } - - protected Link discoverUnique(Link root, String rel) throws Exception { - - MockHttpServletResponse response = mvc.perform(get(root.expand().getHref()).accept(DEFAULT_MEDIA_TYPE)).// - andExpect(status().isOk()).// - andExpect(hasLinkWithRel(rel)).// - andReturn().getResponse(); - - return assertHasLinkWithRel(rel, response); + defaultRequest(get("/").accept(WebTestUtils.DEFAULT_MEDIA_TYPE)).build(); + testUtils = new WebTestUtils(mvc, discoverers); } protected MockHttpServletResponse postAndGet(Link link, Object payload, MediaType mediaType) throws Exception { @@ -157,7 +94,7 @@ public abstract class AbstractWebIntegrationTests { return response; } - return request(response.getHeader("Location")); + return testUtils.request(response.getHeader("Location")); } protected MockHttpServletResponse putAndGet(Link link, Object payload, MediaType mediaType) throws Exception { @@ -168,7 +105,7 @@ public abstract class AbstractWebIntegrationTests { andExpect(status().is(both(greaterThanOrEqualTo(200)).and(lessThan(300)))).// andReturn().getResponse(); - return StringUtils.hasText(response.getContentAsString()) ? response : request(link); + return StringUtils.hasText(response.getContentAsString()) ? response : testUtils.request(link); } protected MockHttpServletResponse patchAndGet(Link link, Object payload, MediaType mediaType) throws Exception { @@ -180,7 +117,7 @@ public abstract class AbstractWebIntegrationTests { andExpect(status().isNoContent()).// andReturn().getResponse(); - return StringUtils.hasText(response.getContentAsString()) ? response : request(href); + return StringUtils.hasText(response.getContentAsString()) ? response : testUtils.request(href); } protected void deleteAndVerify(Link link) throws Exception { @@ -196,17 +133,6 @@ public abstract class AbstractWebIntegrationTests { andExpect(status().isNotFound()); } - protected Link assertHasLinkWithRel(String rel, MockHttpServletResponse response) throws Exception { - - String content = response.getContentAsString(); - Link link = getDiscoverer(response).findLinkWithRel(rel, content); - - assertThat("Expected to find link with rel " + rel + " but found none in " + content + "!", link, - is(notNullValue())); - - return link; - } - protected Link assertHasContentLinkWithRel(String rel, MockHttpServletResponse response) throws Exception { return assertContentLinkWithRel(rel, response, true); } @@ -241,22 +167,11 @@ public abstract class AbstractWebIntegrationTests { protected void assertDoesNotHaveLinkWithRel(String rel, MockHttpServletResponse response) throws Exception { String content = response.getContentAsString(); - Link link = getDiscoverer(response).findLinkWithRel(rel, content); + Link link = testUtils.getDiscoverer(response).findLinkWithRel(rel, content); assertThat("Expected not to find link with rel " + rel + " but found " + link + "!", link, is(nullValue())); } - private LinkDiscoverer getDiscoverer(MockHttpServletResponse response) { - - String contentType = response.getContentType(); - LinkDiscoverer linkDiscovererFor = discoverers.getLinkDiscovererFor(contentType); - - assertThat("Did not find a LinkDiscoverer for returned media type " + contentType + "!", linkDiscovererFor, - is(notNullValue())); - - return linkDiscovererFor; - } - @SuppressWarnings("unchecked") protected T assertHasJsonPathValue(String path, MockHttpServletResponse response) throws Exception { @@ -296,22 +211,6 @@ public abstract class AbstractWebIntegrationTests { return jsonQueryResults; } - protected ResultMatcher hasLinkWithRel(final String rel) { - - return new ResultMatcher() { - - @Override - public void match(MvcResult result) throws Exception { - - MockHttpServletResponse response = result.getResponse(); - String s = response.getContentAsString(); - - assertThat("Expected to find link with rel " + rel + " but found none in " + s, // - getDiscoverer(response).findLinkWithRel(rel, s), notNullValue()); - } - }; - } - protected ResultMatcher doesNotHaveLinkWithRel(final String rel) { return new ResultMatcher() { @@ -323,161 +222,11 @@ public abstract class AbstractWebIntegrationTests { String s = response.getContentAsString(); assertThat("Expected not to find link with rel " + rel + " but found one in " + s, // - getDiscoverer(response).findLinkWithRel(rel, s), nullValue()); + testUtils.getDiscoverer(response).findLinkWithRel(rel, s), nullValue()); } }; } - // Root test cases - - @Test - public void exposesRootResource() throws Exception { - - ResultActions actions = mvc.perform(get("/").accept(DEFAULT_MEDIA_TYPE)).andExpect(status().isOk()); - - for (String rel : expectedRootLinkRels()) { - actions.andExpect(hasLinkWithRel(rel)); - } - } - - /** - * @see DATAREST-113 - */ - @Test - public void exposesSchemasForResourcesExposed() throws Exception { - - MockHttpServletResponse response = request("/"); - - for (String rel : expectedRootLinkRels()) { - - Link link = assertHasLinkWithRel(rel, response); - - // Resource - request(link); - - // Schema - TODO:Improve by using hypermedia - mvc.perform(get(link.expand().getHref() + "/schema").// - accept(MediaType.parseMediaType("application/schema+json"))).// - andExpect(status().isOk()); - } - } - - /** - * @see DATAREST-203 - */ - @Test - public void servesHalWhenRequested() throws Exception { - - mvc.perform(get("/")). // - andExpect(content().contentType(MediaTypes.HAL_JSON)). // - andExpect(jsonPath("$._links", notNullValue())); - } - - /** - * @see DATAREST-203 - */ - @Test - public void servesHalWhenJsonIsRequested() throws Exception { - - mvc.perform(get("/").accept(MediaType.APPLICATION_JSON)). // - andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)). // - andExpect(jsonPath("$._links", notNullValue())); - } - - /** - * @see DATAREST-203 - */ - @Test - public void exposesSearchesForRootResources() throws Exception { - - MockHttpServletResponse response = request("/"); - - for (String rel : expectedRootLinkRels()) { - - Link link = assertHasLinkWithRel(rel, response); - String rootResourceRepresentation = request(link).getContentAsString(); - Link searchLink = getDiscoverer(response).findLinkWithRel("search", rootResourceRepresentation); - - if (searchLink != null) { - request(searchLink); - } - } - } - - @Test - public void nic() throws Exception { - - Map payloads = getPayloadToPost(); - assumeFalse(payloads.isEmpty()); - - MockHttpServletResponse response = request("/"); - - for (String rel : expectedRootLinkRels()) { - - String payload = payloads.get(rel); - - if (payload != null) { - - Link link = assertHasLinkWithRel(rel, response); - String target = link.expand().getHref(); - - MockHttpServletRequestBuilder request = post(target).// - content(payload).// - contentType(MediaType.APPLICATION_JSON); - - mvc.perform(request). // - andExpect(status().isCreated()); - } - } - } - - /** - * @see DATAREST-198 - */ - @Test - public void accessLinkedResources() throws Exception { - - MockHttpServletResponse rootResource = request("/"); - - for (Entry> linked : getRootAndLinkedResources().entrySet()) { - - Link resourceLink = assertHasLinkWithRel(linked.getKey(), rootResource); - MockHttpServletResponse resource = request(resourceLink); - - for (String linkedRel : linked.getValue()) { - - // Find URIs pointing to linked resources - String jsonPath = String.format("$..%s._links.%s.href", linked.getKey(), linkedRel); - String representation = resource.getContentAsString(); - JSONArray uris = JsonPath.read(representation, jsonPath); - - for (Object href : uris) { - - follow(href.toString()). // - andExpect(status().isOk()); - } - } - } - } - - /** - * @see DATAREST-230 - */ - @Test - public void exposesDescriptionAsAlpsDocuments() throws Exception { - - MediaType ALPS_MEDIA_TYPE = MediaType.valueOf("application/alps+json"); - - MockHttpServletResponse response = request("/"); - Link profileLink = assertHasLinkWithRel("profile", response); - - mvc.perform(// - get(profileLink.expand().getHref()).// - accept(ALPS_MEDIA_TYPE)).// - andExpect(status().isOk()).// - andExpect(content().contentType(ALPS_MEDIA_TYPE)); - } - protected abstract Iterable expectedRootLinkRels(); protected Map getPayloadToPost() throws Exception { 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 new file mode 100644 index 000000000..72c805e71 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java @@ -0,0 +1,201 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.rest.webmvc; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assume.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import java.util.List; +import java.util.Map; + +import net.minidev.json.JSONArray; + +import org.junit.Test; +import org.springframework.hateoas.Link; +import org.springframework.hateoas.MediaTypes; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; + +import com.jayway.jsonpath.JsonPath; + +/** + * This class contains a common test suite used to verify multiple data stores with the same domain space. + * When verifying support of a new data store, it's good to start with extending this suite of tests. However, + * if the data store doesn't map well onto this, then a good alternative would be write a new test suite + * using {@link org.springframework.data.rest.webmvc.AbstractWebIntegrationTests AbstractWebIntegrationTests} as + * the test harness. + * + * @author Oliver Gierke + * @author Greg Turnquist + */ +public abstract class CommonWebTests extends AbstractWebIntegrationTests { + + // Root test cases + + @Test + public void exposesRootResource() throws Exception { + + ResultActions actions = mvc.perform(get("/").accept(WebTestUtils.DEFAULT_MEDIA_TYPE)).andExpect(status().isOk()); + + for (String rel : expectedRootLinkRels()) { + actions.andExpect(webTestUtils.hasLinkWithRel(rel)); + } + } + + /** + * @see DATAREST-113 + */ + @Test + public void exposesSchemasForResourcesExposed() throws Exception { + + MockHttpServletResponse response = webTestUtils.request("/"); + + for (String rel : expectedRootLinkRels()) { + + Link link = webTestUtils.assertHasLinkWithRel(rel, response); + + // Resource + webTestUtils.request(link); + + // Schema - TODO:Improve by using hypermedia + mvc.perform(get(link.expand().getHref() + "/schema").// + accept(MediaType.parseMediaType("application/schema+json"))).// + andExpect(status().isOk()); + } + } + + /** + * @see DATAREST-203 + */ + @Test + public void servesHalWhenRequested() throws Exception { + + mvc.perform(get("/")). // + andExpect(content().contentType(MediaTypes.HAL_JSON)). // + andExpect(jsonPath("$._links", notNullValue())); + } + + /** + * @see DATAREST-203 + */ + @Test + public void servesHalWhenJsonIsRequested() throws Exception { + + mvc.perform(get("/").accept(MediaType.APPLICATION_JSON)). // + andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)). // + andExpect(jsonPath("$._links", notNullValue())); + } + + /** + * @see DATAREST-203 + */ + @Test + public void exposesSearchesForRootResources() throws Exception { + + MockHttpServletResponse response = webTestUtils.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); + + if (searchLink != null) { + webTestUtils.request(searchLink); + } + } + } + + @Test + public void nic() throws Exception { + + Map payloads = getPayloadToPost(); + assumeFalse(payloads.isEmpty()); + + MockHttpServletResponse response = webTestUtils.request("/"); + + for (String rel : expectedRootLinkRels()) { + + String payload = payloads.get(rel); + + if (payload != null) { + + Link link = webTestUtils.assertHasLinkWithRel(rel, response); + String target = link.expand().getHref(); + + MockHttpServletRequestBuilder request = post(target).// + content(payload).// + contentType(MediaType.APPLICATION_JSON); + + mvc.perform(request). // + andExpect(status().isCreated()); + } + } + } + + /** + * @see DATAREST-198 + */ + @Test + public void accessLinkedResources() throws Exception { + + MockHttpServletResponse rootResource = webTestUtils.request("/"); + + for (Map.Entry> linked : getRootAndLinkedResources().entrySet()) { + + Link resourceLink = webTestUtils.assertHasLinkWithRel(linked.getKey(), rootResource); + MockHttpServletResponse resource = webTestUtils.request(resourceLink); + + for (String linkedRel : linked.getValue()) { + + // Find URIs pointing to linked resources + String jsonPath = String.format("$..%s._links.%s.href", linked.getKey(), linkedRel); + String representation = resource.getContentAsString(); + JSONArray uris = JsonPath.read(representation, jsonPath); + + for (Object href : uris) { + + webTestUtils.follow(href.toString()). // + andExpect(status().isOk()); + } + } + } + } + + /** + * @see DATAREST-230 + */ + @Test + public void exposesDescriptionAsAlpsDocuments() throws Exception { + + MediaType ALPS_MEDIA_TYPE = MediaType.valueOf("application/alps+json"); + + MockHttpServletResponse response = webTestUtils.request("/"); + Link profileLink = webTestUtils.assertHasLinkWithRel("profile", response); + + mvc.perform(// + get(profileLink.expand().getHref()).// + accept(ALPS_MEDIA_TYPE)).// + andExpect(status().isOk()).// + andExpect(content().contentType(ALPS_MEDIA_TYPE)); + } + +} 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 new file mode 100644 index 000000000..0ecb83456 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/TestMvcClient.java @@ -0,0 +1,272 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import java.util.List; + +import org.springframework.hateoas.Link; +import org.springframework.hateoas.LinkDiscoverer; +import org.springframework.hateoas.LinkDiscoverers; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +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.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +/** + * Helper methods for web integration testing. + * + * @author Oliver Gierke + * @author Greg Turnquist + */ +public class WebTestUtils { + + 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) { + this.mvc = mvc; + this.discoverers = discoverers; + } + + /** + * Initializes web tests. Will register a {@link MockHttpServletRequest} for the current thread. + */ + public static void initWebTest() { + + MockHttpServletRequest request = new MockHttpServletRequest(); + ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); + RequestContextHolder.setRequestAttributes(requestAttributes); + } + + public static void assertAllowHeaders(HttpEntity response, HttpMethod... methods) { + + HttpHeaders headers = response.getHeaders(); + + assertThat(headers.getAllow(), hasSize(methods.length)); + assertThat(headers.getAllow(), hasItems(methods)); + } + + /** + * 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 + * @return a mocked servlet response with results from GET [href] + * @throws Exception + */ + public MockHttpServletResponse request(String href, MediaType contentType) throws Exception { + return mvc.perform(get(href).accept(contentType)). // + andExpect(status().isOk()). // + andExpect(content().contentType(contentType)). // + andReturn().getResponse(); + } + + /** + * Convenience wrapper that first expands the link using URI substitution before requesting with the + * default media type. + * + * @param link + * @return + * @throws Exception + */ + public MockHttpServletResponse request(Link link) throws Exception { + return request(link.expand().getHref()); + } + + /** + * Convenience wrapper that first expands the link using URI substitution and then GET [href] using + * an explicit media type + * + * @param link + * @param mediaType + * @return + * @throws Exception + */ + public MockHttpServletResponse request(Link link, MediaType mediaType) throws Exception { + return request(link.expand().getHref(), mediaType); + } + + /** + * Convenience wrapper to GET [href] using the default media type. + * + * @param href + * @return + * @throws Exception + */ + public MockHttpServletResponse request(String href) throws Exception { + return request(href, DEFAULT_MEDIA_TYPE); + } + + /** + * For a given link, expand the href using URI substitution and then do a simple GET. + * + * @param link + * @return + * @throws Exception + */ + public ResultActions follow(Link link) throws Exception { + return follow(link.expand().getHref()); + } + + /** + * Follow URL supplied as a string. NOTE: Assumes no URI templates. + * + * @param href + * @return + * @throws Exception + */ + public ResultActions follow(String href) throws Exception { + return mvc.perform(get(href)); + } + + /** + * Discover list of URIs associated with a rel, starting at the root node ("/") + * + * @param rel + * @return + * @throws Exception + */ + public List discover(String rel) throws Exception { + return discover(new Link("/"), rel); + } + + /** + * Discover single URI associated with a rel, starting at the root node ("/") + * @param rel + * @return + * @throws Exception + */ + public Link discoverUnique(String rel) throws Exception { + + List discover = discover(rel); + assertThat(discover, hasSize(1)); + return discover.get(0); + } + + /** + * Given a URI (root), discover the URIs for a given rel. + * + * @param root - URI to start from + * @param rel - name of the relationship to seek links + * @return list of {@link org.springframework.hateoas.Link Link} objects associated with the rel + * @throws Exception + */ + public List discover(Link root, String rel) throws Exception { + + MockHttpServletResponse response = mvc.perform(get(root.expand().getHref()).accept(DEFAULT_MEDIA_TYPE)).// + andExpect(status().isOk()).// + andExpect(hasLinkWithRel(rel)).// + andReturn().getResponse(); + + String s = response.getContentAsString(); + return getDiscoverer(response).findLinksWithRel(rel, s); + } + + /** + * Given a URI (root), discover the unique URI for a given rel. NOTE: Assumes there is only one URI + * + * @param root + * @param rel + * @return {@link org.springframework.hateoas.Link Link} tied to a given rel + * @throws Exception + */ + public Link discoverUnique(Link root, String rel) throws Exception { + + MockHttpServletResponse response = mvc.perform(get(root.expand().getHref()).accept(DEFAULT_MEDIA_TYPE)).// + andExpect(status().isOk()).// + andExpect(hasLinkWithRel(rel)).// + andReturn().getResponse(); + + return assertHasLinkWithRel(rel, response); + } + + /** + * For a given servlet response, verify that the provided rel exists in its hypermedia. If so, + * return the URI link. + * + * @param rel + * @param response + * @return {@link org.springframework.hateoas.Link} of the rel found in the response + * @throws Exception + */ + public Link assertHasLinkWithRel(String rel, MockHttpServletResponse response) throws Exception { + + String content = response.getContentAsString(); + Link link = getDiscoverer(response).findLinkWithRel(rel, content); + + assertThat("Expected to find link with rel " + rel + " but found none in " + content + "!", link, + is(notNullValue())); + + return link; + } + + /** + * MockMvc matcher used to verify existence of rel with URI link + * + * @param rel + * @return + */ + public ResultMatcher hasLinkWithRel(final String rel) { + + return new ResultMatcher() { + + @Override + public void match(MvcResult result) throws Exception { + + MockHttpServletResponse response = result.getResponse(); + String s = response.getContentAsString(); + + assertThat("Expected to find link with rel " + rel + " but found none in " + s, // + getDiscoverer(response).findLinkWithRel(rel, s), notNullValue()); + } + }; + } + + /** + * Using the servlet response's content type, find the corresponding link discoverer. + * + * @param response + * @return {@link org.springframework.hateoas.LinkDiscoverer} + */ + public LinkDiscoverer getDiscoverer(MockHttpServletResponse response) { + + String contentType = response.getContentType(); + LinkDiscoverer linkDiscovererFor = discoverers.getLinkDiscovererFor(contentType); + + assertThat("Did not find a LinkDiscoverer for returned media type " + contentType + "!", linkDiscovererFor, + is(notNullValue())); + + return linkDiscovererFor; + } + + +} diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/WebTestUtils.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/WebTestUtils.java deleted file mode 100644 index f70ea863a..000000000 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/WebTestUtils.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.rest.webmvc; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; - -/** - * Helper methods for web integration testing. - * - * @author Oliver Gierke - */ -public class WebTestUtils { - - /** - * Initializes web tests. Will register a {@link MockHttpServletRequest} for the current thread. - */ - public static void initWebTest() { - - MockHttpServletRequest request = new MockHttpServletRequest(); - ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); - RequestContextHolder.setRequestAttributes(requestAttributes); - } - - public static void assertAllowHeaders(HttpEntity response, HttpMethod... methods) { - - HttpHeaders headers = response.getHeaders(); - - assertThat(headers.getAllow(), hasSize(methods.length)); - assertThat(headers.getAllow(), hasItems(methods)); - } -} diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/gemfire/GemfireWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/gemfire/GemfireWebTests.java index a92aef524..15c982471 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/gemfire/GemfireWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/gemfire/GemfireWebTests.java @@ -17,14 +17,14 @@ package org.springframework.data.rest.webmvc.gemfire; import java.util.Arrays; -import org.springframework.data.rest.webmvc.AbstractWebIntegrationTests; +import org.springframework.data.rest.webmvc.CommonWebTests; import org.springframework.test.context.ContextConfiguration; /** * @author Oliver Gierke */ @ContextConfiguration(classes = GemfireRepositoryConfig.class) -public class GemfireWebTests extends AbstractWebIntegrationTests { +public class GemfireWebTests extends CommonWebTests { /* * (non-Javadoc) 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 193d8c897..b9636f967 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 @@ -33,7 +33,7 @@ import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.rest.core.mapping.ResourceMappings; -import org.springframework.data.rest.webmvc.AbstractWebIntegrationTests; +import org.springframework.data.rest.webmvc.CommonWebTests; import org.springframework.hateoas.Link; import org.springframework.hateoas.RelProvider; import org.springframework.http.MediaType; @@ -57,7 +57,7 @@ import com.jayway.jsonpath.JsonPath; */ @Transactional @ContextConfiguration(classes = JpaRepositoryConfig.class) -public class JpaWebTests extends AbstractWebIntegrationTests { +public class JpaWebTests extends CommonWebTests { private static final MediaType TEXT_URI_LIST = MediaType.valueOf("text/uri-list"); static final String LINK_TO_SIBLINGS_OF = "$._embedded..[?(@.firstName == '%s')]._links.siblings.href[0]"; @@ -125,17 +125,17 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void accessPersons() throws Exception { - MockHttpServletResponse response = request("/people?page=0&size=1"); + MockHttpServletResponse response = testUtils.request("/people?page=0&size=1"); - Link nextLink = assertHasLinkWithRel(Link.REL_NEXT, response); + Link nextLink = testUtils.assertHasLinkWithRel(Link.REL_NEXT, response); assertDoesNotHaveLinkWithRel(Link.REL_PREVIOUS, response); - response = request(nextLink); - assertHasLinkWithRel(Link.REL_PREVIOUS, response); - nextLink = assertHasLinkWithRel(Link.REL_NEXT, response); + response = testUtils.request(nextLink); + testUtils.assertHasLinkWithRel(Link.REL_PREVIOUS, response); + nextLink = testUtils.assertHasLinkWithRel(Link.REL_NEXT, response); - response = request(nextLink); - assertHasLinkWithRel(Link.REL_PREVIOUS, response); + response = testUtils.request(nextLink); + testUtils.assertHasLinkWithRel(Link.REL_PREVIOUS, response); assertDoesNotHaveLinkWithRel(Link.REL_NEXT, response); } @@ -145,13 +145,13 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void exposesLinkForRelatedResource() throws Exception { - MockHttpServletResponse response = request("/"); - Link ordersLink = assertHasLinkWithRel("orders", response); + MockHttpServletResponse response = testUtils.request("/"); + Link ordersLink = testUtils.assertHasLinkWithRel("orders", response); - MockHttpServletResponse orders = request(ordersLink); + MockHttpServletResponse orders = testUtils.request(ordersLink); Link creatorLink = assertHasContentLinkWithRel("creator", orders); - assertThat(request(creatorLink), is(notNullValue())); + assertThat(testUtils.request(creatorLink), is(notNullValue())); } /** @@ -160,10 +160,10 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void exposesInlinedEntities() throws Exception { - MockHttpServletResponse response = request("/"); - Link ordersLink = assertHasLinkWithRel("orders", response); + MockHttpServletResponse response = testUtils.request("/"); + Link ordersLink = testUtils.assertHasLinkWithRel("orders", response); - MockHttpServletResponse orders = request(ordersLink); + MockHttpServletResponse orders = testUtils.request(ordersLink); assertHasJsonPathValue("$..lineItems", orders); } @@ -185,7 +185,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void createPersonThenVerifyIgnoredAttributesDontExist() throws Exception { - Link peopleLink = discoverUnique("people"); + Link peopleLink = testUtils.discoverUnique("people"); ObjectMapper mapper = new ObjectMapper(); Person frodo = new Person("Frodo", "Baggins"); frodo.setAge(77); @@ -208,12 +208,12 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void createThenPatch() throws Exception { - Link peopleLink = discoverUnique("people"); + Link peopleLink = testUtils.discoverUnique("people"); MockHttpServletResponse bilbo = postAndGet(peopleLink, "{ \"firstName\" : \"Bilbo\", \"lastName\" : \"Baggins\" }", MediaType.APPLICATION_JSON); - Link bilboLink = assertHasLinkWithRel("self", bilbo); + Link bilboLink = testUtils.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 AbstractWebIntegrationTests { @Test public void createThenPut() throws Exception { - Link peopleLink = discoverUnique("people"); + Link peopleLink = testUtils.discoverUnique("people"); MockHttpServletResponse bilbo = postAndGet(peopleLink,// "{ \"firstName\" : \"Bilbo\", \"lastName\" : \"Baggins\" }",// MediaType.APPLICATION_JSON); - Link bilboLink = assertHasLinkWithRel("self", bilbo); + Link bilboLink = testUtils.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 AbstractWebIntegrationTests { @Test public void propertiesCanHaveNulls() throws Exception { - Link peopleLink = discoverUnique("people"); + Link peopleLink = testUtils.discoverUnique("people"); Person frodo = new Person(); frodo.setFirstName("Frodo"); @@ -396,39 +396,39 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void putShouldWorkDespiteExistingLinks() throws Exception { - Link peopleLink = discoverUnique("people"); + Link peopleLink = testUtils.discoverUnique("people"); Person frodo = new Person("Frodo", "Baggins"); String frodoString = mapper.writeValueAsString(frodo); MockHttpServletResponse createdPerson = postAndGet(peopleLink, frodoString, MediaType.APPLICATION_JSON); - Link frodoLink = assertHasLinkWithRel("self", createdPerson); + Link frodoLink = testUtils.assertHasLinkWithRel("self", createdPerson); assertJsonPathEquals("$.firstName", "Frodo", createdPerson); String bilboWithFrodosLinks = createdPerson.getContentAsString().replace("Frodo", "Bilbo"); MockHttpServletResponse overwrittenResponse = putAndGet(frodoLink, bilboWithFrodosLinks, MediaType.APPLICATION_JSON); - assertHasLinkWithRel("self", overwrittenResponse); + testUtils.assertHasLinkWithRel("self", overwrittenResponse); assertJsonPathEquals("$.firstName", "Bilbo", overwrittenResponse); } private List preparePersonResources(Person primary, Person... persons) throws Exception { - Link peopleLink = discoverUnique("people"); + Link peopleLink = testUtils.discoverUnique("people"); List links = new ArrayList(); MockHttpServletResponse primaryResponse = postAndGet(peopleLink, mapper.writeValueAsString(primary), MediaType.APPLICATION_JSON); - links.add(assertHasLinkWithRel("siblings", primaryResponse)); + links.add(testUtils.assertHasLinkWithRel("siblings", primaryResponse)); for (Person person : persons) { String payload = mapper.writeValueAsString(person); MockHttpServletResponse response = postAndGet(peopleLink, payload, MediaType.APPLICATION_JSON); - links.add(assertHasLinkWithRel(Link.REL_SELF, response)); + links.add(testUtils.assertHasLinkWithRel(Link.REL_SELF, response)); } return links; @@ -440,7 +440,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void doesNotAllowGetToCollectionResourceIfFindAllIsNotExported() throws Exception { - Link link = discoverUnique("addresses"); + Link link = testUtils.discoverUnique("addresses"); mvc.perform(get(link.getHref())).// andExpect(status().isMethodNotAllowed()); @@ -452,7 +452,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void doesNotAllowPostToCollectionResourceIfSaveIsNotExported() throws Exception { - Link link = discoverUnique("addresses"); + Link link = testUtils.discoverUnique("addresses"); mvc.perform(post(link.getHref()).content("{}").contentType(MediaType.APPLICATION_JSON)).// andExpect(status().isMethodNotAllowed()); @@ -467,9 +467,9 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void returnsProjectionIfRequested() throws Exception { - Link orders = discoverUnique("orders"); + Link orders = testUtils.discoverUnique("orders"); - MockHttpServletResponse response = request(orders); + MockHttpServletResponse response = testUtils.request(orders); Link orderLink = assertContentLinkWithRel("self", response, true).expand(); UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(orderLink.getHref()); @@ -497,18 +497,18 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void onlyLinksShouldAppearWhenExecuteSearchCompact() throws Exception { - Link peopleLink = discoverUnique("people"); + Link peopleLink = testUtils.discoverUnique("people"); Person daenerys = new Person("Daenerys", "Targaryen"); String daenerysString = mapper.writeValueAsString(daenerys); MockHttpServletResponse createdPerson = postAndGet(peopleLink, daenerysString, MediaType.APPLICATION_JSON); - Link daenerysLink = assertHasLinkWithRel("self", createdPerson); + Link daenerysLink = testUtils.assertHasLinkWithRel("self", createdPerson); assertJsonPathEquals("$.firstName", "Daenerys", createdPerson); - Link searchLink = discoverUnique(peopleLink, "search"); - Link byFirstNameLink = discoverUnique(searchLink, "findFirstPersonByFirstName"); + Link searchLink = testUtils.discoverUnique(peopleLink, "search"); + Link byFirstNameLink = testUtils.discoverUnique(searchLink, "findFirstPersonByFirstName"); - MockHttpServletResponse response = request(byFirstNameLink.expand("Daenerys"), + MockHttpServletResponse response = testUtils.request(byFirstNameLink.expand("Daenerys"), MediaType.parseMediaType("application/x-spring-data-compact+json")); String responseBody = response.getContentAsString(); @@ -526,9 +526,9 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void rendersExcerptProjectionsCorrectly() throws Exception { - Link authorsLink = discoverUnique("authors"); + Link authorsLink = testUtils.discoverUnique("authors"); - MockHttpServletResponse response = request(authorsLink); + MockHttpServletResponse response = testUtils.request(authorsLink); String firstAuthorPath = "$._embedded.authors[0]"; // Has main content @@ -542,7 +542,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests { String content = response.getContentAsString(); String href = JsonPath.read(content, firstAuthorPath.concat("._links.self.href")); - follow(new Link(href)).andExpect(hasLinkWithRel("books")); + testUtils.follow(new Link(href)).andExpect(testUtils.hasLinkWithRel("books")); } /** @@ -551,7 +551,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void returns404WhenTryingToDeleteANonExistingResource() throws Exception { - Link authorsLink = discoverUnique("authors"); + Link authorsLink = testUtils.discoverUnique("authors"); mvc.perform(delete(authorsLink.getHref().concat("/{id}"), 4711)).// andExpect(status().isNotFound()); @@ -563,20 +563,20 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Test public void execturesSearchThatTakesASort() throws Exception { - Link booksLink = discoverUnique("books"); - Link searchLink = discoverUnique(booksLink, "search"); - Link findBySortedLink = discoverUnique(searchLink, "find-by-sorted"); + Link booksLink = testUtils.discoverUnique("books"); + Link searchLink = testUtils.discoverUnique(booksLink, "search"); + Link findBySortedLink = testUtils.discoverUnique(searchLink, "find-by-sorted"); // Assert sort options advertised assertThat(findBySortedLink.isTemplated(), is(true)); assertThat(findBySortedLink.getVariableNames(), contains("sort")); // Assert results returned as specified - follow(findBySortedLink.expand("title,desc")).// + testUtils.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")); - follow(findBySortedLink.expand("title,asc")).// + testUtils.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 AbstractWebIntegrationTests { */ private void assertSiblingNames(Link link, String... siblingNames) throws Exception { - String responseBody = request(link).getContentAsString(); + String responseBody = testUtils.request(link).getContentAsString(); List persons = JsonPath.read(responseBody, "$._embedded.people[*].firstName"); assertThat(persons, hasSize(siblingNames.length)); @@ -599,7 +599,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests { private void assertPersonWithNameAndSiblingLink(String name) throws Exception { - MockHttpServletResponse response = request(discoverUnique("people")); + MockHttpServletResponse response = testUtils.request(testUtils.discoverUnique("people")); String jsonPath = String.format("$._embedded.people[?(@.firstName == '%s')][0]", name); @@ -610,7 +610,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests { // Assert sibling link exposed in resource pointed to Link selfLink = new Link(JsonPath. read(john, "$._links.self.href")); - follow(selfLink).// + testUtils.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 e20b6c81e..7e7e36498 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 @@ -25,7 +25,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.rest.webmvc.AbstractWebIntegrationTests; +import org.springframework.data.rest.webmvc.CommonWebTests; import org.springframework.data.rest.webmvc.RestMediaTypes; import org.springframework.hateoas.Link; import org.springframework.http.MediaType; @@ -38,9 +38,10 @@ import com.jayway.jsonpath.JsonPath; * Integration tests for MongoDB repositories. * * @author Oliver Gierke + * @author Greg Turnquist */ @ContextConfiguration(classes = MongoDbRepositoryConfig.class) -public class MongoWebTests extends AbstractWebIntegrationTests { +public class MongoWebTests extends CommonWebTests { @Autowired ProfileRepository repository; @Autowired UserRepository userRepository; @@ -88,17 +89,17 @@ public class MongoWebTests extends AbstractWebIntegrationTests { @Test public void foo() throws Exception { - Link profileLink = discoverUnique("profiles"); - follow(profileLink).// + Link profileLink = testUtils.discoverUnique("profiles"); + testUtils.follow(profileLink).// andExpect(jsonPath("$._embedded.profiles").value(hasSize(2))); } @Test public void rendersEmbeddedDocuments() throws Exception { - Link usersLink = discoverUnique("users"); - Link userLink = assertHasContentLinkWithRel("self", request(usersLink)); - follow(userLink).// + Link usersLink = testUtils.discoverUnique("users"); + Link userLink = assertHasContentLinkWithRel("self", testUtils.request(usersLink)); + testUtils.follow(userLink).// andExpect(jsonPath("$.address.zipCode").value(is(notNullValue()))); } @@ -108,22 +109,22 @@ public class MongoWebTests extends AbstractWebIntegrationTests { @Test public void executeQueryMethodWithPrimitiveReturnType() throws Exception { - Link profiles = discoverUnique("profiles"); - Link profileSearches = discoverUnique(profiles, "search"); - Link countByTypeLink = discoverUnique(profileSearches, "countByType"); + Link profiles = testUtils.discoverUnique("profiles"); + Link profileSearches = testUtils.discoverUnique(profiles, "search"); + Link countByTypeLink = testUtils.discoverUnique(profileSearches, "countByType"); assertThat(countByTypeLink.isTemplated(), is(true)); assertThat(countByTypeLink.getVariableNames(), hasItem("type")); - MockHttpServletResponse response = request(countByTypeLink.expand("Twitter")); + MockHttpServletResponse response = testUtils.request(countByTypeLink.expand("Twitter")); assertThat(response.getContentAsString(), is("1")); } @Test public void testname() throws Exception { - Link usersLink = discoverUnique("users"); - Link userLink = assertHasContentLinkWithRel("self", request(usersLink)); + Link usersLink = testUtils.discoverUnique("users"); + Link userLink = assertHasContentLinkWithRel("self", testUtils.request(usersLink)); MockHttpServletResponse response = patchAndGet(userLink, "{\"lastname\" : null, \"address\" : { \"zipCode\" : \"ZIP\"}}", MediaType.APPLICATION_JSON); @@ -135,8 +136,8 @@ public class MongoWebTests extends AbstractWebIntegrationTests { @Test public void testname2() throws Exception { - Link usersLink = discoverUnique("users"); - Link userLink = assertHasContentLinkWithRel("self", request(usersLink)); + Link usersLink = testUtils.discoverUnique("users"); + Link userLink = assertHasContentLinkWithRel("self", testUtils.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 caf6cb024..38531268e 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 @@ -30,7 +30,7 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.neo4j.config.EnableNeo4jRepositories; import org.springframework.data.neo4j.config.Neo4jConfiguration; -import org.springframework.data.rest.webmvc.AbstractWebIntegrationTests; +import org.springframework.data.rest.webmvc.CommonWebTests; import org.springframework.hateoas.Link; import org.springframework.test.context.ContextConfiguration; import org.springframework.transaction.annotation.EnableTransactionManagement; @@ -42,7 +42,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; */ @ContextConfiguration @Ignore -public class Neo4jWebTests extends AbstractWebIntegrationTests { +public class Neo4jWebTests extends CommonWebTests { @Configuration @ComponentScan @@ -86,13 +86,13 @@ public class Neo4jWebTests extends AbstractWebIntegrationTests { public void deletesCustomer() throws Exception { // Lookup customer - Link customers = discoverUnique("customers"); - Link customerLink = assertHasContentLinkWithRel("self", request(customers)); + Link customers = testUtils.discoverUnique("customers"); + Link customerLink = assertHasContentLinkWithRel("self", testUtils.request(customers)); // Delete customer mvc.perform(delete(customerLink.getHref())); // Assert no customers anymore - assertDoesNotHaveContentLinkWithRel("self", request(customers)); + assertDoesNotHaveContentLinkWithRel("self", testUtils.request(customers)); } }