Search in sources :

Example 6 with PagedResources

use of org.springframework.hateoas.PagedResources in project taskana by Taskana.

the class TaskControllerIntTest method testGetAllTasksWithAdminRole.

@Test
public void testGetAllTasksWithAdminRole() {
    RestTemplate template = getRestTemplate();
    HttpHeaders headers = new HttpHeaders();
    // Role Admin
    headers.add("Authorization", "Basic YWRtaW46YWRtaW4=");
    HttpEntity<String> request = new HttpEntity<String>(headers);
    ResponseEntity<PagedResources<TaskSummaryResource>> response = template.exchange("http://127.0.0.1:" + port + "/v1/tasks", HttpMethod.GET, request, new ParameterizedTypeReference<PagedResources<TaskSummaryResource>>() {
    });
    assertNotNull(response.getBody().getLink(Link.REL_SELF));
    assertEquals(70, response.getBody().getContent().size());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) PagedResources(org.springframework.hateoas.PagedResources) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with PagedResources

use of org.springframework.hateoas.PagedResources in project taskana by Taskana.

the class TaskControllerIntTest method testGetLastPageSortedByDue.

@Test
@Ignore
public void testGetLastPageSortedByDue() {
    RestTemplate template = getRestTemplate();
    HttpHeaders headers = new HttpHeaders();
    // Role Admin
    headers.add("Authorization", "Basic YWRtaW46YWRtaW4=");
    HttpEntity<String> request = new HttpEntity<String>(headers);
    ResponseEntity<PagedResources<TaskSummaryResource>> response = template.exchange("http://127.0.0.1:" + port + "/v1/tasks?sortBy=due&order=desc&page=14&pageSize=5", HttpMethod.GET, request, new ParameterizedTypeReference<PagedResources<TaskSummaryResource>>() {
    });
    assertEquals(5, response.getBody().getContent().size());
    assertTrue(response.getBody().getLink(Link.REL_LAST).getHref().contains("page=14"));
    assertEquals("TKI:000000000000000000000000000000000004", response.getBody().getContent().iterator().next().getTaskId());
    assertNotNull(response.getBody().getLink(Link.REL_SELF));
    assertTrue(response.getBody().getLink(Link.REL_SELF).getHref().endsWith("/v1/tasks?sortBy=due&order=desc&page=14&pageSize=5"));
    assertNotNull(response.getBody().getLink("allTasks"));
    assertTrue(response.getBody().getLink("allTasks").getHref().endsWith("/v1/tasks"));
    assertNotNull(response.getBody().getLink(Link.REL_FIRST));
    assertNotNull(response.getBody().getLink(Link.REL_LAST));
    assertNotNull(response.getBody().getLink(Link.REL_PREVIOUS));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) PagedResources(org.springframework.hateoas.PagedResources) RestTemplate(org.springframework.web.client.RestTemplate) Ignore(org.junit.Ignore) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with PagedResources

use of org.springframework.hateoas.PagedResources in project taskana by Taskana.

the class WorkbasketSummaryResourcesAssembler method toResources.

public PagedResources<WorkbasketSummaryResource> toResources(List<WorkbasketSummary> workbasketSummaries, PageMetadata pageMetadata) {
    WorkbasketSummaryResourceAssembler assembler = new WorkbasketSummaryResourceAssembler();
    List<WorkbasketSummaryResource> resources = assembler.toResources(workbasketSummaries);
    PagedResources<WorkbasketSummaryResource> pagedResources = new PagedResources<WorkbasketSummaryResource>(resources, pageMetadata);
    UriComponentsBuilder original = getBuilderForOriginalUri();
    pagedResources.add(new Link(original.toUriString()).withSelfRel());
    if (pageMetadata != null) {
        pagedResources.add(linkTo(WorkbasketController.class).withRel("allWorkbaskets"));
        pagedResources.add(new Link(original.replaceQueryParam("page", 1).toUriString()).withRel(Link.REL_FIRST));
        pagedResources.add(new Link(original.replaceQueryParam("page", pageMetadata.getTotalPages()).toUriString()).withRel(Link.REL_LAST));
        if (pageMetadata.getNumber() > 1) {
            pagedResources.add(new Link(original.replaceQueryParam("page", pageMetadata.getNumber() - 1).toUriString()).withRel(Link.REL_PREVIOUS));
        }
        if (pageMetadata.getNumber() < pageMetadata.getTotalPages()) {
            pagedResources.add(new Link(original.replaceQueryParam("page", pageMetadata.getNumber() + 1).toUriString()).withRel(Link.REL_NEXT));
        }
    }
    return pagedResources;
}
Also used : WorkbasketSummaryResource(pro.taskana.rest.resource.WorkbasketSummaryResource) PagedResources(org.springframework.hateoas.PagedResources) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) Link(org.springframework.hateoas.Link)

Example 9 with PagedResources

use of org.springframework.hateoas.PagedResources in project spring-data-commons by spring-projects.

the class PagedResourcesAssembler method addPaginationLinks.

private <R> PagedResources<R> addPaginationLinks(PagedResources<R> resources, Page<?> page, Optional<Link> link) {
    UriTemplate base = getUriTemplate(link);
    boolean isNavigable = page.hasPrevious() || page.hasNext();
    if (isNavigable || forceFirstAndLastRels) {
        resources.add(createLink(base, PageRequest.of(0, page.getSize(), page.getSort()), Link.REL_FIRST));
    }
    if (page.hasPrevious()) {
        resources.add(createLink(base, page.previousPageable(), Link.REL_PREVIOUS));
    }
    Link selfLink = // 
    link.map(it -> it.withSelfRel()).orElseGet(() -> createLink(base, page.getPageable(), Link.REL_SELF));
    resources.add(selfLink);
    if (page.hasNext()) {
        resources.add(createLink(base, page.nextPageable(), Link.REL_NEXT));
    }
    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));
    }
    return resources;
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Link(org.springframework.hateoas.Link) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) EmbeddedWrapper(org.springframework.hateoas.core.EmbeddedWrapper) PageMetadata(org.springframework.hateoas.PagedResources.PageMetadata) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) EmbeddedWrappers(org.springframework.hateoas.core.EmbeddedWrappers) Resource(org.springframework.hateoas.Resource) ResourceSupport(org.springframework.hateoas.ResourceSupport) ArrayList(java.util.ArrayList) PagedResources(org.springframework.hateoas.PagedResources) List(java.util.List) UriTemplate(org.springframework.hateoas.UriTemplate) ResourceAssembler(org.springframework.hateoas.ResourceAssembler) MethodParameter(org.springframework.core.MethodParameter) Optional(java.util.Optional) Pageable(org.springframework.data.domain.Pageable) Nullable(org.springframework.lang.Nullable) Collections(java.util.Collections) Assert(org.springframework.util.Assert) UriComponents(org.springframework.web.util.UriComponents) UriTemplate(org.springframework.hateoas.UriTemplate) Link(org.springframework.hateoas.Link)

Example 10 with PagedResources

use of org.springframework.hateoas.PagedResources in project taskana by Taskana.

the class WorkbasketControllerIntTest method testGetSecondPageSortedByKey.

@Test
public void testGetSecondPageSortedByKey() {
    RestTemplate template = getRestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x");
    HttpEntity<String> request = new HttpEntity<String>(headers);
    ResponseEntity<PagedResources<WorkbasketSummaryResource>> response = template.exchange("http://127.0.0.1:" + port + "/v1/workbaskets?sortBy=key&order=desc&page=2&pagesize=5", HttpMethod.GET, request, new ParameterizedTypeReference<PagedResources<WorkbasketSummaryResource>>() {
    });
    assertEquals(5, response.getBody().getContent().size());
    assertEquals("USER_1_1", response.getBody().getContent().iterator().next().key);
    assertNotNull(response.getBody().getLink(Link.REL_SELF));
    assertTrue(response.getBody().getLink(Link.REL_SELF).getHref().endsWith("/v1/workbaskets?sortBy=key&order=desc&page=2&pagesize=5"));
    assertNotNull(response.getBody().getLink("allWorkbaskets"));
    assertTrue(response.getBody().getLink("allWorkbaskets").getHref().endsWith("/v1/workbaskets"));
    assertNotNull(response.getBody().getLink(Link.REL_FIRST));
    assertNotNull(response.getBody().getLink(Link.REL_LAST));
    assertNotNull(response.getBody().getLink(Link.REL_NEXT));
    assertNotNull(response.getBody().getLink(Link.REL_PREVIOUS));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) PagedResources(org.springframework.hateoas.PagedResources) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

PagedResources (org.springframework.hateoas.PagedResources)11 Test (org.junit.Test)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 HttpEntity (org.springframework.http.HttpEntity)9 HttpHeaders (org.springframework.http.HttpHeaders)9 RestTemplate (org.springframework.web.client.RestTemplate)9 Ignore (org.junit.Ignore)3 Link (org.springframework.hateoas.Link)2 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)2 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 MethodParameter (org.springframework.core.MethodParameter)1 Page (org.springframework.data.domain.Page)1 PageRequest (org.springframework.data.domain.PageRequest)1 Pageable (org.springframework.data.domain.Pageable)1 PageMetadata (org.springframework.hateoas.PagedResources.PageMetadata)1 Resource (org.springframework.hateoas.Resource)1