@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());
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);
}