DATAREST-1213 - Polishing.

Simplified tests and domain code. Reinstantiated accidentally removed method in PersistentEntityResource.

Original pull request: #355.
This commit is contained in:
Oliver Drotbohm
2020-10-26 22:17:23 +01:00
parent 055c4528e8
commit f00451dc7e
7 changed files with 65 additions and 84 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2019-2020 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.
@@ -15,6 +15,8 @@
*/
package org.springframework.data.rest.webmvc.jpa;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@@ -23,41 +25,18 @@ import javax.persistence.Version;
/**
* @author Dario Seidl
*/
@Data
@Entity
public class Category {
class Category {
public @Id @GeneratedValue Long id;
public @Version Long version = 0L;
public String name;
public Category() {
}
@SuppressWarnings("unused")
private Category() {}
public Category(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2019-2020 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.
@@ -22,7 +22,7 @@ import org.springframework.data.rest.core.config.Projection;
* @author Dario Seidl
*/
@Projection(name = "open", types = Category.class)
public interface CategoryProjection {
interface CategoryProjection {
String getName();

View File

@@ -21,7 +21,7 @@ import org.springframework.data.rest.core.annotation.RepositoryRestResource;
/**
* @author Dario Seidl
*/
@RepositoryRestResource(collectionResourceRel = "categories", path = "categories")
public interface CategoryRepository extends CrudRepository<Category, Long> {
@RepositoryRestResource
interface CategoryRepository extends CrudRepository<Category, Long> {
}

View File

@@ -58,6 +58,7 @@ import org.springframework.web.util.UriTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
/**
* Web integration tests specific to JPA.
@@ -257,48 +258,6 @@ public class JpaWebTests extends CommonWebTests {
assertNull(JsonPath.read(frodo.getContentAsString(), "$.lastName"));
}
@Test // DATAREST-1213
public void createThenPatchWithProjection() throws Exception {
Link categoriesLink = client.discoverUnique(LinkRelation.of("categories"));
MockHttpServletResponse test = postAndGet(categoriesLink, "{ \"name\" : \"test\" }",
MediaType.APPLICATION_JSON);
Link testLink = client.assertHasLinkWithRel(IanaLinkRelations.SELF, test);
assertThat((String) JsonPath.read(test.getContentAsString(), "$.name")).isEqualTo("test");
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(testLink.getHref());
String uri = builder.queryParam("projection", "open").build().toUriString();
MockHttpServletResponse patched = patchAndGet(new Link(uri), "{ \"name\" : \"patched\" }", MediaType.APPLICATION_JSON);
assertThat((String) JsonPath.read(patched.getContentAsString(), "$.name")).isEqualTo("patched");
assertThat((String) JsonPath.read(patched.getContentAsString(), "$.calculatedName")).isEqualTo("calculated-patched");
}
@Test // DATAREST-1213
public void createThenPutWithProjection() throws Exception {
Link categoriesLink = client.discoverUnique(LinkRelation.of("categories"));
MockHttpServletResponse test = postAndGet(categoriesLink, "{ \"name\" : \"test\" }",
MediaType.APPLICATION_JSON);
Link testLink = client.assertHasLinkWithRel(IanaLinkRelations.SELF, test);
assertThat((String) JsonPath.read(test.getContentAsString(), "$.name")).isEqualTo("test");
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(testLink.getHref());
String uri = builder.queryParam("projection", "open").build().toUriString();
MockHttpServletResponse patched = putAndGet(new Link(uri), "{ \"name\" : \"put\" }", MediaType.APPLICATION_JSON);
assertThat((String) JsonPath.read(patched.getContentAsString(), "$.name")).isEqualTo("put");
assertThat((String) JsonPath.read(patched.getContentAsString(), "$.calculatedName")).isEqualTo("calculated-put");
}
@Test
public void listsSiblingsWithContentCorrectly() throws Exception {
assertPersonWithNameAndSiblingLink("John");
@@ -746,6 +705,32 @@ public class JpaWebTests extends CommonWebTests {
andExpect(client.hasLinkWithRel(IanaLinkRelations.SELF));
}
@Test // DATAREST-1213
public void createThenPatchWithProjection() throws Exception {
Link link = createCategory();
String payload = "{ \"name\" : \"patched\" }";
MockHttpServletResponse patched = patchAndGet(link, payload, MediaType.APPLICATION_JSON);
ReadContext content = JsonPath.parse(patched.getContentAsString());
assertThat(content.read("$.name", String.class)).isEqualTo("patched");
assertThat(content.read("$.calculatedName", String.class)).isEqualTo("calculated-patched");
}
@Test // DATAREST-1213
public void createThenPutWithProjection() throws Exception {
Link link = createCategory();
String payload = "{ \"name\" : \"put\" }";
MockHttpServletResponse patched = putAndGet(link, payload, MediaType.APPLICATION_JSON);
ReadContext content = JsonPath.parse(patched.getContentAsString());
assertThat(content.read("$.name", String.class)).isEqualTo("put");
assertThat(content.read("$.calculatedName", String.class)).isEqualTo("calculated-put");
}
private List<Link> preparePersonResources(Person primary, Person... persons) throws Exception {
Link peopleLink = client.discoverUnique(LinkRelation.of("people"));
@@ -831,6 +816,21 @@ public class JpaWebTests extends CommonWebTests {
andExpect(jsonPath("$._links.siblings", is(notNullValue())));
}
private Link createCategory() throws Exception {
Link categoriesLink = client.discoverUnique(LinkRelation.of("categories"));
MockHttpServletResponse test = postAndGet(categoriesLink, "{ \"name\" : \"test\" }",
MediaType.APPLICATION_JSON);
Link testLink = client.assertHasLinkWithRel(IanaLinkRelations.SELF, test);
assertThat((String) JsonPath.read(test.getContentAsString(), "$.name")).isEqualTo("test");
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(testLink.getHref());
return Link.of(builder.queryParam("projection", "open").build().toUriString());
}
private static String toUriList(Link... links) {
List<String> uris = new ArrayList<>(links.length);

View File

@@ -61,7 +61,7 @@ public class HttpHeadersPreparer {
public HttpHeaders prepareHeaders(Optional<PersistentEntityResource> resource) {
return resource//
.map(it -> prepareHeaders(it.getPersistentEntity(), it.getTargetEntity()))//
.map(it -> prepareHeaders(it.getPersistentEntity(), it.getTarget()))//
.orElseGet(() -> new HttpHeaders());
}

View File

@@ -95,17 +95,19 @@ public class PersistentEntityResource extends EntityModel<Object> {
}
/**
* Returns the underlying instance. If the instance is a dynamic JDK proxy, the proxy target is returned.
* Returns the underlying instance. If the instance is a {@link TargetAware}, the {@link TargetAware}'s target is
* returned.
*
* @return
* @see #getContent()
*/
public Object getTargetEntity() {
public Object getTarget() {
Object content = getContent();
if (content instanceof TargetAware) {
return ((TargetAware) content).getTarget();
} else {
return content;
}
return content instanceof TargetAware
? ((TargetAware) content).getTarget()
: content;
}
/**
@@ -114,7 +116,7 @@ public class PersistentEntityResource extends EntityModel<Object> {
* @return
*/
public PersistentPropertyAccessor<?> getPropertyAccessor() {
return entity.getPropertyAccessor(getTargetEntity());
return entity.getPropertyAccessor(getTarget());
}
/**

View File

@@ -73,7 +73,7 @@ public final class ETag {
Assert.notNull(resource, "PersistentEntityResource must not be null!");
return from(resource.getPersistentEntity(), resource.getTargetEntity());
return from(resource.getPersistentEntity(), resource.getTarget());
}
/**