diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java index 8c45e07fc..bee82255b 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -55,7 +55,7 @@ * {@link org.springframework.data.repository.Repository} is exported under that URL path segment. Also ensures the * {@link OpenEntityManagerInViewInterceptor} is registered in the application context. The OEMIVI is required for the * REST exporter to function properly. - * + * * @author Jon Brisbin * @author Oliver Gierke * @author Mark Paluch @@ -75,7 +75,7 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping { /** * Creates a new {@link RepositoryRestHandlerMapping} for the given {@link ResourceMappings} and * {@link RepositoryRestConfiguration}. - * + * * @param mappings must not be {@literal null}. * @param config must not be {@literal null}. */ @@ -102,8 +102,8 @@ public RepositoryRestHandlerMapping(ResourceMappings mappings, RepositoryRestCon this.mappings = mappings; this.configuration = config; this.repositories = repositories; - this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings, repositories, - NoOpStringValueResolver.INSTANCE); + this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings, + NoOpStringValueResolver.INSTANCE, repositories); } /** @@ -122,8 +122,8 @@ public void setEmbeddedValueResolver(StringValueResolver resolver) { super.setEmbeddedValueResolver(resolver); - this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings, repositories, - resolver == null ? NoOpStringValueResolver.INSTANCE : resolver); + this.corsConfigurationAccessor = new RepositoryCorsConfigurationAccessor(mappings, + resolver == null ? NoOpStringValueResolver.INSTANCE : resolver, repositories); } /* @@ -223,7 +223,7 @@ protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequ /** * Returns the first segment of the given repository lookup path. - * + * * @param repositoryLookupPath must not be {@literal null}. * @return */ @@ -266,14 +266,14 @@ public String resolveStringValue(String value) { static class RepositoryCorsConfigurationAccessor { private final @NonNull ResourceMappings mappings; - private final @NonNull Repositories repositories; private final @NonNull StringValueResolver embeddedValueResolver; + private final Repositories repositories; CorsConfiguration findCorsConfiguration(String lookupPath) { ResourceMetadata resource = getResourceMetadata(getRepositoryBasePath(lookupPath)); - return resource != null ? createConfiguration( + return resource != null && repositories != null ? createConfiguration( repositories.getRepositoryInformationFor(resource.getDomainType()).getRepositoryInterface()) : null; } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryCorsConfigurationAccessorUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryCorsConfigurationAccessorUnitTests.java index d0e66e28f..92764ee5f 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryCorsConfigurationAccessorUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryCorsConfigurationAccessorUnitTests.java @@ -15,8 +15,10 @@ */ package org.springframework.data.rest.webmvc; +import static java.util.Collections.*; import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.*; +import static org.mockito.Mockito.*; import org.junit.Before; import org.junit.Test; @@ -24,7 +26,9 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.data.repository.support.Repositories; +import org.springframework.data.rest.core.Path; import org.springframework.data.rest.core.mapping.ResourceMappings; +import org.springframework.data.rest.core.mapping.ResourceMetadata; import org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping.NoOpStringValueResolver; import org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping.RepositoryCorsConfigurationAccessor; import org.springframework.web.bind.annotation.CrossOrigin; @@ -49,7 +53,7 @@ public class RepositoryCorsConfigurationAccessorUnitTests { @Before public void before() throws Exception { - accessor = new RepositoryCorsConfigurationAccessor(mappings, repositories, NoOpStringValueResolver.INSTANCE); + accessor = new RepositoryCorsConfigurationAccessor(mappings, NoOpStringValueResolver.INSTANCE, repositories); } @Test // DATAREST-573 @@ -82,6 +86,21 @@ public void createConfigurationShouldConstructFullCorsConfiguration() { assertThat(configuration.getMaxAge(), is(1234L)); } + @Test // DATAREST-994 + public void returnsNullCorsConfigurationWithNullRepositories() { + + accessor = new RepositoryCorsConfigurationAccessor(mappings, NoOpStringValueResolver.INSTANCE, null); + + ResourceMetadata resourceMetadata = mock(ResourceMetadata.class); + when(resourceMetadata.getPath()).thenReturn(new Path("/people")); + when(resourceMetadata.isExported()).thenReturn(true); + + when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true); + when(mappings.iterator()).thenReturn(singletonList(resourceMetadata).iterator()); + + assertThat(accessor.findCorsConfiguration("/people"), is(nullValue())); + } + interface PlainRepository {} @CrossOrigin diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java index e49f25924..cd43863b0 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java @@ -42,7 +42,7 @@ /** * Unit tests for {@link RepositoryRestHandlerMapping}. - * + * * @author Oliver Gierke * @author Greg Turnquist */ @@ -212,4 +212,9 @@ public void rejectsUnexpandedUriTemplateWithNotFound() throws Exception { assertThat(handlerMapping.getHandler(mockRequest), is(nullValue())); } + + @Test // DATAREST-994 + public void twoArgumentConstructorDoesNotThrowException() { + new RepositoryRestHandlerMapping(mappings, configuration); + } }