Search in sources :

Example 11 with MailContentStore

use of org.xwiki.mail.MailContentStore in project xwiki-platform by xwiki.

the class PrepareMailRunnableTest method prepareMailWhenIteratorFails.

@Test
public void prepareMailWhenIteratorFails() throws Exception {
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    final MimeMessage message1 = new MimeMessage(session);
    message1.setText("Content1");
    MimeMessage message2 = new MimeMessage(session);
    message2.setText("Content2");
    String batchId1 = UUID.randomUUID().toString();
    String batchId2 = UUID.randomUUID().toString();
    ExecutionContext context1 = new ExecutionContext();
    XWikiContext xContext1 = new XWikiContext();
    xContext1.setWikiId("wiki1");
    context1.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext1);
    ExecutionContext context2 = new ExecutionContext();
    XWikiContext xContext2 = new XWikiContext();
    xContext2.setWikiId("wiki2");
    context2.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext2);
    MemoryMailListener listener1 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item1 = new PrepareMailQueueItem(new Iterable<MimeMessage>() {

        @Override
        public Iterator<MimeMessage> iterator() {
            return new Iterator<MimeMessage>() {

                int index = 0;

                @Override
                public boolean hasNext() {
                    return true;
                }

                @Override
                public MimeMessage next() {
                    if (index++ == 0) {
                        return message1;
                    }
                    throw new RuntimeException("Iterator failure");
                }

                @Override
                public void remove() {
                }
            };
        }
    }, session, listener1, batchId1, context1);
    MemoryMailListener listener2 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item2 = new PrepareMailQueueItem(Arrays.asList(message2), session, listener2, batchId2, context2);
    MailQueueManager prepareMailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, PrepareMailQueueItem.class));
    MailQueueManager sendMailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, SendMailQueueItem.class));
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            MimeMessage message = (MimeMessage) args[1];
            message.saveChanges();
            return null;
        }
    }).when(contentStore).save(any(String.class), any(ExtendedMimeMessage.class));
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            SendMailQueueItem item = (SendMailQueueItem) args[0];
            ((UpdateableMailStatusResult) item.getListener().getMailStatusResult()).incrementCurrentSize();
            return null;
        }
    }).when(sendMailQueueManager).addToQueue(any(SendMailQueueItem.class));
    // Prepare 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    prepareMailQueueManager.addToQueue(item1);
    prepareMailQueueManager.addToQueue(item2);
    MailRunnable runnable = this.mocker.getComponentUnderTest();
    Thread thread = new Thread(runnable);
    thread.start();
    // Wait for the mails to have been processed.
    try {
        listener1.getMailStatusResult().waitTillProcessed(10000L);
        listener2.getMailStatusResult().waitTillProcessed(10000L);
    } finally {
        runnable.stopProcessing();
        thread.interrupt();
        thread.join();
    }
    MailStatusResult result1 = listener1.getMailStatusResult();
    MailStatusResult result2 = listener2.getMailStatusResult();
    // Despite the errors, both process should be ended with known total number of mails
    assertTrue(result1.isProcessed());
    assertTrue(result2.isProcessed());
    // This is the real test: we verify that there's been an error while sending each email.
    assertNotNull(listener1.getMailStatusResult().getByState(MailState.PREPARE_SUCCESS).next());
    assertNotNull(listener2.getMailStatusResult().getByState(MailState.PREPARE_SUCCESS).next());
    assertFalse(listener1.getMailStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
    assertFalse(listener2.getMailStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
    assertEquals("Failure during preparation phase of thread [" + batchId1 + "]", logRule.getMessage(0));
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Iterator(java.util.Iterator) MailContentStore(org.xwiki.mail.MailContentStore) XWikiContext(com.xpn.xwiki.XWikiContext) ExecutionContext(org.xwiki.context.ExecutionContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatusResult(org.xwiki.mail.MailStatusResult) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) Session(javax.mail.Session) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 MailContentStore (org.xwiki.mail.MailContentStore)11 MailStatus (org.xwiki.mail.MailStatus)8 ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)7 MailListener (org.xwiki.mail.MailListener)6 MailStoreException (org.xwiki.mail.MailStoreException)6 MailStatusStore (org.xwiki.mail.MailStatusStore)5 Properties (java.util.Properties)4 Session (javax.mail.Session)4 MimeMessage (javax.mail.internet.MimeMessage)4 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)4 MemoryMailListener (org.xwiki.mail.internal.MemoryMailListener)4 UpdateableMailStatusResult (org.xwiki.mail.internal.UpdateableMailStatusResult)4 MailSender (org.xwiki.mail.MailSender)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ExecutionContext (org.xwiki.context.ExecutionContext)2 MailStatusResult (org.xwiki.mail.MailStatusResult)2 Iterator (java.util.Iterator)1