Search in sources :

Example 1 with MailContentStore

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

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

Example 3 with MailContentStore

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

the class DatabaseMailResenderTest method resendAsynchronouslySingleMessageWhenMailContentStoreLoadingFails.

@Test
public void resendAsynchronouslySingleMessageWhenMailContentStoreLoadingFails() throws Exception {
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    when(contentStore.load(any(), eq("batchId"), eq("messageId"))).thenThrow(new MailStoreException("error"));
    try {
        this.mocker.getComponentUnderTest().resendAsynchronously("batchId", "messageId");
        fail("Should have thrown an exception here");
    } catch (MailStoreException expected) {
        assertEquals("error", expected.getMessage());
    }
}
Also used : MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) Test(org.junit.Test)

Example 4 with MailContentStore

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

the class DatabaseMailResenderTest method resendAsynchronouslySingleMesssage.

@Test
public void resendAsynchronouslySingleMesssage() throws Exception {
    MailListener listener = this.mocker.registerMockComponent(MailListener.class, "database");
    ExtendedMimeMessage message = new ExtendedMimeMessage();
    String batchId = UUID.randomUUID().toString();
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    when(contentStore.load(any(), eq(batchId), eq("messageId"))).thenReturn(message);
    MailSender sender = this.mocker.getInstance(MailSender.class);
    when(sender.sendAsynchronously(eq(Arrays.asList(message)), any(), any(MailListener.class))).thenReturn(new DefaultMailResult(batchId));
    this.mocker.getComponentUnderTest().resendAsynchronously(batchId, "messageId");
    // The test is here
    verify(sender).sendAsynchronously(eq(Arrays.asList(message)), any(), same(listener));
    verify(listener).getMailStatusResult();
}
Also used : MailListener(org.xwiki.mail.MailListener) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailContentStore(org.xwiki.mail.MailContentStore) MailSender(org.xwiki.mail.MailSender) Test(org.junit.Test)

Example 5 with MailContentStore

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

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