### Question:
PagerImpl implements Pager { @Nonnull @Override public List<PagerPage> getPages() { List<PagerPage> pages = new ArrayList<>(); for (int currentIndex = 1; currentIndex <= totalPages; currentIndex++) { Map<String, String> pageParameters = new HashMap<>(existingQueryParameters); pageParameters.put("page", Integer.toString(currentIndex)); if (inDisplayRange(totalPages, currentIndex) || currentIndex == 1 || currentIndex == totalPages) { pages.add(new PagerPageImpl(currentIndex, pageParameters, true)); } } return pages; } PagerImpl(final Map<String, String> existingQueryParameters, final int totalPages, final int currentPageIndex); @Override int getCurrentPage(); @Override int getTotalPages(); @Nonnull @Override List<PagerPage> getPages(); @Nonnull @Override Map<String, String> getPreviousPageParameters(); @Nonnull @Override Map<String, String> getNextPageParameters(); @Override boolean getMorePagesBefore(); @Override boolean getMorePagesAfter(); static final int MAXIMUM_PAGE_DISPLAY_COUNT; static final int PAGINATION_RANGE_SIZE; }### Answer:
@Test public void testAlwaysReturnPageResultsWithCorrectSize() { PagerImpl pager = new PagerImpl(Collections.singletonMap(PARAMETER_KEY, PARAMETER_VALUE), TOTAL_PAGES, 1); assertThat(pager.getPages()).hasSize(PagerImpl.PAGINATION_RANGE_SIZE + 1); pager = new PagerImpl(Collections.singletonMap(PARAMETER_KEY, PARAMETER_VALUE), TOTAL_PAGES, TOTAL_PAGES); assertThat(pager.getPages()).hasSize(PagerImpl.PAGINATION_RANGE_SIZE + 1); }
@Test public void testAlwaysReturnsFirstAndLastPage() { PagerImpl pager = new PagerImpl(Collections.singletonMap(PARAMETER_KEY, PARAMETER_VALUE), TOTAL_PAGES, 1); assertThat(pager.getPages().stream().map(page -> page.getPageNumber()).collect(Collectors.toList())) .contains(1, TOTAL_PAGES); pager = new PagerImpl(Collections.singletonMap(PARAMETER_KEY, PARAMETER_VALUE), TOTAL_PAGES, TOTAL_PAGES); assertThat(pager.getPages().stream().map(page -> page.getPageNumber()).collect(Collectors.toList())) .contains(1, TOTAL_PAGES); pager = new PagerImpl(Collections.singletonMap(PARAMETER_KEY, PARAMETER_VALUE), TOTAL_PAGES, 10); assertThat(pager.getPages().stream().map(page -> page.getPageNumber()).collect(Collectors.toList())) .contains(1, TOTAL_PAGES); }
@Test public void testPageEdgeConditionsProvidePage() { PagerImpl pager = new PagerImpl(Collections.singletonMap(PARAMETER_KEY, PARAMETER_VALUE), 1, 1); assertThat(pager.getPages()).hasSize(1); pager = new PagerImpl(Collections.singletonMap(PARAMETER_KEY, PARAMETER_VALUE), 2, 1); assertThat(pager.getPages()).hasSize(2); pager = new PagerImpl(Collections.singletonMap(PARAMETER_KEY, PARAMETER_VALUE), 4, 1); assertThat(pager.getPages().stream().map(page -> page.getPageNumber()).collect(Collectors.toList())).contains(1, 2, 3, 4); pager = new PagerImpl(Collections.singletonMap(PARAMETER_KEY, PARAMETER_VALUE), PagerImpl.MAXIMUM_PAGE_DISPLAY_COUNT, 1); assertThat(pager.getPages()).hasSize(PagerImpl.MAXIMUM_PAGE_DISPLAY_COUNT); }
@Test public void testUsesPageSubsetWhenOverMaxPageResults() { PagerImpl pager = new PagerImpl(Collections.singletonMap(PARAMETER_KEY, PARAMETER_VALUE), PagerImpl.MAXIMUM_PAGE_DISPLAY_COUNT + 1, 5); assertThat(pager.getPages()).hasSize(PagerImpl.PAGINATION_RANGE_SIZE + 2); }