use of org.xwiki.mail.script.ScriptMailResult in project xwiki-platform by xwiki.
the class ScriptingIntegrationTest method sendHTMLAndCalendarInvitationMail.
@Test
public void sendHTMLAndCalendarInvitationMail() throws Exception {
ScriptMimeMessage message = this.scriptService.createMessage("john@doe.com", "subject");
message.addPart("text/html", "<font size=\"\\\"2\\\"\">simple meeting invitation</font>");
// @formatter:off
String calendarContent = "BEGIN:VCALENDAR\r\n" + "METHOD:REQUEST\r\n" + "PRODID: Meeting\r\n" + "VERSION:2.0\r\n" + "BEGIN:VEVENT\r\n" + "DTSTAMP:20140616T164100\r\n" + "DTSTART:20140616T164100\r\n" + "DTEND:20140616T194100\r\n" + "SUMMARY:test request\r\n" + "UID:324\r\n" + "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:john@doe.com\r\n" + "ORGANIZER:MAILTO:john@doe.com\r\n" + "LOCATION:on the net\r\n" + "DESCRIPTION:learn some stuff\r\n" + "SEQUENCE:0\r\n" + "PRIORITY:5\r\n" + "CLASS:PUBLIC\r\n" + "STATUS:CONFIRMED\r\n" + "TRANSP:OPAQUE\r\n" + "BEGIN:VALARM\r\n" + "ACTION:DISPLAY\r\n" + "DESCRIPTION:REMINDER\r\n" + "TRIGGER;RELATED=START:-PT00H15M00S\r\n" + "END:VALARM\r\n" + "END:VEVENT\r\n" + "END:VCALENDAR";
// @formatter:on
message.addPart("text/calendar;method=CANCEL", calendarContent, Collections.<String, Object>singletonMap("headers", Collections.singletonMap("Content-Class", "urn:content-classes:calendarmessage")));
ScriptMailResult result = this.scriptService.send(Arrays.asList(message));
// Verify that there are no errors and that 1 mail was sent
assertNull(this.scriptService.getLastError());
assertTrue(result.getStatusResult().isProcessed());
assertEquals(1, result.getStatusResult().getProcessedMailCount());
// Verify that all mails have been sent properly
assertFalse("There should not be any failed result!", result.getStatusResult().getByState(MailState.SEND_ERROR).hasNext());
assertFalse("There should not be any failed result!", result.getStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
assertFalse("There should not be any mails in the ready state!", result.getStatusResult().getByState(MailState.PREPARE_SUCCESS).hasNext());
// Verify that the mail has been received (wait maximum 30 seconds).
this.mail.waitForIncomingEmail(30000L, 1);
MimeMessage[] messages = this.mail.getReceivedMessages();
assertEquals(1, messages.length);
assertEquals("subject", messages[0].getHeader("Subject", null));
assertEquals("john@doe.com", messages[0].getHeader("To", null));
assertEquals(2, ((MimeMultipart) messages[0].getContent()).getCount());
BodyPart htmlBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
assertEquals("text/html", htmlBodyPart.getHeader("Content-Type")[0]);
assertEquals("<font size=\"\\\"2\\\"\">simple meeting invitation</font>", htmlBodyPart.getContent());
BodyPart calendarBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(1);
assertEquals("text/calendar;method=CANCEL", calendarBodyPart.getHeader("Content-Type")[0]);
InputStream is = (InputStream) calendarBodyPart.getContent();
assertEquals(calendarContent, IOUtils.toString(is));
}
use of org.xwiki.mail.script.ScriptMailResult in project xwiki-platform by xwiki.
the class MailStorageScriptServiceTest 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");
MailStatusResult statusResult1 = mock(MailStatusResult.class, "status1");
when(statusResult1.getTotalMailCount()).thenReturn(1L);
MailStatusResult statusResult2 = mock(MailStatusResult.class, "status2");
when(statusResult2.getTotalMailCount()).thenReturn(2L);
List<Pair<MailStatus, MailStatusResult>> results = new ArrayList<>();
results.add(new ImmutablePair<>(status1, statusResult1));
results.add(new ImmutablePair<>(status2, statusResult2));
MailResender resender = this.mocker.getInstance(MailResender.class, "database");
when(resender.resendAsynchronously(filterMap, 5, 10)).thenReturn(results);
List<ScriptMailResult> scriptResults = this.mocker.getComponentUnderTest().resendAsynchronously(filterMap, 5, 10);
assertEquals(2, scriptResults.size());
assertEquals("batch1", scriptResults.get(0).getBatchId());
assertEquals(1L, scriptResults.get(0).getStatusResult().getTotalMailCount());
assertEquals("batch2", scriptResults.get(1).getBatchId());
assertEquals(2L, scriptResults.get(1).getStatusResult().getTotalMailCount());
}
use of org.xwiki.mail.script.ScriptMailResult in project xwiki-platform by xwiki.
the class ScriptingIntegrationTest method sendTextMail.
@Test
public void sendTextMail() throws Exception {
ScriptMimeMessage message1 = this.scriptService.createMessage("john@doe.com", "subject");
message1.addPart("text/plain", "some text here");
ScriptMimeMessage message2 = this.scriptService.createMessage("john@doe.com", "subject");
message2.addPart("text/plain", "some text here");
ScriptMimeMessage message3 = this.scriptService.createMessage("john@doe.com", "subject");
message3.addPart("text/plain", "some text here");
// Send 3 mails (3 times the same mail) to verify we can send several emails at once.
List<ScriptMimeMessage> messagesList = Arrays.asList(message1, message2, message3);
ScriptMailResult result = this.scriptService.sendAsynchronously(messagesList, "memory");
// Verify that there are no errors
assertNull(this.scriptService.getLastError());
// Wait for all mails to be sent
result.getStatusResult().waitTillProcessed(30000L);
assertTrue(result.getStatusResult().isProcessed());
// Verify that all mails have been sent properly
assertFalse("There should not be any failed result!", result.getStatusResult().getByState(MailState.SEND_ERROR).hasNext());
assertFalse("There should not be any failed result!", result.getStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
assertFalse("There should not be any mails in the ready state!", result.getStatusResult().getByState(MailState.PREPARE_SUCCESS).hasNext());
// Verify that the mails have been received (wait maximum 30 seconds).
this.mail.waitForIncomingEmail(30000L, 3);
MimeMessage[] messages = this.mail.getReceivedMessages();
assertEquals(3, messages.length);
assertEquals("subject", messages[0].getHeader("Subject", null));
assertEquals("john@doe.com", messages[0].getHeader("To", null));
assertEquals(1, ((MimeMultipart) messages[0].getContent()).getCount());
BodyPart textBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
assertEquals("text/plain", textBodyPart.getHeader("Content-Type")[0]);
assertEquals("some text here", textBodyPart.getContent());
}
use of org.xwiki.mail.script.ScriptMailResult in project xwiki-platform by xwiki.
the class MailStorageScriptServiceTest method resendWhenMailResendingFailed.
@Test
public void resendWhenMailResendingFailed() throws Exception {
MailResender resender = this.mocker.getInstance(MailResender.class, "database");
when(resender.resendAsynchronously("batchId", "messageId")).thenThrow(new MailStoreException("error"));
ScriptMailResult result = this.mocker.getComponentUnderTest().resend("batchId", "messageId");
assertNull(result);
assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage());
}
use of org.xwiki.mail.script.ScriptMailResult in project xwiki-platform by xwiki.
the class MailStorageScriptServiceTest method resendAsynchronouslySeveralMessagesWhenMailResendingFailed.
@Test
public void resendAsynchronouslySeveralMessagesWhenMailResendingFailed() throws Exception {
Map filterMap = Collections.singletonMap("state", "prepare_%");
MailResender resender = this.mocker.getInstance(MailResender.class, "database");
when(resender.resendAsynchronously(filterMap, 5, 10)).thenThrow(new MailStoreException("error"));
List<ScriptMailResult> scriptResults = this.mocker.getComponentUnderTest().resendAsynchronously(filterMap, 5, 10);
assertNull(scriptResults);
assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage());
}
Aggregations