ConfluenceRestClient implements ConfluenceClient { @Override public List<ConfluencePage> getChildPages(String contentId) { int start = 0; int limit = 25; ArrayList<ConfluencePage> childPages = new ArrayList<>(); boolean fetchMore = true; while (fetchMore) { List<ConfluencePage> nextChildPages = getNextChildPages(contentId, limit, start); childPages.addAll(nextChildPages); start++; fetchMore = nextChildPages.size() == limit; } return childPages; } ConfluenceRestClient(String rootConfluenceUrl, boolean disableSslVerification, Double maxRequestsPerSecond, String username, String password);  ConfluenceRestClient(String rootConfluenceUrl, ProxyConfiguration proxyConfiguration, boolean disableSslVerification, Double maxRequestsPerSecond, String username, String password);  ConfluenceRestClient(String rootConfluenceUrl, CloseableHttpClient httpClient, Double maxRequestsPerSecond, String username, String password); @Override String addPageUnderAncestor(String spaceKey, String ancestorId, String title, String content, String versionMessage); @Override void updatePage(String contentId, String ancestorId, String title, String content, int newVersion, String versionMessage); @Override void deletePage(String contentId); @Override String getPageByTitle(String spaceKey, String title); @Override void addAttachment(String contentId, String attachmentFileName, InputStream attachmentContent); @Override void updateAttachmentContent(String contentId, String attachmentId, InputStream attachmentContent); @Override void deleteAttachment(String attachmentId); @Override ConfluenceAttachment getAttachmentByFileName(String contentId, String attachmentFileName); @Override ConfluencePage getPageWithContentAndVersionById(String contentId); @Override List<ConfluencePage> getChildPages(String contentId); @Override List<ConfluenceAttachment> getAttachments(String contentId); @Override void setPropertyByKey(String contentId, String key, String value); @Override String getPropertyByKey(String contentId, String key); @Override void deletePropertyByKey(String contentId, String key); @Override List<String> getLabels(String contentId); @Override void addLabels(String contentId, List<String> labels); @Override void deleteLabel(String contentId, String label);  }
@Test public void getChildPages_withValidParametersAndFirstResultSizeSmallerThanLimit_returnsListOfChildPagesWithTitleContentVersionAndId() throws Exception { String resultSet = generateJsonPageResults(2); CloseableHttpClient httpClientMock = recordHttpClientForSingleResponseWithContentAndStatusCode("{\"results\": [" + resultSet + "], \"size\": 2}", 200); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient(CONFLUENCE_ROOT_URL, httpClientMock, null, null, null); String contentId = "1234"; List<ConfluencePage> childPages = confluenceRestClient.getChildPages(contentId); ConfluencePage childOne = new ConfluencePage("1", "Page 1", 1); ConfluencePage childTwo = new ConfluencePage("2", "Page 2", 1); assertThat(childPages, contains(childOne, childTwo)); }
@Test public void getChildPages_withValidParametersAndFirstResultSizeHasSameSizeAsLimit_sendsASecondRequestToFetchNextChildPages() throws Exception { String firstResultSet = "{\"results\": [" + generateJsonPageResults(25) + "], \"size\": 25}"; String secondResultSet = "{\"results\": [], \"size\": 0}"; List<String> jsonResponses = asList(firstResultSet, secondResultSet); List<Integer> statusCodes = asList(200, 200); CloseableHttpClient httpClientMock = recordHttpClientForMultipleResponsesWithContentAndStatusCode(jsonResponses, statusCodes); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient(CONFLUENCE_ROOT_URL, httpClientMock, null, null, null); String contentId = "1234"; ArgumentCaptor<HttpGet> httpGetArgumentCaptor = ArgumentCaptor.forClass(HttpGet.class); List<ConfluencePage> childPages = confluenceRestClient.getChildPages(contentId); assertThat(childPages.size(), is(25)); verify(httpClientMock, times(2)).execute(httpGetArgumentCaptor.capture()); assertThat(httpGetArgumentCaptor.getAllValues().get(0).getURI().toString(), containsString("start=0")); assertThat(httpGetArgumentCaptor.getAllValues().get(1).getURI().toString(), containsString("start=1")); }
@Test public void getChildPages_withValidParametersAndFirstResultSizeIsHigherThanLimit_sendsASecondRequestToFetchNextChildPages() throws Exception { String firstResultSet = "{\"results\": [" + generateJsonPageResults(25) + "], \"size\": 25}"; String secondResultSet = "{\"results\": [" + generateJsonPageResults(24) + "], \"size\": 24}"; List<String> jsonResponses = asList(firstResultSet, secondResultSet); List<Integer> statusCodes = asList(200, 200); CloseableHttpClient httpClientMock = recordHttpClientForMultipleResponsesWithContentAndStatusCode(jsonResponses, statusCodes); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient(CONFLUENCE_ROOT_URL, httpClientMock, null, null, null); String contentId = "1234"; ArgumentCaptor<HttpGet> httpGetArgumentCaptor = ArgumentCaptor.forClass(HttpGet.class); List<ConfluencePage> childPages = confluenceRestClient.getChildPages(contentId); assertThat(childPages.size(), is(49)); verify(httpClientMock, times(2)).execute(httpGetArgumentCaptor.capture()); assertThat(httpGetArgumentCaptor.getAllValues().get(0).getURI().toString(), containsString("start=0")); assertThat(httpGetArgumentCaptor.getAllValues().get(1).getURI().toString(), containsString("start=1")); }