MailboxFactory { public MailboxBuilder builder() { QuotaLoader defaultQuotaLoader = new DefaultQuotaLoader(quotaRootResolver, quotaManager); return new MailboxBuilder(this, defaultQuotaLoader); } @Inject  MailboxFactory(MailboxManager mailboxManager, QuotaManager quotaManager, QuotaRootResolver quotaRootResolver); MailboxBuilder builder();  }
@Test public void mailboxFromMailboxIdShouldReturnAbsentWhenDoesntExist() throws Exception { Optional<Mailbox> mailbox = sut.builder() .id(InMemoryId.of(123)) .session(mailboxSession) .build(); assertThat(mailbox).isEmpty(); }
@Test public void mailboxFromMailboxIdShouldReturnPresentWhenExists() throws Exception { MailboxPath mailboxPath = MailboxPath.forUser(user, "myBox"); mailboxManager.createMailbox(mailboxPath, mailboxSession); MailboxId mailboxId = mailboxManager.getMailbox(mailboxPath, mailboxSession).getId(); Optional<Mailbox> mailbox = sut.builder() .id(mailboxId) .session(mailboxSession) .build(); assertThat(mailbox).isPresent(); assertThat(mailbox.get().getId()).isEqualTo(mailboxId); }
@Test public void buildShouldRelyOnPreloadedMailboxes() throws Exception { MailboxPath inbox = MailboxPath.inbox(user); Optional<MailboxId> inboxId = mailboxManager.createMailbox(inbox, mailboxSession); Optional<MailboxId> otherId = mailboxManager.createMailbox(inbox.child("child", '.'), mailboxSession); InMemoryId preLoadedId = InMemoryId.of(45); Mailbox retrievedMailbox = sut.builder() .id(otherId.get()) .session(mailboxSession) .usingPreloadedMailboxesMetadata(Optional.of(ImmutableList.of(new MailboxMetaData( inbox, preLoadedId, DELIMITER, MailboxMetaData.Children.NO_INFERIORS, MailboxMetaData.Selectability.NONE, MailboxACL.EMPTY, MailboxCounters.builder() .mailboxId(preLoadedId) .count(0) .unseen(0) .build())))) .build() .get(); assertThat(retrievedMailbox.getParentId()) .contains(preLoadedId); }
@Test public void ownerShouldHaveFullRightsViaMayProperties() throws Exception { MailboxPath inbox = MailboxPath.forUser(user, "inbox"); Optional<MailboxId> mailboxId = mailboxManager.createMailbox(inbox, mailboxSession); Mailbox retrievedMailbox = sut.builder() .id(mailboxId.get()) .session(mailboxSession) .build() .get(); softly.assertThat(retrievedMailbox.isMayAddItems()).isTrue(); softly.assertThat(retrievedMailbox.isMayCreateChild()).isTrue(); softly.assertThat(retrievedMailbox.isMayDelete()).isTrue(); softly.assertThat(retrievedMailbox.isMayReadItems()).isTrue(); softly.assertThat(retrievedMailbox.isMayRemoveItems()).isTrue(); softly.assertThat(retrievedMailbox.isMayRename()).isTrue(); }
@Test public void delegatedUserShouldHaveMayAddItemsWhenAllowedToInsert() throws Exception { MailboxPath inbox = MailboxPath.inbox(user); Optional<MailboxId> mailboxId = mailboxManager.createMailbox(inbox, mailboxSession); mailboxManager.applyRightsCommand(inbox, MailboxACL.command() .forUser(otherUser) .rights(MailboxACL.Right.Insert, MailboxACL.Right.Lookup) .asAddition(), mailboxSession); Mailbox retrievedMailbox = sut.builder() .id(mailboxId.get()) .session(otherMailboxSession) .build() .get(); softly.assertThat(retrievedMailbox.isMayAddItems()).isTrue(); softly.assertThat(retrievedMailbox.isMayCreateChild()).isFalse(); softly.assertThat(retrievedMailbox.isMayDelete()).isFalse(); softly.assertThat(retrievedMailbox.isMayReadItems()).isFalse(); softly.assertThat(retrievedMailbox.isMayRemoveItems()).isFalse(); softly.assertThat(retrievedMailbox.isMayRename()).isFalse(); }
@Test public void delegatedUserShouldHaveMayReadItemsWhenAllowedToRead() throws Exception { MailboxPath inbox = MailboxPath.inbox(user); Optional<MailboxId> mailboxId = mailboxManager.createMailbox(inbox, mailboxSession); mailboxManager.applyRightsCommand(inbox, MailboxACL.command() .forUser(otherUser) .rights(MailboxACL.Right.Read, MailboxACL.Right.Lookup) .asAddition(), mailboxSession); Mailbox retrievedMailbox = sut.builder() .id(mailboxId.get()) .session(otherMailboxSession) .build() .get(); softly.assertThat(retrievedMailbox.isMayAddItems()).isFalse(); softly.assertThat(retrievedMailbox.isMayCreateChild()).isFalse(); softly.assertThat(retrievedMailbox.isMayDelete()).isFalse(); softly.assertThat(retrievedMailbox.isMayReadItems()).isTrue(); softly.assertThat(retrievedMailbox.isMayRemoveItems()).isFalse(); softly.assertThat(retrievedMailbox.isMayRename()).isFalse(); }
@Test public void delegatedUserShouldHaveMayRemoveItemsWhenAllowedToRemoveItems() throws Exception { MailboxPath inbox = MailboxPath.inbox(user); Optional<MailboxId> mailboxId = mailboxManager.createMailbox(inbox, mailboxSession); mailboxManager.applyRightsCommand(inbox, MailboxACL.command() .forUser(otherUser) .rights(MailboxACL.Right.DeleteMessages, MailboxACL.Right.Lookup) .asAddition(), mailboxSession); Mailbox retrievedMailbox = sut.builder() .id(mailboxId.get()) .session(otherMailboxSession) .build() .get(); softly.assertThat(retrievedMailbox.isMayAddItems()).isFalse(); softly.assertThat(retrievedMailbox.isMayCreateChild()).isFalse(); softly.assertThat(retrievedMailbox.isMayDelete()).isFalse(); softly.assertThat(retrievedMailbox.isMayReadItems()).isFalse(); softly.assertThat(retrievedMailbox.isMayRemoveItems()).isTrue(); softly.assertThat(retrievedMailbox.isMayRename()).isFalse(); }
@Test public void mailboxFromMetaDataShouldReturnPresentStoredValue() throws Exception { String name = "myBox"; MailboxPath mailboxPath = MailboxPath.forUser(user, name); mailboxManager.createMailbox(mailboxPath, mailboxSession); mailboxManager.setRights(mailboxPath, MailboxACL.EMPTY.apply(MailboxACL.command() .forUser(OTHER_USER) .rights(MailboxACL.Right.Lookup, MailboxACL.Right.Read) .asAddition()), mailboxSession); MailboxMetaData metaData = mailboxManager.search(MailboxQuery.privateMailboxesBuilder(mailboxSession).build(), mailboxSession) .toStream() .filter(metadata -> metadata.getPath().equals(mailboxPath)) .findFirst() .get(); Optional<Mailbox> mailbox = sut.builder() .mailboxMetadata(metaData) .session(mailboxSession) .build(); softly.assertThat(mailbox).isPresent(); softly.assertThat(mailbox).map(Mailbox::getId).contains(metaData.getId()); softly.assertThat(mailbox).map(Mailbox::getName).contains(name); softly.assertThat(mailbox).map(Mailbox::getTotalMessages).contains(Number.ZERO); softly.assertThat(mailbox).map(Mailbox::getUnreadMessages).contains(Number.ZERO); softly.assertThat(mailbox).map(Mailbox::getSharedWith).contains(new Rights(ImmutableMap.of( OTHER_USER, ImmutableList.of(Rights.Right.Lookup, Rights.Right.Read)))); }
@Test public void buildShouldThrowWhenBothMetadataAndId() { assertThatThrownBy(() -> sut.builder() .session(mailboxSession) .id(mock(MailboxId.class)) .mailboxMetadata(mock(MailboxMetaData.class)) .build()) .isInstanceOf(IllegalStateException.class); }
@Test public void buildShouldThrowWhenNoId() { assertThatThrownBy(() -> sut.builder() .session(mailboxSession) .build()) .isInstanceOf(IllegalStateException.class); }