public Attachment build() { Preconditions.checkState(bytes != null, "'bytes' is mandatory"); Preconditions.checkState(type != null, "'type' is mandatory"); AttachmentId builtAttachmentId = attachmentId(); Preconditions.checkState(builtAttachmentId != null, "'attachmentId' is mandatory"); return new Attachment(bytes, builtAttachmentId, type, size()); }
@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 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 attachmentsByIdShouldReturnMapWhenExist() { AttachmentId attachmentId = AttachmentId.from("1"); AttachmentId attachmentId2 = AttachmentId.from("2"); Set<AttachmentId> attachmentIds = ImmutableSet.of(attachmentId, attachmentId2); Attachment attachment = Attachment.builder() .attachmentId(attachmentId) .bytes("attachment".getBytes()) .type("type") .build(); Attachment attachment2 = Attachment.builder() .attachmentId(attachmentId2) .bytes("attachment2".getBytes()) .type("type") .build(); when(attachmentMapper.getAttachmentsAsFuture(attachmentIds)) .thenReturn(CompletableFuture.completedFuture(ImmutableList.of(attachment, attachment2))); Map<AttachmentId, Attachment> attachmentsById = testee.attachmentsById(attachmentIds) .join(); assertThat(attachmentsById).hasSize(2) .containsOnly(MapEntry.entry(attachmentId, attachment), MapEntry.entry(attachmentId2, attachment2)); }
@Test public void getAttachmentsShouldWorkWithDuplicatedAttachments() { 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))); Optional<String> name = Optional.of("name1"); Optional<Cid> cid = Optional.empty(); boolean isInlined = false; MessageAttachmentRepresentation attachmentRepresentation = new MessageAttachmentRepresentation(attachmentId, name, cid, isInlined); Collection<MessageAttachment> attachments = testee.getAttachments(ImmutableList.of(attachmentRepresentation, attachmentRepresentation)) .join(); MessageAttachment expectedAttachment = new MessageAttachment(attachment, name, cid, isInlined); assertThat(attachments).hasSize(2) .containsOnly(expectedAttachment, expectedAttachment); }
@Test void retrieveAllShouldReturnStoredAttachments() throws Exception { Attachment attachment1 = Attachment.builder() .attachmentId(ATTACHMENT_ID) .type("application/json") .bytes("{\"property\":`\"value1\"}".getBytes(StandardCharsets.UTF_8)) .build(); Attachment attachment2 = Attachment.builder() .attachmentId(ATTACHMENT_ID_2) .type("application/json") .bytes("{\"property\":`\"value2\"}".getBytes(StandardCharsets.UTF_8)) .build(); testee.storeAttachment(attachment1).join(); testee.storeAttachment(attachment2).join(); assertThat( testee.retrieveAll() .collect(Guavate.toImmutableList())) .containsOnly(attachment1, attachment2); }
@BeforeEach void setUp(CassandraCluster cassandra) { attachmentDAO = new CassandraAttachmentDAO(cassandra.getConf(), CassandraUtils.WITH_DEFAULT_CONFIGURATION, CassandraConfiguration.DEFAULT_CONFIGURATION); attachmentDAOV2 = new CassandraAttachmentDAOV2(BLOB_ID_FACTORY, cassandra.getConf()); blobsDAO = new CassandraBlobsDAO(cassandra.getConf()); migration = new AttachmentV2Migration(attachmentDAO, attachmentDAOV2, blobsDAO); attachment1 = Attachment.builder() .attachmentId(ATTACHMENT_ID) .type("application/json") .bytes("{\"property\":`\"value1\"}".getBytes(StandardCharsets.UTF_8)) .build(); attachment2 = Attachment.builder() .attachmentId(ATTACHMENT_ID_2) .type("application/json") .bytes("{\"property\":`\"value2\"}".getBytes(StandardCharsets.UTF_8)) .build(); }
@Test void getAttachmentsShouldCombineElementsFromV1AndV2() throws Exception { Attachment attachment = Attachment.builder() .attachmentId(ATTACHMENT_ID_1) .type("application/json") .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8)) .build(); Attachment otherAttachment = Attachment.builder() .attachmentId(ATTACHMENT_ID_2) .type("application/json") .bytes("{\"property\":`\"different\"}".getBytes(StandardCharsets.UTF_8)) .build(); BlobId blobId = blobsDAO.save(attachment.getBytes()).join(); attachmentDAOV2.storeAttachment(CassandraAttachmentDAOV2.from(attachment, blobId)).join(); attachmentDAO.storeAttachment(otherAttachment).join(); assertThat(attachmentMapper.getAttachments(ImmutableList.of(ATTACHMENT_ID_1, ATTACHMENT_ID_2))) .containsExactly(attachment, otherAttachment); } }
@Test void getAttachmentsShouldReturnV2WhenV2AndV1() throws Exception { Attachment attachment = Attachment.builder() .attachmentId(ATTACHMENT_ID_1) .type("application/json") .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8)) .build(); Attachment otherAttachment = Attachment.builder() .attachmentId(ATTACHMENT_ID_1) .type("application/json") .bytes("{\"property\":`\"different\"}".getBytes(StandardCharsets.UTF_8)) .build(); BlobId blobId = blobsDAO.save(attachment.getBytes()).join(); attachmentDAOV2.storeAttachment(CassandraAttachmentDAOV2.from(attachment, blobId)).join(); attachmentDAO.storeAttachment(otherAttachment).join(); assertThat(attachmentMapper.getAttachments(ImmutableList.of(ATTACHMENT_ID_1))) .containsExactly(attachment); }
@Test public void getAttachmentsShouldWorkWithDuplicatedIds() { 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))); Optional<String> name1 = Optional.of("name1"); Optional<String> name2 = Optional.of("name2"); Optional<Cid> cid = Optional.empty(); boolean isInlined = false; MessageAttachmentRepresentation attachmentRepresentation1 = new MessageAttachmentRepresentation(attachmentId, name1, cid, isInlined); MessageAttachmentRepresentation attachmentRepresentation2 = new MessageAttachmentRepresentation(attachmentId, name2, cid, isInlined); Collection<MessageAttachment> attachments = testee.getAttachments(ImmutableList.of(attachmentRepresentation1, attachmentRepresentation2)) .join(); assertThat(attachments).hasSize(2) .containsOnly(new MessageAttachment(attachment, name1, cid, isInlined), new MessageAttachment(attachment, name2, cid, isInlined)); }
@Test void getAttachmentShouldReturnV2WhenPresentInV1AndV2() throws Exception { Attachment attachment = Attachment.builder() .attachmentId(ATTACHMENT_ID_1) .type("application/json") .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8)) .build(); Attachment otherAttachment = Attachment.builder() .attachmentId(ATTACHMENT_ID_1) .type("application/json") .bytes("{\"property\":`\"different\"}".getBytes(StandardCharsets.UTF_8)) .build(); BlobId blobId = blobsDAO.save(attachment.getBytes()).join(); attachmentDAOV2.storeAttachment(CassandraAttachmentDAOV2.from(attachment, blobId)).join(); attachmentDAO.storeAttachment(otherAttachment).join(); assertThat(attachmentMapper.getAttachment(ATTACHMENT_ID_1)) .isEqualTo(attachment); }
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 (expected = IllegalStateException.class) public void buildShouldThrowWhenBytesIsNotProvided() { Attachment.builder() .attachmentId(AttachmentId.random()) .build(); }
@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 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); }
@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); } }
@Test (expected = IllegalStateException.class) public void buildShouldThrowWhenTypeIsNotProvided() { Attachment.builder() .attachmentId(AttachmentId.random()) .bytes("mystream".getBytes(CHARSET)) .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 (expected = IllegalArgumentException.class) public void builderShouldThrowWhenAttachmentIdIsNull() { Attachment.builder() .attachmentId(null); }