DATAREST-1105 - Polishing.

Simplified code a bit. Formatting. Adapted integration tests to make sure URI templates are always expanded.

Original pull request: #273.
This commit is contained in:
Oliver Gierke
2017-07-19 16:05:27 +02:00
parent 0d7d84571f
commit 5836baaabf
5 changed files with 25 additions and 28 deletions

View File

@@ -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());
}
}
}

View File

@@ -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");
}

View File

@@ -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;
}
}

View File

@@ -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)));

View File

@@ -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<Link> 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 {}
}
}