DATAREST-169 - Added integration test to access property.
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user