private Optional<Blob> getBlobFromAttachment(BlobId blobId, MailboxSession mailboxSession) throws MailboxException { try { AttachmentId attachmentId = AttachmentId.from(blobId); return Optional.of(attachmentManager.getAttachment(attachmentId, mailboxSession).toBlob()); } catch (AttachmentNotFoundException e) { return Optional.empty(); } }
private AttachmentId getAttachmentId(Attachment attachment) { return AttachmentId.from(attachment.getBlobId().getRawValue()); } }
@Test public void fromShouldThrowWhenIdIsNull() { String value = null; assertThatThrownBy(() -> AttachmentId.from(value)).isInstanceOf(NullPointerException.class); }
@Test public void fromShouldThrowWhenBlobIdIsNull() { BlobId value = null; assertThatThrownBy(() -> AttachmentId.from(value)).isInstanceOf(NullPointerException.class); }
@Test public void fromShouldThrowWhenIdIsEmpty() { assertThatThrownBy(() -> AttachmentId.from("")).isInstanceOf(IllegalArgumentException.class); }
@Test public void asUUIDShouldReturnAValidUUID() { AttachmentId attachmentId = AttachmentId.from("magic"); assertThat(attachmentId.asUUID()) .isEqualTo(UUID.fromString("2f3a4fcc-ca64-36e3-9bcf-33e92dd93135")); } }
@Test public void attachmentsByIdShouldReturnEmptyMapWhenAttachmentsDontExists() { AttachmentId attachmentId = AttachmentId.from("1"); AttachmentId attachmentId2 = AttachmentId.from("2"); Set<AttachmentId> attachmentIds = ImmutableSet.of(attachmentId, attachmentId2); when(attachmentMapper.getAttachmentsAsFuture(attachmentIds)) .thenReturn(CompletableFuture.completedFuture(ImmutableList.of())); Map<AttachmentId, Attachment> attachmentsById = testee.attachmentsById(attachmentIds) .join(); assertThat(attachmentsById).hasSize(0); }
@Test public void fromStringShouldWork() { String expectedId = "f07e5a815613c5abeddc4b682247a4c42d8a95df"; AttachmentId attachmentId = AttachmentId.from(expectedId); assertThat(attachmentId.getId()).isEqualTo(expectedId); }
@Test void migrationShouldReturnPartialWhenSavingAttachmentIdForMessageIdFail() throws Exception { CassandraMessageDAO cassandraMessageDAO = mock(CassandraMessageDAO.class); CassandraAttachmentMessageIdDAO attachmentMessageIdDAO = mock(CassandraAttachmentMessageIdDAO.class); CassandraMessageDAO.MessageIdAttachmentIds messageIdAttachmentIds = mock(CassandraMessageDAO.MessageIdAttachmentIds.class); migration = new AttachmentMessageIdCreation(cassandraMessageDAO, attachmentMessageIdDAO); when(messageIdAttachmentIds.getAttachmentId()).thenReturn(ImmutableSet.of(AttachmentId.from("any"))); when(cassandraMessageDAO.retrieveAllMessageIdAttachmentIds()) .thenReturn(CompletableFuture.completedFuture(Stream.of(messageIdAttachmentIds))); when(attachmentMessageIdDAO.storeAttachmentForMessageId(any(AttachmentId.class), any(MessageId.class))) .thenThrow(new RuntimeException()); assertThat(migration.run()).isEqualTo(Migration.Result.PARTIAL); }
@Test public void fromBlobIdShouldWork() { String expectedId = "f07e5a815613c5abeddc4b682247a4c42d8a95df"; AttachmentId attachmentId = AttachmentId.from(BlobId.fromString(expectedId)); assertThat(attachmentId.getId()).isEqualTo(expectedId); }
@Test public void buildShouldWorkWhenMandatoryAttributesAreGiven() { AttachmentId attachmentId = AttachmentId.from("1"); MessageAttachmentRepresentation expectedMessageAttachmentRepresentation = new MessageAttachmentRepresentation(attachmentId, Optional.empty(), Optional.empty(), false); MessageAttachmentRepresentation messageAttachmentRepresentation = MessageAttachmentRepresentation.builder() .attachmentId(attachmentId) .build(); assertThat(messageAttachmentRepresentation).isEqualTo(expectedMessageAttachmentRepresentation); }
private Attachment attachment(Row row) { return Attachment.builder() .attachmentId(AttachmentId.from(row.getString(ID))) .bytes(row.getBytes(PAYLOAD).array()) .type(row.getString(TYPE)) .build(); } }
private static DAOAttachment fromRow(Row row, BlobId.Factory blobIfFactory) { return new DAOAttachment( AttachmentId.from(row.getString(ID)), blobIfFactory.from(row.getString(BLOB_ID)), row.getString(TYPE), row.getLong(SIZE)); }
@Test public void buildShouldAcceptInlineAndWithoutCid() { AttachmentId attachmentId = AttachmentId.from("1"); MessageAttachmentRepresentation expectedMessageAttachmentRepresentation = new MessageAttachmentRepresentation(attachmentId, Optional.empty(), Optional.empty(), true); MessageAttachmentRepresentation messageAttachmentRepresentation = MessageAttachmentRepresentation.builder() .attachmentId(attachmentId) .isInline(true) .build(); assertThat(messageAttachmentRepresentation).isEqualTo(expectedMessageAttachmentRepresentation); }
@Test public void getAttachmentsShouldReturnEmptyByDefault() { AttachmentId attachmentId = AttachmentId.from("1"); Set<AttachmentId> attachmentIds = ImmutableSet.of(attachmentId); Attachment attachment = Attachment.builder() .attachmentId(attachmentId) .bytes("attachment".getBytes()) .type("type") .build(); when(attachmentMapper.getAttachmentsAsFuture(attachmentIds)) .thenReturn(CompletableFuture.completedFuture(ImmutableList.of(attachment))); Collection<MessageAttachment> attachments = testee.getAttachments(ImmutableList.of()) .join(); assertThat(attachments).isEmpty(); }
@Test public void buildShouldSetIsInlineDefaultValueWhenNotGiven() { AttachmentId attachmentId = AttachmentId.from("1"); MessageAttachmentRepresentation messageAttachmentRepresentation = MessageAttachmentRepresentation.builder() .attachmentId(attachmentId) .build(); assertThat(messageAttachmentRepresentation.isInline()).isFalse(); }
@Test public void buildShouldSetAttributesWhenAllAreGiven() { AttachmentId attachmentId = AttachmentId.from("1"); MessageAttachmentRepresentation expectedMessageAttachmentRepresentation = new MessageAttachmentRepresentation(attachmentId, Optional.of("name"), Optional.of(Cid.from("cid")), true); MessageAttachmentRepresentation messageAttachmentRepresentation = MessageAttachmentRepresentation.builder() .attachmentId(attachmentId) .name("name") .cid(Cid.from("cid")) .isInline(true) .build(); assertThat(messageAttachmentRepresentation).isEqualTo(expectedMessageAttachmentRepresentation); } }
private MessageAttachmentRepresentation messageAttachmentByIdFrom(UDTValue udtValue) { return MessageAttachmentRepresentation.builder() .attachmentId(AttachmentId.from(udtValue.getString(Attachments.ID))) .name(udtValue.getString(Attachments.NAME)) .cid(cidParser.parse(udtValue.getString(CassandraMessageV2Table.Attachments.CID))) .isInline(udtValue.getBool(Attachments.IS_INLINE)) .build(); }
@Test public void assertAttachmentsExistShouldThrowWhenUnknownBlobId() throws MailboxException { BlobId unknownBlobId = BlobId.of("unknownBlobId"); AttachmentId unknownAttachmentId = AttachmentId.from(unknownBlobId.getRawValue()); when(attachmentManager.exists(unknownAttachmentId, session)).thenReturn(false); assertThatThrownBy(() -> sut.assertAttachmentsExist( new ValueWithId.CreationMessageEntry( creationMessageId, creationMessageBuilder.attachments( Attachment.builder().size(12L).type("image/jpeg").blobId(unknownBlobId).build()) .build() ), session)) .isInstanceOf(AttachmentsNotFoundException.class); }
@Test public void assertAttachmentsExistShouldNotThrowWhenAttachmentExists() throws Exception { BlobId blobId = BlobId.of("unknownBlobId"); AttachmentId attachmentId = AttachmentId.from(blobId.getRawValue()); when(attachmentManager.exists(attachmentId, session)).thenReturn(true); sut.assertAttachmentsExist( new ValueWithId.CreationMessageEntry( creationMessageId, creationMessageBuilder.attachments( Attachment.builder().size(12L).type("image/jpeg").blobId(blobId).build()) .build() ), session); }