diff --git a/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/CommonWebTests.java b/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/CommonWebTests.java index 2905d7f19..5f8c13fad 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/CommonWebTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/CommonWebTests.java @@ -173,8 +173,8 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { for (Object href : uris) { - client.follow(href.toString()). // - andExpect(status().isOk()); + client.follow(new Link(href.toString())) // + .andExpect(status().isOk()); } } } 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 06690e534..b0ed04164 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 @@ -335,7 +335,7 @@ public class JpaWebTests extends CommonWebTests { patchAndGet(frodosSiblingsLink, links.get(3).getHref(), TEXT_URI_LIST); String pippinId = new UriTemplate("/people/{id}").match(links.get(3).getHref()).get("id"); - deleteAndVerify(new Link(frodosSiblingsLink.getHref() + "/" + pippinId)); + deleteAndVerify(new Link(frodosSiblingsLink.expand().getHref() + "/" + pippinId)); assertSiblingNames(frodosSiblingsLink, "Bilbo", "Merry"); } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/mapping/Associations.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/mapping/Associations.java index bbcb67602..55359e0ee 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/mapping/Associations.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/mapping/Associations.java @@ -15,13 +15,14 @@ */ package org.springframework.data.rest.webmvc.mapping; +import static org.springframework.hateoas.TemplateVariable.VariableType.*; + import lombok.Getter; import lombok.NonNull; import lombok.RequiredArgsConstructor; import java.util.Collections; import java.util.List; -import java.util.stream.Stream; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.PersistentEntity; @@ -38,8 +39,6 @@ import org.springframework.hateoas.TemplateVariables; import org.springframework.hateoas.UriTemplate; import org.springframework.util.Assert; -import static org.springframework.hateoas.TemplateVariable.VariableType.REQUEST_PARAM; - /** * A value object to for {@link Link}s representing associations. * @@ -73,31 +72,14 @@ public class Associations { ResourceMapping propertyMapping = metadata.getMappingFor(property); String href = path.slash(propertyMapping.getPath()).toString(); - String uri = new UriTemplate(href, getProjectionVariable(property)).toString(); - return Collections.singletonList(new Link(uri, propertyMapping.getRel())); + UriTemplate template = new UriTemplate(href, getProjectionVariable(property)); + + return Collections.singletonList(new Link(template, propertyMapping.getRel())); } return Collections.emptyList(); } - private TemplateVariables getProjectionVariable(PersistentProperty property) { - ProjectionDefinitionConfiguration projectionConfiguration = config.getProjectionConfiguration(); - if (isProjectionPresent(property, projectionConfiguration)) { - return new TemplateVariables(new TemplateVariable(projectionConfiguration.getParameterName(), REQUEST_PARAM)); - } else { - return TemplateVariables.NONE; - } - } - - private boolean isProjectionPresent(PersistentProperty property, ProjectionDefinitionConfiguration projectionConfiguration) { - return Stream.of(property.getType(), - property.getActualType(), - property.getRawType(), - property.getComponentType(), - property.getMapValueType()) - .anyMatch(projectionConfiguration::hasProjectionFor); - } - /** * Returns the {@link ResourceMetadata} for the given type. * @@ -164,4 +146,13 @@ public class Associations { metadata = mappings.getMetadataFor(property.getActualType()); return metadata == null ? false : metadata.isExported(); } + + private TemplateVariables getProjectionVariable(PersistentProperty property) { + + ProjectionDefinitionConfiguration projectionConfiguration = config.getProjectionConfiguration(); + + return projectionConfiguration.hasProjectionFor(property.getActualType()) // + ? new TemplateVariables(new TemplateVariable(projectionConfiguration.getParameterName(), REQUEST_PARAM)) // + : TemplateVariables.NONE; + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AssociationLinksUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AssociationLinksUnitTests.java index bd577f152..9cd19f522 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AssociationLinksUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AssociationLinksUnitTests.java @@ -45,6 +45,7 @@ import org.springframework.hateoas.Link; * Unit tests for {@link Associations}. * * @author Oliver Gierke + * @author Haroun Pacquee */ @RunWith(MockitoJUnitRunner.class) public class AssociationLinksUnitTests { @@ -61,7 +62,9 @@ public class AssociationLinksUnitTests { @Before public void setUp() { + doReturn(projectionDefinitionConfig).when(config).getProjectionConfiguration(); + this.mappingContext = new KeyValueMappingContext<>(); this.entity = mappingContext.getRequiredPersistentEntity(Sample.class); this.mappings = new PersistentEntitiesResourceMappings(new PersistentEntities(Arrays.asList(mappingContext))); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mapping/AssociationsUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mapping/AssociationsUnitTests.java index 03ebecc56..79c5891d4 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mapping/AssociationsUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mapping/AssociationsUnitTests.java @@ -133,11 +133,14 @@ public class AssociationsUnitTests { assertThat(links).hasSize(0); } - @Test + @Test // DATAREST-1105 public void detectsProjectionsForAssociationLinks() { + String projectionParameterName = "projection"; + doReturn(true).when(projectionDefinitionConfiguration).hasProjectionFor(RelatedAndExported.class); doReturn(projectionParameterName).when(projectionDefinitionConfiguration).getParameterName(); + List links = associations.getLinksFor(getAssociation(Root.class, "relatedAndExported"), new Path("")); assertThat(links).hasSize(1); @@ -162,4 +165,4 @@ public class AssociationsUnitTests { static class RelatedAndExported {} static class RelatedButNotExported {} -} \ No newline at end of file +}