james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [09/12] james-project git commit: JAMES-2555 ReIndexerImpl tests should rely less on mocks
Date Fri, 12 Oct 2018 08:29:00 GMT
JAMES-2555 ReIndexerImpl tests should rely less on mocks

We should use an in-memory test system for mailboxManager rather than emulating ours on top
of mocks.

Mocks are still useful to check side effects on the indexer


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/12a3445d
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/12a3445d
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/12a3445d

Branch: refs/heads/master
Commit: 12a3445dea0569740ca6e95ea5e4812bd93b9bb6
Parents: 3f637e7
Author: Benoit Tellier <btellier@linagora.com>
Authored: Tue Oct 9 09:30:04 2018 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Fri Oct 12 15:27:34 2018 +0700

----------------------------------------------------------------------
 mailbox/tools/indexer/pom.xml                   |  11 ++
 .../tools/indexer/ReIndexerImplTest.java        | 131 ++++++++-----------
 2 files changed, 63 insertions(+), 79 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/12a3445d/mailbox/tools/indexer/pom.xml
----------------------------------------------------------------------
diff --git a/mailbox/tools/indexer/pom.xml b/mailbox/tools/indexer/pom.xml
index 4f44d85..e0874f0 100644
--- a/mailbox/tools/indexer/pom.xml
+++ b/mailbox/tools/indexer/pom.xml
@@ -53,6 +53,17 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>apache-james-mailbox-memory</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>apache-james-mailbox-memory</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-classic</artifactId>
             <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/james-project/blob/12a3445d/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/ReIndexerImplTest.java
----------------------------------------------------------------------
diff --git a/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/ReIndexerImplTest.java
b/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/ReIndexerImplTest.java
index b7c583c..cab234b 100644
--- a/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/ReIndexerImplTest.java
+++ b/mailbox/tools/indexer/src/test/java/org/apache/mailbox/tools/indexer/ReIndexerImplTest.java
@@ -19,120 +19,93 @@
 
 package org.apache.mailbox.tools.indexer;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
 
-import org.apache.james.mailbox.MailboxListener;
-import org.apache.james.mailbox.MailboxManager;
 import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver;
+import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.indexer.ReIndexer;
-import org.apache.james.mailbox.mock.MockMailboxSession;
+import org.apache.james.mailbox.inmemory.InMemoryMailboxManager;
+import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources;
+import org.apache.james.mailbox.model.ComposedMessageId;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
-import org.apache.james.mailbox.model.MessageRange;
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
-import org.apache.james.mailbox.store.MessageBuilder;
-import org.apache.james.mailbox.store.mail.MailboxMapper;
-import org.apache.james.mailbox.store.mail.MessageMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
 import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
-import org.assertj.core.util.Lists;
 import org.junit.Before;
 import org.junit.Test;
-
-import com.google.common.collect.ImmutableList;
+import org.mockito.ArgumentCaptor;
 
 public class ReIndexerImplTest {
 
-    public static final MailboxPath INBOX = MailboxPath.forUser("benwa@apache.org", "INBOX");
-    public static final int LIMIT = 0;
-    private MailboxManager mailboxManager;
-    private MailboxSessionMapperFactory mailboxSessionMapperFactory;
+    private static final String USERNAME = "benwa@apache.org";
+    public static final MailboxPath INBOX = MailboxPath.forUser(USERNAME, "INBOX");
+    private InMemoryMailboxManager mailboxManager;
     private ListeningMessageSearchIndex messageSearchIndex;
 
     private ReIndexer reIndexer;
 
     @Before
-    public void setUp() {
-        mailboxManager = mock(MailboxManager.class);
-        mailboxSessionMapperFactory = mock(MailboxSessionMapperFactory.class);
+    public void setUp() throws MailboxException {
+        mailboxManager = new InMemoryIntegrationResources().createMailboxManager(new SimpleGroupMembershipResolver());
+        MailboxSessionMapperFactory mailboxSessionMapperFactory = mailboxManager.getMapperFactory();
         messageSearchIndex = mock(ListeningMessageSearchIndex.class);
         reIndexer = new ReIndexerImpl(mailboxManager, messageSearchIndex, mailboxSessionMapperFactory);
     }
 
     @Test
-    public void test() throws Exception {
-        final MockMailboxSession mockMailboxSession = new MockMailboxSession("re-indexing");
-        when(mailboxManager.createSystemSession(any(String.class)))
-            .thenReturn(mockMailboxSession);
-        final MessageMapper messageMapper = mock(MessageMapper.class);
-        final MailboxMapper mailboxMapper = mock(MailboxMapper.class);
-        when(mailboxSessionMapperFactory.getMessageMapper(any(MailboxSession.class)))
-            .thenReturn(messageMapper);
-        when(mailboxSessionMapperFactory.getMailboxMapper(any(MailboxSession.class)))
-            .thenReturn(mailboxMapper);
-        final MailboxMessage message = new MessageBuilder().build();
-        final SimpleMailbox mailbox = new SimpleMailbox(INBOX, 42);
-        mailbox.setMailboxId(message.getMailboxId());
-        when(mailboxMapper.findMailboxByPath(INBOX)).thenReturn(mailbox);
-        when(messageMapper.findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Full,
LIMIT))
-            .thenReturn(Lists.newArrayList(message).iterator());
+    public void reIndexShouldBeWellPerformed() throws Exception {
+        MailboxSession systemSession = mailboxManager.createSystemSession(USERNAME);
+        MailboxId mailboxId = mailboxManager.createMailbox(INBOX, systemSession).get();
+        ComposedMessageId createdMessage = mailboxManager.getMailbox(INBOX, systemSession)
+            .appendMessage(
+                MessageManager.AppendCommand.builder().build("header: value\r\n\r\nbody"),
+                systemSession);
 
         reIndexer.reIndex(INBOX);
 
-        verify(mailboxManager).createSystemSession(any(String.class));
-        verify(mailboxSessionMapperFactory).getMailboxMapper(mockMailboxSession);
-        verify(mailboxSessionMapperFactory).getMessageMapper(mockMailboxSession);
-        verify(mailboxMapper).findMailboxByPath(INBOX);
-        verify(messageMapper).findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Full,
LIMIT);
-        verify(mailboxManager).addListener(eq(INBOX), any(MailboxListener.class), any(MailboxSession.class));
-        verify(mailboxManager).removeListener(eq(INBOX), any(MailboxListener.class), any(MailboxSession.class));
-        verify(messageSearchIndex).add(any(MailboxSession.class), eq(mailbox), eq(message));
-        verify(messageSearchIndex).deleteAll(any(MailboxSession.class), eq(mailbox));
-        verifyNoMoreInteractions(mailboxMapper, mailboxSessionMapperFactory, messageSearchIndex,
messageMapper, mailboxMapper);
+        ArgumentCaptor<MailboxMessage> messageCaptor = ArgumentCaptor.forClass(MailboxMessage.class);
+        ArgumentCaptor<Mailbox> mailboxCaptor1 = ArgumentCaptor.forClass(Mailbox.class);
+        ArgumentCaptor<Mailbox> mailboxCaptor2 = ArgumentCaptor.forClass(Mailbox.class);
+
+        verify(messageSearchIndex).deleteAll(any(MailboxSession.class), mailboxCaptor1.capture());
+        verify(messageSearchIndex).add(any(MailboxSession.class), mailboxCaptor2.capture(),
messageCaptor.capture());
+        verifyNoMoreInteractions(messageSearchIndex);
+
+        assertThat(mailboxCaptor1.getValue()).matches(mailbox -> mailbox.getMailboxId().equals(mailboxId));
+        assertThat(mailboxCaptor2.getValue()).matches(mailbox -> mailbox.getMailboxId().equals(mailboxId));
+        assertThat(messageCaptor.getValue()).matches(message -> message.getMailboxId().equals(mailboxId)
+            && message.getUid().equals(createdMessage.getUid()));
     }
 
     @Test
     public void mailboxPathUserShouldBeUsedWhenReIndexing() throws Exception {
-        MockMailboxSession systemMailboxSession = new MockMailboxSession("re-indexing");
-        when(mailboxManager.createSystemSession("re-indexing"))
-            .thenReturn(systemMailboxSession);
-        MailboxMapper mailboxMapper = mock(MailboxMapper.class);
-        when(mailboxSessionMapperFactory.getMailboxMapper(systemMailboxSession))
-            .thenReturn(mailboxMapper);
-
-        String user1 = "user1@james.org";
-        MailboxPath user1MailboxPath = MailboxPath.forUser(user1, "Inbox");
-        MockMailboxSession user1MailboxSession = new MockMailboxSession(user1);
-        when(mailboxManager.createSystemSession(user1))
-            .thenReturn(user1MailboxSession);
-        MailboxMapper user1MailboxMapper = mock(MailboxMapper.class);
-        when(mailboxSessionMapperFactory.getMailboxMapper(user1MailboxSession))
-            .thenReturn(user1MailboxMapper);
-        Mailbox user1Mailbox = mock(Mailbox.class);
-        when(user1MailboxMapper.findMailboxByPath(user1MailboxPath))
-            .thenReturn(user1Mailbox);
-        MessageMapper user1MessageMapper = mock(MessageMapper.class);
-        when(mailboxSessionMapperFactory.getMessageMapper(user1MailboxSession))
-            .thenReturn(user1MessageMapper);
-        MailboxMessage user1MailboxMessage = mock(MailboxMessage.class);
-        when(user1MessageMapper.findInMailbox(user1Mailbox, MessageRange.all(), MessageMapper.FetchType.Full,
ReIndexerImpl.NO_LIMIT))
-            .thenReturn(ImmutableList.of(user1MailboxMessage).iterator());
-        when(user1MailboxMessage.getUid())
-            .thenReturn(MessageUid.of(1));
-
-        when(mailboxManager.list(systemMailboxSession))
-            .thenReturn(ImmutableList.of(user1MailboxPath));
+        MailboxSession systemSession = mailboxManager.createSystemSession(USERNAME);
+        MailboxId mailboxId = mailboxManager.createMailbox(INBOX, systemSession).get();
+        ComposedMessageId createdMessage = mailboxManager.getMailbox(INBOX, systemSession)
+            .appendMessage(
+                MessageManager.AppendCommand.builder().build("header: value\r\n\r\nbody"),
+                systemSession);
 
         reIndexer.reIndex();
-
-        verify(messageSearchIndex).deleteAll(user1MailboxSession, user1Mailbox);
-        verify(messageSearchIndex).add(user1MailboxSession, user1Mailbox, user1MailboxMessage);
+        ArgumentCaptor<MailboxMessage> messageCaptor = ArgumentCaptor.forClass(MailboxMessage.class);
+        ArgumentCaptor<Mailbox> mailboxCaptor1 = ArgumentCaptor.forClass(Mailbox.class);
+        ArgumentCaptor<Mailbox> mailboxCaptor2 = ArgumentCaptor.forClass(Mailbox.class);
+
+        verify(messageSearchIndex).deleteAll(any(MailboxSession.class), mailboxCaptor1.capture());
+        verify(messageSearchIndex).add(any(MailboxSession.class), mailboxCaptor2.capture(),
messageCaptor.capture());
+        verifyNoMoreInteractions(messageSearchIndex);
+
+        assertThat(mailboxCaptor1.getValue()).matches(mailbox -> mailbox.getMailboxId().equals(mailboxId));
+        assertThat(mailboxCaptor2.getValue()).matches(mailbox -> mailbox.getMailboxId().equals(mailboxId));
+        assertThat(messageCaptor.getValue()).matches(message -> message.getMailboxId().equals(mailboxId)
+            && message.getUid().equals(createdMessage.getUid()));
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message