JAMES-1785 Testing for SystemMailboxesProviderImpl
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/54fe9a4b Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/54fe9a4b Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/54fe9a4b Branch: refs/heads/master Commit: 54fe9a4bd00b69cc048d554464ee987f3c302343 Parents: 1acd459 Author: Quynh Nguyen <[email protected]> Authored: Wed Jan 18 16:29:53 2017 +0700 Committer: Quynh Nguyen <[email protected]> Committed: Tue Jan 24 09:46:01 2017 +0700 ---------------------------------------------------------------------- .../utils/SystemMailboxesProviderImplTest.java | 137 ++++++++++++++++++- 1 file changed, 132 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/54fe9a4b/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/SystemMailboxesProviderImplTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/SystemMailboxesProviderImplTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/SystemMailboxesProviderImplTest.java index 3ee868d..1d40c41 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/SystemMailboxesProviderImplTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/SystemMailboxesProviderImplTest.java @@ -19,15 +19,142 @@ package org.apache.james.jmap.utils; -import org.junit.Ignore; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.stream.Stream; + +import org.apache.james.jmap.exceptions.MailboxRoleNotFoundException; +import org.apache.james.jmap.model.mailbox.Role; +import org.apache.james.mailbox.MailboxManager; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.MessageManager; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.manager.MailboxManagerFixture; +import org.apache.james.mailbox.mock.MockMailboxSession; +import org.apache.james.mailbox.model.MailboxId; +import org.apache.james.mailbox.model.MailboxMetaData; +import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.mailbox.model.MailboxQuery; +import org.apache.james.mailbox.model.TestId; +import org.apache.james.mailbox.store.SimpleMailboxMetaData; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.google.common.collect.ImmutableList; public class SystemMailboxesProviderImplTest { - @Ignore("1716 this class needs a test suite") + private static final MailboxPath INBOX = MailboxManagerFixture.MAILBOX_PATH1; + private static final MailboxPath OUTBOX = MailboxManagerFixture.MAILBOX_PATH2; + private static final char DELIMITER = '.'; + + private static final MailboxId inboxId = TestId.of(1); + private static final MailboxId outboxId = TestId.of(2); + + private static final MailboxMetaData inboxMetadata = new SimpleMailboxMetaData(INBOX, inboxId, DELIMITER); + private static final MailboxMetaData outboxMetadata = new SimpleMailboxMetaData(OUTBOX, outboxId, DELIMITER); + + private MailboxSession mailboxSession = new MockMailboxSession("user"); + private SystemMailboxesProviderImpl systemMailboxProvider; + + private MailboxManager mailboxManager; + + private MessageManager inboxMessageManager; + private MessageManager outboxMessageManager; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + mailboxManager = mock(MailboxManager.class); + inboxMessageManager = mock(MessageManager.class); + outboxMessageManager = mock(MessageManager.class); + + systemMailboxProvider = new SystemMailboxesProviderImpl(mailboxManager); + } + + @Test + public void findMailboxesShouldReturnEmptyWhenEmptySearchResult() throws Exception { + when(mailboxManager.search(any(MailboxQuery.class), eq(mailboxSession))).thenReturn(ImmutableList.of()); + + assertThat(systemMailboxProvider.listMailboxes(Role.INBOX, mailboxSession)).isEmpty(); + } + + @Test + public void findMailboxesShouldFilterTheMailboxByItsRole() throws Exception { + when(mailboxManager.search(any(MailboxQuery.class), eq(mailboxSession))).thenReturn(ImmutableList.of(inboxMetadata, outboxMetadata)); + when(mailboxManager.getMailbox(eq(INBOX), eq(mailboxSession))).thenReturn(inboxMessageManager); + + Stream<MessageManager> result = systemMailboxProvider.listMailboxes(Role.INBOX, mailboxSession); + + assertThat(result).hasSize(1).containsOnly(inboxMessageManager); + } + + @Test + public void findMailboxesShouldThrowWhenMailboxManagerHasErrorWhenSearching() throws Exception { + expectedException.expect(MailboxException.class); + + when(mailboxManager.search(any(MailboxQuery.class), eq(mailboxSession))).thenThrow(MailboxException.class); + + systemMailboxProvider.listMailboxes(Role.INBOX, mailboxSession); + } + + @Test + public void findMailboxesShouldBeEmptyWhenMailboxManagerCanNotGetMailbox() throws Exception { + expectedException.expect(MailboxException.class); + + when(mailboxManager.search(any(MailboxQuery.class), eq(mailboxSession))).thenReturn(ImmutableList.of(inboxMetadata, outboxMetadata)); + when(mailboxManager.getMailbox(eq(INBOX), eq(mailboxSession))).thenThrow(MailboxException.class); + + assertThat(systemMailboxProvider.listMailboxes(Role.INBOX, mailboxSession)).isEmpty(); + } + + @Test + public void findMailboxesShouldReturnWhenMailboxManagerCanNotGetMailboxOfNonFilterMailbox() throws Exception { + when(mailboxManager.search(any(MailboxQuery.class), eq(mailboxSession))).thenReturn(ImmutableList.of(inboxMetadata, outboxMetadata)); + + when(mailboxManager.getMailbox(eq(INBOX), eq(mailboxSession))).thenReturn(inboxMessageManager); + when(mailboxManager.getMailbox(eq(OUTBOX), eq(mailboxSession))).thenThrow(MailboxException.class); + + Stream<MessageManager> result = systemMailboxProvider.listMailboxes(Role.INBOX, mailboxSession); + + assertThat(result).hasSize(1).containsOnly(inboxMessageManager); + + } + @Test - public void missingTestSuite() { - //TODO this class needs a test suite + public void findMailboxShouldThrowWhenEmptySearchResult() throws Exception { + expectedException.expect(MailboxRoleNotFoundException.class); + + when(mailboxManager.search(any(MailboxQuery.class), eq(mailboxSession))).thenReturn(ImmutableList.of()); + + systemMailboxProvider.findMailbox(Role.INBOX, mailboxSession); + } + + @Test + public void findMailboxShouldThrowWhenCanNotFindAny() throws Exception { + expectedException.expect(MailboxRoleNotFoundException.class); + + when(mailboxManager.search(any(MailboxQuery.class), eq(mailboxSession))).thenReturn(ImmutableList.of(outboxMetadata)); + + systemMailboxProvider.findMailbox(Role.INBOX, mailboxSession); } - + + @Test + public void findMailboxShouldReturnMailboxByRole() throws Exception { + when(mailboxManager.search(any(MailboxQuery.class), eq(mailboxSession))).thenReturn(ImmutableList.of(inboxMetadata, outboxMetadata)); + when(mailboxManager.getMailbox(eq(INBOX), eq(mailboxSession))).thenReturn(inboxMessageManager); + + MessageManager result = systemMailboxProvider.findMailbox(Role.INBOX, mailboxSession); + + assertThat(result).isEqualTo(inboxMessageManager); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
