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 a5e6b4ec4..eebb191ea 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 @@ -42,6 +42,8 @@ import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import com.jayway.jsonpath.JsonPath; + /** * @author Oliver Gierke */ @@ -113,6 +115,16 @@ public abstract class AbstractWebIntegrationTests { return link; } + protected Link assertHasContentLinkWithRel(String rel, MockHttpServletResponse response) throws Exception { + + String href = JsonPath + .read(response.getContentAsString(), String.format("$..links[?(@.rel == '%s')].href[0]", rel)).toString(); + assertThat("Expected to find a link with rel" + rel + " in the content section of the response!", href, + is(notNullValue())); + + return new Link(href, rel); + } + protected void assertDoesNotHaveLinkWithRel(String rel, MockHttpServletResponse response) throws Exception { String content = response.getContentAsString(); 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 a7b7f9b4b..8e390a18b 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 @@ -27,7 +27,7 @@ import org.springframework.data.rest.webmvc.jpa.CreditCard; import org.springframework.data.rest.webmvc.jpa.JpaRepositoryConfig; import org.springframework.data.rest.webmvc.jpa.Order; import org.springframework.data.rest.webmvc.jpa.Person; -import org.springframework.data.rest.webmvc.jpa.PersonLoader; +import org.springframework.data.rest.webmvc.jpa.TestDataPopulator; import org.springframework.hateoas.PagedResources; import org.springframework.hateoas.Resource; import org.springframework.hateoas.Resources; @@ -44,12 +44,12 @@ import org.springframework.transaction.annotation.Transactional; @Transactional public class RepositorySearchControllerIntegrationTests extends AbstractControllerIntegrationTests { - @Autowired PersonLoader loader; + @Autowired TestDataPopulator loader; @Autowired RepositorySearchController controller; @Before public void setUp() { - loader.populateRepository(); + loader.populateRepositories(); } @Test 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 bbca18dfa..85b9ba5f0 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 @@ -15,6 +15,8 @@ */ package org.springframework.data.rest.webmvc.jpa; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -39,7 +41,7 @@ import org.springframework.transaction.annotation.Transactional; @ContextConfiguration(classes = JpaRepositoryConfig.class) public class JpaWebTests extends AbstractWebIntegrationTests { - @Autowired PersonLoader loader; + @Autowired TestDataPopulator loader; @Autowired ResourceMappings mappings; /* @@ -49,7 +51,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Override @Before public void setUp() { - loader.populateRepository(); + loader.populateRepositories(); super.setUp(); } @@ -89,4 +91,19 @@ public class JpaWebTests extends AbstractWebIntegrationTests { assertHasLinkWithRel(Link.REL_PREVIOUS, response); assertDoesNotHaveLinkWithRel(Link.REL_NEXT, response); } + + /** + * @see DATAREST-169 + */ + @Test + public void exposesCreatorOfAnOrder() throws Exception { + + MockHttpServletResponse response = request("/"); + Link ordersLink = assertHasLinkWithRel("orders", response); + + MockHttpServletResponse orders = request(ordersLink); + + Link creatorLink = assertHasContentLinkWithRel("creator", orders); + assertThat(request(creatorLink), is(notNullValue())); + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java index 3eab44513..5c0bf7c6c 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java @@ -16,17 +16,21 @@ package org.springframework.data.rest.webmvc.jpa; import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; +import javax.persistence.Table; /** * @author Oliver Gierke */ @Entity +@Table(name = "ORDERS") public class Order { - @Id private Long id; - @ManyToOne private Person creator; + @Id @GeneratedValue private Long id; + @ManyToOne(fetch = FetchType.LAZY) private Person creator; public Order(Person creator) { this.creator = creator; diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/PersonLoader.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/TestDataPopulator.java similarity index 59% rename from spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/PersonLoader.java rename to spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/TestDataPopulator.java index 956ce6599..d7bba5053 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/PersonLoader.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/TestDataPopulator.java @@ -9,16 +9,35 @@ import org.springframework.stereotype.Component; * @author Jon Brisbin */ @Component -public class PersonLoader { +public class TestDataPopulator { private final PersonRepository people; + private final OrderRepository orders; @Autowired - public PersonLoader(PersonRepository people) { + public TestDataPopulator(PersonRepository people, OrderRepository orders) { this.people = people; + this.orders = orders; } - public void populateRepository() { + public void populateRepositories() { + + populatePeople(); + populateOrders(); + } + + private void populateOrders() { + + if (orders.count() != 0) { + return; + } + + Person person = people.findAll().iterator().next(); + + orders.save(new Order(person)); + } + + private void populatePeople() { if (people.count() != 0) { return;