DATAREST-473 - Introduced configuration option to control default exposure of repositories.

RepositoryRestConfiguration now exposes a setRepositoryDetectionStrategy(…) to define which repositories should be detected for exposure by default. The default value for that will consider the repository interfaces visibility but also take the exported flag of @(Repository)RestResource into account. See all other options in RepositoryDetectionStrategies.

Tweaked the auto-registration of excerpt projections to avoid a circular dependency between configuration and resource mappings and moved it onto a BeanPostProcessor implementation.
This commit is contained in:
Oliver Gierke
2015-11-25 11:32:34 +01:00
parent b663b5ff73
commit dc36dfb67a
13 changed files with 416 additions and 85 deletions

View File

@@ -17,14 +17,9 @@ package org.springframework.data.rest.core.config;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.data.rest.core.config.ProjectionDefinitionConfiguration.ProjectionDefinitionKey;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
/**
* Unit tests for {@link ProjectionDefinitionConfiguration}.
@@ -155,19 +150,6 @@ public class ProjectionDefinitionConfigurationUnitTests {
is(typeCompatibleWith(ParentProjection.class)));
}
/**
* @see DATAREST-577
*/
@Test
public void registersExcerptProjectionsByDefault() {
ResourceMetadata metadata = mock(ResourceMetadata.class);
doReturn(SampleProjection.class).when(metadata).getExcerptProjection();
assertThat(new ProjectionDefinitionConfiguration(Arrays.asList(metadata)).getProjectionsFor(Integer.class),
Matchers.<String, Class<?>> hasEntry("name", SampleProjection.class));
}
@Projection(name = "name", types = Integer.class)
interface SampleProjection {}

View File

@@ -27,6 +27,7 @@ import org.springframework.data.repository.core.support.DefaultRepositoryMetadat
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies;
/**
* Unit tests for {@link RepositoryCollectionResourceMapping}.
@@ -105,7 +106,8 @@ public class RepositoryCollectionResourceMappingUnitTests {
}
};
RepositoryCollectionResourceMapping mapping = new RepositoryCollectionResourceMapping(metadata);
RepositoryCollectionResourceMapping mapping = new RepositoryCollectionResourceMapping(metadata,
RepositoryDetectionStrategies.DEFAULT);
assertThat(mapping.getPath(), is(new Path("/objects")));
}
@@ -113,7 +115,7 @@ public class RepositoryCollectionResourceMappingUnitTests {
private static CollectionResourceMapping getResourceMappingFor(Class<?> repositoryInterface) {
RepositoryMetadata metadata = new DefaultRepositoryMetadata(repositoryInterface);
return new RepositoryCollectionResourceMapping(metadata);
return new RepositoryCollectionResourceMapping(metadata, RepositoryDetectionStrategies.DEFAULT);
}
public static class Person {}

View File

@@ -0,0 +1,122 @@
/*
* 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 static org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Test;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies;
/**
* Unit tests for {@link RepositoryDetectionStrategies}.
*
* @author Oliver Gierke
* @soundtrack Katinka - Ausverkauf
*/
@SuppressWarnings("serial")
public class RepositoryDetectionStrategiesUnitTests {
/**
* @see DATAREST-473
*/
@Test
public void allExposesAllRepositories() {
assertExposures(ALL, new HashMap<Class<?>, Boolean>() {
{
put(AnnotatedRepository.class, true);
put(HiddenRepository.class, true);
put(PublicRepository.class, true);
put(PackageProtectedRepository.class, true);
}
});
}
/**
* @see DATAREST-473
*/
@Test
public void defaultHonorsVisibilityAndAnnotations() {
assertExposures(DEFAULT, new HashMap<Class<?>, Boolean>() {
{
put(AnnotatedRepository.class, true);
put(HiddenRepository.class, false);
put(PublicRepository.class, true);
put(PackageProtectedRepository.class, false);
}
});
}
/**
* @see DATAREST-473
*/
@Test
public void visibilityHonorsTypeVisibilityOnly() {
assertExposures(VISIBILITY, new HashMap<Class<?>, Boolean>() {
{
put(AnnotatedRepository.class, false);
put(HiddenRepository.class, true);
put(PublicRepository.class, true);
put(PackageProtectedRepository.class, false);
}
});
}
/**
* @see DATAREST-473
*/
@Test
public void annotatedHonorsAnnotationsOnly() {
assertExposures(ANNOTATED, new HashMap<Class<?>, Boolean>() {
{
put(AnnotatedRepository.class, true);
put(HiddenRepository.class, false);
put(PublicRepository.class, false);
put(PackageProtectedRepository.class, false);
}
});
}
private static void assertExposures(RepositoryDetectionStrategy strategy, Map<Class<?>, Boolean> expected) {
for (Entry<Class<?>, Boolean> entry : expected.entrySet()) {
assertThat(strategy.isExported(new DefaultRepositoryMetadata(entry.getKey())), is(entry.getValue()));
}
}
interface PackageProtectedRepository extends Repository<Object, Long> {}
public interface PublicRepository extends Repository<Object, Long> {}
@RepositoryRestResource
interface AnnotatedRepository extends Repository<Object, Long> {}
@RepositoryRestResource(exported = false)
public interface HiddenRepository extends Repository<Object, Long> {}
}

View File

@@ -30,6 +30,7 @@ import org.springframework.data.repository.core.support.DefaultRepositoryMetadat
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies;
/**
* Unit tests for {@link RepositoryMethodResourceMapping}.
@@ -39,7 +40,8 @@ import org.springframework.data.rest.core.annotation.RestResource;
public class RepositoryMethodResourceMappingUnitTests {
RepositoryMetadata metadata = new DefaultRepositoryMetadata(PersonRepository.class);
RepositoryCollectionResourceMapping resourceMapping = new RepositoryCollectionResourceMapping(metadata);
RepositoryCollectionResourceMapping resourceMapping = new RepositoryCollectionResourceMapping(metadata,
RepositoryDetectionStrategies.DEFAULT);
@Test
public void defaultsMappingToMethodName() throws Exception {

View File

@@ -38,6 +38,8 @@ 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;
import org.springframework.data.rest.core.domain.jpa.Person;
import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies;
import org.springframework.hateoas.core.EvoInflectorRelProvider;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@@ -62,7 +64,8 @@ public class RepositoryResourceMappingsIntegrationTests {
public void setUp() {
Repositories repositories = new Repositories(factory);
this.mappings = new RepositoryResourceMappings(repositories, new PersistentEntities(Arrays.asList(mappingContext)));
this.mappings = new RepositoryResourceMappings(repositories, new PersistentEntities(Arrays.asList(mappingContext)),
new EvoInflectorRelProvider(), RepositoryDetectionStrategies.DEFAULT);
}
@Test