001 /* 002 * Copyright 2005-2007 The Kuali Foundation 003 * 004 * 005 * Licensed under the Educational Community License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.opensource.org/licenses/ecl2.php 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.kuali.rice.kew.notification; 018 019 import static org.junit.Assert.assertEquals; 020 import static org.junit.Assert.assertNull; 021 import mocks.MockEmailNotificationService; 022 023 import org.junit.Test; 024 import org.kuali.rice.kew.api.WorkflowDocument; 025 import org.kuali.rice.kew.api.WorkflowDocumentFactory; 026 import org.kuali.rice.kew.api.action.ActionRequestType; 027 import org.kuali.rice.kew.doctype.bo.DocumentType; 028 import org.kuali.rice.kew.preferences.Preferences; 029 import org.kuali.rice.kew.preferences.service.PreferencesService; 030 import org.kuali.rice.kew.service.KEWServiceLocator; 031 import org.kuali.rice.kew.test.KEWTestCase; 032 import org.kuali.rice.kew.util.KEWConstants; 033 034 035 public class NotificationServiceTest extends KEWTestCase { 036 037 protected void loadTestData() throws Exception { 038 loadXmlFile("NotificationConfig.xml"); 039 } 040 041 /** 042 * Tests that when a user is routed to twice at the same time that only email is sent to them. 043 */ 044 @Test public void testNoDuplicateEmails() throws Exception { 045 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), "NotificationTest"); 046 document.route(""); 047 048 assertEquals("rkirkend should only have one email.", 1, getMockEmailService().immediateReminderEmailsSent("rkirkend", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 049 assertEquals("ewestfal should only have one email.", 1, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 050 assertEquals("jhopf should only have one email.", 1, getMockEmailService().immediateReminderEmailsSent("jhopf", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 051 // bmcgough is doing primary delegation so he should not recieve an email notification 052 assertEquals("bmcgough should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("bmcgough", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 053 // jitrue should have no email because he is a secondary delegate and his default preferences should be set up to not send an email 054 assertEquals("jitrue should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("jitrue", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 055 // user1 took action so they should _not_ be sent any emails 056 assertEquals("user1 should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("user1", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 057 058 } 059 060 /** 061 * Tests that the notification preferences for emails work properly. There are four different preferences: 062 * 063 * 1) Email notification type (none, immediate, daily, weekly) - defaults to immediate 064 * 2) Send primary delegation notifications - defaults to true 065 * 3) Send secondary delegation notifications - defaults to false 066 */ 067 @Test public void testEmailPreferences() throws Exception { 068 String ewestfalPrincipalId = getPrincipalIdForName("ewestfal"); 069 String jitruePrincipalId = getPrincipalIdForName("jitrue"); 070 String rkirkendPrincipalId = getPrincipalIdForName("rkirkend"); 071 String jhopfPrincipalId = getPrincipalIdForName("jhopf"); 072 String bmcgoughPrincipalId = getPrincipalIdForName("bmcgough"); 073 String user1PrincipalId = getPrincipalIdForName("user1"); 074 075 // test that the users with secondary delegations have default preferences 076 assertDefaultNotificationPreferences(ewestfalPrincipalId); 077 assertDefaultNotificationPreferences(jitruePrincipalId); 078 assertDefaultNotificationPreferences(rkirkendPrincipalId); 079 assertDefaultNotificationPreferences(jhopfPrincipalId); 080 assertDefaultNotificationPreferences(bmcgoughPrincipalId); 081 // the rest of the default setup is actually tested by testNoDuplicateEmails 082 083 // now turn on secondary notification for ewestfal and jitrue, turn off email notification for ewestfal 084 Preferences prefs = getPreferencesService().getPreferences(ewestfalPrincipalId); 085 prefs.setNotifySecondaryDelegation(KEWConstants.PREFERENCES_YES_VAL); 086 prefs.setEmailNotification(KEWConstants.EMAIL_RMNDR_NO_VAL); 087 getPreferencesService().savePreferences(ewestfalPrincipalId, prefs); 088 prefs = getPreferencesService().getPreferences(jitruePrincipalId); 089 prefs.setNotifySecondaryDelegation(KEWConstants.PREFERENCES_YES_VAL); 090 getPreferencesService().savePreferences(jitruePrincipalId, prefs); 091 092 // also turn off primary delegation notification for rkirkend 093 prefs = getPreferencesService().getPreferences(rkirkendPrincipalId); 094 prefs.setNotifyPrimaryDelegation(KEWConstants.PREFERENCES_NO_VAL); 095 getPreferencesService().savePreferences(rkirkendPrincipalId, prefs); 096 097 // also turn notification to daily for bmcgough 098 prefs = getPreferencesService().getPreferences(bmcgoughPrincipalId); 099 prefs.setEmailNotification(KEWConstants.EMAIL_RMNDR_DAY_VAL); 100 getPreferencesService().savePreferences(bmcgoughPrincipalId, prefs); 101 102 // also turn off notification for jhopf 103 prefs = getPreferencesService().getPreferences(jhopfPrincipalId); 104 prefs.setEmailNotification(KEWConstants.EMAIL_RMNDR_NO_VAL); 105 getPreferencesService().savePreferences(jhopfPrincipalId, prefs); 106 107 // route the document 108 WorkflowDocument document = WorkflowDocumentFactory.createDocument(user1PrincipalId, "NotificationTest"); 109 document.route(""); 110 111 // both ewestfal and jitrue should have one email 112 assertEquals("ewestfal should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 113 assertEquals("jitrue should have one email.", 1, getMockEmailService().immediateReminderEmailsSent("jitrue", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 114 115 // rkirkend (the primary delegate) should now have no emails 116 assertEquals("rkirkend should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("rkirkend", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 117 118 // jhopf should now have no emails since his top-level requests are no longer notified 119 assertEquals("jhopf should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("jhopf", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 120 121 // bmcgough should now have no emails since his notification preference is DAILY 122 assertEquals("bmcgough should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("bmcgough", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 123 } 124 125 /** 126 * Tests that the fromNotificationAddress on the document type works properly. Used to test implementation of KULWF-628. 127 */ 128 @Test public void testDocumentTypeNotificationFromAddress() throws Exception { 129 String user1PrincipalId = getPrincipalIdForName("user1"); 130 131 // first test that the notification from addresses are configured correctly 132 DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationTest"); 133 assertNull("Wrong notification from address, should be null.", documentType.getNotificationFromAddress()); 134 assertNull("Wrong actual notification from address, should be null.", documentType.getActualNotificationFromAddress()); 135 136 // test the parent document type 137 documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressParent"); 138 assertEquals("Wrong notification from address.", "fakey@mcfakey.com", documentType.getNotificationFromAddress()); 139 140 // test a child document type which overrides the parent's address 141 documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressChild"); 142 assertEquals("Wrong notification from address.", "fakey@mcchild.com", documentType.getNotificationFromAddress()); 143 144 // test a child document type which doesn't override the parent's address 145 documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressChildInherited"); 146 assertEquals("Wrong notification from address.", "fakey@mcfakey.com", documentType.getNotificationFromAddress()); 147 148 // Do an app specific route to a document which should send an email to fakey@mcchild.com 149 WorkflowDocument document = WorkflowDocumentFactory.createDocument(user1PrincipalId, "NotificationFromAddressChild"); 150 document.adHocToPrincipal(ActionRequestType.APPROVE, "Initial", "", getPrincipalIdForName("ewestfal"), "", true); 151 document.route(""); 152 153 // verify that ewestfal was sent an email 154 assertEquals("ewestfal should have an email.", 1, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ)); 155 156 // we currently have no way from this test to determine the email address used for notification 157 } 158 159 private void assertDefaultNotificationPreferences(String principalId) throws Exception { 160 Preferences prefs = getPreferencesService().getPreferences(principalId); 161 assertEquals(KEWConstants.EMAIL_RMNDR_IMMEDIATE, prefs.getEmailNotification()); 162 assertEquals(KEWConstants.PREFERENCES_YES_VAL, prefs.getNotifyPrimaryDelegation()); 163 assertEquals(KEWConstants.PREFERENCES_NO_VAL, prefs.getNotifySecondaryDelegation()); 164 } 165 166 private PreferencesService getPreferencesService() { 167 return KEWServiceLocator.getPreferencesService(); 168 } 169 170 private MockEmailNotificationService getMockEmailService() { 171 return (MockEmailNotificationService)KEWServiceLocator.getActionListEmailService(); 172 } 173 174 }