Search in sources :

Example 6 with MailStatus

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

the class MailStorageScriptService method delete.

/**
 * Delete all messages from a batch (both the statuses in the database and the serialized messages on the file
 * system).
 *
 * @param batchId the id of the batch for which to delete all messages
 */
public void delete(String batchId) {
    // Note: We don't need to check permissions since the call to load() will do that anyway.
    Map<String, Object> filterMap = Collections.<String, Object>singletonMap("batchId", batchId);
    List<MailStatus> statuses = load(filterMap, 0, 0, null, false);
    if (statuses != null) {
        for (MailStatus status : statuses) {
            delete(batchId, status.getMessageId());
        }
    }
}
Also used : MailStatus(org.xwiki.mail.MailStatus)

Example 7 with MailStatus

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

the class DatabaseMailListenerTest method onSendMessageSuccess.

@Test
public void onSendMessageSuccess() 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);
    MailListener listener = this.mocker.getComponentUnderTest();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    listener.onSendMessageSuccess(this.message, Collections.<String, Object>emptyMap());
    verify(mailStatusStore).load(this.messageId);
    verify(mailStatusStore).save(argThat(new isSameMailStatus(MailState.SEND_SUCCESS, "otherwiki")), anyMap());
    MailContentStore mailContentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    verify(mailContentStore).delete(this.batchId, this.messageId);
}
Also used : MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailContentStore(org.xwiki.mail.MailContentStore) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Example 8 with MailStatus

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

the class DatabaseMailListenerTest method onSendMessageError.

@Test
public void onSendMessageError() 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);
    MailListener listener = this.mocker.getComponentUnderTest();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    listener.onSendMessageError(this.message, new Exception("Error"), Collections.<String, Object>emptyMap());
    verify(mailStatusStore).load(this.messageId);
    verify(mailStatusStore).save(argThat(new isSameMailStatus(MailState.SEND_ERROR, "otherwiki")), anyMap());
}
Also used : MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStatus(org.xwiki.mail.MailStatus) MailStoreException(org.xwiki.mail.MailStoreException) Test(org.junit.Test)

Example 9 with MailStatus

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

the class DatabaseMailListenerTest method onSendMessageFatalError.

@Test
public void onSendMessageFatalError() 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);
    MailListener listener = this.mocker.getComponentUnderTest();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    listener.onSendMessageFatalError(this.messageId, new Exception("Error"), Collections.<String, Object>emptyMap());
    verify(mailStatusStore).load(this.messageId);
    verify(mailStatusStore).save(argThat(new isSameMailStatus(MailState.SEND_FATAL_ERROR, "otherwiki")), anyMap());
}
Also used : MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStatus(org.xwiki.mail.MailStatus) MailStoreException(org.xwiki.mail.MailStoreException) Test(org.junit.Test)

Example 10 with MailStatus

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

the class DatabaseMailResenderTest method resendAsynchronouslySeveralMessagesWhenMailContentStoreLoadingFailsForFirstMessage.

@Test
public void resendAsynchronouslySeveralMessagesWhenMailContentStoreLoadingFailsForFirstMessage() throws Exception {
    Map filterMap = Collections.singletonMap("state", "prepare_%");
    MailStatus status1 = new MailStatus();
    status1.setBatchId("batch1");
    status1.setMessageId("message1");
    MailStatus status2 = new MailStatus();
    status2.setBatchId("batch2");
    status2.setMessageId("message2");
    List<MailStatus> statuses = new ArrayList<>();
    statuses.add(status1);
    statuses.add(status2);
    MailStatusStore statusStore = this.mocker.getInstance(MailStatusStore.class, "database");
    when(statusStore.load(filterMap, 0, 0, null, true)).thenReturn(statuses);
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    when(contentStore.load(any(), eq("batch1"), eq("message1"))).thenThrow(new MailStoreException("error1"));
    ExtendedMimeMessage message2 = new ExtendedMimeMessage();
    when(contentStore.load(any(), eq("batch2"), eq("message2"))).thenReturn(message2);
    MailSender sender = this.mocker.getInstance(MailSender.class);
    this.mocker.getComponentUnderTest().resendAsynchronously(filterMap, 0, 0);
    // The test is here
    verify(this.mocker.getMockedLogger()).warn("Failed to load mail content for batchId [{}], messageId [{}]", "batch1", "message1");
    verify(sender).sendAsynchronously(eq(Arrays.asList(message2)), any(), any(MailListener.class));
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) ArrayList(java.util.ArrayList) MailSender(org.xwiki.mail.MailSender) Map(java.util.Map) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

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