PaginationBuilderService { public Map<String, Object> buildPages( Integer selectedPage, int totalPages, int depthSize ){ logger.trace("Map<String,Object> buildPages(selectedPage= {},totalPages={},depthSize={})",new Object[]{selectedPage,totalPages,depthSize}); Map<String,Object> pagination = new HashMap<String, Object>(); int returnSize = Math.min(((depthSize*2)+1),totalPages); Integer[] pages = new Integer[returnSize]; if(selectedPage <= depthSize+1){ logger.trace("Map<String,Object> buildPages(pages) Keeping to left side no need to scroll "); for(int i =1; i <= returnSize ; i++){ pages[i-1]=i; } }else if( selectedPage > totalPages - (depthSize*2)){ logger.trace("Map<String,Object> buildPages(pages) Working from right <- left, not enough spaces on the right {}",returnSize); for(int i =0 ; i < returnSize; i++){ pages[i] = (totalPages-returnSize+1)+i; } }else{ pages[depthSize]=selectedPage; for(int i=0; i< depthSize; i++){ pages[i]= selectedPage-(depthSize-i); pages[depthSize+1+i] = selectedPage+1+i; } } pagination.put("first",1); pagination.put("pages",pages); pagination.put("last",totalPages); pagination.put("selected",selectedPage); return pagination; }  Map<String, Object> buildPages(
            Integer selectedPage,
            int totalPages,
            int depthSize
    );  }
@Test public void testPaginationFitsLeft(){ Map<String,Object> stringObjectMap = paginationBuilderService.buildPages(3, 15, 4); Integer[] pages = (Integer[]) stringObjectMap.get("pages"); Integer selected = (Integer) stringObjectMap.get("selected"); Assert.assertEquals(3,selected, 0); Assert.assertEquals(9, pages.length); testPages(pages, new Integer[]{ 1,2,3,4,5,6,7,8,9 }); }
@Test public void testTotalCalculatedPagesLessThanTotal(){ Map<String,Object> stringObjectMap = paginationBuilderService.buildPages(3, 5, 4); Integer[] pages = (Integer[]) stringObjectMap.get("pages"); Integer selected = (Integer) stringObjectMap.get("selected"); Assert.assertEquals(3,selected, 0); Assert.assertEquals(5, pages.length); testPages(pages, new Integer[]{ 1,2,3,4,5 }); }
@Test public void testEdgeCaseCenter(){ Map<String,Object> stringObjectMap = paginationBuilderService.buildPages(5, 10, 4); Integer[] pages = (Integer[]) stringObjectMap.get("pages"); Integer selected = (Integer) stringObjectMap.get("selected"); Assert.assertEquals(5,selected, 0); Assert.assertEquals(9, pages.length); testPages(pages, new Integer[]{ 1,2,3,4,5,6,7,8,9 }); }
@Test public void testScrollRightFitSize(){ Map<String,Object> stringObjectMap = paginationBuilderService.buildPages(6, 10, 4); Integer[] pages = (Integer[]) stringObjectMap.get("pages"); Integer selected = (Integer) stringObjectMap.get("selected"); Assert.assertEquals(6,selected, 0); Assert.assertEquals(9, pages.length); testPages(pages, new Integer[]{ 2,3,4,5,6,7,8,9,10 }); }
@Test public void testScrollRightNotFitSize(){ Map<String,Object> stringObjectMap = paginationBuilderService.buildPages(9, 12, 4); Integer[] pages = (Integer[]) stringObjectMap.get("pages"); Integer selected = (Integer) stringObjectMap.get("selected"); Assert.assertEquals(9,selected, 0); Assert.assertEquals(9, pages.length); testPages(pages, new Integer[]{ 4,5,6,7,8,9,10,11,12 }); }
@Test public void testEnd(){ Map<String,Object> stringObjectMap = paginationBuilderService.buildPages(13, 15, 6); Integer[] pages = (Integer[]) stringObjectMap.get("pages"); Integer selected = (Integer) stringObjectMap.get("selected"); Assert.assertEquals(13,selected, 0); Assert.assertEquals(13, pages.length); testPages(pages, new Integer[]{ 3,4,5,6,7,8,9,10,11,12,13,14,15 }); }
@Test public void testProductionCase(){ Map<String,Object> stringObjectMap = paginationBuilderService.buildPages(19, 166, 4); Integer[] pages = (Integer[]) stringObjectMap.get("pages"); Integer selected = (Integer) stringObjectMap.get("selected"); Assert.assertEquals(19,selected, 0); Assert.assertEquals(9, pages.length); testPages(pages, new Integer[]{ 15,16,17,18,19,20,21,22,23 }); }