ConfluenceRestClient implements ConfluenceClient { @Override public String getPageByTitle(String spaceKey, String title) throws NotFoundException, MultipleResultsException { HttpGet pageByTitleRequest = this.httpRequestFactory.getPageByTitleRequest(spaceKey, title); return sendRequestAndFailIfNot20x(pageByTitleRequest, (response) -> { JsonNode jsonNode = parseJsonResponse(response); int numberOfResults = jsonNode.get("size").asInt(); if (numberOfResults == 0) { throw new NotFoundException(); } if (numberOfResults > 1) { throw new MultipleResultsException(); } String contentId = extractIdFromJsonNode(jsonNode.withArray("results").elements().next()); 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 getPageByTitle_withValidParameters_sendsGetRequestAndReturnsFirstResultId() throws Exception { String expectedContentId = "1234"; CloseableHttpClient httpClientMock = recordHttpClientForSingleResponseWithContentAndStatusCode("{\"results\": [{\"id\":\"" + expectedContentId + "\"}], \"size\": 1}", 200); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient(CONFLUENCE_ROOT_URL, httpClientMock, null, null, null); String contentId = confluenceRestClient.getPageByTitle("~personalSpace", "Some title"); assertThat(contentId, is(expectedContentId)); }
@Test(expected = NotFoundException.class) public void getPageByTitle_withEmptyResult_throwsPageNotFoundException() throws Exception { CloseableHttpClient httpClientMock = recordHttpClientForSingleResponseWithContentAndStatusCode("{\"size\": 0}", 200); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient(CONFLUENCE_ROOT_URL, httpClientMock, null, null, null); confluenceRestClient.getPageByTitle("~personalSpace", "Some title"); }
@Test(expected = MultipleResultsException.class) public void getPageByTitle_withMultipleResults_throwsMultipleResultsException() throws Exception { CloseableHttpClient httpClientMock = recordHttpClientForSingleResponseWithContentAndStatusCode("{\"size\": 2}", 200); ConfluenceRestClient confluenceRestClient = new ConfluenceRestClient(CONFLUENCE_ROOT_URL, httpClientMock, null, null, null); confluenceRestClient.getPageByTitle("~personalSpace", "Some title"); }