View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.notification;
17  
18  import mocks.MockEmailNotificationService;
19  import org.junit.Test;
20  import org.kuali.rice.kew.api.KewApiServiceLocator;
21  import org.kuali.rice.kew.api.WorkflowDocument;
22  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
23  import org.kuali.rice.kew.api.action.ActionRequestType;
24  import org.kuali.rice.kew.api.preferences.Preferences;
25  import org.kuali.rice.kew.api.preferences.PreferencesService;
26  import org.kuali.rice.kew.doctype.bo.DocumentType;
27  import org.kuali.rice.kew.service.KEWServiceLocator;
28  import org.kuali.rice.kew.test.KEWTestCase;
29  import org.kuali.rice.kew.api.KewApiConstants;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertNull;
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(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
49  		assertEquals("ewestfal should only have one email.", 1, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
50  		assertEquals("jhopf should only have one email.", 1, getMockEmailService().immediateReminderEmailsSent("jhopf", document.getDocumentId(), KewApiConstants.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(), KewApiConstants.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(), KewApiConstants.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(), KewApiConstants.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          Preferences.Builder preferencesBuilder = Preferences.Builder.create(prefs);
86  		preferencesBuilder.setNotifySecondaryDelegation(KewApiConstants.PREFERENCES_YES_VAL);
87  		preferencesBuilder.setEmailNotification(KewApiConstants.EMAIL_RMNDR_NO_VAL);
88  		getPreferencesService().savePreferences(ewestfalPrincipalId, preferencesBuilder.build());
89  		prefs = getPreferencesService().getPreferences(jitruePrincipalId);
90          preferencesBuilder = Preferences.Builder.create(prefs);
91  		preferencesBuilder.setNotifySecondaryDelegation(KewApiConstants.PREFERENCES_YES_VAL);
92  		getPreferencesService().savePreferences(jitruePrincipalId, preferencesBuilder.build());
93  
94  		// also turn off primary delegation notification for rkirkend
95  		prefs = getPreferencesService().getPreferences(rkirkendPrincipalId);
96          preferencesBuilder = Preferences.Builder.create(prefs);
97  		preferencesBuilder.setNotifyPrimaryDelegation(KewApiConstants.PREFERENCES_NO_VAL);
98  		getPreferencesService().savePreferences(rkirkendPrincipalId, preferencesBuilder.build());
99  
100 		// also turn notification to daily for bmcgough
101 		prefs = getPreferencesService().getPreferences(bmcgoughPrincipalId);
102         preferencesBuilder = Preferences.Builder.create(prefs);
103 		preferencesBuilder.setEmailNotification(KewApiConstants.EMAIL_RMNDR_DAY_VAL);
104 		getPreferencesService().savePreferences(bmcgoughPrincipalId, preferencesBuilder.build());
105 
106 		// also turn off notification for jhopf
107 		prefs = getPreferencesService().getPreferences(jhopfPrincipalId);
108         preferencesBuilder = Preferences.Builder.create(prefs);
109 		preferencesBuilder.setEmailNotification(KewApiConstants.EMAIL_RMNDR_NO_VAL);
110 		getPreferencesService().savePreferences(jhopfPrincipalId, preferencesBuilder.build());
111 
112 		// route the document
113 		WorkflowDocument document = WorkflowDocumentFactory.createDocument(user1PrincipalId, "NotificationTest");
114 		document.route("");
115 
116 		// both ewestfal and jitrue should have one email
117 		assertEquals("ewestfal should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
118 		assertEquals("jitrue should have one email.", 1, getMockEmailService().immediateReminderEmailsSent("jitrue", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
119 
120 		// rkirkend (the primary delegate) should now have no emails
121 		assertEquals("rkirkend should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("rkirkend", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
122 
123 		// jhopf should now have no emails since his top-level requests are no longer notified
124 		assertEquals("jhopf should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("jhopf", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
125 
126 		// bmcgough should now have no emails since his notification preferences is DAILY
127 		assertEquals("bmcgough should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("bmcgough", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
128 	}
129 
130 	/**
131 	 * Tests that the fromNotificationAddress on the document type works properly.  Used to test implementation of KULWF-628.
132 	 */
133 	@Test public void testDocumentTypeNotificationFromAddress() throws Exception {
134 		String user1PrincipalId = getPrincipalIdForName("user1");
135 
136 		// first test that the notification from addresses are configured correctly
137 		DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationTest");
138 		assertNull("Wrong notification from address, should be null.", documentType.getNotificationFromAddress());
139         assertNull("Wrong actual notification from address, should be null.", documentType.getActualNotificationFromAddress());
140 
141 		// test the parent document type
142 		documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressParent");
143 		assertEquals("Wrong notification from address.", "fakey@mcfakey.com", documentType.getNotificationFromAddress());
144 
145 		// test a child document type which overrides the parent's address
146 		documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressChild");
147 		assertEquals("Wrong notification from address.", "fakey@mcchild.com", documentType.getNotificationFromAddress());
148 
149 		// test a child document type which doesn't override the parent's address
150 		documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressChildInherited");
151 		assertEquals("Wrong notification from address.", "fakey@mcfakey.com", documentType.getNotificationFromAddress());
152 
153 		// Do an app specific route to a document which should send an email to fakey@mcchild.com
154 		WorkflowDocument document = WorkflowDocumentFactory.createDocument(user1PrincipalId, "NotificationFromAddressChild");
155 		document.adHocToPrincipal(ActionRequestType.APPROVE, "Initial", "", getPrincipalIdForName("ewestfal"), "", true);
156 		document.route("");
157 
158 		// verify that ewestfal was sent an email
159 		assertEquals("ewestfal should have an email.", 1, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
160 
161 		// we currently have no way from this test to determine the email address used for notification
162 	}
163 
164 	private void assertDefaultNotificationPreferences(String principalId) throws Exception {
165 		Preferences prefs = getPreferencesService().getPreferences(principalId);
166 		assertEquals(KewApiConstants.EMAIL_RMNDR_IMMEDIATE, prefs.getEmailNotification());
167 		assertEquals(KewApiConstants.PREFERENCES_YES_VAL, prefs.getNotifyPrimaryDelegation());
168 		assertEquals(KewApiConstants.PREFERENCES_NO_VAL, prefs.getNotifySecondaryDelegation());
169 	}
170 
171 	private PreferencesService getPreferencesService() {
172 		return KewApiServiceLocator.getPreferencesService();
173 	}
174 
175 	private MockEmailNotificationService getMockEmailService() {
176 		return (MockEmailNotificationService)KEWServiceLocator.getActionListEmailService();
177 	}
178 
179 }