Notification Service: NotifierImplTest
21 November 2008Even though I have written the implementation of the Notifier interface, I decided to release the test first, just keep with the whole TDD concept of test first, code later. The Notifier interface has five different methods, which are really just five different ways to ask for the same thing, so I created five different tests, just so I could place a call to each of the five methods:
package org.restafarian.notify.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.restafarian.notify.beans.NoticeTemplate;
import org.restafarian.notify.manager.NoticeTemplateManager;
import org.restafarian.notify.service.Notifier;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import com.dumbster.smtp.SimpleSmtpServer;
import com.dumbster.smtp.SmtpMessage;
public class NotifierImplTest extends AbstractDependencyInjectionSpringContextTests {
protected final Log log = logger;
private NoticeTemplate noticeTemplate;
private NoticeTemplateManager noticeTemplateManager;
private Notifier notifier;
protected String[] getConfigLocations() {
setAutowireMode(AUTOWIRE_BY_NAME);
return new String[] {"/context/applicationContext*.xml"};
}
/**
* Tests the notifier with one address
*/
public void testSendNotification1() {
// start the dummy smtp server
SimpleSmtpServer server = SimpleSmtpServer.start();
// create a template for this test
insertTestTemplate();
// run the test
notifier.sendNotification("test@localhost", "test/test", new HashMap());
// remove the template
deleteTestTemplate();
// stop the dummy smtp server
server.stop();
// verify the results
assertTrue(server.getReceivedEmailSize() == 1);
Iterator emailIter = server.getReceivedEmail();
SmtpMessage email = (SmtpMessage)emailIter.next();
assertTrue(email.getHeaderValue("Subject").equals("This is a test template"));
assertTrue(email.getBody().equals("This is a test template"));
}
/**
* Tests the notifier with multiple addresses
*/
public void testSendNotification2() {
// start the dummy smtp server
SimpleSmtpServer server = SimpleSmtpServer.start();
// create a template for this test
insertTestTemplate();
// run the test
List sendTo = new ArrayList();
sendTo.add("test1@localhost");
sendTo.add("test2@localhost");
notifier.sendNotification(sendTo, "test/test", new HashMap());
// remove the template
deleteTestTemplate();
// stop the dummy smtp server
server.stop();
// verify the results
assertTrue(server.getReceivedEmailSize() == 2);
Iterator emailIter = server.getReceivedEmail();
SmtpMessage email = (SmtpMessage)emailIter.next();
assertTrue(email.getHeaderValue("Subject").equals("This is a test template"));
assertTrue(email.getBody().equals("This is a test template"));
}
/**
* Tests the notifier with complex recipients
*/
public void testSendNotification3() {
// start the dummy smtp server
SimpleSmtpServer server = SimpleSmtpServer.start();
// create a template for this test
insertTestTemplate();
// run the test
List sendTo = new ArrayList();
Map recipient1 = new HashMap();
recipient1.put("emailAddress", "test1@localhost");
recipient1.put("name", "recipient1");
sendTo.add(recipient1);
Map recipient2 = new HashMap();
recipient2.put("emailAddress", "test2@localhost");
recipient2.put("name", "recipient2");
sendTo.add(recipient2);
Map recipient3 = new HashMap();
recipient3.put("emailAddress", "test3@localhost");
recipient3.put("name", "recipient3");
sendTo.add(recipient3);
notifier.sendNotification(sendTo, "emailAddress", "test/test", new HashMap());
// remove the template
deleteTestTemplate();
// stop the dummy smtp server
server.stop();
// verify the results
assertTrue(server.getReceivedEmailSize() == 3);
Iterator emailIter = server.getReceivedEmail();
SmtpMessage email = (SmtpMessage)emailIter.next();
assertTrue(email.getHeaderValue("Subject").equals("This is a test template"));
assertTrue(email.getBody().equals("This is a test template"));
}
/**
* Tests the notifier with complex recipients and from address
*/
public void testSendNotification4() {
// start the dummy smtp server
SimpleSmtpServer server = SimpleSmtpServer.start();
// create a template for this test
insertTestTemplate();
// run the test
List sendTo = new ArrayList();
Map recipient1 = new HashMap();
recipient1.put("emailAddress", "test1@localhost");
recipient1.put("name", "recipient1");
sendTo.add(recipient1);
Map recipient2 = new HashMap();
recipient2.put("emailAddress", "test2@localhost");
recipient2.put("name", "recipient2");
sendTo.add(recipient2);
Map recipient3 = new HashMap();
recipient3.put("emailAddress", "test3@localhost");
recipient3.put("name", "recipient3");
sendTo.add(recipient3);
notifier.sendNotification("no-reply@localhost", sendTo, "emailAddress", "test/test",
new HashMap());
// remove the template
deleteTestTemplate();
// stop the dummy smtp server
server.stop();
// verify the results
assertTrue(server.getReceivedEmailSize() == 3);
Iterator emailIter = server.getReceivedEmail();
SmtpMessage email = (SmtpMessage)emailIter.next();
assertTrue(email.getHeaderValue("Subject").equals("This is a test template"));
assertTrue(email.getBody().equals("This is a test template"));
}
/**
* Tests the notifier with variable data in the template
*/
public void testSendNotification5() {
// start the dummy smtp server
SimpleSmtpServer server = SimpleSmtpServer.start();
// run the test
List sendTo = new ArrayList();
Map recipient1 = new HashMap();
recipient1.put("emailAddress", "test1@localhost");
recipient1.put("name", "recipient1");
sendTo.add(recipient1);
notifier.sendNotification("no-reply@localhost", sendTo, "emailAddress", buildNoticeTemplate(),
new HashMap());
// stop the dummy smtp server
server.stop();
// verify the results
assertTrue(server.getReceivedEmailSize() == 1);
Iterator emailIter = server.getReceivedEmail();
SmtpMessage email = (SmtpMessage)emailIter.next();
assertTrue(email.getHeaderValue("Subject").equals("Hello recipient1!"));
assertTrue(email.getBody().equals("Your e-mail address is test1@localhost."));
}
/**
* Inserts a test template to be used by the send notification test
*/
private void insertTestTemplate() {
Date rightNow = new Date();
String userId = "NotifierImplTest";
noticeTemplate = new NoticeTemplate();
noticeTemplate.setContext("test");
noticeTemplate.setName("test");
noticeTemplate.setDescription("This is a test template");
noticeTemplate.setContentType("text/plain");
noticeTemplate.setDefaultDeliveryMethod("email");
noticeTemplate.setTitleSubject("This is a test template");
noticeTemplate.setBody("This is a test template");
noticeTemplate.setCreationDate(rightNow);
noticeTemplate.setCreatedBy(userId);
noticeTemplate.setLastUpdate(rightNow);
noticeTemplate.setLastUpdateBy(userId);
noticeTemplateManager.save(noticeTemplate);
}
/**
* Removes the test template used by the send notification test
*/
private void deleteTestTemplate() {
noticeTemplateManager.delete(noticeTemplate.getId());
}
/**
* Builds and returns a test template to be used by the send notification test
*/
private NoticeTemplate buildNoticeTemplate() {
Date rightNow = new Date();
String userId = "NotifierImplTest";
noticeTemplate = new NoticeTemplate();
noticeTemplate.setContext("test");
noticeTemplate.setName("test");
noticeTemplate.setDescription("This is a test template");
noticeTemplate.setContentType("text/plain");
noticeTemplate.setDefaultDeliveryMethod("email");
noticeTemplate.setTitleSubject("Hello $!{recipient.name}!");
noticeTemplate.setBody("Your e-mail address is $!{recipient.emailAddress}.");
noticeTemplate.setCreationDate(rightNow);
noticeTemplate.setCreatedBy(userId);
noticeTemplate.setLastUpdate(rightNow);
noticeTemplate.setLastUpdateBy(userId);
return noticeTemplate;
}
/**
* @return the notifier
*/
public Notifier getNotifier() {
return notifier;
}
/**
* @param notifier the notifier to set
*/
public void setNotifier(Notifier notifier) {
this.notifier = notifier;
}
/**
* @return the noticeTemplateManager
*/
public NoticeTemplateManager getNoticeTemplateManager() {
return noticeTemplateManager;
}
/**
* @param noticeTemplateManager the noticeTemplateManager to set
*/
public void setNoticeTemplateManager(NoticeTemplateManager noticeTemplateManager) {
this.noticeTemplateManager = noticeTemplateManager;
}
}
Each test gets a little progressively more complex, with the final test incorporating some variable data in the template, just to exercise the Velocity service for good measure. Each of the tests use the Dumbster utility to emulate an SMTP server, which I think really works out slick. Since each test hits all of the relevant parts of the service, this is more of an integration test than a unit test, but I don’t see anything wrong with that. In fact, I think it’s pretty cool to be able to try out the complete package from end to end.
Now that we have the tests out of the way, next time we can take a look at the actual implementation, which works as is, but still has a few things I’d like to add at some point. But we might as well take a look at this version as it stands today, and then we can talk about all of the other little tidbits that I would like to toss in a little later.
Leave a reply
You must be logged in to post a comment.






