ConfluenceRestClient implements ConfluenceClient { @Override public String addPageUnderAncestor(String spaceKey, String ancestorId, String title, String content, String versionMessage) { HttpPost addPageUnderSpaceRequest = this.httpRequestFactory.addPageUnderAncestorRequest(spaceKey, ancestorId, title, content, versionMessage); return sendRequestAndFailIfNot20x(addPageUnderSpaceRequest, (response) -> { String contentId = extractIdFromJsonNode(parseJsonResponse(response)); return contentId; }); } 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 addPageUnderAncestor_withValidParameters_returnsCreatedPageContentId() throws Exception { String expectedContentId = "1234"; CloseableHttpClient httpClientMock = recordHttpClientForSingleResponseWithContentAndStatusCode("{\"id\":\"" + expectedContentId + "\"}", 200); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient(CONFLUENCE_ROOT_URL, httpClientMock, null, null, null); String contentId = confluenceRestClient.addPageUnderAncestor("~personalSpace", "123", "Hello", "Content", "Version Message"); assertThat(contentId, is(expectedContentId)); }
@Test public void addPageUnderAncestor_withUnsuccessfulRequest_throwsExceptionWithRequestInformationAndRootCause() throws Exception { IOException exception = new IOException("expected"); CloseableHttpClient httpClientMock = recordHttpClientForRequestException(exception); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient(CONFLUENCE_ROOT_URL, httpClientMock, null, null, null); this.expectedException.expect(RequestFailedException.class); this.expectedException.expectCause(is(equalTo(exception))); this.expectedException.expectMessage("request failed (" + "request: POST http: "{\"title\":\"Hello\"," + "\"space\":{\"key\":\"~personalSpace\"}," + "\"body\":{\"storage\":{\"value\":\"Content\",\"representation\":\"storage\"}}," + "\"ancestors\":[{\"id\":\"123\"}]," + "\"version\":{\"number\":1,\"message\":\"Version Message\"}," + "\"type\":\"page\"}, " + "response: <none>, " + "reason: 'expected'" + ")"); confluenceRestClient.addPageUnderAncestor("~personalSpace", "123", "Hello", "Content", "Version Message"); }
@Test public void addPageUnderAncestor_withUnsuccessfulResponse_throwsExceptionWithResponseInformation() throws Exception { CloseableHttpClient httpClientMock = recordHttpClientForSingleResponseWithContentAndStatusCode("{\"some\": \"json\"}", 404, "reason"); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient(CONFLUENCE_ROOT_URL, httpClientMock, null, null, null); this.expectedException.expect(RequestFailedException.class); this.expectedException.expectMessage("request failed (" + "request: POST http: "{\"title\":\"Hello\"," + "\"space\":{\"key\":\"~personalSpace\"}," + "\"body\":{\"storage\":{\"value\":\"Content\",\"representation\":\"storage\"}}," + "\"ancestors\":[{\"id\":\"123\"}]," + "\"version\":{\"number\":1,\"message\":\"Version Message\"}," + "\"type\":\"page\"}, " + "response: 404 reason " + "{\"some\": \"json\"}" + ")"); confluenceRestClient.addPageUnderAncestor("~personalSpace", "123", "Hello", "Content", "Version Message"); }