Search in sources :

Example 1 with Links

use of io.gravitee.rest.api.portal.rest.model.Links in project gravitee-management-rest-api by gravitee-io.

the class AbstractResource method computePaginatedLinks.

protected Links computePaginatedLinks(Integer page, Integer size, Integer totalItems) {
    Links paginatedLinks = null;
    if (size == 0) {
        return paginatedLinks;
    }
    Integer totalPages = (int) Math.ceil((double) totalItems / size);
    if (page > 0 && page <= totalPages) {
        if (totalPages == 1) {
            paginatedLinks = new Links().self(uriInfo.getRequestUri().toString());
        } else if (totalPages > 1) {
            final String pageToken = "{page}";
            final String sizeToken = "{size}";
            String linkTemplate = uriInfo.getRequestUri().toString();
            final MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
            if (queryParameters.isEmpty()) {
                linkTemplate += "?" + PaginationParam.PAGE_QUERY_PARAM_NAME + "=" + pageToken;
            } else {
                final String queryPage = queryParameters.getFirst(PaginationParam.PAGE_QUERY_PARAM_NAME);
                final String querySize = queryParameters.getFirst(PaginationParam.SIZE_QUERY_PARAM_NAME);
                if (queryPage != null) {
                    linkTemplate = linkTemplate.replaceFirst(PaginationParam.PAGE_QUERY_PARAM_NAME + "=(\\w*)", PaginationParam.PAGE_QUERY_PARAM_NAME + "=" + pageToken);
                } else {
                    linkTemplate += "&" + PaginationParam.PAGE_QUERY_PARAM_NAME + "=" + pageToken;
                }
                if (querySize != null) {
                    linkTemplate = linkTemplate.replaceFirst(PaginationParam.SIZE_QUERY_PARAM_NAME + "=(\\w*)", PaginationParam.SIZE_QUERY_PARAM_NAME + "=" + sizeToken);
                }
            }
            Integer firstPage = 1;
            Integer lastPage = totalPages;
            Integer nextPage = Math.min(page + 1, lastPage);
            Integer prevPage = Math.max(firstPage, page - 1);
            paginatedLinks = new Links().first(linkTemplate.replace(pageToken, String.valueOf(firstPage)).replace(sizeToken, String.valueOf(size))).last(linkTemplate.replace(pageToken, String.valueOf(lastPage)).replace(sizeToken, String.valueOf(size))).next(linkTemplate.replace(pageToken, String.valueOf(nextPage)).replace(sizeToken, String.valueOf(size))).prev(linkTemplate.replace(pageToken, String.valueOf(prevPage)).replace(sizeToken, String.valueOf(size))).self(uriInfo.getRequestUri().toString());
            if (page == 1) {
                paginatedLinks.setPrev(null);
            } else if (page.equals(totalPages)) {
                paginatedLinks.setNext(null);
            }
        }
    }
    return paginatedLinks;
}
Also used : Links(io.gravitee.rest.api.portal.rest.model.Links)

Example 2 with Links

use of io.gravitee.rest.api.portal.rest.model.Links in project gravitee-management-rest-api by gravitee-io.

the class ApplicationLogsResourceTest method shouldGetNoLogAndNoLink.

@Test
public void shouldGetNoLogAndNoLink() {
    SearchLogResponse<ApplicationRequestItem> emptySearchResponse = new SearchLogResponse<>(0);
    emptySearchResponse.setLogs(Collections.emptyList());
    doReturn(emptySearchResponse).when(logsService).findByApplication(eq(APPLICATION), any());
    final Response response = target(APPLICATION).path("logs").queryParam("page", 1).queryParam("size", 10).queryParam("query", APPLICATION).queryParam("from", 0).queryParam("to", 100).queryParam("field", APPLICATION).queryParam("order", "ASC").request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    LogsResponse logsResponse = response.readEntity(LogsResponse.class);
    assertEquals(0, logsResponse.getData().size());
    Links links = logsResponse.getLinks();
    assertNull(links);
}
Also used : LogsResponse(io.gravitee.rest.api.portal.rest.model.LogsResponse) Response(javax.ws.rs.core.Response) SearchLogResponse(io.gravitee.rest.api.model.log.SearchLogResponse) LogsResponse(io.gravitee.rest.api.portal.rest.model.LogsResponse) ApplicationRequestItem(io.gravitee.rest.api.model.log.ApplicationRequestItem) SearchLogResponse(io.gravitee.rest.api.model.log.SearchLogResponse) Links(io.gravitee.rest.api.portal.rest.model.Links) Test(org.junit.Test)

Example 3 with Links

use of io.gravitee.rest.api.portal.rest.model.Links in project gravitee-management-rest-api by gravitee-io.

the class CategoriesResourceTest method shouldGetNoPublishedApiAndNoLink.

@Test
public void shouldGetNoPublishedApiAndNoLink() {
    doReturn(new ArrayList<>()).when(categoryService).findAll();
    // Test with default limit
    final Response response = target().request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    CategoriesResponse categoriesResponse = response.readEntity(CategoriesResponse.class);
    assertEquals(0, categoriesResponse.getData().size());
    Links links = categoriesResponse.getLinks();
    assertNull(links);
    // Test with small limit
    final Response anotherResponse = target().queryParam("page", 2).queryParam("size", 1).request().get();
    assertEquals(HttpStatusCode.OK_200, anotherResponse.getStatus());
    categoriesResponse = anotherResponse.readEntity(CategoriesResponse.class);
    assertEquals(0, categoriesResponse.getData().size());
    links = categoriesResponse.getLinks();
    assertNull(links);
}
Also used : ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) CategoriesResponse(io.gravitee.rest.api.portal.rest.model.CategoriesResponse) Links(io.gravitee.rest.api.portal.rest.model.Links) CategoriesResponse(io.gravitee.rest.api.portal.rest.model.CategoriesResponse) Test(org.junit.Test)

Example 4 with Links

use of io.gravitee.rest.api.portal.rest.model.Links in project gravitee-management-rest-api by gravitee-io.

the class PaginationLinkTest method shouldHaveNoLinkWithNoElement.

@Test
public void shouldHaveNoLinkWithNoElement() {
    Integer page = 1;
    Integer size = 10;
    Integer totalItems = 0;
    Links links = paginatedResourceForTest.computePaginatedLinks(page, size, totalItems);
    assertNull(links);
}
Also used : Links(io.gravitee.rest.api.portal.rest.model.Links) Test(org.junit.Test)

Example 5 with Links

use of io.gravitee.rest.api.portal.rest.model.Links in project gravitee-management-rest-api by gravitee-io.

the class PaginationLinkTest method shouldHaveNoLinkWithZeroPageNumber.

@Test
public void shouldHaveNoLinkWithZeroPageNumber() {
    Integer page = 0;
    Integer size = 10;
    Integer totalItems = 100;
    Links links = paginatedResourceForTest.computePaginatedLinks(page, size, totalItems);
    assertNull(links);
}
Also used : Links(io.gravitee.rest.api.portal.rest.model.Links) Test(org.junit.Test)

Aggregations

Links (io.gravitee.rest.api.portal.rest.model.Links)8 Test (org.junit.Test)6 Response (javax.ws.rs.core.Response)3 SearchLogResponse (io.gravitee.rest.api.model.log.SearchLogResponse)2 LogsResponse (io.gravitee.rest.api.portal.rest.model.LogsResponse)2 LogQuery (io.gravitee.rest.api.model.analytics.query.LogQuery)1 ApplicationRequestItem (io.gravitee.rest.api.model.log.ApplicationRequestItem)1 CategoriesResponse (io.gravitee.rest.api.portal.rest.model.CategoriesResponse)1 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1