use of org.jboss.pnc.rest.api.parameters.PageParameters in project pnc by project-ncl.
the class RemoteCollectionTest method shouldIterateOverPages.
@Test
public void shouldIterateOverPages() throws Exception {
AtomicInteger index = new AtomicInteger();
Function<PageParameters, Page<Entity>> endpoint = (parameters) -> {
Collection<Entity> collection = new ArrayList();
collection.add(new Entity(index.getAndIncrement()));
collection.add(new Entity(index.getAndIncrement()));
return new Page<>(parameters.getPageIndex(), parameters.getPageSize(), 3, 6, collection);
};
RemoteCollectionConfig config = RemoteCollectionConfig.builder().pageSize(2).build();
RemoteCollection<Entity> collection = new DefaultRemoteCollection<>(endpoint, config);
Iterator<Entity> iterator = collection.iterator();
List<Entity> collected = new ArrayList<>();
while (iterator.hasNext()) {
Entity entity = iterator.next();
System.out.println("Received: " + entity.id);
collected.add(entity);
if (collected.size() > 10) {
throw new Exception("Infinite loop.");
}
}
Assert.assertEquals(6, collected.size());
Assert.assertEquals(5, collected.get(5).id);
}
use of org.jboss.pnc.rest.api.parameters.PageParameters in project pnc by project-ncl.
the class DefaultRemoteCollection method loadNextPage.
private Page<T> loadNextPage(Function<PageParameters, Page<T>> endpoint, Page<T> currentPage) {
int newPageIndex = currentPage.getPageIndex() + 1;
logger.debug("Loading new page. Index {}", newPageIndex);
PageParameters pageParametersNext = new PageParameters();
pageParametersNext.setPageSize(currentPage.getPageSize());
pageParametersNext.setPageIndex(newPageIndex);
return endpoint.apply(pageParametersNext);
}
Aggregations