ConfluenceRestClient implements ConfluenceClient { @Override public List<ConfluenceAttachment> getAttachments(String contentId) { int start = 0; int limit = 25; ArrayList<ConfluenceAttachment> attachments = new ArrayList<>(); boolean fetchMore = true; while (fetchMore) { List<ConfluenceAttachment> nextAttachments = getNextAttachments(contentId, limit, start); attachments.addAll(nextAttachments); start++; fetchMore = nextAttachments.size() == limit; } return attachments; } 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 getAttachments_withValidParametersAndFirstResultIsSmallerThanLimit_returnsAttachments() throws Exception { String resultSet = generateJsonAttachmentResults(2); CloseableHttpClient httpClientMock = recordHttpClientForSingleResponseWithContentAndStatusCode("{\"results\": [" + resultSet + "], \"size\": 2}", 200); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient(CONFLUENCE_ROOT_URL, httpClientMock, null, null, null); String contentId = "1234"; List<ConfluenceAttachment> confluenceAttachments = confluenceRestClient.getAttachments(contentId); ConfluenceAttachment attachmentOne = new ConfluenceAttachment("1", "Attachment-1.txt", "/download/Attachment-1.txt", 1); ConfluenceAttachment attachmentTwo = new ConfluenceAttachment("2", "Attachment-2.txt", "/download/Attachment-2.txt", 1); assertThat(confluenceAttachments, contains(attachmentOne, attachmentTwo)); }
@Test public void getAttachments_withValidParametersAndFirstResultSizeHasSameSizeAsLimit_sendsASecondRequestToFetchNextAttachments() throws Exception { String firstResultSet = "{\"results\": [" + generateJsonAttachmentResults(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<ConfluenceAttachment> confluenceAttachments = confluenceRestClient.getAttachments(contentId); assertThat(confluenceAttachments.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 getAttachments_withValidParametersAndFirstResultIsSmallerThanLimit_sendsASecondRequestToFetchNextAttachments() throws Exception { String firstResultSet = "{\"results\": [" + generateJsonAttachmentResults(25) + "], \"size\": 25}"; String secondResultSet = "{\"results\": [" + generateJsonAttachmentResults(24) + "], \"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<ConfluenceAttachment> confluenceAttachments = confluenceRestClient.getAttachments(contentId); assertThat(confluenceAttachments.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")); }