@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); } }
@Override protected MailboxId generateMailboxId() { return InMemoryId.of(counter.incrementAndGet()); } }
@Override protected MailboxId randomMailboxId() { return InMemoryId.of(MAILBOX_ID_GENERATOR.incrementAndGet()); } }
@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); }
@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 idsShouldBePresentWhenListIsNotEmpty() { GetMailboxesRequest getMailboxesRequest = GetMailboxesRequest.builder() .ids(ImmutableList.of(InMemoryId.of(123))) .build(); assertThat(getMailboxesRequest.getIds()).isPresent(); assertThat(getMailboxesRequest.getIds().get()).containsExactly(InMemoryId.of(123)); }
@Test public void mailboxIdShouldBeDeserializableWhenKey() throws Exception { String json = "{ \"map\": {\"123\": \"value\"}}"; MailboxIdKeyTestContainer expected = new MailboxIdKeyTestContainer(ImmutableMap.of(InMemoryId.of(123), "value")); MailboxIdKeyTestContainer actual = testee.forParsing().readValue(json, MailboxIdKeyTestContainer.class); assertThat(actual).isEqualToComparingFieldByField(expected); }
@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 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 hasRoleShouldReturnTrueWhenMailboxHasSameRole() { Mailbox mailbox = Mailbox.builder() .id(InMemoryId.of(0)) .name("name") .role(Optional.of(Role.DRAFTS)) .build(); assertThat(mailbox.hasRole(Role.DRAFTS)).isTrue(); }
@Test public void hasSystemRoleShouldReturnTrueWhenMailboxHasSystemRole() throws Exception { Mailbox mailbox = Mailbox.builder() .name("mailbox") .id(InMemoryId.of(0)) .role(Optional.of(Role.OUTBOX)) .build(); assertThat(mailbox.hasSystemRole()).isTrue(); } }
@Test public void totalMessagesShouldNeverBeNegative() { Mailbox mailbox = Mailbox.builder() .id(InMemoryId.of(1)) .name("name") .totalMessages(-1234) .build(); assertThat(mailbox.getTotalMessages()).isEqualTo(Number.ZERO); }
@Test(expected = IllegalStateException.class) public void buildShouldThrowWhenNameIsNull() { Mailbox.builder() .id(InMemoryId.of(1)) .build(); }
@Test public void mailboxFromMailboxIdShouldReturnAbsentWhenDoesntExist() throws Exception { Optional<Mailbox> mailbox = sut.builder() .id(InMemoryId.of(123)) .session(mailboxSession) .build(); assertThat(mailbox).isEmpty(); }
@Test public void totalMessagesShouldAcceptPositiveValue() { Mailbox mailbox = Mailbox.builder() .id(InMemoryId.of(1)) .name("name") .totalMessages(1234) .build(); Number expectedTotalMessages = Number.fromLong(1234); assertThat(mailbox.getTotalMessages()).isEqualTo(expectedTotalMessages); }
@Test public void unreadMessagesShouldAcceptPositiveValue() { Mailbox mailbox = Mailbox.builder() .id(InMemoryId.of(1)) .name("name") .unreadMessages(1234) .build(); Number expectedTotalMessages = Number.fromLong(1234); assertThat(mailbox.getUnreadMessages()).isEqualTo(expectedTotalMessages); }
@Test public void getParentIdShouldReturnParentIdWhenParentIdGiven() throws Exception { //Given InMemoryId expected = InMemoryId.of(123); MailboxUpdateRequest testee = MailboxUpdateRequest.builder().parentId(expected).name("my box").build(); //When Optional<MailboxId> actual = testee.getParentId(); //Then assertThat(actual).contains(expected); } }