Additional test classes that allow for easier testing of Notifications (emails mostly).

The new TestNotificationProvider acts like a simple callback. You inject an instance of the new NotificationReceiver into the above NotificationProvider and then override the EMailNotificationProvider with the test one.
By doing so, emails will not be sent, but the NotificationContext object, which encapsulates all input state for such emails, can be stored and validated against expected values.

Test code for same.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30618 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Neil McErlean
2011-09-19 21:44:23 +00:00
parent a87c7cb605
commit cd2167de85

View File

@@ -29,6 +29,7 @@ import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.notification.NotificationContext;
import org.alfresco.service.cmr.notification.NotificationProvider;
import org.alfresco.service.cmr.notification.NotificationService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
@@ -37,6 +38,8 @@ import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.BaseAlfrescoTestCase;
import org.alfresco.util.GUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Notification service implementation test.
@@ -46,14 +49,14 @@ import org.alfresco.util.GUID;
*/
public class NotificationServiceImplSystemTest extends BaseAlfrescoTestCase
{
private static final String FROM_USER = "fromUser" + GUID.generate();;
private static final String FROM_USER = "fromUser" + GUID.generate();
private static final String FROM_EMAIL = "test@alfresco.com";
private static final String FROM_FIRST_NAME = "Grace";
private static final String FROM_LAST_NAME = "Wetherall";
private static final String TO_USER1 = "userOne" + GUID.generate();;
private static final String TO_USER2 = "userTwo" + GUID.generate();;
private static final String TO_USER3 = "userThree" + GUID.generate();;
private static final String TO_USER1 = "userOne" + GUID.generate();
private static final String TO_USER2 = "userTwo" + GUID.generate();
private static final String TO_USER3 = "userThree" + GUID.generate();
private static final String EMAIL = "rwetherall@alfresco.com";
private static final String PASSWORD = "password";
@@ -87,11 +90,18 @@ public class NotificationServiceImplSystemTest extends BaseAlfrescoTestCase
private NodeRef template;
/**
* Storage for a {@link NotificationContext} sent from a test method.
*/
private NotificationContext contextSentFromTest = null;
@Override
protected void setUp() throws Exception
{
super.setUp();
contextSentFromTest = null;
// Get the notification service
notificationService = (NotificationService)ctx.getBean("NotificationService");
authenticationService = (MutableAuthenticationService)ctx.getBean("AuthenticationService");
@@ -222,4 +232,102 @@ public class NotificationServiceImplSystemTest extends BaseAlfrescoTestCase
}
});
}
/**
* This method tests the {@link TestNotificationProvider}.
*/
public void testTestNotificationProvider() throws Exception
{
// Create a class to receive the notification.
NotificationReceiver receiver = new NotificationReceiver()
{
@Override
public void receiveNotification(NotificationContext notificationContext)
{
contextSentFromTest = notificationContext;
}
};
// Set up the notification provider.
TestNotificationProvider testNP = new TestNotificationProvider();
testNP.setNotificationService(notificationService);
testNP.setNotificationReceiver(receiver);
testNP.init();
// send the notification
assertTrue(notificationService.getNotificationProviders().contains(TestNotificationProvider.NAME));
NotificationContext context = new NotificationContext();
final String to = "FAO: test object";
context.addTo(to);
notificationService.sendNotification(TestNotificationProvider.NAME, context);
assertNotNull("notification context was null.", contextSentFromTest);
assertTrue("notification context did not contain correct 'to' entry", contextSentFromTest.getTo().contains(to));
}
/**
* This {@link NotificationProvider} is intended for use in test code in order to validate the
* content/state of notifications. It could, for example, be used in a test context as a drop-in
* replacement for the {@link EMailNotificationProvider} for validation and/or to prevent emails being
* sent from test code.
*
* @author Neil Mc Erlean
* @since 4.0
*/
public static class TestNotificationProvider implements NotificationProvider
{
public static final String NAME = TestNotificationProvider.class.getSimpleName();
private static Log log = LogFactory.getLog(TestNotificationProvider.class);
private NotificationService notificationService;
private NotificationReceiver notificationReceiver;
public void setNotificationService(NotificationService notificationService)
{
this.notificationService = notificationService;
}
public void setNotificationReceiver(NotificationReceiver notificationReceiver)
{
this.notificationReceiver = notificationReceiver;
}
/**
* Init method registers provider with notification service.
*/
public void init()
{
notificationService.register(TestNotificationProvider.this);
}
@Override
public String getName()
{
return NAME;
}
@Override
public void sendNotification(NotificationContext notificationContext)
{
if (log.isDebugEnabled())
{
log.debug("Sending notification.");
}
this.notificationReceiver.receiveNotification(notificationContext);
}
}
/**
* Implementations of this interface can be injected into the {@link TestNotificationProvider} in order to receive notifications.
* <p/>
* Only intended for test code at this stage.
*
* @author Neil Mc Erlean
* @since 4.0
*/
public static interface NotificationReceiver
{
public void receiveNotification(NotificationContext notificationContext);
}
}