Search in sources :

Example 11 with ExtendedMimeMessage

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

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

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

the class DatabaseMailListenerTest method setUp.

@Before
public void setUp() throws Exception {
    this.message = new ExtendedMimeMessage();
    this.message.setType("type");
    this.message.saveChanges();
    this.message.setHeader("Message-ID", mimeMessageId);
    this.messageId = message.getUniqueMessageId();
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext executionContext = Mockito.mock(ExecutionContext.class);
    XWikiContext xcontext = Mockito.mock(XWikiContext.class);
    when(xcontext.getWikiId()).thenReturn("mywiki");
    when(executionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)).thenReturn(xcontext);
    when(execution.getContext()).thenReturn(executionContext);
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) Before(org.junit.Before)

Example 14 with ExtendedMimeMessage

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

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

the class UsersMimeMessageIteratorTest method createMessage.

@Test
public void createMessage() throws Exception {
    DocumentReference userReference1 = new DocumentReference("xwiki", "XWiki", "JohnDoe");
    DocumentReference userReference2 = new DocumentReference("xwiki", "XWiki", "JaneDoe");
    DocumentReference userReference3 = new DocumentReference("xwiki", "XWiki", "JonnieDoe");
    List<DocumentReference> userReferences = Arrays.asList(userReference1, userReference2, userReference3);
    Session session = Session.getInstance(new Properties());
    MimeMessageFactory factory = new MimeMessageFactory() {

        @Override
        public MimeMessage createMessage(Object source, Map parameters) throws MessagingException {
            return new ExtendedMimeMessage();
        }
    };
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("parameters", Collections.EMPTY_MAP);
    parameters.put("session", session);
    DocumentAccessBridge accessBridge = mock(DocumentAccessBridge.class);
    when(accessBridge.getProperty(eq(userReference1), any(DocumentReference.class), eq("email"))).thenReturn("john@doe.com");
    when(accessBridge.getProperty(eq(userReference2), any(DocumentReference.class), eq("email"))).thenReturn("jane@doe.com");
    when(accessBridge.getProperty(eq(userReference3), any(DocumentReference.class), eq("email"))).thenReturn("jannie@doe.com");
    ComponentManager componentManager = mock(ComponentManager.class);
    when(componentManager.getInstance(eq(DocumentAccessBridge.class))).thenReturn(accessBridge);
    UsersMimeMessageIterator iterator = new UsersMimeMessageIterator(userReferences, factory, parameters, componentManager);
    assertTrue(iterator.hasNext());
    MimeMessage message1 = iterator.next();
    assertArrayEquals(message1.getRecipients(Message.RecipientType.TO), InternetAddress.parse("john@doe.com"));
    assertTrue(iterator.hasNext());
    MimeMessage message2 = iterator.next();
    assertArrayEquals(message2.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jane@doe.com"));
    assertTrue(iterator.hasNext());
    MimeMessage message3 = iterator.next();
    assertArrayEquals(message3.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jannie@doe.com"));
    assertFalse(iterator.hasNext());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) HashMap(java.util.HashMap) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) ComponentManager(org.xwiki.component.manager.ComponentManager) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session) Test(org.junit.Test)

Aggregations

ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)21 Test (org.junit.Test)13 Map (java.util.Map)7 MailStatus (org.xwiki.mail.MailStatus)6 File (java.io.File)5 MailContentStore (org.xwiki.mail.MailContentStore)5 MailListener (org.xwiki.mail.MailListener)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 Properties (java.util.Properties)4 Session (javax.mail.Session)4 MimeMessage (javax.mail.internet.MimeMessage)4 ArrayList (java.util.ArrayList)3 MailSender (org.xwiki.mail.MailSender)3 MailStoreException (org.xwiki.mail.MailStoreException)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 HashMap (java.util.HashMap)2 MessagingException (javax.mail.MessagingException)2 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)2