DATACMNS-701 - PagedResourcesAssembler now adds first and last links.

We now add first and last links to the PagedResources created in line with the appearance of the prev and next links. The PagedResourcesAssembler can be configured to enforce the rendering using setForceFirstAndLastRels(…).
This commit is contained in:
Oliver Gierke
2015-05-19 08:28:06 +02:00
parent c5aaebd0e4
commit 62170ab767
2 changed files with 93 additions and 10 deletions

View File

@@ -30,6 +30,7 @@ import org.springframework.data.domain.AbstractPageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.Resource;
@@ -47,7 +48,8 @@ import org.springframework.web.util.UriComponentsBuilder;
*/
public class PagedResourcesAssemblerUnitTests {
static final Page<Object> EMPTY_PAGE = new PageImpl<Object>(Collections.emptyList());
static final Pageable PAGEABLE = new PageRequest(0, 20);
static final Page<Person> EMPTY_PAGE = new PageImpl<Person>(Collections.<Person> emptyList(), PAGEABLE, 0);
HateoasPageableHandlerMethodArgumentResolver resolver = new HateoasPageableHandlerMethodArgumentResolver();
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<Person>(resolver, null);
@@ -220,6 +222,57 @@ public class PagedResourcesAssemblerUnitTests {
assembler.toEmptyResource(EMPTY_PAGE, null, null);
}
/**
* @see DATACMNS-701
*/
@Test
public void addsFirstAndLastLinksForMultiplePages() {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
assertThat(resources.getLink(Link.REL_FIRST).getHref(), endsWith("?page=0&size=1"));
assertThat(resources.getLink(Link.REL_LAST).getHref(), endsWith("?page=2&size=1"));
}
/**
* @see DATACMNS-701
*/
@Test
public void doesNotAddFirstLinkForFirstPageByDefault() {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(0));
assertThat(resources.getLink(Link.REL_FIRST), is(nullValue()));
assertThat(resources.getLink(Link.REL_LAST).getHref(), endsWith("?page=2&size=1"));
}
/**
* @see DATACMNS-701
*/
@Test
public void doesNotAddLastLinkForLastPageByDefault() {
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(2));
assertThat(resources.getLink(Link.REL_FIRST).getHref(), endsWith("?page=0&size=1"));
assertThat(resources.getLink(Link.REL_LAST), is(nullValue()));
}
/**
* @see DATACMNS-701
*/
@Test
public void alwaysAddsFirstAndLastLinkIfConfiguredTo() {
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<Person>(resolver, null);
assembler.setForceFirstAndLastRels(true);
PagedResources<Resource<Person>> resources = assembler.toResource(EMPTY_PAGE);
assertThat(resources.getLink(Link.REL_FIRST).getHref(), endsWith("?page=0&size=20"));
assertThat(resources.getLink(Link.REL_LAST).getHref(), endsWith("?page=0&size=20"));
}
private static Page<Person> createPage(int index) {
AbstractPageRequest request = new PageRequest(index, 1);