Search in sources :

Example 6 with MailStoreException

use of org.xwiki.mail.MailStoreException 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 7 with MailStoreException

use of org.xwiki.mail.MailStoreException 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)

Example 8 with MailStoreException

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

the class DatabaseMailListenerTest method onPrepareWhenSaveFails.

@Test
public void onPrepareWhenSaveFails() throws Exception {
    MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
    doThrow(new MailStoreException("error")).when(mailStatusStore).save(any(MailStatus.class), anyMap());
    MailListener listener = this.mocker.getComponentUnderTest();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    listener.onPrepareMessageSuccess(this.message, Collections.<String, Object>emptyMap());
    ArgumentCaptor<MailStatus> statusCapture = ArgumentCaptor.forClass(MailStatus.class);
    verify(mailStatusStore).save(statusCapture.capture(), anyMap());
    assertEquals("Failed to save mail status [messageId = [" + this.messageId + "], batchId = [" + this.batchId + "], state = [prepare_success], date = [" + statusCapture.getValue().getDate() + "], " + "recipients = [<null>], type = [type], wiki = [mywiki]] to the database", this.logRule.getMessage(0));
}
Also used : MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStoreException(org.xwiki.mail.MailStoreException) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Example 9 with MailStoreException

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

the class DatabaseMailListenerTest method onSuccessWhenStatusLoadFails.

@Test
public void onSuccessWhenStatusLoadFails() throws Exception {
    MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
    when(mailStatusStore.load(this.messageId)).thenThrow(new MailStoreException("error"));
    MailListener listener = this.mocker.getComponentUnderTest();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    listener.onSendMessageSuccess(this.message, Collections.<String, Object>emptyMap());
    assertEquals("Error when looking for a previous mail status for message [" + this.messageId + "] of batch [" + batchId + "] and state [send_success].", this.logRule.getMessage(0));
    assertEquals("Forcing a new mail status for message [" + this.messageId + "] of batch [" + batchId + "] to send_success state.", this.logRule.getMessage(1));
    // Verify that save and delete happened
    verify(mailStatusStore).save(any(MailStatus.class), anyMap());
    MailContentStore mailContentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    verify(mailContentStore).delete(any(), any());
}
Also used : MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Example 10 with MailStoreException

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

the class DatabaseMailListenerTest method onSuccessWhenMailContentDeletionFails.

@Test
public void onSuccessWhenMailContentDeletionFails() throws Exception {
    MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
    MailStatus status = new MailStatus(this.batchId, this.message, MailState.PREPARE_SUCCESS);
    status.setWiki("otherwiki");
    when(mailStatusStore.load(this.messageId)).thenReturn(status);
    MailContentStore mailContentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    doThrow(new MailStoreException("error")).when(mailContentStore).delete(this.batchId, this.messageId);
    MailListener listener = this.mocker.getComponentUnderTest();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    listener.onSendMessageSuccess(this.message, Collections.<String, Object>emptyMap());
    assertEquals("Failed to remove previously failing message [" + this.messageId + "] (batch id [" + this.batchId + "]) from the file system. Reason [MailStoreException: error].", this.logRule.getMessage(0));
}
Also used : MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Aggregations

MailStoreException (org.xwiki.mail.MailStoreException)16 MailStatus (org.xwiki.mail.MailStatus)8 Test (org.junit.Test)7 MailContentStore (org.xwiki.mail.MailContentStore)6 XWikiContext (com.xpn.xwiki.XWikiContext)5 XWikiException (com.xpn.xwiki.XWikiException)4 XWikiHibernateBaseStore (com.xpn.xwiki.store.XWikiHibernateBaseStore)4 HibernateException (org.hibernate.HibernateException)4 Session (org.hibernate.Session)4 ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)4 MailListener (org.xwiki.mail.MailListener)4 MailStatusStore (org.xwiki.mail.MailStatusStore)4 File (java.io.File)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 InitializationException (org.xwiki.component.phase.InitializationException)3 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 Session (javax.mail.Session)2 MimeMessage (javax.mail.internet.MimeMessage)2 Query (org.hibernate.Query)2