@Override public MailboxId fromString(String serialized) { return of(Long.valueOf(serialized)); } }
public static InMemoryId of(long value) { return new InMemoryId(value); }
@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); }
@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)); }
when(mockedMailboxIdFactory.fromString(OUTBOX_ID.serialize())) .thenReturn(OUTBOX_ID); when(mockedMailboxManager.getMailbox(OUTBOX_ID, session))
@Override public InMemoryId deserialize(String serializedMailboxId) throws MailboxIdDeserialisationException { try { return InMemoryId.of(Long.valueOf(serializedMailboxId)); } catch (Exception e) { throw new MailboxIdDeserialisationException("Error deserializing " + serializedMailboxId, e); } }
@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); }
@Test public void processShouldCreateWhenKeywords() { SetMessagesRequest createMessageWithKeywords = SetMessagesRequest.builder() .create( creationMessageId, creationMessageBuilder .mailboxId(OUTBOX_ID.serialize()) .keywords(ImmutableMap.of("$Answered", true)) .build()) .build(); SetMessagesResponse result = sut.process(createMessageWithKeywords, session); assertThat(result.getCreated()).isNotEmpty(); assertThat(result.getNotCreated()).isEmpty(); }
@Override protected MailboxId generateMailboxId() { return InMemoryId.of(counter.incrementAndGet()); } }
@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)); }
@Test public void processShouldThrowWhenBothIsFlagAndKeywords() { expectedException.expect(IllegalArgumentException.class); SetMessagesRequest createMessageWithError = SetMessagesRequest.builder() .create( creationMessageId, creationMessageBuilder .mailboxId(OUTBOX_ID.serialize()) .isAnswered(Optional.of(true)) .keywords(ImmutableMap.of("$Answered", true)) .build()) .build(); sut.process(createMessageWithError, session); }
@Override protected MailboxId randomMailboxId() { return InMemoryId.of(MAILBOX_ID_GENERATOR.incrementAndGet()); } }
@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 processShouldCreateWhenIsFlag() { SetMessagesRequest createMessageWithKeywords = SetMessagesRequest.builder() .create( creationMessageId, creationMessageBuilder .mailboxId(OUTBOX_ID.serialize()) .isAnswered(Optional.of(true)) .build()) .build(); SetMessagesResponse result = sut.process(createMessageWithKeywords, session); assertThat(result.getCreated()).isNotEmpty(); assertThat(result.getNotCreated()).isEmpty(); }
@Override public InMemoryId generateId() { return InMemoryId.of(random.nextInt()); }
@Override protected MailboxId generateId() { return InMemoryId.of(counter.incrementAndGet()); } }
@Override protected MailboxId generateId() { return InMemoryId.of(counter.incrementAndGet()); } }
@Test public void mailboxIdShouldBeSerializable() throws Exception { MailboxIdTestContainer container = new MailboxIdTestContainer(InMemoryId.of(123)); String expectedJson = "{\"mailboxId\":\"123\"}"; String actual = testee.forWriting().writeValueAsString(container); assertThat(actual).isEqualTo(expectedJson); }
@Override public MailboxId save(Mailbox mailbox) throws MailboxException { InMemoryId id = (InMemoryId) mailbox.getMailboxId(); if (id == null) { id = InMemoryId.of(mailboxIdGenerator.incrementAndGet()); mailbox.setMailboxId(id); } else { try { Mailbox mailboxWithPreviousName = findMailboxById(id); mailboxesByPath.remove(mailboxWithPreviousName.generateAssociatedPath()); } catch (MailboxNotFoundException e) { // No need to remove the previous mailbox } } Mailbox previousMailbox = mailboxesByPath.putIfAbsent(mailbox.generateAssociatedPath(), mailbox); if (previousMailbox != null) { throw new MailboxExistsException(mailbox.getName()); } return mailbox.getMailboxId(); }
@Test public void idsShouldBePresentWhenListIsNotEmpty() { GetMailboxesRequest getMailboxesRequest = GetMailboxesRequest.builder() .ids(ImmutableList.of(InMemoryId.of(123))) .build(); assertThat(getMailboxesRequest.getIds()).isPresent(); assertThat(getMailboxesRequest.getIds().get()).containsExactly(InMemoryId.of(123)); }