DATACMNS-1042 - PagedResourceAssembler now keeps pagination information for self link.

We now hand the Pageable contained in the Page to the code that constructs the self link.

Added toEmptyResource(Page, Class) to force clients to resolve an optional base link before calling the method. Removed deprecated method to append pagination template parameters.
This commit is contained in:
Oliver Gierke
2017-04-18 09:38:37 +02:00
parent 4918eca7cf
commit 46d7e47239
3 changed files with 59 additions and 46 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.data.web;
import static org.assertj.core.api.Assertions.*;
import java.lang.reflect.Method;
import java.util.Optional;
import org.junit.Before;
import org.junit.Rule;
@@ -130,9 +131,12 @@ public class PagedResourcesAssemblerArgumentResolverUnitTests {
Object result = resolver.resolveArgument(methodParameter, null, null, null);
assertThat(result).isInstanceOf(PagedResourcesAssembler.class);
UriComponents uriComponents = (UriComponents) ReflectionTestUtils.getField(result, "baseUri");
assertThat(uriComponents.getPath()).isEqualTo("/foo/mapping");
Optional<UriComponents> uriComponents = (Optional<UriComponents>) ReflectionTestUtils.getField(result, "baseUri");
assertThat(uriComponents).hasValueSatisfying(it -> {
assertThat(it.getPath()).isEqualTo("/foo/mapping");
});
}
private void assertSelectsParameter(Method method, int expectedIndex) throws Exception {

View File

@@ -18,7 +18,6 @@ package org.springframework.data.web;
import static org.assertj.core.api.Assertions.*;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -111,7 +110,7 @@ public class PagedResourcesAssemblerUnitTests {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1), link);
assertThat(resources.getLink(Link.REL_PREVIOUS).getHref()).startsWith(link.getHref());
assertThat(resources.getLink(Link.REL_SELF)).isNotNull();
assertThat(resources.getLink(Link.REL_SELF)).isEqualTo(link.withSelfRel());
assertThat(resources.getLink(Link.REL_NEXT).getHref()).startsWith(link.getHref());
}
@@ -131,8 +130,7 @@ public class PagedResourcesAssemblerUnitTests {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
Link selfLink = resources.getLink(Link.REL_SELF);
assertThat(selfLink.getHref()).endsWith("localhost");
assertThat(resources.getLink(Link.REL_SELF).getHref()).doesNotContain("{").doesNotContain("}");
}
@Test // DATACMNS-418
@@ -170,7 +168,7 @@ public class PagedResourcesAssemblerUnitTests {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
assertThat(resources.getLink(Link.REL_SELF).getHref()).endsWith("localhost");
assertThat(resources.getLink(Link.REL_SELF).getHref()).doesNotContain("{").doesNotContain("}");
assertThat(resources.getLink(Link.REL_NEXT).getHref()).endsWith("?page=2&size=1");
assertThat(resources.getLink(Link.REL_PREVIOUS).getHref()).endsWith("?page=0&size=1");
}
@@ -178,7 +176,7 @@ public class PagedResourcesAssemblerUnitTests {
@Test // DATACMNS-699
public void generatesEmptyPagedResourceWithEmbeddedWrapper() {
PagedResources<?> result = assembler.toEmptyResource(EMPTY_PAGE, Person.class, null);
PagedResources<?> result = assembler.toEmptyResource(EMPTY_PAGE, Person.class);
Collection<?> content = result.getContent();
assertThat(content).hasSize(1);
@@ -190,12 +188,12 @@ public class PagedResourcesAssemblerUnitTests {
@Test(expected = IllegalArgumentException.class) // DATACMNS-699
public void emptyPageCreatorRejectsPageWithContent() {
assembler.toEmptyResource(createPage(1), Person.class, null);
assembler.toEmptyResource(createPage(1), Person.class);
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-699
public void emptyPageCreatorRejectsNullType() {
assembler.toEmptyResource(EMPTY_PAGE, null, null);
assembler.toEmptyResource(EMPTY_PAGE, null);
}
@Test // DATACMNS-701
@@ -246,6 +244,14 @@ public class PagedResourcesAssemblerUnitTests {
assertThat(assembler.toResource(EMPTY_PAGE)).isInstanceOf(CustomPagedResources.class);
}
@Test // DATACMNS-1042
public void selfLinkContainsCoordinatesForCurrentPage() {
PagedResources<Resource<Person>> resource = assembler.toResource(createPage(0));
assertThat(resource.getLink(Link.REL_SELF).getHref()).endsWith("?page=0&size=1");
}
private static Page<Person> createPage(int index) {
Pageable request = PageRequest.of(index, 1);