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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations