001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.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/ecl1.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 edu.iu.uis.eden.notification;
018:
019: import mocks.MockEmailNotificationService;
020:
021: import org.junit.Test;
022: import org.kuali.workflow.test.WorkflowTestCase;
023:
024: import edu.iu.uis.eden.EdenConstants;
025: import edu.iu.uis.eden.KEWServiceLocator;
026: import edu.iu.uis.eden.clientapp.WorkflowDocument;
027: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
028: import edu.iu.uis.eden.doctype.DocumentType;
029: import edu.iu.uis.eden.preferences.Preferences;
030: import edu.iu.uis.eden.preferences.PreferencesService;
031: import edu.iu.uis.eden.user.AuthenticationUserId;
032: import edu.iu.uis.eden.user.WorkflowUser;
033:
034: public class NotificationServiceTest extends WorkflowTestCase {
035:
036: protected void loadTestData() throws Exception {
037: loadXmlFile("NotificationConfig.xml");
038: }
039:
040: /**
041: * Tests that when a user is routed to twice at the same time that only email is sent to them.
042: */
043: @Test
044: public void testNoDuplicateEmails() throws Exception {
045: WorkflowDocument document = new WorkflowDocument(
046: new NetworkIdVO("user1"), "NotificationTest");
047: document.routeDocument("");
048:
049: assertEquals("rkirkend should only have one email.", 1,
050: getMockEmailService().emailsSent("rkirkend",
051: document.getRouteHeaderId(),
052: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
053: assertEquals("ewestfal should only have one email.", 1,
054: getMockEmailService().emailsSent("ewestfal",
055: document.getRouteHeaderId(),
056: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
057: assertEquals("jhopf should only have one email.", 1,
058: getMockEmailService().emailsSent("jhopf",
059: document.getRouteHeaderId(),
060: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
061: // bmcgough is doing primary delegation so he should not recieve an email notification
062: assertEquals("bmcgough should have no emails.", 0,
063: getMockEmailService().emailsSent("bmcgough",
064: document.getRouteHeaderId(),
065: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
066: // jitrue should have no email because he is a secondary delegate and his default preferences should be set up to not send an email
067: assertEquals("jitrue should have no emails.", 0,
068: getMockEmailService().emailsSent("jitrue",
069: document.getRouteHeaderId(),
070: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
071: // user1 took action so they should _not_ be sent any emails
072: assertEquals("user1 should have no emails.", 0,
073: getMockEmailService().emailsSent("user1",
074: document.getRouteHeaderId(),
075: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
076:
077: }
078:
079: /**
080: * Tests that the notification preferences for emails work properly. There are four different preferences:
081: *
082: * 1) Email notification type (none, immediate, daily, weekly) - defaults to immediate
083: * 2) Send primary delegation notifications - defaults to true
084: * 3) Send secondary delegation notifications - defaults to false
085: */
086: @Test
087: public void testEmailPreferences() throws Exception {
088: WorkflowUser ewestfal = KEWServiceLocator.getUserService()
089: .getWorkflowUser(new AuthenticationUserId("ewestfal"));
090: WorkflowUser jitrue = KEWServiceLocator.getUserService()
091: .getWorkflowUser(new AuthenticationUserId("jitrue"));
092: WorkflowUser rkirkend = KEWServiceLocator.getUserService()
093: .getWorkflowUser(new AuthenticationUserId("rkirkend"));
094: WorkflowUser jhopf = KEWServiceLocator.getUserService()
095: .getWorkflowUser(new AuthenticationUserId("jhopf"));
096: WorkflowUser bmcgough = KEWServiceLocator.getUserService()
097: .getWorkflowUser(new AuthenticationUserId("bmcgough"));
098:
099: // test that the users with secondary delegations have default preferences
100: assertDefaultNotificationPreferences(ewestfal);
101: assertDefaultNotificationPreferences(jitrue);
102: assertDefaultNotificationPreferences(rkirkend);
103: assertDefaultNotificationPreferences(jhopf);
104: assertDefaultNotificationPreferences(bmcgough);
105: // the rest of the default setup is actually tested by testNoDuplicateEmails
106:
107: // now turn on secondary notification for ewestfal and jitrue, turn off email notification for ewestfal
108: Preferences prefs = getPreferencesService().getPreferences(
109: ewestfal);
110: prefs
111: .setNotifySecondaryDelegation(EdenConstants.PREFERENCES_YES_VAL);
112: prefs.setEmailNotification(EdenConstants.EMAIL_RMNDR_NO_VAL);
113: getPreferencesService().savePreferences(ewestfal, prefs);
114: prefs = getPreferencesService().getPreferences(jitrue);
115: prefs
116: .setNotifySecondaryDelegation(EdenConstants.PREFERENCES_YES_VAL);
117: getPreferencesService().savePreferences(jitrue, prefs);
118:
119: // also turn off primary delegation notification for rkirkend
120: prefs = getPreferencesService().getPreferences(rkirkend);
121: prefs
122: .setNotifyPrimaryDelegation(EdenConstants.PREFERENCES_NO_VAL);
123: getPreferencesService().savePreferences(rkirkend, prefs);
124:
125: // also turn notification to daily for bmcgough
126: prefs = getPreferencesService().getPreferences(bmcgough);
127: prefs.setEmailNotification(EdenConstants.EMAIL_RMNDR_DAY_VAL);
128: getPreferencesService().savePreferences(bmcgough, prefs);
129:
130: // also turn off notification for jhopf
131: prefs = getPreferencesService().getPreferences(jhopf);
132: prefs.setEmailNotification(EdenConstants.EMAIL_RMNDR_NO_VAL);
133: getPreferencesService().savePreferences(jhopf, prefs);
134:
135: // route the document
136: WorkflowDocument document = new WorkflowDocument(
137: new NetworkIdVO("user1"), "NotificationTest");
138: document.routeDocument("");
139:
140: // both ewestfal and jitrue should have one email
141: assertEquals("ewestfal should have no emails.", 0,
142: getMockEmailService().emailsSent("ewestfal",
143: document.getRouteHeaderId(),
144: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
145: assertEquals("jitrue should have one email.", 1,
146: getMockEmailService().emailsSent("jitrue",
147: document.getRouteHeaderId(),
148: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
149:
150: // rkirkend (the primary delegate) should now have no emails
151: assertEquals("rkirkend should have no emails.", 0,
152: getMockEmailService().emailsSent("rkirkend",
153: document.getRouteHeaderId(),
154: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
155:
156: // jhopf should now have no emails since his top-level requests are no longer notified
157: assertEquals("jhopf should have no emails.", 0,
158: getMockEmailService().emailsSent("jhopf",
159: document.getRouteHeaderId(),
160: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
161:
162: // bmcgough should now have no emails since his notification preference is DAILY
163: assertEquals("bmcgough should have no emails.", 0,
164: getMockEmailService().emailsSent("bmcgough",
165: document.getRouteHeaderId(),
166: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
167: }
168:
169: /**
170: * Tests that the fromNotificationAddress on the document type works properly. Used to test implementation of KULWF-628.
171: */
172: @Test
173: public void testDocumentTypeNotificationFromAddress()
174: throws Exception {
175: // first test that the notification from addresses are configured correctly
176: DocumentType documentType = KEWServiceLocator
177: .getDocumentTypeService()
178: .findByName("NotificationTest");
179: assertNull("Wrong notification from address, should be null.",
180: documentType.getNotificationFromAddress());
181:
182: // test the parent document type
183: documentType = KEWServiceLocator.getDocumentTypeService()
184: .findByName("NotificationFromAddressParent");
185: assertEquals("Wrong notification from address.",
186: "fakey@mcfakey.com", documentType
187: .getNotificationFromAddress());
188:
189: // test a child document type which overrides the parent's address
190: documentType = KEWServiceLocator.getDocumentTypeService()
191: .findByName("NotificationFromAddressChild");
192: assertEquals("Wrong notification from address.",
193: "fakey@mcchild.com", documentType
194: .getNotificationFromAddress());
195:
196: // test a child document type which doesn't override the parent's address
197: documentType = KEWServiceLocator.getDocumentTypeService()
198: .findByName("NotificationFromAddressChildInherited");
199: assertEquals("Wrong notification from address.",
200: "fakey@mcfakey.com", documentType
201: .getNotificationFromAddress());
202:
203: // Do an app specific route to a document which should send an email to fakey@mcchild.com
204: WorkflowDocument document = new WorkflowDocument(
205: new NetworkIdVO("user1"),
206: "NotificationFromAddressChild");
207: document.appSpecificRouteDocumentToUser(
208: EdenConstants.ACTION_REQUEST_APPROVE_REQ, "Initial",
209: "", new NetworkIdVO("ewestfal"), "", true);
210: document.routeDocument("");
211:
212: // verify that ewestfal was sent an email
213: assertEquals("ewestfal should have an email.", 1,
214: getMockEmailService().emailsSent("ewestfal",
215: document.getRouteHeaderId(),
216: EdenConstants.ACTION_REQUEST_APPROVE_REQ));
217:
218: // we currently have no way from this test to determine the email address used for notification
219: }
220:
221: private void assertDefaultNotificationPreferences(WorkflowUser user)
222: throws Exception {
223: Preferences prefs = getPreferencesService()
224: .getPreferences(user);
225: assertEquals(EdenConstants.EMAIL_RMNDR_IMMEDIATE, prefs
226: .getEmailNotification());
227: assertEquals(EdenConstants.PREFERENCES_YES_VAL, prefs
228: .getNotifyPrimaryDelegation());
229: assertEquals(EdenConstants.PREFERENCES_NO_VAL, prefs
230: .getNotifySecondaryDelegation());
231: }
232:
233: private PreferencesService getPreferencesService() {
234: return KEWServiceLocator.getPreferencesService();
235: }
236:
237: private MockEmailNotificationService getMockEmailService() {
238: return (MockEmailNotificationService) KEWServiceLocator
239: .getActionListEmailService();
240: }
241:
242: }
|