Skip to content

Commit d622ff0

Browse files
committed
DATAREST-994 - Fixed two argument constructor of RepositoryRestHandlerMapping.
Repositories in RepositoryCorsConfigurationAccessor may be null now. findCorsConfiguration returns null when no repositories are provided.
1 parent 06ebcb9 commit d622ff0

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,14 @@ public String resolveStringValue(String value) {
266266
static class RepositoryCorsConfigurationAccessor {
267267

268268
private final @NonNull ResourceMappings mappings;
269-
private final @NonNull Repositories repositories;
269+
private final Repositories repositories;
270270
private final @NonNull StringValueResolver embeddedValueResolver;
271271

272272
CorsConfiguration findCorsConfiguration(String lookupPath) {
273273

274274
ResourceMetadata resource = getResourceMetadata(getRepositoryBasePath(lookupPath));
275275

276-
return resource != null ? createConfiguration(
276+
return resource != null && repositories != null ? createConfiguration(
277277
repositories.getRepositoryInformationFor(resource.getDomainType()).getRepositoryInterface()) : null;
278278
}
279279

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2013-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.rest.webmvc;
17+
18+
import static java.util.Arrays.*;
19+
import static java.util.Collections.singletonList;
20+
import static org.hamcrest.CoreMatchers.*;
21+
import static org.junit.Assert.*;
22+
import static org.mockito.Mockito.*;
23+
24+
import java.util.Collections;
25+
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.mockito.Mock;
30+
import org.mockito.runners.MockitoJUnitRunner;
31+
import org.springframework.data.rest.core.Path;
32+
import org.springframework.data.rest.core.mapping.ResourceMappings;
33+
import org.springframework.data.rest.core.mapping.ResourceMetadata;
34+
35+
/**
36+
* Unit tests for {@link RepositoryRestHandlerMapping.RepositoryCorsConfigurationAccessor}.
37+
*
38+
* @author Jens Schauder
39+
*/
40+
@RunWith(MockitoJUnitRunner.class)
41+
public class RepositoryCorsConfigurationAccessorTest {
42+
43+
@Mock ResourceMappings mappings;
44+
45+
@Before
46+
public void before() {
47+
ResourceMetadata resourceMetadata = mock(ResourceMetadata.class);
48+
when(resourceMetadata.getPath()).thenReturn(new Path("/people"));
49+
when(resourceMetadata.isExported()).thenReturn(true);
50+
51+
when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true);
52+
when(mappings.iterator()).thenReturn(singletonList(resourceMetadata).iterator());
53+
}
54+
55+
@Test // DATAREST-994
56+
public void returnsNullCorsConfigurationWithNullRepositories() {
57+
58+
RepositoryRestHandlerMapping.RepositoryCorsConfigurationAccessor configurationAccessor =
59+
new RepositoryRestHandlerMapping.RepositoryCorsConfigurationAccessor(
60+
mappings,
61+
null,
62+
RepositoryRestHandlerMapping.NoOpStringValueResolver.INSTANCE);
63+
64+
assertThat(configurationAccessor.findCorsConfiguration("/people"), is(nullValue()));
65+
}
66+
67+
}

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
/**
4444
* Unit tests for {@link RepositoryRestHandlerMapping}.
45-
*
45+
*
4646
* @author Oliver Gierke
4747
* @author Greg Turnquist
4848
*/
@@ -212,4 +212,10 @@ public void rejectsUnexpandedUriTemplateWithNotFound() throws Exception {
212212

213213
assertThat(handlerMapping.getHandler(mockRequest), is(nullValue()));
214214
}
215+
216+
@Test // DATAREST-994
217+
public void twoArgumentConstructorDoesNotThrowException() {
218+
219+
new RepositoryRestHandlerMapping(mappings, configuration);
220+
}
215221
}

0 commit comments

Comments
 (0)