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