MailRepositoryStoreService { public Optional<MimeMessage> retrieveMessage(MailRepositoryPath path, MailKey mailKey) throws MailRepositoryStore.MailRepositoryStoreException, MessagingException { return fecthMail(path, mailKey) .map(Throwing.function(Mail::getMessage).sneakyThrow()); } @Inject  MailRepositoryStoreService(MailRepositoryStore mailRepositoryStore); Stream<SingleMailRepositoryResponse> listMailRepositories(); MailRepository createMailRepository(MailRepositoryPath repositoryPath, String protocol); Optional<List<MailKeyDTO>> listMails(MailRepositoryPath path, Offset offset, Limit limit); Optional<Long> size(MailRepositoryPath path); Optional<MailDto> retrieveMail(MailRepositoryPath path, MailKey mailKey, Set<AdditionalField> additionalAttributes); Optional<MimeMessage> retrieveMessage(MailRepositoryPath path, MailKey mailKey); void deleteMail(MailRepositoryPath path, MailKey mailKey); Task createClearMailRepositoryTask(MailRepositoryPath path); Stream<MailRepository> getRepositories(MailRepositoryPath path);  }
@Test public void retrieveMessageShouldThrownWhenUnknownRepository() throws Exception { when(mailRepositoryStore.getByPath(FIRST_REPOSITORY_PATH)).thenReturn(Stream.of()); assertThatThrownBy(() -> testee.retrieveMessage(FIRST_REPOSITORY_PATH, NAME_1)) .isNotInstanceOf(NullPointerException.class); }
@Test public void retrieveMessageShouldThrowWhenMailRepositoryStoreThrows() throws Exception { when(mailRepositoryStore.getByPath(FIRST_REPOSITORY_PATH)) .thenThrow(new MailRepositoryStore.MailRepositoryStoreException("message")); assertThatThrownBy(() -> testee.retrieveMessage(FIRST_REPOSITORY_PATH, NAME_1)) .isInstanceOf(MailRepositoryStore.MailRepositoryStoreException.class); }
@Test public void retrieveMessageShouldReturnEmptyWhenMailNotFound() throws Exception { when(mailRepositoryStore.getByPath(FIRST_REPOSITORY_PATH)).thenReturn(Stream.of(repository)); assertThat(testee.retrieveMessage(FIRST_REPOSITORY_PATH, NAME_1)) .isEmpty(); }
@Test public void retrieveMessageShouldReturnTheMessageWhenMailExists() throws Exception { when(mailRepositoryStore.getByPath(FIRST_REPOSITORY_PATH)).thenReturn(Stream.of(repository)); FakeMail mail = FakeMail.builder() .name(NAME_1.asString()) .fileName("mail.eml") .build(); repository.store(mail); Optional<MimeMessage> mimeMessage = testee.retrieveMessage(FIRST_REPOSITORY_PATH, NAME_1); assertThat(mimeMessage).isNotEmpty(); String eml = IOUtils.toString(new MimeMessageInputStream(mimeMessage.get()), StandardCharsets.UTF_8); String expectedContent = ClassLoaderUtils.getSystemResourceAsString("mail.eml"); assertThat(eml).isEqualToNormalizingNewlines(expectedContent); }