Search in sources :

Example 16 with MailStatus

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

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

Example 18 with MailStatus

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

the class DatabaseMailResenderTest method resendAsynchronouslySeveralMessages.

@Test
public void resendAsynchronouslySeveralMessages() 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");
    ExtendedMimeMessage message1 = new ExtendedMimeMessage();
    when(contentStore.load(any(), eq("batch1"), eq("message1"))).thenReturn(message1);
    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(sender).sendAsynchronously(eq(Arrays.asList(message1)), any(), any(MailListener.class));
    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) 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 19 with MailStatus

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

the class DatabaseMailStatusStoreTest method logsForloadMailStatus.

@Test
public void logsForloadMailStatus() throws Exception {
    Provider<XWikiContext> xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    XWikiContext xcontext = mock(XWikiContext.class);
    when(xcontextProvider.get()).thenReturn(xcontext);
    when(xcontext.getWikiId()).thenReturn("wiki");
    when(xcontext.getMainXWiki()).thenReturn("mainwiki");
    XWikiHibernateStore hibernateStore = mock(XWikiHibernateStore.class);
    ReflectionUtils.setFieldValue(this.mocker.getComponentUnderTest(), "hibernateStore", hibernateStore);
    MailStatus status = new MailStatus();
    status.setBatchId("batchid");
    status.setMessageId("messageid");
    status.setState(MailState.PREPARE_SUCCESS);
    status.setRecipients("recipients");
    when(hibernateStore.executeRead(eq(xcontext), any())).thenReturn(Arrays.asList(status));
    Map<String, Object> filterMap = new LinkedHashMap<>();
    filterMap.put("status", "failed");
    filterMap.put("wiki", "mywiki");
    this.mocker.getComponentUnderTest().load(filterMap, 0, 0, null, false);
    // The test is here, we verify that debug logs are correct
    assertEquals(2, this.logRule.size());
    assertEquals("Find mail statuses for query [from org.xwiki.mail.MailStatus where mail_status like :status " + "and mail_wiki like :wiki] and parameters [[status] = [failed], [wiki] = [mywiki]]", this.logRule.getMessage(0));
    assertEquals("Loaded mail status [messageId = [messageid], batchId = [batchid], state = [prepare_success], " + "date = [<null>], recipients = [recipients]]", this.logRule.getMessage(1));
}
Also used : XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) XWikiContext(com.xpn.xwiki.XWikiContext) MailStatus(org.xwiki.mail.MailStatus) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 20 with MailStatus

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

the class DatabaseMailListener method onPrepareMessageSuccess.

@Override
public void onPrepareMessageSuccess(ExtendedMimeMessage message, Map<String, Object> parameters) {
    super.onPrepareMessageSuccess(message, parameters);
    MailStatus status = new MailStatus(getBatchId(), message, MailState.PREPARE_SUCCESS);
    status.setWiki(((XWikiContext) execution.getContext().getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)).getWikiId());
    saveStatus(status, parameters);
}
Also used : MailStatus(org.xwiki.mail.MailStatus)

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