DATACMNS-407 - Fixed sort parameter building in HateoasAwareSortHandlerMethodArgumentResolver.

We're now replacing the sort parameter for the URI to be created instead of simply appending them to the source URI to prevent previously applied sort parameters to be taken forward.
This commit is contained in:
Oliver Gierke
2013-12-16 12:59:50 +01:00
parent f148d19c81
commit e21a7850f6
2 changed files with 26 additions and 9 deletions

View File

@@ -19,6 +19,8 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.domain.Sort.Direction.*;
import java.net.URI;
import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.Sort;
@@ -32,7 +34,7 @@ import org.springframework.web.util.UriComponentsBuilder;
public class HateoasSortHandlerMethodArgumentResolverUnitTests extends SortHandlerMethodArgumentResolverUnitTests {
@Test
public void buildsUpRequestParameters() {
public void buildsUpRequestParameters() throws Exception {
assertUriStringFor(SORT, "sort=firstname,lastname,desc");
assertUriStringFor(new Sort(ASC, "foo").and(new Sort(DESC, "bar").and(new Sort(ASC, "foobar"))),
@@ -41,9 +43,21 @@ public class HateoasSortHandlerMethodArgumentResolverUnitTests extends SortHandl
"sort=foo,bar,asc&sort=foobar,desc");
}
private void assertUriStringFor(Sort sort, String expected) {
/**
* @see DATACMNS-407
*/
@Test
public void replacesExistingRequestParameters() throws Exception {
assertUriStringFor(SORT, "/?sort=firstname,lastname,desc", "/?sort=foo,asc");
}
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/");
private void assertUriStringFor(Sort sort, String expected) throws Exception {
assertUriStringFor(sort, expected, "/");
}
private void assertUriStringFor(Sort sort, String expected, String baseUri) throws Exception {
UriComponentsBuilder builder = UriComponentsBuilder.fromUri(new URI(baseUri));
MethodParameter parameter = getParameterOfMethod("supportedMethod");
new HateoasSortHandlerMethodArgumentResolver().enhance(builder, parameter, sort);