Skip to content
Snippets Groups Projects
Commit ccb5b74d authored by Vincent Massol's avatar Vincent Massol
Browse files

XWIKI-11701: Improve the mail sender script service API when sending mails to...

XWIKI-11701: Improve the mail sender script service API when sending mails to a group or to a list of users
* Add functional tests for sending a template mail to a list of users
parent 5c4cc5dd
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,8 @@
import java.io.ByteArrayInputStream;
import javax.mail.internet.MimeMessage;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.junit.After;
import org.junit.Assert;
......@@ -115,11 +117,8 @@ public void testMail() throws Exception
Assert.assertTrue(spaceAdministrationPage.hasNotSection("Email", "General"));
Assert.assertTrue(spaceAdministrationPage.hasNotSection("Email", "Mail Sending"));
// Step 4: Try sending a template email (with an attachment) to a single user
// Remove existing pages (for pages that we create below)
// Step 4: Prepare a Template Mail
getUtil().deletePage(getTestClassName(), "MailTemplate");
getUtil().deletePage(getTestClassName(), "SendMail");
// Create a Wiki page containing a Mail Template (ie a XWiki.Mail object)
getUtil().createPage(getTestClassName(), "MailTemplate", "", "");
......@@ -131,6 +130,35 @@ public void testMail() throws Exception
getUtil().attachFile(getTestClassName(), "MailTemplate", "something.txt", bais, true,
new UsernamePasswordCredentials("superadmin", "pass"));
// Step 5: Send a template email (with an attachment) to a single user
sendTemplateMailToSingleUser();
// Step 6: Send a template email to all the users in the XWikiAllGroup Group (we'll create 2 users).
sendTemplateMailToGroup();
// Step 7: Send a template email to 2 users (they have been created in step 5 above)
sendTemplateMailToSeveralUsers();
// Step 8: Navigate to the Mail Sending Status Admin page and assert that the Livetable displays the entry for
// the sent mails
administrationPage = AdministrationPage.gotoPage();
administrationPage.clickSection("Email", "Mail Sending Status");
MailStatusAdministrationSectionPage statusPage = new MailStatusAdministrationSectionPage();
LiveTableElement liveTableElement = statusPage.getLiveTable();
liveTableElement.filterColumn("xwiki-livetable-sendmailstatus-filter-3", "Test");
liveTableElement.filterColumn("xwiki-livetable-sendmailstatus-filter-5", "sent");
liveTableElement.filterColumn("xwiki-livetable-sendmailstatus-filter-6", "xwiki");
assertTrue(liveTableElement.getRowCount() > 2);
liveTableElement.filterColumn("xwiki-livetable-sendmailstatus-filter-4", "john@doe.com");
assertTrue(liveTableElement.getRowCount() > 0);
assertTrue(liveTableElement.hasRow("Error", ""));
}
private void sendTemplateMailToSingleUser() throws Exception
{
// Remove existing pages (for pages that we create below)
getUtil().deletePage(getTestClassName(), "SendMail");
// Create another page with the Velocity script to send the template email
String velocity = "{{velocity}}\n"
+ "#set ($templateReference = $services.model.createDocumentReference('', '" + getTestClassName()
......@@ -160,11 +188,12 @@ public void testMail() throws Exception
// Verify that the mail has been received.
this.mail.waitForIncomingEmail(10000L, 1);
assertEquals(1, this.mail.getReceivedMessages().length);
assertEquals("Status for John on " + getTestClassName() + ".SendMail",
this.mail.getReceivedMessages()[0].getSubject());
// Step 5: Try sending a template email to all the users in the XWikiAllGroup Group (we'll create 2 users).
assertEquals(1, getNumberOfReceivedMessagesWithSubject(
"Status for John on " + getTestClassName() + ".SendMail"));
}
private void sendTemplateMailToGroup() throws Exception
{
// Remove existing pages (for pages that we create below)
getUtil().deletePage(getTestClassName(), "SendMailGroup");
......@@ -173,13 +202,13 @@ public void testMail() throws Exception
getUtil().createUser("user2", "password2", getUtil().getURLToNonExistentPage(), "email", "user2@doe.com");
// Create another page with the Velocity script to send the template email
velocity = "{{velocity}}\n"
String velocity = "{{velocity}}\n"
+ "#set ($templateParameters = {'velocityVariables' : { 'name' : 'John', 'doc' : $doc }, "
+ "'language' : 'en', 'from' : 'localhost@xwiki.org'})\n"
+ "'language' : 'en', 'from' : 'localhost@xwiki.org'})\n"
+ "#set ($templateReference = $services.model.createDocumentReference('', '" + getTestClassName()
+ "', 'MailTemplate'))\n"
+ "#set ($groupParameters = {'hint' : 'template', 'source' : $templateReference, "
+ "'parameters' : $templateParameters, 'type' : 'Test'})\n"
+ "'parameters' : $templateParameters, 'type' : 'Test'})\n"
+ "#set ($groupReference = $services.model.createDocumentReference('', 'XWiki', 'XWikiAllGroup'))\n"
+ "#set ($messages = $services.mailsender.createMessages('group', $groupReference, $groupParameters))\n"
+ "#set ($result = $services.mailsender.send($messages, 'database'))\n"
......@@ -194,31 +223,67 @@ public void testMail() throws Exception
+ "#end\n"
+ "{{/velocity}}";
// This will create the page and execute its content and thus send the mail
vp = getUtil().createPage(getTestClassName(), "SendMailGroup", velocity, "");
ViewPage vp = getUtil().createPage(getTestClassName(), "SendMailGroup", velocity, "");
// Verify that the page doesn't display any content (unless there's an error!)
assertEquals("", vp.getContent());
// Verify that the mail has been received (first mail above + the 2 mails sent to the group)
// Verify that the mails have been received (first mail above + the 2 mails sent to the group)
this.mail.waitForIncomingEmail(10000L, 3);
assertEquals(3, this.mail.getReceivedMessages().length);
assertEquals("Status for John on " + getTestClassName() + ".SendMailGroup",
this.mail.getReceivedMessages()[1].getSubject());
assertEquals("Status for John on " + getTestClassName() + ".SendMailGroup",
this.mail.getReceivedMessages()[2].getSubject());
assertEquals(2, getNumberOfReceivedMessagesWithSubject(
"Status for John on " + getTestClassName() + ".SendMailGroup"));
}
// Step 6: Navigate to the Mail Sending Status Admin page and assert that the Livetable displays the entry for
// the sent mails
administrationPage = AdministrationPage.gotoPage();
administrationPage.clickSection("Email", "Mail Sending Status");
MailStatusAdministrationSectionPage statusPage = new MailStatusAdministrationSectionPage();
LiveTableElement liveTableElement = statusPage.getLiveTable();
liveTableElement.filterColumn("xwiki-livetable-sendmailstatus-filter-3", "Test");
liveTableElement.filterColumn("xwiki-livetable-sendmailstatus-filter-5", "sent");
liveTableElement.filterColumn("xwiki-livetable-sendmailstatus-filter-6", "xwiki");
assertTrue(liveTableElement.getRowCount() > 2);
liveTableElement.filterColumn("xwiki-livetable-sendmailstatus-filter-4", "john@doe.com");
assertTrue(liveTableElement.getRowCount() > 0);
assertTrue(liveTableElement.hasRow("Error", ""));
private void sendTemplateMailToSeveralUsers() throws Exception
{
// Remove existing pages (for pages that we create below)
getUtil().deletePage(getTestClassName(), "SendMailUsers");
// Create another page with the Velocity script to send the template email
String velocity = "{{velocity}}\n"
+ "#set ($templateParameters = {'velocityVariables' : { 'name' : 'John', 'doc' : $doc }, "
+ "'language' : 'en', 'from' : 'localhost@xwiki.org'})\n"
+ "#set ($templateReference = $services.model.createDocumentReference('', '" + getTestClassName()
+ "', 'MailTemplate'))\n"
+ "#set ($usersParameters = {'hint' : 'template', 'source' : $templateReference, "
+ "'parameters' : $templateParameters, 'type' : 'Test'})\n"
+ "#set ($user1Reference = $services.model.createDocumentReference('', 'XWiki', 'user1'))\n"
+ "#set ($user2Reference = $services.model.createDocumentReference('', 'XWiki', 'user2'))\n"
+ "#set ($messages = $services.mailsender.createMessages('users', [$user1Reference, $user2Reference], "
+ "$usersParameters))\n"
+ "#set ($result = $services.mailsender.send($messages, 'database'))\n"
+ "#if ($services.mailsender.lastError)\n"
+ " {{error}}$exceptiontool.getStackTrace($services.mailsender.lastError){{/error}}\n"
+ "#end\n"
+ "#foreach ($status in $result.statusResult.getByState('FAILED'))\n"
+ " {{error}}\n"
+ " $status.messageId - $status.errorSummary\n"
+ " $status.errorDescription\n"
+ " {{/error}}\n"
+ "#end\n"
+ "{{/velocity}}";
// This will create the page and execute its content and thus send the mail
ViewPage vp = getUtil().createPage(getTestClassName(), "SendMailUsers", velocity, "");
// Verify that the page doesn't display any content (unless there's an error!)
assertEquals("", vp.getContent());
// Verify that the mails has been received (3 mails above + the 2 mails sent to the 2 users)
this.mail.waitForIncomingEmail(10000L, 5);
assertEquals(5, this.mail.getReceivedMessages().length);
assertEquals(2, getNumberOfReceivedMessagesWithSubject(
"Status for John on " + getTestClassName() + ".SendMailUsers"));
}
private int getNumberOfReceivedMessagesWithSubject(String subject) throws Exception
{
int count = 0;
for (MimeMessage message : this.mail.getReceivedMessages()) {
if (message.getSubject().equals(subject)) {
count++;
}
}
return count;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment