Permalink
Browse files

Added more unit tests for the mailman component

  • Loading branch information...
1 parent e812da7 commit ce8de9bca37f31dfad6c34e3e69c03bec55d158d @codders committed Sep 17, 2009
@@ -153,4 +153,12 @@ protected void activate(ComponentContext componentContext) {
listManagementPassword = (String)componentContext.getProperties().get(LIST_MANAGEMENT_PASSWORD);
}
+ void setMailmanManager(MailmanManager mailmanManager) {
+ this.mailmanManager = mailmanManager;
+ }
+
+ void setSlingRepository(SlingRepository slingRepository) {
+ this.slingRepository = slingRepository;
+ }
+
}
@@ -0,0 +1,145 @@
+package org.sakaiproject.kernel.mailman.impl;
+
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expect;
+
+import org.apache.jackrabbit.api.security.user.Group;
+import org.apache.jackrabbit.api.security.user.User;
+import org.apache.sling.jcr.api.SlingRepository;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.service.event.Event;
+import org.sakaiproject.kernel.api.personal.PersonalConstants;
+import org.sakaiproject.kernel.api.personal.PersonalUtils;
+import org.sakaiproject.kernel.api.user.AuthorizableEvent;
+import org.sakaiproject.kernel.api.user.AuthorizableEvent.Operation;
+import org.sakaiproject.kernel.mailman.MailmanManager;
+import org.sakaiproject.kernel.testutils.easymock.AbstractEasyMockTest;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+public class MailmanGroupManagerTest extends AbstractEasyMockTest {
+
+ private MailmanManager mailmanManager;
+ private SlingRepository slingRepository;
+ private MailmanGroupManager groupManager;
+
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ mailmanManager = createMock(MailmanManager.class);
+ slingRepository = createMock(SlingRepository.class);
+ groupManager = new MailmanGroupManager();
+ groupManager.setMailmanManager(mailmanManager);
+ groupManager.setSlingRepository(slingRepository);
+ }
+
+ @Test
+ public void testHandleGroupAdd() throws MailmanException {
+ String groupName = "g-testgroup";
+ String topic = "org/apache/sling/jackrabbit/usermanager/event/create";
+ Dictionary<String,Object> properties = new Hashtable<String, Object>();
+ properties.put(AuthorizableEvent.OPERATION, Operation.create);
+ properties.put(AuthorizableEvent.PRINCIPAL_NAME, groupName);
+ properties.put(AuthorizableEvent.TOPIC, topic);
+ Event event = new Event(topic, properties);
+
+ mailmanManager.createList(groupName, groupName + "@example.com", null);
+ replay();
+ groupManager.handleEvent(event);
+ verify();
+ }
+
+ @Test
+ public void testHandleGroupRemove() throws MailmanException {
+ String groupName = "g-testgroup";
+ String topic = "org/apache/sling/jackrabbit/usermanager/event/delete";
+ Dictionary<String,Object> properties = new Hashtable<String, Object>();
+ properties.put(AuthorizableEvent.OPERATION, Operation.delete);
+ properties.put(AuthorizableEvent.PRINCIPAL_NAME, groupName);
+ properties.put(AuthorizableEvent.TOPIC, topic);
+ Event event = new Event(topic, properties);
+
+ mailmanManager.deleteList(groupName, null);
+ replay();
+ groupManager.handleEvent(event);
+ verify();
+ }
+
+ @Test
+ public void testHandleGroupJoin() throws MailmanException, RepositoryException {
+ String groupName = "g-testgroup";
+ Group dummyGroup = createDummyGroup(groupName);
+ String user = "testuser";
+ User dummyUser = createDummyUser(user);
+ String testAddress = "test@test.com";
+ addUserEmailExpectation(user, testAddress);
+ String topic = "org/apache/sling/jackrabbit/usermanager/event/join";
+ Dictionary<String,Object> properties = new Hashtable<String, Object>();
+ properties.put(AuthorizableEvent.OPERATION, Operation.join);
+ properties.put(AuthorizableEvent.PRINCIPAL_NAME, groupName);
+ properties.put(AuthorizableEvent.TOPIC, topic);
+ properties.put(AuthorizableEvent.USER, dummyUser);
+ properties.put(AuthorizableEvent.GROUP, dummyGroup);
+ Event event = new Event(topic, properties);
+
+ expect(mailmanManager.addMember(groupName, null, testAddress)).andReturn(true);
+ replay();
+ groupManager.handleEvent(event);
+ verify();
+ }
+
+ @Test
+ public void testHandleGroupPart() throws MailmanException, RepositoryException {
+ String groupName = "g-testgroup";
+ Group dummyGroup = createDummyGroup(groupName);
+ String user = "testuser";
+ User dummyUser = createDummyUser(user);
+ String testAddress = "test@test.com";
+ addUserEmailExpectation(user, testAddress);
+ String topic = "org/apache/sling/jackrabbit/usermanager/event/part";
+ Dictionary<String,Object> properties = new Hashtable<String, Object>();
+ properties.put(AuthorizableEvent.OPERATION, Operation.part);
+ properties.put(AuthorizableEvent.PRINCIPAL_NAME, groupName);
+ properties.put(AuthorizableEvent.TOPIC, topic);
+ properties.put(AuthorizableEvent.USER, dummyUser);
+ properties.put(AuthorizableEvent.GROUP, dummyGroup);
+ Event event = new Event(topic, properties);
+
+ expect(mailmanManager.removeMember(groupName, null, testAddress)).andReturn(true);
+ replay();
+ groupManager.handleEvent(event);
+ verify();
+ }
+
+ private void addUserEmailExpectation(String username, String testAddress) throws RepositoryException {
+ String profileNodePath = PersonalUtils.getProfilePath(username);
+ Session session = createMock(Session.class);
+ Node node = createMock(Node.class);
+ Property property = createMock(Property.class);
+ expect(slingRepository.loginAdministrative(null)).andReturn(session);
+ expect(session.getItem(eq(profileNodePath))).andReturn(node);
+ expect(node.hasProperty(eq(PersonalConstants.EMAIL_ADDRESS))).andReturn(true);
+ expect(node.getProperty(eq(PersonalConstants.EMAIL_ADDRESS))).andReturn(property);
+ expect(property.getString()).andReturn(testAddress);
+ session.logout();
+ }
+
+ private Group createDummyGroup(String groupName) throws RepositoryException {
+ Group group = createMock(Group.class);
+ expect(group.getID()).andReturn(groupName).anyTimes();
+ return group;
+ }
+
+ private User createDummyUser(String userName) throws RepositoryException {
+ User user = createMock(User.class);
+ expect(user.getID()).andReturn(userName).anyTimes();
+ return user;
+ }
+}
@@ -121,4 +121,5 @@ public void testUnsubscribeFails() throws Exception {
assertFalse("Expected list removal to fail", succeeded);
verify();
}
+
}

0 comments on commit ce8de9b

Please sign in to comment.