From f00451dc7e7f761c911849ff8f7859b68b5cee97 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Mon, 26 Oct 2020 22:17:23 +0100 Subject: [PATCH] DATAREST-1213 - Polishing. Simplified tests and domain code. Reinstantiated accidentally removed method in PersistentEntityResource. Original pull request: #355. --- .../data/rest/webmvc/jpa/Category.java | 35 ++------ .../rest/webmvc/jpa/CategoryProjection.java | 4 +- .../rest/webmvc/jpa/CategoryRepository.java | 4 +- .../data/rest/webmvc/jpa/JpaWebTests.java | 84 +++++++++---------- .../data/rest/webmvc/HttpHeadersPreparer.java | 2 +- .../rest/webmvc/PersistentEntityResource.java | 18 ++-- .../data/rest/webmvc/support/ETag.java | 2 +- 7 files changed, 65 insertions(+), 84 deletions(-) diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/Category.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/Category.java index 95ecdb36a..d5a912353 100644 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/Category.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/Category.java @@ -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; - } } diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CategoryProjection.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CategoryProjection.java index a6ccf5de9..5ff0cfda0 100644 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CategoryProjection.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CategoryProjection.java @@ -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(); diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CategoryRepository.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CategoryRepository.java index 58398fcb6..f7f578870 100644 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CategoryRepository.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CategoryRepository.java @@ -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 { +@RepositoryRestResource +interface CategoryRepository extends CrudRepository { } diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java index e7fd3e051..0347e0319 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java @@ -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 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 uris = new ArrayList<>(links.length); diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/HttpHeadersPreparer.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/HttpHeadersPreparer.java index 7f38194b5..2e0ad08e6 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/HttpHeadersPreparer.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/HttpHeadersPreparer.java @@ -61,7 +61,7 @@ public class HttpHeadersPreparer { public HttpHeaders prepareHeaders(Optional resource) { return resource// - .map(it -> prepareHeaders(it.getPersistentEntity(), it.getTargetEntity()))// + .map(it -> prepareHeaders(it.getPersistentEntity(), it.getTarget()))// .orElseGet(() -> new HttpHeaders()); } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResource.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResource.java index f957c181c..ae5e6f1ed 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResource.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResource.java @@ -95,17 +95,19 @@ public class PersistentEntityResource extends EntityModel { } /** - * 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 { * @return */ public PersistentPropertyAccessor getPropertyAccessor() { - return entity.getPropertyAccessor(getTargetEntity()); + return entity.getPropertyAccessor(getTarget()); } /** diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/ETag.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/ETag.java index f66aef492..ceb5b682c 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/ETag.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/ETag.java @@ -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()); } /**