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);
}
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);
}
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);
}
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));
}
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());
}
Aggregations