@Test public void getByteShouldReturnByteArrayRepresentingTheAttachment() { String input = "mystream"; Attachment attachment = Attachment.builder() .bytes(input.getBytes(CHARSET)) .type("content") .build(); byte[] bytes = attachment.getBytes(); assertThat(new String(bytes, CHARSET)).isEqualTo(input); }
@Test public void buildShouldSetTheSize() { String input = "mystream"; Attachment attachment = Attachment.builder() .bytes(input.getBytes(CHARSET)) .type("content") .build(); assertThat(attachment.getSize()).isEqualTo(input.getBytes(CHARSET).length); }
@Test public void streamShouldBeConsumedOneTime() throws Exception { String input = "mystream"; Attachment attachment = Attachment.builder() .bytes(input.getBytes(CHARSET)) .type("content") .build(); InputStream stream = attachment.getStream(); assertThat(stream).isNotNull(); assertThat(IOUtils.toString(stream, CHARSET)).isEqualTo(input); }
@Test void getAttachmentShouldReturnAttachmentWhenStored() { Attachment attachment = Attachment.builder() .attachmentId(ATTACHMENT_ID) .type("application/json") .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8)) .build(); BlobId blobId = BLOB_ID_FACTORY.from("blobId"); DAOAttachment daoAttachment = CassandraAttachmentDAOV2.from(attachment, blobId); testee.storeAttachment(daoAttachment).join(); Optional<DAOAttachment> actual = testee.getAttachment(ATTACHMENT_ID).join(); assertThat(actual).contains(daoAttachment); } }
private Attachment attachment(Row row) { return Attachment.builder() .attachmentId(AttachmentId.from(row.getString(ID))) .bytes(row.getBytes(PAYLOAD).array()) .type(row.getString(TYPE)) .build(); } }
public Attachment toAttachment(byte[] data) { return Attachment.builder() .attachmentId(attachmentId) .type(type) .bytes(data) .build(); }
@Test public void isInlinedWithCidShouldReturnFalseWhenIsInlineButNoCid() throws Exception { Attachment attachment = Attachment.builder() .bytes("content".getBytes()) .type("type") .build(); MessageAttachment messageAttachment = MessageAttachment.builder() .attachment(attachment) .name("name") .isInline(true) .build(); assertThat(messageAttachment.isInlinedWithCid()).isFalse(); } }
@Test void deleteAttachmentShouldRemoveAttachment() throws Exception { Attachment attachment = Attachment.builder() .attachmentId(ATTACHMENT_ID) .type("application/json") .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8)) .build(); testee.storeAttachment(attachment).join(); testee.deleteAttachment(attachment.getAttachmentId()).join(); assertThat(testee.getAttachment(attachment.getAttachmentId()).join()) .isEmpty(); } }
@Test public void isInlinedWithCidShouldReturnTrueWhenIsInlineAndHasCid() throws Exception { Attachment attachment = Attachment.builder() .bytes("content".getBytes()) .type("type") .build(); MessageAttachment messageAttachment = MessageAttachment.builder() .attachment(attachment) .name("name") .cid(Cid.from("cid")) .isInline(true) .build(); assertThat(messageAttachment.isInlinedWithCid()).isTrue(); }
@Test void getAttachmentShouldReturnV1WhenV2Absent() throws Exception { Attachment attachment = Attachment.builder() .attachmentId(ATTACHMENT_ID_1) .type("application/json") .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8)) .build(); attachmentDAO.storeAttachment(attachment).join(); assertThat(attachmentMapper.getAttachment(ATTACHMENT_ID_1)) .isEqualTo(attachment); }
private MessageAttachment convertReportToAttachment(MDNReport mdnReport) { Attachment attachment = Attachment.builder() .bytes(mdnReport.formattedValue().getBytes(StandardCharsets.UTF_8)) .type(MDN.DISPOSITION_CONTENT_TYPE) .build(); return MessageAttachment.builder() .attachment(attachment) .isInline(true) .build(); }
@Test void getAttachmentShouldReturnAttachmentWhenStored() throws Exception { Attachment attachment = Attachment.builder() .attachmentId(ATTACHMENT_ID) .type("application/json") .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8)) .build(); testee.storeAttachment(attachment).join(); Optional<Attachment> actual = testee.getAttachment(ATTACHMENT_ID).join(); assertThat(actual).contains(attachment); }
@Test void getAttachmentsShouldReturnV1WhenV2Absent() throws Exception { Attachment attachment = Attachment.builder() .attachmentId(ATTACHMENT_ID_1) .type("application/json") .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8)) .build(); attachmentDAO.storeAttachment(attachment).join(); assertThat(attachmentMapper.getAttachments(ImmutableList.of(ATTACHMENT_ID_1))) .containsExactly(attachment); }
@Test public void buildShouldWorkWhenMandatoryAttributesAreGiven() { Attachment attachment = Attachment.builder() .bytes("content".getBytes()) .type("type") .build(); MessageAttachment expectedMessageAttachment = new MessageAttachment(attachment, Optional.empty(), Optional.empty(), false); MessageAttachment messageAttachment = MessageAttachment.builder() .attachment(attachment) .build(); assertThat(messageAttachment).isEqualTo(expectedMessageAttachment); }
private UploadResponse uploadContent(String contentType, InputStream inputStream, MailboxSession session) throws IOException, MailboxException { Attachment attachment = Attachment.builder() .bytes(ByteStreams.toByteArray(inputStream)) .type(contentType) .build(); attachmentManager.storeAttachment(attachment, session); return UploadResponse.builder() .blobId(attachment.getAttachmentId().getId()) .type(attachment.getType()) .size(attachment.getSize()) .build(); }
private MessageAttachment createAttachment() { return MessageAttachment.builder() .attachment(Attachment.builder() .bytes("content".getBytes(StandardCharsets.UTF_8)) .type("type") .build()) .build(); } }
@Test public void buildShouldAcceptIsInlineAndNoCid() { Attachment attachment = Attachment.builder() .bytes("content".getBytes()) .type("type") .build(); MessageAttachment messageAttachment = MessageAttachment.builder() .attachment(attachment) .isInline(true) .build(); assertThat(messageAttachment.isInline()).isTrue(); }
@Test public void streamShouldBeConsumedMoreThanOneTime() throws Exception { String input = "mystream"; Attachment attachment = Attachment.builder() .bytes(input.getBytes(CHARSET)) .type("content") .build(); attachment.getStream(); InputStream stream = attachment.getStream(); assertThat(stream).isNotNull(); assertThat(IOUtils.toString(stream, CHARSET)).isEqualTo(input); }
@Test (expected = IllegalArgumentException.class) public void builderShouldThrowWhenTypeIsEmpty() { Attachment.builder() .type(""); }
@Test (expected = IllegalArgumentException.class) public void builderShouldThrowWhenTypeIsNull() { Attachment.builder() .type(null); }