Browse Source
PagedResourcesAssembler now adds a LinkTemplate to every PagedResources instance created and pulls the template variable information from the registered resolvers for Pageable and Sort. Extended PageableResourceAssemblerArgumentResolver to create a special MethodParameterAwarePagedResourcesAssembler so that the assembler will take qualifier information into account when being injected into controller methods. Upgraded to Spring HATEOAS 0.9.0.BUILD-SNAPSHOT to be able to benefit from new methods added to MethodParameters as well as the LinkTemplate class. Updated Sonargraph architecture definition to allow the web layer to access logging.pull/61/head
10 changed files with 395 additions and 11 deletions
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
/* |
||||
* Copyright 2014 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.web; |
||||
|
||||
import org.springframework.core.MethodParameter; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.web.util.UriComponents; |
||||
|
||||
/** |
||||
* Custom {@link PagedResourcesAssembler} that is aware of the {@link MethodParameter} it shall create links for. |
||||
* |
||||
* @author Oliver Gierke |
||||
* @since 1.7 |
||||
*/ |
||||
class MethodParameterAwarePagedResourcesAssembler<T> extends PagedResourcesAssembler<T> { |
||||
|
||||
private final MethodParameter parameter; |
||||
|
||||
/** |
||||
* Creates a new {@link MethodParameterAwarePagedResourcesAssembler} using the given {@link MethodParameter}, |
||||
* {@link HateoasPageableHandlerMethodArgumentResolver} and base URI. |
||||
* |
||||
* @param parameter must not be {@literal null}. |
||||
* @param resolver can be {@literal null}. |
||||
* @param baseUri can be {@literal null}. |
||||
*/ |
||||
public MethodParameterAwarePagedResourcesAssembler(MethodParameter parameter, |
||||
HateoasPageableHandlerMethodArgumentResolver resolver, UriComponents baseUri) { |
||||
|
||||
super(resolver, baseUri); |
||||
|
||||
Assert.notNull(parameter, "Method parameter must not be null!"); |
||||
this.parameter = parameter; |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see org.springframework.data.web.PagedResourcesAssembler#getMethodParameter() |
||||
*/ |
||||
@Override |
||||
protected MethodParameter getMethodParameter() { |
||||
return parameter; |
||||
} |
||||
} |
||||
@ -0,0 +1,182 @@
@@ -0,0 +1,182 @@
|
||||
/* |
||||
* Copyright 2014 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.web; |
||||
|
||||
import static org.hamcrest.CoreMatchers.*; |
||||
import static org.junit.Assert.*; |
||||
|
||||
import java.lang.reflect.Method; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.junit.rules.ExpectedException; |
||||
import org.springframework.beans.factory.annotation.Qualifier; |
||||
import org.springframework.core.MethodParameter; |
||||
import org.springframework.data.domain.Pageable; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
||||
/** |
||||
* Unit tests for {@link PagedResourcesAssemblerArgumentResolver}. |
||||
* |
||||
* @author Oliver Gierke |
||||
* @since 1.7 |
||||
*/ |
||||
public class PagedResourcesAssemblerArgumentResolverUnitTests { |
||||
|
||||
PagedResourcesAssemblerArgumentResolver resolver; |
||||
|
||||
public @Rule ExpectedException exception = ExpectedException.none(); |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
|
||||
WebTestUtils.initWebTest(); |
||||
|
||||
HateoasPageableHandlerMethodArgumentResolver hateoasPageableHandlerMethodArgumentResolver = new HateoasPageableHandlerMethodArgumentResolver(); |
||||
this.resolver = new PagedResourcesAssemblerArgumentResolver(hateoasPageableHandlerMethodArgumentResolver, null); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-418 |
||||
*/ |
||||
@Test |
||||
public void createsPlainAssemblerWithoutContext() throws Exception { |
||||
|
||||
Method method = Controller.class.getMethod("noContext", PagedResourcesAssembler.class); |
||||
Object result = resolver.resolveArgument(new MethodParameter(method, 0), null, null, null); |
||||
|
||||
assertThat(result, is(instanceOf(PagedResourcesAssembler.class))); |
||||
assertThat(result, is(not(instanceOf(MethodParameterAwarePagedResourcesAssembler.class)))); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-418 |
||||
*/ |
||||
@Test |
||||
public void selectsUniquePageableParameter() throws Exception { |
||||
|
||||
Method method = Controller.class.getMethod("unique", PagedResourcesAssembler.class, Pageable.class); |
||||
assertSelectsParameter(method, 1); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-418 |
||||
*/ |
||||
@Test |
||||
public void selectsUniquePageableParameterForQualifiedAssembler() throws Exception { |
||||
|
||||
Method method = Controller.class.getMethod("unnecessarilyQualified", PagedResourcesAssembler.class, Pageable.class); |
||||
assertSelectsParameter(method, 1); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-418 |
||||
*/ |
||||
@Test |
||||
public void selectsUniqueQualifiedPageableParameter() throws Exception { |
||||
|
||||
Method method = Controller.class.getMethod("qualifiedUnique", PagedResourcesAssembler.class, Pageable.class); |
||||
assertSelectsParameter(method, 1); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-418 |
||||
*/ |
||||
@Test |
||||
public void selectsQualifiedPageableParameter() throws Exception { |
||||
|
||||
Method method = Controller.class.getMethod("qualified", PagedResourcesAssembler.class, Pageable.class, |
||||
Pageable.class); |
||||
assertSelectsParameter(method, 1); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-418 |
||||
*/ |
||||
@Test |
||||
public void rejectsAmbiguousPageableParameters() throws Exception { |
||||
assertRejectsAmbiguity("unqualifiedAmbiguity"); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-418 |
||||
*/ |
||||
@Test |
||||
public void rejectsAmbiguousPageableParametersForQualifiedAssembler() throws Exception { |
||||
assertRejectsAmbiguity("assemblerQualifiedAmbiguity"); |
||||
} |
||||
|
||||
/** |
||||
* @see DATACMNS-418 |
||||
*/ |
||||
@Test |
||||
public void rejectsAmbiguityWithoutMatchingQualifiers() throws Exception { |
||||
assertRejectsAmbiguity("noMatchingQualifiers"); |
||||
} |
||||
|
||||
private void assertSelectsParameter(Method method, int expectedIndex) throws Exception { |
||||
|
||||
MethodParameter parameter = new MethodParameter(method, 0); |
||||
|
||||
Object result = resolver.resolveArgument(parameter, null, null, null); |
||||
assertMethodParameterAwarePagedResourcesAssemblerFor(result, new MethodParameter(method, expectedIndex)); |
||||
} |
||||
|
||||
private static void assertMethodParameterAwarePagedResourcesAssemblerFor(Object result, MethodParameter parameter) { |
||||
|
||||
assertThat(result, is(instanceOf(MethodParameterAwarePagedResourcesAssembler.class))); |
||||
MethodParameterAwarePagedResourcesAssembler<?> assembler = (MethodParameterAwarePagedResourcesAssembler<?>) result; |
||||
|
||||
assertThat(assembler.getMethodParameter(), is(parameter)); |
||||
} |
||||
|
||||
private void assertRejectsAmbiguity(String methodName) throws Exception { |
||||
|
||||
Method method = Controller.class.getMethod(methodName, PagedResourcesAssembler.class, Pageable.class, |
||||
Pageable.class); |
||||
|
||||
exception.expect(IllegalStateException.class); |
||||
resolver.resolveArgument(new MethodParameter(method, 0), null, null, null); |
||||
} |
||||
|
||||
@RequestMapping("/") |
||||
static interface Controller { |
||||
|
||||
void noContext(PagedResourcesAssembler<Object> resolver); |
||||
|
||||
void unique(PagedResourcesAssembler<Object> assembler, Pageable pageable); |
||||
|
||||
void unnecessarilyQualified(@Qualifier("qualified") PagedResourcesAssembler<Object> assembler, Pageable pageable); |
||||
|
||||
void qualifiedUnique(@Qualifier("qualified") PagedResourcesAssembler<Object> assembler, |
||||
@Qualifier("qualified") Pageable pageable); |
||||
|
||||
void qualified(@Qualifier("qualified") PagedResourcesAssembler<Object> resolver, |
||||
@Qualifier("qualified") Pageable pageable, Pageable unqualified); |
||||
|
||||
void unqualifiedAmbiguity(PagedResourcesAssembler<Object> assembler, Pageable pageable, Pageable unqualified); |
||||
|
||||
void assemblerQualifiedAmbiguity(@Qualifier("qualified") PagedResourcesAssembler<Object> assembler, |
||||
Pageable pageable, Pageable unqualified); |
||||
|
||||
void noMatchingQualifiers(@Qualifier("qualified") PagedResourcesAssembler<Object> assembler, Pageable pageable, |
||||
@Qualifier("qualified2") Pageable unqualified); |
||||
|
||||
@RequestMapping("/{variable}/foo") |
||||
void methodWithPathVariable(PagedResourcesAssembler<Object> assembler); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue