DATAREST-514 - Fixed resource exposure for customized associations.
We no correctly handle the customized association path if @RestResource is used on an association. Took the chance to refactor the resource mapping subsystem quite significantly to improve the handling of property mappings. Those had been externalized before.
This commit is contained in:
@@ -24,9 +24,12 @@ import java.util.List;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.PrePersist;
|
||||
|
||||
import org.springframework.data.rest.core.annotation.RestResource;
|
||||
|
||||
/**
|
||||
* An entity that represents a person.
|
||||
*
|
||||
@@ -40,6 +43,7 @@ public class Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
@OneToMany private List<Person> siblings = Collections.emptyList();
|
||||
private @RestResource(path = "father-mapped") @ManyToOne Person father;
|
||||
private Date created;
|
||||
|
||||
public Person() {}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2015 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.core.mapping;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.mapping.context.PersistentEntities;
|
||||
import org.springframework.data.mongodb.core.mapping.DBRef;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
|
||||
import org.springframework.data.rest.core.annotation.RestResource;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MappingResourceMetadataUnitTests {
|
||||
|
||||
MongoMappingContext context = new MongoMappingContext();
|
||||
|
||||
@Test
|
||||
public void allowsLookupOfPropertyByMappedName() {
|
||||
|
||||
ResourceMappings resourceMappings = new PersistentEntitiesResourceMappings(new PersistentEntities(
|
||||
Arrays.asList(context)));
|
||||
|
||||
MongoPersistentEntity<?> entity = context.getPersistentEntity(Entity.class);
|
||||
MongoPersistentProperty property = entity.getPersistentProperty("related");
|
||||
|
||||
MappingResourceMetadata metadata = new MappingResourceMetadata(entity, resourceMappings).init();
|
||||
|
||||
PropertyAwareResourceMapping propertyMapping = metadata.getProperty("foo");
|
||||
|
||||
assertThat(propertyMapping, is(notNullValue()));
|
||||
assertThat(propertyMapping.getProperty(), is((Object) property));
|
||||
assertThat(metadata.getMappingFor(property).getPath().matches("foo"), is(true));
|
||||
}
|
||||
|
||||
static class Entity {
|
||||
@DBRef @RestResource(rel = "foo", path = "foo") private Related related;
|
||||
}
|
||||
|
||||
static class Related {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -18,11 +18,13 @@ package org.springframework.data.rest.core.mapping;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.mapping.context.PersistentEntities;
|
||||
import org.springframework.data.mongodb.core.mapping.DBRef;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
||||
@@ -30,7 +32,6 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
|
||||
import org.springframework.data.rest.core.Path;
|
||||
import org.springframework.data.rest.core.annotation.Description;
|
||||
import org.springframework.data.rest.core.annotation.RestResource;
|
||||
import org.springframework.data.rest.core.mapping.RepositoryResourceMappings.PersistentPropertyResourceMapping;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PersistentPropertyResourceMapping}.
|
||||
@@ -113,10 +114,10 @@ public class PersistentPropertyResourceMappingUnitTests {
|
||||
MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entity);
|
||||
MongoPersistentProperty property = persistentEntity.getPersistentProperty(propertyName);
|
||||
|
||||
CollectionResourceMapping entityResourceMapping = new TypeBasedCollectionResourceMapping(entity);
|
||||
ResourceMapping propertyTypeMapping = new TypeBasedCollectionResourceMapping(property.getType());
|
||||
ResourceMappings resourceMappings = new PersistentEntitiesResourceMappings(new PersistentEntities(
|
||||
Arrays.asList(mappingContext)));
|
||||
|
||||
return new PersistentPropertyResourceMapping(property, propertyTypeMapping, entityResourceMapping);
|
||||
return new PersistentPropertyResourceMapping(property, resourceMappings);
|
||||
}
|
||||
|
||||
public static class Entity {
|
||||
|
||||
@@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
@@ -27,11 +28,12 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.context.PersistentEntities;
|
||||
import org.springframework.data.repository.support.Repositories;
|
||||
import org.springframework.data.rest.core.Path;
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.core.domain.jpa.Author;
|
||||
import org.springframework.data.rest.core.domain.jpa.CreditCard;
|
||||
import org.springframework.data.rest.core.domain.jpa.JpaRepositoryConfig;
|
||||
@@ -41,7 +43,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link ResourceMappings}.
|
||||
* Integration tests for {@link RepositoryResourceMappings}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Greg Trunquist
|
||||
@@ -49,9 +51,10 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = JpaRepositoryConfig.class)
|
||||
@Transactional
|
||||
public class ResourceMappingsIntegrationTests {
|
||||
public class RepositoryResourceMappingsIntegrationTests {
|
||||
|
||||
@Autowired ListableBeanFactory factory;
|
||||
@Autowired JpaMetamodelMappingContext mappingContext;
|
||||
|
||||
ResourceMappings mappings;
|
||||
|
||||
@@ -59,18 +62,18 @@ public class ResourceMappingsIntegrationTests {
|
||||
public void setUp() {
|
||||
|
||||
Repositories repositories = new Repositories(factory);
|
||||
this.mappings = new RepositoryResourceMappings(new RepositoryRestConfiguration(), repositories);
|
||||
this.mappings = new RepositoryResourceMappings(repositories, new PersistentEntities(Arrays.asList(mappingContext)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsAllMappings() {
|
||||
assertThat(mappings, is(Matchers.<ResourceMetadata> iterableWithSize(8)));
|
||||
assertThat(mappings, is(Matchers.<ResourceMetadata> iterableWithSize(4)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exportsResourceAndSearchesForPersons() {
|
||||
|
||||
ResourceMetadata personMappings = mappings.getMappingFor(Person.class);
|
||||
ResourceMetadata personMappings = mappings.getMetadataFor(Person.class);
|
||||
|
||||
assertThat(personMappings.isExported(), is(true));
|
||||
assertThat(personMappings.getSearchResourceMappings().isExported(), is(true));
|
||||
@@ -79,7 +82,7 @@ public class ResourceMappingsIntegrationTests {
|
||||
@Test
|
||||
public void doesNotExportAnyMappingsForHiddenRepository() {
|
||||
|
||||
ResourceMetadata creditCardMapping = mappings.getMappingFor(CreditCard.class);
|
||||
ResourceMetadata creditCardMapping = mappings.getMetadataFor(CreditCard.class);
|
||||
|
||||
assertThat(creditCardMapping.isExported(), is(false));
|
||||
assertThat(creditCardMapping.getSearchResourceMappings().isExported(), is(false));
|
||||
@@ -95,7 +98,7 @@ public class ResourceMappingsIntegrationTests {
|
||||
PersistentEntity<?, ?> entity = repositories.getPersistentEntity(Person.class);
|
||||
PersistentProperty<?> property = entity.getPersistentProperty("siblings");
|
||||
|
||||
ResourceMetadata metadata = mappings.getMappingFor(Person.class);
|
||||
ResourceMetadata metadata = mappings.getMetadataFor(Person.class);
|
||||
ResourceMapping mapping = metadata.getMappingFor(property);
|
||||
|
||||
assertThat(mapping.getRel(), is("siblings"));
|
||||
@@ -112,7 +115,7 @@ public class ResourceMappingsIntegrationTests {
|
||||
assertThat(mappings.exportsTopLevelResourceFor("people"), is(true));
|
||||
assertThat(mappings.exportsTopLevelResourceFor("orders"), is(true));
|
||||
|
||||
ResourceMetadata creditCardMapping = mappings.getMappingFor(CreditCard.class);
|
||||
ResourceMetadata creditCardMapping = mappings.getMetadataFor(CreditCard.class);
|
||||
assertThat(creditCardMapping, is(notNullValue()));
|
||||
assertThat(creditCardMapping.getPath(), is(new Path("creditCards")));
|
||||
assertThat(creditCardMapping.isExported(), is(false));
|
||||
@@ -125,12 +128,12 @@ public class ResourceMappingsIntegrationTests {
|
||||
@Test
|
||||
public void skipsSearchMethodsNotExported() {
|
||||
|
||||
ResourceMetadata creditCardMetadata = mappings.getMappingFor(CreditCard.class);
|
||||
ResourceMetadata creditCardMetadata = mappings.getMetadataFor(CreditCard.class);
|
||||
SearchResourceMappings searchResourceMappings = creditCardMetadata.getSearchResourceMappings();
|
||||
|
||||
assertThat(searchResourceMappings, is(Matchers.<MethodResourceMapping> iterableWithSize(0)));
|
||||
|
||||
ResourceMetadata personMetadata = mappings.getMappingFor(Person.class);
|
||||
ResourceMetadata personMetadata = mappings.getMetadataFor(Person.class);
|
||||
List<String> methodNames = new ArrayList<String>();
|
||||
|
||||
for (MethodResourceMapping method : personMetadata.getSearchResourceMappings()) {
|
||||
@@ -147,7 +150,7 @@ public class ResourceMappingsIntegrationTests {
|
||||
@Test
|
||||
public void exposesMethodResourceMappingInPackageProtectedButExportedRepo() {
|
||||
|
||||
ResourceMetadata metadata = mappings.getMappingFor(Author.class);
|
||||
ResourceMetadata metadata = mappings.getMetadataFor(Author.class);
|
||||
assertThat(metadata.isExported(), is(true));
|
||||
|
||||
SearchResourceMappings searchMappings = metadata.getSearchResourceMappings();
|
||||
@@ -161,4 +164,15 @@ public class ResourceMappingsIntegrationTests {
|
||||
assertThat(methodMapping.isExported(), is(true));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testname() {
|
||||
|
||||
ResourceMetadata metadata = mappings.getMetadataFor(Person.class);
|
||||
|
||||
PropertyAwareResourceMapping propertyMapping = metadata.getProperty("father-mapped");
|
||||
|
||||
assertThat(propertyMapping.getRel(), is("father"));
|
||||
assertThat(propertyMapping.getPath(), is(new Path("father-mapped")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user