Skip to content

Commit f00451d

Browse files
committed
DATAREST-1213 - Polishing.
Simplified tests and domain code. Reinstantiated accidentally removed method in PersistentEntityResource. Original pull request: #355.
1 parent 055c452 commit f00451d

File tree

7 files changed

+65
-84
lines changed

7 files changed

+65
-84
lines changed
Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original author or authors.
2+
* Copyright 2019-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.rest.webmvc.jpa;
1717

18+
import lombok.Data;
19+
1820
import javax.persistence.Entity;
1921
import javax.persistence.GeneratedValue;
2022
import javax.persistence.Id;
@@ -23,41 +25,18 @@
2325
/**
2426
* @author Dario Seidl
2527
*/
28+
@Data
2629
@Entity
27-
public class Category {
30+
class Category {
2831

2932
public @Id @GeneratedValue Long id;
3033
public @Version Long version = 0L;
3134
public String name;
3235

33-
public Category() {
34-
}
36+
@SuppressWarnings("unused")
37+
private Category() {}
3538

3639
public Category(String name) {
3740
this.name = name;
3841
}
39-
40-
public Long getId() {
41-
return id;
42-
}
43-
44-
public void setId(Long id) {
45-
this.id = id;
46-
}
47-
48-
public Long getVersion() {
49-
return version;
50-
}
51-
52-
public void setVersion(Long version) {
53-
this.version = version;
54-
}
55-
56-
public String getName() {
57-
return name;
58-
}
59-
60-
public void setName(String name) {
61-
this.name = name;
62-
}
6342
}

spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CategoryProjection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original author or authors.
2+
* Copyright 2019-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@
2222
* @author Dario Seidl
2323
*/
2424
@Projection(name = "open", types = Category.class)
25-
public interface CategoryProjection {
25+
interface CategoryProjection {
2626

2727
String getName();
2828

spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CategoryRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* @author Dario Seidl
2323
*/
24-
@RepositoryRestResource(collectionResourceRel = "categories", path = "categories")
25-
public interface CategoryRepository extends CrudRepository<Category, Long> {
24+
@RepositoryRestResource
25+
interface CategoryRepository extends CrudRepository<Category, Long> {
2626

2727
}

spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
import com.fasterxml.jackson.databind.ObjectMapper;
6060
import com.jayway.jsonpath.JsonPath;
61+
import com.jayway.jsonpath.ReadContext;
6162

6263
/**
6364
* Web integration tests specific to JPA.
@@ -257,48 +258,6 @@ public void createThenPut() throws Exception {
257258
assertNull(JsonPath.read(frodo.getContentAsString(), "$.lastName"));
258259
}
259260

260-
@Test // DATAREST-1213
261-
public void createThenPatchWithProjection() throws Exception {
262-
263-
Link categoriesLink = client.discoverUnique(LinkRelation.of("categories"));
264-
265-
MockHttpServletResponse test = postAndGet(categoriesLink, "{ \"name\" : \"test\" }",
266-
MediaType.APPLICATION_JSON);
267-
268-
Link testLink = client.assertHasLinkWithRel(IanaLinkRelations.SELF, test);
269-
270-
assertThat((String) JsonPath.read(test.getContentAsString(), "$.name")).isEqualTo("test");
271-
272-
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(testLink.getHref());
273-
String uri = builder.queryParam("projection", "open").build().toUriString();
274-
275-
MockHttpServletResponse patched = patchAndGet(new Link(uri), "{ \"name\" : \"patched\" }", MediaType.APPLICATION_JSON);
276-
277-
assertThat((String) JsonPath.read(patched.getContentAsString(), "$.name")).isEqualTo("patched");
278-
assertThat((String) JsonPath.read(patched.getContentAsString(), "$.calculatedName")).isEqualTo("calculated-patched");
279-
}
280-
281-
@Test // DATAREST-1213
282-
public void createThenPutWithProjection() throws Exception {
283-
284-
Link categoriesLink = client.discoverUnique(LinkRelation.of("categories"));
285-
286-
MockHttpServletResponse test = postAndGet(categoriesLink, "{ \"name\" : \"test\" }",
287-
MediaType.APPLICATION_JSON);
288-
289-
Link testLink = client.assertHasLinkWithRel(IanaLinkRelations.SELF, test);
290-
291-
assertThat((String) JsonPath.read(test.getContentAsString(), "$.name")).isEqualTo("test");
292-
293-
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(testLink.getHref());
294-
String uri = builder.queryParam("projection", "open").build().toUriString();
295-
296-
MockHttpServletResponse patched = putAndGet(new Link(uri), "{ \"name\" : \"put\" }", MediaType.APPLICATION_JSON);
297-
298-
assertThat((String) JsonPath.read(patched.getContentAsString(), "$.name")).isEqualTo("put");
299-
assertThat((String) JsonPath.read(patched.getContentAsString(), "$.calculatedName")).isEqualTo("calculated-put");
300-
}
301-
302261
@Test
303262
public void listsSiblingsWithContentCorrectly() throws Exception {
304263
assertPersonWithNameAndSiblingLink("John");
@@ -746,6 +705,32 @@ public void appliesSortByEmbeddedAssociation() throws Exception {
746705
andExpect(client.hasLinkWithRel(IanaLinkRelations.SELF));
747706
}
748707

708+
@Test // DATAREST-1213
709+
public void createThenPatchWithProjection() throws Exception {
710+
711+
Link link = createCategory();
712+
String payload = "{ \"name\" : \"patched\" }";
713+
714+
MockHttpServletResponse patched = patchAndGet(link, payload, MediaType.APPLICATION_JSON);
715+
ReadContext content = JsonPath.parse(patched.getContentAsString());
716+
717+
assertThat(content.read("$.name", String.class)).isEqualTo("patched");
718+
assertThat(content.read("$.calculatedName", String.class)).isEqualTo("calculated-patched");
719+
}
720+
721+
@Test // DATAREST-1213
722+
public void createThenPutWithProjection() throws Exception {
723+
724+
Link link = createCategory();
725+
String payload = "{ \"name\" : \"put\" }";
726+
727+
MockHttpServletResponse patched = putAndGet(link, payload, MediaType.APPLICATION_JSON);
728+
ReadContext content = JsonPath.parse(patched.getContentAsString());
729+
730+
assertThat(content.read("$.name", String.class)).isEqualTo("put");
731+
assertThat(content.read("$.calculatedName", String.class)).isEqualTo("calculated-put");
732+
}
733+
749734
private List<Link> preparePersonResources(Person primary, Person... persons) throws Exception {
750735

751736
Link peopleLink = client.discoverUnique(LinkRelation.of("people"));
@@ -831,6 +816,21 @@ private void assertPersonWithNameAndSiblingLink(String name) throws Exception {
831816
andExpect(jsonPath("$._links.siblings", is(notNullValue())));
832817
}
833818

819+
private Link createCategory() throws Exception {
820+
821+
Link categoriesLink = client.discoverUnique(LinkRelation.of("categories"));
822+
823+
MockHttpServletResponse test = postAndGet(categoriesLink, "{ \"name\" : \"test\" }",
824+
MediaType.APPLICATION_JSON);
825+
826+
Link testLink = client.assertHasLinkWithRel(IanaLinkRelations.SELF, test);
827+
828+
assertThat((String) JsonPath.read(test.getContentAsString(), "$.name")).isEqualTo("test");
829+
830+
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(testLink.getHref());
831+
return Link.of(builder.queryParam("projection", "open").build().toUriString());
832+
}
833+
834834
private static String toUriList(Link... links) {
835835

836836
List<String> uris = new ArrayList<>(links.length);

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/HttpHeadersPreparer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public HttpHeadersPreparer(AuditableBeanWrapperFactory auditableBeanWrapperFacto
6161
public HttpHeaders prepareHeaders(Optional<PersistentEntityResource> resource) {
6262

6363
return resource//
64-
.map(it -> prepareHeaders(it.getPersistentEntity(), it.getTargetEntity()))//
64+
.map(it -> prepareHeaders(it.getPersistentEntity(), it.getTarget()))//
6565
.orElseGet(() -> new HttpHeaders());
6666
}
6767

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResource.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,19 @@ public boolean isNested() {
9595
}
9696

9797
/**
98-
* Returns the underlying instance. If the instance is a dynamic JDK proxy, the proxy target is returned.
98+
* Returns the underlying instance. If the instance is a {@link TargetAware}, the {@link TargetAware}'s target is
99+
* returned.
99100
*
100101
* @return
102+
* @see #getContent()
101103
*/
102-
public Object getTargetEntity() {
104+
public Object getTarget() {
105+
103106
Object content = getContent();
104-
if (content instanceof TargetAware) {
105-
return ((TargetAware) content).getTarget();
106-
} else {
107-
return content;
108-
}
107+
108+
return content instanceof TargetAware
109+
? ((TargetAware) content).getTarget()
110+
: content;
109111
}
110112

111113
/**
@@ -114,7 +116,7 @@ public Object getTargetEntity() {
114116
* @return
115117
*/
116118
public PersistentPropertyAccessor<?> getPropertyAccessor() {
117-
return entity.getPropertyAccessor(getTargetEntity());
119+
return entity.getPropertyAccessor(getTarget());
118120
}
119121

120122
/**

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/ETag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static ETag from(PersistentEntityResource resource) {
7373

7474
Assert.notNull(resource, "PersistentEntityResource must not be null!");
7575

76-
return from(resource.getPersistentEntity(), resource.getTargetEntity());
76+
return from(resource.getPersistentEntity(), resource.getTarget());
7777
}
7878

7979
/**

0 commit comments

Comments
 (0)