private Optional<MailboxId> readCreationIdAsMailboxId(MailboxCreationId creationId) { try { return Optional.of(mailboxIdFactory.fromString(creationId.getCreationId())); } catch (Exception e) { return Optional.empty(); } }
@Override public MailboxId deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { return factory.fromString(p.getValueAsString()); } }
private Optional<MessageSearchIndex.SearchResult> extractContentFromHit(SearchHit hit) { SearchHitField mailboxId = hit.field(JsonMessageConstants.MAILBOX_ID); SearchHitField uid = hit.field(JsonMessageConstants.UID); Optional<SearchHitField> id = retrieveMessageIdField(hit); if (mailboxId != null && uid != null) { Number uidAsNumber = uid.getValue(); return Optional.of( new MessageSearchIndex.SearchResult( id.map(field -> messageIdFactory.fromString(field.getValue())), mailboxIdFactory.fromString(mailboxId.getValue()), MessageUid.of(uidAsNumber.longValue()))); } else { LOGGER.warn("Can not extract UID, MessageID and/or MailboxId for search result {}", hit.getId()); return Optional.empty(); } }
when(mockedMailboxIdFactory.fromString(OUTBOX_ID.serialize())) .thenReturn(OUTBOX_ID); when(mockedMailboxManager.getMailbox(OUTBOX_ID, session))
@Test public void processShouldNotSpoolMailWhenNotSavingToOutbox() throws Exception { // When SetMessagesRequest notInOutboxCreationRequest = SetMessagesRequest.builder() .create(CreationMessageId.of("anything-really"), creationMessageBuilder.mailboxId("any-id-but-outbox-id") .build()) .build(); when(mockedMailboxManager.getMailbox(any(MailboxId.class), any())) .thenReturn(outbox); when(mockedMailboxIdFactory.fromString(anyString())).thenReturn(OUTBOX_ID); sut.process(notInOutboxCreationRequest, session); // Then verify(mockedMailSpool, never()).send(any(Mail.class), any(MailMetadata.class)); }
@Test public void processShouldNotSendWhenSavingToDrafts() throws Exception { // When CreationMessageId creationMessageId = CreationMessageId.of("anything-really"); SetMessagesRequest createMessageInDrafts = SetMessagesRequest.builder() .create( creationMessageId, creationMessageBuilder.mailboxId(DRAFTS_ID.serialize()).build()) .build(); when(mockedMailboxManager.getMailbox(any(MailboxId.class), any())) .thenReturn(drafts); when(mockedMailboxIdFactory.fromString(anyString())).thenReturn(DRAFTS_ID); sut.process(createMessageInDrafts, session); // Then verify(mockedMailSpool, never()).send(any(Mail.class), any(MailMetadata.class)); }
@Test public void assertIsUserOwnerOfMailboxesShouldNotThrowWhenUserIsTheOwnerOfTheMailbox() throws Exception { InMemoryId mailboxId = InMemoryId.of(6789); MessageManager mailbox = mock(MessageManager.class); when(mockedMailboxManager.getMailbox(mailboxId, session)) .thenReturn(mailbox); when(mockedMailboxIdFactory.fromString(mailboxId.serialize())) .thenReturn(mailboxId); when(mailbox.getMailboxPath()) .thenReturn(MailboxPath.forUser(USER, mailboxId.serialize())); sut.assertIsUserOwnerOfMailboxes(ImmutableList.of(mailboxId), session); }
@Override public Object deserializeKey(String key, DeserializationContext ctxt) { return factory.fromString(key); } }
@Test public void assertIsUserOwnerOfMailboxesShouldThrowWhenRetrievingMailboxPathFails() throws Exception { InMemoryId mailboxId = InMemoryId.of(6789); MessageManager mailbox = mock(MessageManager.class); when(mockedMailboxManager.getMailbox(mailboxId, session)) .thenReturn(mailbox); when(mockedMailboxIdFactory.fromString(mailboxId.serialize())) .thenReturn(mailboxId); when(mailbox.getMailboxPath()) .thenThrow(new MailboxException()); assertThatThrownBy(() -> sut.assertIsUserOwnerOfMailboxes(ImmutableList.of(mailboxId), session)); }
@Test public void validateIsUserOwnerOfMailboxesShouldThrowWhenMailboxIdDoesntExist() throws Exception { InMemoryId mailboxId = InMemoryId.of(6789); when(mockedMailboxManager.getMailbox(mailboxId, session)) .thenThrow(new MailboxNotFoundException(mailboxId)); when(mockedMailboxIdFactory.fromString(mailboxId.serialize())) .thenReturn(mailboxId); assertThatThrownBy(() -> sut.assertIsUserOwnerOfMailboxes(ImmutableList.of(mailboxId), session)); }
@Override public RegistrationKey fromString(String asString) { return new MailboxIdRegistrationKey(mailboxIdFactory.fromString(asString)); } }
@Test public void assertIsUserOwnerOfMailboxesShouldThrowWhenUserIsNotTheOwnerOfTheMailbox() throws Exception { InMemoryId mailboxId = InMemoryId.of(6789); MessageManager mailbox = mock(MessageManager.class); when(mockedMailboxManager.getMailbox(mailboxId, session)) .thenReturn(mailbox); when(mockedMailboxIdFactory.fromString(mailboxId.serialize())) .thenReturn(mailboxId); when(mailbox.getMailboxPath()) .thenReturn(MailboxPath.forUser("otheruser@example.com", mailboxId.serialize())); assertThatThrownBy(() -> sut.assertIsUserOwnerOfMailboxes(ImmutableList.of(mailboxId), session)) .isInstanceOf(MailboxNotOwnedException.class); }