HttpRequestFactory { HttpPost addAttachmentRequest(String contentId, String attachmentFileName, InputStream attachmentContent) { assertMandatoryParameter(isNotBlank(contentId), "contentId"); assertMandatoryParameter(isNotBlank(attachmentFileName), "attachmentFileName"); assertMandatoryParameter(attachmentContent != null, "attachmentContent"); HttpPost attachmentPostRequest = new HttpPost(this.confluenceRestApiEndpoint + "/content/" + contentId + "/child/attachment"); attachmentPostRequest.addHeader(new BasicHeader("X-Atlassian-Token", "no-check")); HttpEntity multipartEntity = multipartEntity(attachmentFileName, attachmentContent); attachmentPostRequest.setEntity(multipartEntity); return attachmentPostRequest; } HttpRequestFactory(String rootConfluenceUrl); HttpGet getAttachmentsRequest(String contentId, Integer limit, Integer start, String expandOptions); HttpGet getAttachmentContentRequest(String relativeDownloadLink); HttpGet getPropertyByKeyRequest(String contentId, String key); HttpDelete deletePropertyByKeyRequest(String contentId, String key); HttpPost setPropertyByKeyRequest(String contentId, String key, String value);  }
@Test public void addAttachmentRequest_withValidParameters_returnsValidHttpPostWithMultipartEntity() throws Exception { String contentId = "1234"; String attachmentFileName = "attachment.txt"; InputStream attachmentContent = new ByteArrayInputStream("Some text".getBytes()); HttpPost addAttachmentRequest = this.httpRequestFactory.addAttachmentRequest(contentId, attachmentFileName, attachmentContent); assertThat(addAttachmentRequest.getMethod(), is("POST")); assertThat(addAttachmentRequest.getURI().toString(), is(CONFLUENCE_REST_API_ENDPOINT + "/content/" + contentId + "/child/attachment")); assertThat(addAttachmentRequest.getFirstHeader("X-Atlassian-Token").getValue(), is("no-check")); ByteArrayOutputStream entityContent = new ByteArrayOutputStream(); addAttachmentRequest.getEntity().writeTo(entityContent); String multiPartPayload = entityContent.toString("UTF-8"); assertThat(multiPartPayload, containsString("attachment.txt")); assertThat(multiPartPayload, containsString("Some text")); }
@Test public void addAttachmentRequest_withEmptyContentId_throwsIllegalArgumentException() { this.expectedException.expect(IllegalArgumentException.class); this.expectedException.expectMessage("contentId must be set"); this.httpRequestFactory.addAttachmentRequest("", "file.txt", new ByteArrayInputStream("hello".getBytes())); }
@Test public void addAttachmentRequest_withEmptyAttachmentFileName_throwsIllegalArgumentException() { this.expectedException.expect(IllegalArgumentException.class); this.expectedException.expectMessage("attachmentFileName"); this.httpRequestFactory.addAttachmentRequest("1234", "", new ByteArrayInputStream("hello".getBytes())); }
@Test public void addAttachmentRequest_withNullAttachmentContent_throwsIllegalArgumentException() { this.expectedException.expect(IllegalArgumentException.class); this.expectedException.expectMessage("attachmentContent"); this.httpRequestFactory.addAttachmentRequest("1234", "file.txt", null); }