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 @Test public void testDocumentTypeNotificationFromAddress() throws Exception {
134 String user1PrincipalId = getPrincipalIdForName("user1");
135
136
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
142 documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressParent");
143 assertEquals("Wrong notification from address.", "fakey@mcfakey.com", documentType.getNotificationFromAddress());
144
145
146 documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressChild");
147 assertEquals("Wrong notification from address.", "fakey@mcchild.com", documentType.getNotificationFromAddress());
148
149
150 documentType = KEWServiceLocator.getDocumentTypeService().findByName("NotificationFromAddressChildInherited");
151 assertEquals("Wrong notification from address.", "fakey@mcfakey.com", documentType.getNotificationFromAddress());
152
153
154 WorkflowDocument document = WorkflowDocumentFactory.createDocument(user1PrincipalId, "NotificationFromAddressChild");
155 document.adHocToPrincipal(ActionRequestType.APPROVE, "Initial", "", getPrincipalIdForName("ewestfal"), "", true);
156 document.route("");
157
158
159 assertEquals("ewestfal should have an email.", 1, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
160
161
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 }