DATAREST-1176 - Polishing.

Moved the flag to decide whether to expose repository methods by default to the RepositoryDetectionStrategy interface, so that it can be directly accessed and the test on the particular enum value is not needed anymore and thus also not duplicated into different parts of the codebase.

Added more tests to actually verify behavior on CrudMethodsSupportedHttpMethods. DefaultExposureAwareCrudMethods uses @RequiredArgumentConstructor again.
This commit is contained in:
Oliver Gierke
2018-01-15 15:08:45 +01:00
parent 5172f89281
commit 6b61d2b026
8 changed files with 88 additions and 38 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.rest.core.mapping;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.rest.core.mapping.ResourceType.*;
import static org.springframework.http.HttpMethod.*;
@@ -23,6 +24,7 @@ import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -48,8 +50,12 @@ import org.springframework.http.HttpMethod;
@RunWith(MockitoJUnitRunner.class)
public class CrudMethodsSupportedHttpMethodsUnitTests {
@Mock
private RepositoryResourceMappings mappings;
@Mock RepositoryResourceMappings mappings;
@Before
public void setUp() {
when(mappings.exposeMethodsByDefault()).thenReturn(true);
}
@Test // DATACMNS-589, DATAREST-409
public void doesNotSupportAnyHttpMethodForEmptyRepository() {
@@ -122,12 +128,24 @@ public class CrudMethodsSupportedHttpMethodsUnitTests {
assertMethodsSupported(getSupportedHttpMethodsFor(NoFindOne.class), ITEM, false, DELETE);
}
@Test // DATAREST-1176
public void onlyExposesExplicitlyAnnotatedMethodsIfConfigured() {
reset(mappings);
when(mappings.exposeMethodsByDefault()).thenReturn(false);
assertMethodsSupported(getSupportedHttpMethodsFor(MethodsExplicitlyExportedRepository.class), COLLECTION, true,
POST, OPTIONS);
assertMethodsSupported(getSupportedHttpMethodsFor(MethodsExplicitlyExportedRepository.class), ITEM, true, OPTIONS,
PUT, PATCH);
}
private SupportedHttpMethods getSupportedHttpMethodsFor(Class<?> repositoryInterface) {
RepositoryMetadata metadata = new DefaultRepositoryMetadata(repositoryInterface);
CrudMethods crudMethods = new DefaultCrudMethods(metadata);
return new CrudMethodsSupportedHttpMethods(crudMethods, mappings);
return new CrudMethodsSupportedHttpMethods(crudMethods, mappings.exposeMethodsByDefault());
}
private static void assertMethodsSupported(SupportedHttpMethods methods, ResourceType type, boolean supported,
@@ -165,6 +183,15 @@ public class CrudMethodsSupportedHttpMethodsUnitTests {
interface EntityRepository extends CrudRepository<Entity, Long> {}
interface MethodsExplicitlyExportedRepository extends Repository<Object, Long> {
@RestResource
<S extends Object> S save(S entity);
@RestResource
void delete(Object entity);
}
class Entity {
Entity embedded;

View File

@@ -92,7 +92,7 @@ public class RepositoryDetectionStrategiesUnitTests {
@Test // DATAREST-1176
public void onlyExplicitAnnotatedMethodsAreExposed() {
assertExposures(EXPLICIT_METHOD_ANNOTATED, new HashMap<Class<?>, Boolean>() {
assertExposures(EXPLICITLY_ANNOTATED, new HashMap<Class<?>, Boolean>() {
{
put(AnnotatedRepository.class, true);
put(HiddenRepository.class, false);
@@ -100,6 +100,8 @@ public class RepositoryDetectionStrategiesUnitTests {
put(PackageProtectedRepository.class, false);
}
});
assertThat(EXPLICITLY_ANNOTATED.exposeMethodsByDefault()).isFalse();
}
private static void assertExposures(RepositoryDetectionStrategy strategy, Map<Class<?>, Boolean> expected) {

View File

@@ -38,9 +38,9 @@ import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.Re
*/
public class RepositoryMethodResourceMappingUnitTests {
RepositoryDetectionStrategy strategy = RepositoryDetectionStrategies.DEFAULT;
RepositoryMetadata metadata = new DefaultRepositoryMetadata(PersonRepository.class);
RepositoryCollectionResourceMapping resourceMapping = new RepositoryCollectionResourceMapping(metadata,
RepositoryDetectionStrategies.DEFAULT);
RepositoryCollectionResourceMapping resourceMapping = new RepositoryCollectionResourceMapping(metadata, strategy);
@Test
public void defaultsMappingToMethodName() throws Exception {
@@ -131,8 +131,7 @@ public class RepositoryMethodResourceMappingUnitTests {
}
private RepositoryMethodResourceMapping getMappingFor(Method method) {
RepositoryDetectionStrategy strategy = RepositoryDetectionStrategies.DEFAULT;
return new RepositoryMethodResourceMapping(method, resourceMapping, metadata, strategy);
return new RepositoryMethodResourceMapping(method, resourceMapping, metadata, strategy.exposeMethodsByDefault());
}
static class Person {}