Skip to content

DATACMNS-1470 - Upgrade to Spring HATEOAS 1.0 API. #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATACMNS-1470-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand All @@ -20,6 +20,7 @@
<vavr>0.9.3</vavr>
<scala>2.11.7</scala>
<xmlbeam>1.4.15</xmlbeam>
<spring-hateoas>1.0.0.BUILD-SNAPSHOT</spring-hateoas>

<java-module-name>spring.data.commons</java-module-name>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.IanaLinkRelation;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.PagedResources.PageMetadata;
Expand Down Expand Up @@ -220,27 +221,27 @@ private <R> PagedResources<R> addPaginationLinks(PagedResources<R> resources, Pa
boolean isNavigable = page.hasPrevious() || page.hasNext();

if (isNavigable || forceFirstAndLastRels) {
resources.add(createLink(base, PageRequest.of(0, page.getSize(), page.getSort()), Link.REL_FIRST));
resources.add(createLink(base, PageRequest.of(0, page.getSize(), page.getSort()), IanaLinkRelation.FIRST.value()));
}

if (page.hasPrevious()) {
resources.add(createLink(base, page.previousPageable(), Link.REL_PREVIOUS));
resources.add(createLink(base, page.previousPageable(), IanaLinkRelation.PREV.value()));
}

Link selfLink = link.map(it -> it.withSelfRel())//
.orElseGet(() -> createLink(base, page.getPageable(), Link.REL_SELF));
.orElseGet(() -> createLink(base, page.getPageable(), IanaLinkRelation.SELF.value()));

resources.add(selfLink);

if (page.hasNext()) {
resources.add(createLink(base, page.nextPageable(), Link.REL_NEXT));
resources.add(createLink(base, page.nextPageable(), IanaLinkRelation.NEXT.value()));
}

if (isNavigable || forceFirstAndLastRels) {

int lastIndex = page.getTotalPages() == 0 ? 0 : page.getTotalPages() - 1;

resources.add(createLink(base, PageRequest.of(lastIndex, page.getSize(), page.getSort()), Link.REL_LAST));
resources.add(createLink(base, PageRequest.of(lastIndex, page.getSize(), page.getSort()), IanaLinkRelation.LAST.value()));
}

return resources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.hateoas.IanaLinkRelation;
import org.springframework.hateoas.Link;

/**
Expand Down Expand Up @@ -159,7 +160,9 @@ static class LinkedPageAdapter extends PageAdapter {

@Override
protected List<Link> getLinks(Page<?> source) {
return Arrays.asList(new Link(Link.REL_NEXT, "next"), new Link(Link.REL_PREVIOUS, "previous"));
return Arrays.asList(
new Link(IanaLinkRelation.NEXT.value(), IanaLinkRelation.NEXT.value()),
new Link(IanaLinkRelation.PREV.value(), IanaLinkRelation.PREVIOUS.value()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.IanaLinkRelation;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.PagedResources.PageMetadata;
Expand Down Expand Up @@ -65,29 +66,29 @@ public void addsNextLinkForFirstPage() {

PagedResources<Resource<Person>> resources = assembler.toResource(createPage(0));

assertThat(resources.getLink(Link.REL_PREVIOUS)).isNull();
assertThat(resources.getLink(Link.REL_SELF)).isNotNull();
assertThat(resources.getLink(Link.REL_NEXT)).isNotNull();
assertThat(resources.getLink(IanaLinkRelation.PREV.value())).isEmpty();
assertThat(resources.getLink(IanaLinkRelation.SELF.value())).isNotEmpty();
assertThat(resources.getLink(IanaLinkRelation.NEXT.value())).isNotEmpty();
}

@Test
public void addsPreviousAndNextLinksForMiddlePage() {

PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));

assertThat(resources.getLink(Link.REL_PREVIOUS)).isNotNull();
assertThat(resources.getLink(Link.REL_SELF)).isNotNull();
assertThat(resources.getLink(Link.REL_NEXT)).isNotNull();
assertThat(resources.getLink(IanaLinkRelation.PREV.value())).isNotEmpty();
assertThat(resources.getLink(IanaLinkRelation.SELF.value())).isNotEmpty();
assertThat(resources.getLink(IanaLinkRelation.NEXT.value())).isNotEmpty();
}

@Test
public void addsPreviousLinkForLastPage() {

PagedResources<Resource<Person>> resources = assembler.toResource(createPage(2));

assertThat(resources.getLink(Link.REL_PREVIOUS)).isNotNull();
assertThat(resources.getLink(Link.REL_SELF)).isNotNull();
assertThat(resources.getLink(Link.REL_NEXT)).isNull();
assertThat(resources.getLink(IanaLinkRelation.PREV.value())).isNotEmpty();
assertThat(resources.getLink(IanaLinkRelation.SELF.value())).isNotEmpty();
assertThat(resources.getLink(IanaLinkRelation.NEXT.value())).isEmpty();
}

@Test
Expand All @@ -98,9 +99,9 @@ public void usesBaseUriIfConfigured() {
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<>(resolver, baseUri);
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));

assertThat(resources.getLink(Link.REL_PREVIOUS).getHref()).startsWith(baseUri.toUriString());
assertThat(resources.getLink(Link.REL_SELF)).isNotNull();
assertThat(resources.getLink(Link.REL_NEXT).getHref()).startsWith(baseUri.toUriString());
assertThat(resources.getRequiredLink(IanaLinkRelation.PREV.value()).getHref()).startsWith(baseUri.toUriString());
assertThat(resources.getRequiredLink(IanaLinkRelation.SELF.value())).isNotNull();
assertThat(resources.getRequiredLink(IanaLinkRelation.NEXT.value()).getHref()).startsWith(baseUri.toUriString());
}

@Test
Expand All @@ -110,9 +111,9 @@ public void usesCustomLinkProvided() {

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)).isEqualTo(link.withSelfRel());
assertThat(resources.getLink(Link.REL_NEXT).getHref()).startsWith(link.getHref());
assertThat(resources.getRequiredLink(IanaLinkRelation.PREV.value()).getHref()).startsWith(link.getHref());
assertThat(resources.getRequiredLink(IanaLinkRelation.SELF.value())).isEqualTo(link.withSelfRel());
assertThat(resources.getRequiredLink(IanaLinkRelation.NEXT.value()).getHref()).startsWith(link.getHref());
}

@Test // DATACMNS-358
Expand All @@ -131,7 +132,7 @@ public void createsACanonicalLinkWithoutTemplateParameters() {

PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));

assertThat(resources.getLink(Link.REL_SELF).getHref()).doesNotContain("{").doesNotContain("}");
assertThat(resources.getRequiredLink(IanaLinkRelation.SELF.value()).getHref()).doesNotContain("{").doesNotContain("}");
}

@Test // DATACMNS-418
Expand All @@ -141,8 +142,8 @@ public void invokesCustomElementResourceAssembler() {

PagedResources<PersonResource> resources = assembler.toResource(createPage(0), personAssembler);

assertThat(resources.hasLink(Link.REL_SELF)).isTrue();
assertThat(resources.hasLink(Link.REL_NEXT)).isTrue();
assertThat(resources.hasLink(IanaLinkRelation.SELF.value())).isTrue();
assertThat(resources.hasLink(IanaLinkRelation.NEXT.value())).isTrue();
Collection<PersonResource> content = resources.getContent();
assertThat(content).hasSize(1);
assertThat(content.iterator().next().name).isEqualTo("Dave");
Expand All @@ -163,18 +164,18 @@ public void createsPaginationLinksForOneIndexedArgumentResolverCorrectly() {
// We expect 2 as the created page has index 1. Pages itself are always 0 indexed, so we created page 2 above.
assertThat(resource.getMetadata().getNumber()).isEqualTo(2);

assertThat(getQueryParameters(resource.getLink("prev"))).containsEntry("page", "1");
assertThat(getQueryParameters(resource.getLink("next"))).containsEntry("page", "3");
assertThat(getQueryParameters(resource.getRequiredLink("prev"))).containsEntry("page", "1");
assertThat(getQueryParameters(resource.getRequiredLink("next"))).containsEntry("page", "3");
}

@Test // DATACMNS-515
public void generatedLinksShouldNotBeTemplated() {

PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));

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");
assertThat(resources.getRequiredLink(IanaLinkRelation.SELF.value()).getHref()).doesNotContain("{").doesNotContain("}");
assertThat(resources.getRequiredLink(IanaLinkRelation.NEXT.value()).getHref()).endsWith("?page=2&size=1");
assertThat(resources.getRequiredLink(IanaLinkRelation.PREV.value()).getHref()).endsWith("?page=0&size=1");
}

@Test // DATACMNS-699
Expand Down Expand Up @@ -205,26 +206,26 @@ 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");
assertThat(resources.getRequiredLink(IanaLinkRelation.FIRST.value()).getHref()).endsWith("?page=0&size=1");
assertThat(resources.getRequiredLink(IanaLinkRelation.LAST.value()).getHref()).endsWith("?page=2&size=1");
}

@Test // DATACMNS-701
public void addsFirstAndLastLinksForFirstPage() {

PagedResources<Resource<Person>> resources = assembler.toResource(createPage(0));

assertThat(resources.getLink(Link.REL_FIRST).getHref()).endsWith("?page=0&size=1");
assertThat(resources.getLink(Link.REL_LAST).getHref()).endsWith("?page=2&size=1");
assertThat(resources.getRequiredLink(IanaLinkRelation.FIRST.value()).getHref()).endsWith("?page=0&size=1");
assertThat(resources.getRequiredLink(IanaLinkRelation.LAST.value()).getHref()).endsWith("?page=2&size=1");
}

@Test // DATACMNS-701
public void addsFirstAndLastLinksForLastPage() {

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).getHref()).endsWith("?page=2&size=1");
assertThat(resources.getRequiredLink(IanaLinkRelation.FIRST.value()).getHref()).endsWith("?page=0&size=1");
assertThat(resources.getRequiredLink(IanaLinkRelation.LAST.value()).getHref()).endsWith("?page=2&size=1");
}

@Test // DATACMNS-701
Expand All @@ -235,8 +236,8 @@ public void alwaysAddsFirstAndLastLinkIfConfiguredTo() {

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");
assertThat(resources.getRequiredLink(IanaLinkRelation.FIRST.value()).getHref()).endsWith("?page=0&size=20");
assertThat(resources.getRequiredLink(IanaLinkRelation.LAST.value()).getHref()).endsWith("?page=0&size=20");
}

@Test // DATACMNS-802
Expand All @@ -253,7 +254,7 @@ public void selfLinkContainsCoordinatesForCurrentPage() {

PagedResources<Resource<Person>> resource = assembler.toResource(createPage(0));

assertThat(resource.getLink(Link.REL_SELF).getHref()).endsWith("?page=0&size=1");
assertThat(resource.getRequiredLink(IanaLinkRelation.SELF.value()).getHref()).endsWith("?page=0&size=1");
}

private static Page<Person> createPage(int index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static org.assertj.core.api.Assertions.*;

import java.util.Arrays;
import java.util.Collections;

import org.junit.Before;
Expand All @@ -32,7 +31,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.data.web.WebTestUtils;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.IanaLinkRelation;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.Resource;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -71,9 +70,9 @@ public void injectsPagedResourcesAssembler() {

PagedResources<Resource<Person>> resources = controller.sample(PageRequest.of(1, 1));

assertThat(resources.getLink(Link.REL_PREVIOUS)).isNotNull();
assertThat(resources.getLink(Link.REL_NEXT)).isNotNull();
assertThat(resources.getLink(Link.REL_SELF)).isNotNull();
assertThat(resources.getLink(IanaLinkRelation.PREV.value())).isNotNull();
assertThat(resources.getLink(IanaLinkRelation.NEXT.value())).isNotNull();
assertThat(resources.getLink(IanaLinkRelation.SELF.value())).isNotNull();
}

@Test // DATACMNS-471
Expand Down