Search in sources :

Example 11 with MailStatus

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

the class DatabaseMailStatusResultTest method getAll.

@Test
public void getAll() throws Exception {
    MailStatusStore store = mock(MailStatusStore.class);
    MailStatus status = new MailStatus();
    when(store.load(Collections.<String, Object>singletonMap("batchId", "batchid"), 0, 0, "date", true)).thenReturn(Arrays.asList(status));
    DatabaseMailStatusResult result = new DatabaseMailStatusResult(store);
    result.setBatchId("batchid");
    Iterator<MailStatus> resultStatuses = result.getAll();
    MailStatus resultStatus = resultStatuses.next();
    assertSame(status, resultStatus);
    assertFalse(resultStatuses.hasNext());
}
Also used : MailStatusStore(org.xwiki.mail.MailStatusStore) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Example 12 with MailStatus

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

the class PrepareMailRunnableTest method prepareMailWhenContentStoreFails.

@Test
public void prepareMailWhenContentStoreFails() throws Exception {
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    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(Arrays.asList(message1), session, listener1, batchId1, context1);
    MemoryMailListener listener2 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item2 = new PrepareMailQueueItem(Arrays.asList(message2), session, listener2, batchId2, context2);
    MailQueueManager mailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, PrepareMailQueueItem.class));
    // Make the content store save fail
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    doThrow(new MailStoreException("error")).when(contentStore).save(any(String.class), any(ExtendedMimeMessage.class));
    // Prepare 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    mailQueueManager.addToQueue(item1);
    mailQueueManager.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.
    MailStatus status1 = result1.getByState(MailState.PREPARE_ERROR).next();
    assertEquals("MailStoreException: error", status1.getErrorSummary());
    MailStatus status2 = result2.getByState(MailState.PREPARE_ERROR).next();
    assertEquals("MailStoreException: error", status2.getErrorSummary());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatusResult(org.xwiki.mail.MailStatusResult) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MailStatus(org.xwiki.mail.MailStatus) Session(javax.mail.Session) Test(org.junit.Test)

Example 13 with MailStatus

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

the class SendMailRunnableTest method sendMailWhenSendingFails.

@Test
public void sendMailWhenSendingFails() throws Exception {
    // Create a Session with an invalid host so that it generates an error
    Properties properties = new Properties();
    properties.setProperty("mail.smtp.host", "xwiki-unknown");
    Session session = Session.getDefaultInstance(properties);
    MimeMessage msg1 = new MimeMessage(session);
    msg1.setText("Content1");
    ExtendedMimeMessage message1 = new ExtendedMimeMessage(msg1);
    String id1 = message1.getUniqueMessageId();
    MimeMessage msg2 = new MimeMessage(session);
    msg2.setText("Content2");
    ExtendedMimeMessage message2 = new ExtendedMimeMessage(msg2);
    String id2 = message2.getUniqueMessageId();
    MemoryMailListener listener = this.mocker.getInstance(MailListener.class, "memory");
    String batchId = UUID.randomUUID().toString();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    ((UpdateableMailStatusResult) listener.getMailStatusResult()).setTotalSize(2);
    SendMailQueueItem item1 = new SendMailQueueItem(id1, session, listener, batchId, "xwiki");
    SendMailQueueItem item2 = new SendMailQueueItem(id2, session, listener, batchId, "xwiki");
    MailQueueManager mailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, SendMailQueueItem.class));
    // Simulate loading the message from the content store
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    when(contentStore.load(session, batchId, id1)).thenReturn(message1);
    when(contentStore.load(session, batchId, id2)).thenReturn(message2);
    // Send 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    mailQueueManager.addToQueue(item1);
    mailQueueManager.addToQueue(item2);
    MailRunnable runnable = this.mocker.getComponentUnderTest();
    Thread thread = new Thread(runnable);
    thread.start();
    // Wait for the mails to have been processed.
    try {
        listener.getMailStatusResult().waitTillProcessed(10000L);
    } finally {
        runnable.stopProcessing();
        thread.interrupt();
        thread.join();
    }
    // This is the real test: we verify that there's been an error while sending each email.
    Iterator<MailStatus> statuses = listener.getMailStatusResult().getByState(MailState.SEND_ERROR);
    int errorCount = 0;
    while (statuses.hasNext()) {
        MailStatus status = statuses.next();
        // Note: I would have liked to assert the exact message but it seems there can be different ones returned.
        // During my tests I got 2 different ones:
        // "UnknownHostException: xwiki-unknown"
        // "ConnectException: Connection refused"
        // Thus for now I only assert that there's an error set, but not its content.
        assertTrue(status.getErrorSummary() != null);
        errorCount++;
    }
    assertEquals(2, errorCount);
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailContentStore(org.xwiki.mail.MailContentStore) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatus(org.xwiki.mail.MailStatus) Session(javax.mail.Session) Test(org.junit.Test)

Example 14 with MailStatus

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

the class SendMailRunnableTest method sendMailWhenMailRetrievalFails.

@Test
public void sendMailWhenMailRetrievalFails() throws Exception {
    // Create a Session with an invalid host so that it generates an error
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    MimeMessage msg1 = new MimeMessage(session);
    msg1.setText("Content1");
    ExtendedMimeMessage message1 = new ExtendedMimeMessage(msg1);
    String id1 = message1.getUniqueMessageId();
    MimeMessage msg2 = new MimeMessage(session);
    msg2.setText("Content2");
    ExtendedMimeMessage message2 = new ExtendedMimeMessage(msg2);
    String id2 = message2.getUniqueMessageId();
    MemoryMailListener listener = this.mocker.getInstance(MailListener.class, "memory");
    String batchId = UUID.randomUUID().toString();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    ((UpdateableMailStatusResult) listener.getMailStatusResult()).setTotalSize(2);
    listener.onPrepareMessageSuccess(message1, Collections.<String, Object>emptyMap());
    SendMailQueueItem item1 = new SendMailQueueItem(id1, session, listener, batchId, "xwiki");
    listener.onPrepareMessageSuccess(message2, Collections.<String, Object>emptyMap());
    SendMailQueueItem item2 = new SendMailQueueItem(id2, session, listener, batchId, "xwiki");
    MailQueueManager mailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, SendMailQueueItem.class));
    // Simulate loading the message from the content store
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    when(contentStore.load(session, batchId, id1)).thenThrow(new MailStoreException("Store failure on message 1"));
    when(contentStore.load(session, batchId, id2)).thenThrow(new MailStoreException("Store failure on message 2"));
    // Send 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    mailQueueManager.addToQueue(item1);
    mailQueueManager.addToQueue(item2);
    MailRunnable runnable = this.mocker.getComponentUnderTest();
    Thread thread = new Thread(runnable);
    thread.start();
    // Wait for the mails to have been processed.
    try {
        listener.getMailStatusResult().waitTillProcessed(10000L);
    } finally {
        runnable.stopProcessing();
        thread.interrupt();
        thread.join();
    }
    // This is the real test: we verify that there's been an error while sending each email.
    Iterator<MailStatus> statuses = listener.getMailStatusResult().getByState(MailState.SEND_FATAL_ERROR);
    int errorCount = 0;
    while (statuses.hasNext()) {
        MailStatus status = statuses.next();
        // Note: I would have liked to assert the exact message but it seems there can be different ones returned.
        // During my tests I got 2 different ones:
        // "UnknownHostException: xwiki-unknown"
        // "ConnectException: Connection refused"
        // Thus for now I only assert that there's an error set, but not its content.
        assertEquals("MailStoreException: Store failure on message " + ++errorCount, status.getErrorSummary());
    }
    assertEquals(2, errorCount);
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatus(org.xwiki.mail.MailStatus) Session(javax.mail.Session) Test(org.junit.Test)

Example 15 with MailStatus

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

the class DatabaseMailResender method resendAsynchronously.

@Override
public List<Pair<MailStatus, MailStatusResult>> resendAsynchronously(Map<String, Object> filterMap, int offset, int count) throws MailStoreException {
    List<Pair<MailStatus, MailStatusResult>> results = new ArrayList<>();
    List<MailStatus> statuses = this.store.load(filterMap, offset, count, null, true);
    for (MailStatus status : statuses) {
        try {
            results.add(new ImmutablePair<>(status, resendAsynchronously(status.getBatchId(), status.getMessageId())));
        } catch (MailStoreException e) {
            // Failed to load the message from the content store and thus the mail couldn't be resent
            // Log a warning but continue to try to send the other mails...
            this.logger.warn("Failed to load mail content for batchId [{}], messageId [{}]", status.getBatchId(), status.getMessageId());
        }
    }
    return results;
}
Also used : MailStoreException(org.xwiki.mail.MailStoreException) ArrayList(java.util.ArrayList) MailStatus(org.xwiki.mail.MailStatus) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

MailStatus (org.xwiki.mail.MailStatus)27 Test (org.junit.Test)14 MailStoreException (org.xwiki.mail.MailStoreException)9 MailStatusStore (org.xwiki.mail.MailStatusStore)8 ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)7 MailContentStore (org.xwiki.mail.MailContentStore)7 MailListener (org.xwiki.mail.MailListener)7 ArrayList (java.util.ArrayList)4 XWikiContext (com.xpn.xwiki.XWikiContext)3 Properties (java.util.Properties)3 Session (javax.mail.Session)3 MimeMessage (javax.mail.internet.MimeMessage)3 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)3 MemoryMailListener (org.xwiki.mail.internal.MemoryMailListener)3 UpdateableMailStatusResult (org.xwiki.mail.internal.UpdateableMailStatusResult)3 Map (java.util.Map)2 MailSender (org.xwiki.mail.MailSender)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiHibernateBaseStore (com.xpn.xwiki.store.XWikiHibernateBaseStore)1 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)1