1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
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  		
52  		assertEquals("bmcgough should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("bmcgough", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
53  		
54  		assertEquals("jitrue should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("jitrue", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
55  		
56  		assertEquals("user1 should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("user1", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
57  
58  	}
59  
60  	
61  
62  
63  
64  
65  
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  		
76  		assertDefaultNotificationPreferences(ewestfalPrincipalId);
77  		assertDefaultNotificationPreferences(jitruePrincipalId);
78  		assertDefaultNotificationPreferences(rkirkendPrincipalId);
79  		assertDefaultNotificationPreferences(jhopfPrincipalId);
80  		assertDefaultNotificationPreferences(bmcgoughPrincipalId);
81  		
82  
83  		
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  		
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 		
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 		
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 		
113 		WorkflowDocument document = WorkflowDocumentFactory.createDocument(user1PrincipalId, "NotificationTest");
114 		document.route("");
115 
116 		
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 		
121 		assertEquals("rkirkend should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("rkirkend", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
122 
123 		
124 		assertEquals("jhopf should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("jhopf", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
125 
126 		
127 		assertEquals("bmcgough should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("bmcgough", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
128 	}
129 
130 
131     
132 
133 
134     @Test public void testIndDocTypeEmailPreferences() throws Exception {
135         String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
136         String jitruePrincipalId = getPrincipalIdForName("jitrue");
137         String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
138         String jhopfPrincipalId = getPrincipalIdForName("jhopf");
139         String bmcgoughPrincipalId = getPrincipalIdForName("bmcgough");
140         String user1PrincipalId = getPrincipalIdForName("user1");
141 
142         Preferences prefs = getPreferencesService().getPreferences(ewestfalPrincipalId);
143         Preferences.Builder preferencesBuilder = Preferences.Builder.create(prefs);
144 
145         prefs = getPreferencesService().getPreferences(ewestfalPrincipalId);
146         preferencesBuilder = Preferences.Builder.create(prefs);
147         preferencesBuilder.setEmailNotification(KewApiConstants.EMAIL_RMNDR_WEEK_VAL);
148         preferencesBuilder.setDocumentTypeNotificationPreference("NotificationTest", "immediate");
149         getPreferencesService().savePreferences(ewestfalPrincipalId, preferencesBuilder.build());
150 
151         
152         prefs = getPreferencesService().getPreferences(jhopfPrincipalId);
153         preferencesBuilder = Preferences.Builder.create(prefs);
154         preferencesBuilder.setEmailNotification(KewApiConstants.EMAIL_RMNDR_NO_VAL);
155         preferencesBuilder.setDocumentTypeNotificationPreference("NotificationTest", "daily");
156         getPreferencesService().savePreferences(jhopfPrincipalId, preferencesBuilder.build());
157 
158         
159         WorkflowDocument document = WorkflowDocumentFactory.createDocument(user1PrincipalId, "NotificationTest");
160         document.route("");
161 
162         assertEquals("ewestfal should have one emails.", 1, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
163         assertEquals("rkirkend should only have one email.", 1, getMockEmailService().immediateReminderEmailsSent("rkirkend", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
164         assertEquals("jhopf should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("jhopf", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
165         assertEquals("jitrue should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("jitrue", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
166         assertEquals("bmcgough should have no emails.", 0, getMockEmailService().immediateReminderEmailsSent("bmcgough", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
167     }
168 
169 	
170 
171 
172 	@Test public void testDocumentTypeNotificationFromAddress() throws Exception {
173 		String user1PrincipalId = getPrincipalIdForName("user1");
174 
175 		
176 		DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationTest");
177 		assertNull("Wrong notification from address, should be null.", documentType.getNotificationFromAddress());
178         assertNull("Wrong actual notification from address, should be null.", documentType.getActualNotificationFromAddress());
179 
180 		
181 		documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressParent");
182 		assertEquals("Wrong notification from address.", "fakey@mcfakey.com", documentType.getNotificationFromAddress());
183 
184 		
185 		documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressChild");
186 		assertEquals("Wrong notification from address.", "fakey@mcchild.com", documentType.getNotificationFromAddress());
187 
188 		
189 		documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressChildInherited");
190 		assertEquals("Wrong notification from address.", "fakey@mcfakey.com", documentType.getNotificationFromAddress());
191 
192 		
193 		WorkflowDocument document = WorkflowDocumentFactory.createDocument(user1PrincipalId, "NotificationFromAddressChild");
194 		document.adHocToPrincipal(ActionRequestType.APPROVE, "Initial", "", getPrincipalIdForName("ewestfal"), "", true);
195 		document.route("");
196 
197 		
198 		assertEquals("ewestfal should have an email.", 1, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
199 
200 		
201 	}
202 
203 	private void assertDefaultNotificationPreferences(String principalId) throws Exception {
204 		Preferences prefs = getPreferencesService().getPreferences(principalId);
205 		assertEquals(KewApiConstants.EMAIL_RMNDR_IMMEDIATE, prefs.getEmailNotification());
206 		assertEquals(KewApiConstants.PREFERENCES_YES_VAL, prefs.getNotifyPrimaryDelegation());
207 		assertEquals(KewApiConstants.PREFERENCES_NO_VAL, prefs.getNotifySecondaryDelegation());
208 	}
209 
210 	private PreferencesService getPreferencesService() {
211 		return KewApiServiceLocator.getPreferencesService();
212 	}
213 
214 	private MockEmailNotificationService getMockEmailService() {
215 		return (MockEmailNotificationService)KEWServiceLocator.getActionListEmailService();
216 	}
217 
218 }