EntityCollectionPageSource implements PageSource<T> { @Override public List<T> fetch(final long offset, final long limit, final LongConsumer totalSizeSink) { LOGGER.trace("Requested with offset {}, limit {}.", offset, limit); final int pageId = offset < 1 ? 0 : (int) (offset / pageSize); final PaginatedResult<T> result = api.execute(function, this.select, pageId, pageSize); totalSizeSink.accept(result.getTotalResultCount()); return result.getPage(); } EntityCollectionPageSource(final PaginatedApi<T, S> api, final Function<S, List<T>> function,
            final Select select, final int pageSize); @Override List<T> fetch(final long offset, final long limit, final LongConsumer totalSizeSink);  }
@Test void firstPage() { final List<Integer> allResults = FUNCTION.apply(null); final List<Integer> subpage = allResults.subList(0, PAGE_SIZE); when(api.execute(eq(FUNCTION), eq(SELECT), eq(0), eq(PAGE_SIZE))) .thenReturn(new PaginatedResult<>(subpage, allResults.size())); final PageSource<Integer> source = new EntityCollectionPageSource<>(api, FUNCTION, SELECT, PAGE_SIZE); final LongConsumer consumer = mock(LongConsumer.class); final List<Integer> result = source.fetch(0, 1, consumer); assertThat(result).containsExactly(subpage.toArray(new Integer[PAGE_SIZE])); verify(consumer).accept(eq((long) allResults.size())); }
@Test void negativePageWorksAsFirstPage() { final List<Integer> allResults = FUNCTION.apply(null); final List<Integer> subpage = allResults.subList(0, PAGE_SIZE); when(api.execute(eq(FUNCTION), eq(SELECT), eq(0), eq(PAGE_SIZE))) .thenReturn(new PaginatedResult<>(subpage, allResults.size())); final PageSource<Integer> source = new EntityCollectionPageSource<>(api, FUNCTION, SELECT, PAGE_SIZE); final LongConsumer consumer = mock(LongConsumer.class); final List<Integer> result = source.fetch(-1, 1, consumer); assertThat(result).containsExactly(subpage.toArray(new Integer[PAGE_SIZE])); verify(consumer).accept(eq((long) allResults.size())); }
@Test void secondPage() { final List<Integer> allResults = FUNCTION.apply(null); final List<Integer> subpage = allResults.subList(PAGE_SIZE, 2 * PAGE_SIZE); when(api.execute(eq(FUNCTION), eq(SELECT), eq(1), eq(PAGE_SIZE))) .thenReturn(new PaginatedResult<>(subpage, allResults.size())); final PageSource<Integer> source = new EntityCollectionPageSource<>(api, FUNCTION, SELECT, PAGE_SIZE); final LongConsumer consumer = mock(LongConsumer.class); final List<Integer> result = source.fetch(PAGE_SIZE, 1, consumer); assertThat(result).containsExactly(subpage.toArray(new Integer[PAGE_SIZE])); verify(consumer).accept(eq((long) allResults.size())); }