StoreBlobManager implements BlobManager { @Override public Blob retrieve(BlobId blobId, MailboxSession mailboxSession) throws MailboxException, BlobNotFoundException { return getBlobFromAttachment(blobId, mailboxSession) .orElseGet(() -> getBlobFromMessage(blobId, mailboxSession) .orElseThrow(() -> new BlobNotFoundException(blobId))); } @Inject  StoreBlobManager(AttachmentManager attachmentManager, MessageIdManager messageIdManager,
                            MessageId.Factory messageIdFactory); @Override BlobId toBlobId(MessageId messageId); @Override Blob retrieve(BlobId blobId, MailboxSession mailboxSession); static final ContentType MESSAGE_RFC822_CONTENT_TYPE; }
@Test void retrieveShouldReturnBlobWhenAttachment() throws Exception { when(attachmentManager.getAttachment(ATTACHMENT_ID, session)) .thenReturn(AttachmentMetadata.builder() .attachmentId(ATTACHMENT_ID) .size(BYTES.length) .type(CONTENT_TYPE) .build()); when(attachmentManager.loadAttachmentContent(ATTACHMENT_ID, session)) .thenReturn(new ByteArrayInputStream(BYTES)); Blob blob = blobManager.retrieve(BLOB_ID_ATTACHMENT, session); SoftAssertions.assertSoftly(Throwing.consumer( softly -> { assertThat(blob.getBlobId()).isEqualTo(BlobId.fromString(ATTACHMENT_ID.getId())); assertThat(blob.getContentType()).isEqualTo(CONTENT_TYPE); assertThat(blob.getSize()).isEqualTo(BYTES.length); assertThat(blob.getStream()).hasSameContentAs(new ByteArrayInputStream(BYTES)); })); }
@Test void retrieveShouldThrowWhenNotFound() throws Exception { when(attachmentManager.getAttachment(ATTACHMENT_ID, session)) .thenThrow(new AttachmentNotFoundException(ID)); when(messageIdManager.getMessage(MESSAGE_ID, FetchGroup.FULL_CONTENT, session)) .thenReturn(ImmutableList.of()); assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_ATTACHMENT, session)) .isInstanceOf(BlobNotFoundException.class); }
@Test void retrieveShouldReturnBlobWhenMessage() throws Exception { when(attachmentManager.getAttachment(any(), any())) .thenThrow(new AttachmentNotFoundException(ID)); MessageResult messageResult = mock(MessageResult.class); Content content = new ByteContent(BYTES); when(messageResult.getFullContent()).thenReturn(content); when(messageIdManager.getMessage(MESSAGE_ID, FetchGroup.FULL_CONTENT, session)) .thenReturn(ImmutableList.of(messageResult)); Blob blob = blobManager.retrieve(BLOB_ID_MESSAGE, session); SoftAssertions.assertSoftly(Throwing.consumer( softly -> { assertThat(blob.getBlobId()).isEqualTo(BLOB_ID_MESSAGE); assertThat(blob.getContentType()).isEqualTo(MESSAGE_RFC822_CONTENT_TYPE); assertThat(blob.getSize()).isEqualTo(BYTES.length); assertThat(blob.getStream()).hasSameContentAs(new ByteArrayInputStream(BYTES)); })); }
@Test void retrieveShouldThrowOnMailboxExceptionWhenRetrievingAttachment() throws Exception { when(attachmentManager.getAttachment(any(), any())) .thenThrow(new MailboxException()); assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session)) .isInstanceOf(MailboxException.class); }
@Test void retrieveShouldThrowOnRuntimeExceptionWhenRetrievingAttachment() throws Exception { when(attachmentManager.getAttachment(any(), any())) .thenThrow(new RuntimeException()); assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session)) .isInstanceOf(RuntimeException.class); }
@Test void retrieveShouldThrowOnRuntimeExceptionWhenRetrievingMessage() throws Exception { when(attachmentManager.getAttachment(any(), any())) .thenThrow(new AttachmentNotFoundException(ID)); when(messageIdManager.getMessage(MESSAGE_ID, FetchGroup.FULL_CONTENT, session)) .thenThrow(new RuntimeException()); assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session)) .isInstanceOf(RuntimeException.class); }
@Test void retrieveShouldThrowOnMailboxExceptionWhenRetrievingMessage() throws Exception { when(attachmentManager.getAttachment(any(), any())) .thenThrow(new AttachmentNotFoundException(ID)); when(messageIdManager.getMessage(MESSAGE_ID, FetchGroup.FULL_CONTENT, session)) .thenThrow(new MailboxException()); assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session)) .isInstanceOf(RuntimeException.class); }
@Test void retrieveShouldThrowOnMailboxExceptionWhenRetrievingMessageContent() throws Exception { when(attachmentManager.getAttachment(any(), any())) .thenThrow(new AttachmentNotFoundException(ID)); MessageResult messageResult = mock(MessageResult.class); when(messageResult.getFullContent()).thenThrow(new MailboxException()); when(messageIdManager.getMessage(MESSAGE_ID, FetchGroup.FULL_CONTENT, session)) .thenReturn(ImmutableList.of(messageResult)); assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session)) .isInstanceOf(RuntimeException.class); }
@Test void retrieveShouldThrowOnRuntimeExceptionWhenRetrievingMessageContent() throws Exception { when(attachmentManager.getAttachment(any(), any())) .thenThrow(new AttachmentNotFoundException(ID)); MessageResult messageResult = mock(MessageResult.class); when(messageResult.getFullContent()).thenThrow(new RuntimeException()); when(messageIdManager.getMessage(MESSAGE_ID, FetchGroup.FULL_CONTENT, session)) .thenReturn(ImmutableList.of(messageResult)); assertThatThrownBy(() -> blobManager.retrieve(BLOB_ID_MESSAGE, session)) .isInstanceOf(RuntimeException.class); }
@Test void retrieveShouldThrowOnIOExceptionWhenRetrievingMessageContentInputStream() throws Exception { when(attachmentManager.getAttachment(any(), any())) .thenThrow(new AttachmentNotFoundException(ID)); MessageResult messageResult = mock(MessageResult.class); Content content = mock(Content.class); when(content.getInputStream()).thenThrow(new IOException()); when(messageResult.getFullContent()).thenReturn(content); when(messageIdManager.getMessage(MESSAGE_ID, FetchGroup.FULL_CONTENT, session)) .thenReturn(ImmutableList.of(messageResult)); Blob blob = blobManager.retrieve(BLOB_ID_MESSAGE, session); assertThatThrownBy(blob::getStream) .isInstanceOf(IOException.class); }
@Test void retrieveShouldThrowOnRuntimeExceptionWhenRetrievingMessageContentInputStream() throws Exception { when(attachmentManager.getAttachment(any(), any())) .thenThrow(new AttachmentNotFoundException(ID)); MessageResult messageResult = mock(MessageResult.class); Content content = mock(Content.class); when(content.getInputStream()).thenThrow(new RuntimeException()); when(messageResult.getFullContent()).thenReturn(content); when(messageIdManager.getMessage(MESSAGE_ID, FetchGroup.FULL_CONTENT, session)) .thenReturn(ImmutableList.of(messageResult)); Blob blob = blobManager.retrieve(BLOB_ID_MESSAGE, session); assertThatThrownBy(blob::getStream) .isInstanceOf(RuntimeException.class); }