public MessageAttachment build() { Preconditions.checkState(attachment != null, "'attachment' is mandatory"); return new MessageAttachment(attachment, name, cid, isInline.orElse(false)); } }
@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); }
@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 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 public void getAttachmentsShouldReturnMultipleAttachmentWhenSeveralAttachmentsRepresentation() { AttachmentId attachmentId1 = AttachmentId.from("1"); AttachmentId attachmentId2 = AttachmentId.from("2"); Set<AttachmentId> attachmentIds = ImmutableSet.of(attachmentId1, attachmentId2); Attachment attachment1 = Attachment.builder() .attachmentId(attachmentId1) .bytes("attachment1".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(attachment1, attachment2))); Optional<String> name1 = Optional.of("name1"); Optional<String> name2 = Optional.of("name2"); Optional<Cid> cid = Optional.empty(); boolean isInlined = false; MessageAttachmentRepresentation attachmentRepresentation1 = new MessageAttachmentRepresentation(attachmentId1, name1, cid, isInlined); MessageAttachmentRepresentation attachmentRepresentation2 = new MessageAttachmentRepresentation(attachmentId2, name2, cid, isInlined); Collection<MessageAttachment> attachments = testee.getAttachments(ImmutableList.of(attachmentRepresentation1, attachmentRepresentation2)) .join(); assertThat(attachments).hasSize(2) .containsOnly(new MessageAttachment(attachment1, name1, cid, isInlined), new MessageAttachment(attachment2, name2, cid, isInlined)); }
@Test public void buildShouldSetAttributesWhenAllAreGiven() { Attachment attachment = Attachment.builder() .bytes("content".getBytes()) .type("type") .build(); MessageAttachment expectedMessageAttachment = new MessageAttachment(attachment, Optional.of("name"), Optional.of(Cid.from("cid")), true); MessageAttachment messageAttachment = MessageAttachment.builder() .attachment(attachment) .name("name") .cid(Cid.from("cid")) .isInline(true) .build(); assertThat(messageAttachment).isEqualTo(expectedMessageAttachment); }