View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.notification;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNull;
21  import mocks.MockEmailNotificationService;
22  
23  import org.junit.Test;
24  import org.kuali.rice.kew.api.WorkflowDocument;
25  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
26  import org.kuali.rice.kew.api.action.ActionRequestType;
27  import org.kuali.rice.kew.doctype.bo.DocumentType;
28  import org.kuali.rice.kew.preferences.Preferences;
29  import org.kuali.rice.kew.preferences.service.PreferencesService;
30  import org.kuali.rice.kew.service.KEWServiceLocator;
31  import org.kuali.rice.kew.test.KEWTestCase;
32  import org.kuali.rice.kew.util.KEWConstants;
33  
34  
35  public class NotificationServiceTest extends KEWTestCase {
36  
37  	protected void loadTestData() throws Exception {
38          loadXmlFile("NotificationConfig.xml");
39      }
40  
41  	/**
42  	 * Tests that when a user is routed to twice at the same time that only email is sent to them.
43  	 */
44  	@Test public void testNoDuplicateEmails() throws Exception {
45  		WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), "NotificationTest");
46  		document.route("");
47  
48  		assertEquals("rkirkend should only have one email.", 1, getMockEmailService().immediateReminderEmailsSent("rkirkend", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ));
49  		assertEquals("ewestfal should only have one email.", 1, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ));
50  		assertEquals("jhopf should only have one email.", 1, getMockEmailService().immediateReminderEmailsSent("jhopf", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ));
51  		// bmcgough is doing primary delegation so he should not recieve an email notification
52  		assertEquals("bmcgough should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("bmcgough", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ));
53  		// jitrue should have no email because he is a secondary delegate and his default preferences should be set up to not send an email
54  		assertEquals("jitrue should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("jitrue", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ));
55  		// user1 took action so they should _not_ be sent any emails
56  		assertEquals("user1 should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("user1", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ));
57  
58  	}
59  
60  	/**
61  	 * Tests that the notification preferences for emails work properly.  There are four different preferences:
62  	 *
63  	 * 1) Email notification type (none, immediate, daily, weekly) - defaults to immediate
64  	 * 2) Send primary delegation notifications - defaults to true
65  	 * 3) Send secondary delegation notifications - defaults to false
66  	 */
67  	@Test public void testEmailPreferences() throws Exception {
68  		String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
69  		String jitruePrincipalId = getPrincipalIdForName("jitrue");
70  		String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
71  		String jhopfPrincipalId = getPrincipalIdForName("jhopf");
72  		String bmcgoughPrincipalId = getPrincipalIdForName("bmcgough");
73  		String user1PrincipalId = getPrincipalIdForName("user1");
74  
75  		// test that the users with secondary delegations have default preferences
76  		assertDefaultNotificationPreferences(ewestfalPrincipalId);
77  		assertDefaultNotificationPreferences(jitruePrincipalId);
78  		assertDefaultNotificationPreferences(rkirkendPrincipalId);
79  		assertDefaultNotificationPreferences(jhopfPrincipalId);
80  		assertDefaultNotificationPreferences(bmcgoughPrincipalId);
81  		// the rest of the default setup is actually tested by testNoDuplicateEmails
82  
83  		// now turn on secondary notification for ewestfal and jitrue, turn off email notification for ewestfal
84  		Preferences prefs = getPreferencesService().getPreferences(ewestfalPrincipalId);
85  		prefs.setNotifySecondaryDelegation(KEWConstants.PREFERENCES_YES_VAL);
86  		prefs.setEmailNotification(KEWConstants.EMAIL_RMNDR_NO_VAL);
87  		getPreferencesService().savePreferences(ewestfalPrincipalId, prefs);
88  		prefs = getPreferencesService().getPreferences(jitruePrincipalId);
89  		prefs.setNotifySecondaryDelegation(KEWConstants.PREFERENCES_YES_VAL);
90  		getPreferencesService().savePreferences(jitruePrincipalId, prefs);
91  
92  		// also turn off primary delegation notification for rkirkend
93  		prefs = getPreferencesService().getPreferences(rkirkendPrincipalId);
94  		prefs.setNotifyPrimaryDelegation(KEWConstants.PREFERENCES_NO_VAL);
95  		getPreferencesService().savePreferences(rkirkendPrincipalId, prefs);
96  
97  		// also turn notification to daily for bmcgough
98  		prefs = getPreferencesService().getPreferences(bmcgoughPrincipalId);
99  		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 }