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.mail;
17
18 import mocks.MockEmailNotificationService;
19 import mocks.MockEmailNotificationServiceImpl;
20 import org.junit.Test;
21 import org.kuali.rice.core.api.config.property.ConfigContext;
22 import org.kuali.rice.kew.api.KewApiServiceLocator;
23 import org.kuali.rice.kew.api.WorkflowDocument;
24 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
25 import org.kuali.rice.kew.api.action.ActionRequestType;
26 import org.kuali.rice.kew.api.preferences.Preferences;
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.*;
32
33 public class EmailReminderLifecycleTest extends KEWTestCase {
34
35 private static final String DEFAULT_EMAIL_CRON_WEEKLY = "0 0 2 ? * 2";
36 private static final String DEFAULT_EMAIL_CRON_DAILY = "0 0 1 * * ?";
37
38 private EmailReminderLifecycle emailReminderLifecycle;
39
40 /**
41 * This method used to reset email sending to false for both daily and weekly reminders
42 *
43 * @see org.kuali.rice.test.RiceTestCase#tearDown()
44 */
45 @Override
46 public void tearDown() throws Exception {
47 ConfigContext.getCurrentContextConfig().putProperty(KewApiConstants.DAILY_EMAIL_ACTIVE, "false");
48 ConfigContext.getCurrentContextConfig().putProperty(KewApiConstants.WEEKLY_EMAIL_ACTIVE, "false");
49 super.tearDown();
50 }
51
52 @Test public void testDailyEmails() throws Exception {
53 // fire daily every 2 seconds
54 ConfigContext.getCurrentContextConfig().putProperty(KewApiConstants.DAILY_EMAIL_CRON_EXPRESSION, "0/2 * * * * ?");
55 // turn daily on and weekly off
56 ConfigContext.getCurrentContextConfig().putProperty(KewApiConstants.DAILY_EMAIL_ACTIVE, "true");
57 ConfigContext.getCurrentContextConfig().putProperty(KewApiConstants.WEEKLY_EMAIL_ACTIVE, "false");
58
59 String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
60 String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
61
62 // setup ewestfal to recieve daily emails
63 Preferences prefs = KewApiServiceLocator.getPreferencesService().getPreferences(ewestfalPrincipalId);
64 Preferences.Builder builder = Preferences.Builder.create(prefs);
65 builder.setEmailNotification(KewApiConstants.DAILY);
66 KewApiServiceLocator.getPreferencesService().savePreferences(ewestfalPrincipalId, builder.build());
67
68 WorkflowDocument document = WorkflowDocumentFactory.createDocument(rkirkendPrincipalId, "TestDocumentType");
69 document.adHocToPrincipal(ActionRequestType.APPROVE, "", ewestfalPrincipalId, "", Boolean.TRUE);
70 document.route("");
71
72 document = WorkflowDocumentFactory.loadDocument(ewestfalPrincipalId, document.getDocumentId());
73 assertTrue(document.isApprovalRequested());
74
75 int emailsSent = getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ);
76 assertEquals("ewestfal should have no emails.", 0, emailsSent);
77 MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED = false;
78 MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED = false;
79
80 // let's fire up the lifecycle
81 emailReminderLifecycle = new EmailReminderLifecycle();
82 emailReminderLifecycle.start();
83
84 // sleep for 10 seconds
85 Thread.sleep(10000);
86
87 // send daily reminder should have now been called
88 assertTrue("daily reminder should have been called.", MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED);
89 assertFalse("weekly reminder should NOT have been called.", MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED);
90
91 emailReminderLifecycle.stop();
92
93 // setting cron to empty so job will cease
94 ConfigContext.getCurrentContextConfig().putProperty(KewApiConstants.DAILY_EMAIL_CRON_EXPRESSION, DEFAULT_EMAIL_CRON_DAILY);
95
96 // try restarting to verify rescheduling of tasks
97 emailReminderLifecycle.start();
98 emailReminderLifecycle.stop();
99 }
100
101 @Test public void testWeeklyEmails() throws Exception {
102 // fire daily every 2 seconds
103 ConfigContext.getCurrentContextConfig().putProperty(KewApiConstants.WEEKLY_EMAIL_CRON_EXPRESSION, "0/2 * * * * ?");
104 // turn weekly on and daily off
105 ConfigContext.getCurrentContextConfig().putProperty(KewApiConstants.WEEKLY_EMAIL_ACTIVE, "true");
106 ConfigContext.getCurrentContextConfig().putProperty(KewApiConstants.DAILY_EMAIL_ACTIVE, "false");
107
108 String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
109 String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
110
111 // setup ewestfal to recieve weekly emails
112 Preferences prefs = KewApiServiceLocator.getPreferencesService().getPreferences(ewestfalPrincipalId);
113 Preferences.Builder builder = Preferences.Builder.create(prefs);
114 builder.setEmailNotification(KewApiConstants.WEEKLY);
115 KewApiServiceLocator.getPreferencesService().savePreferences(ewestfalPrincipalId, builder.build());
116
117 WorkflowDocument document = WorkflowDocumentFactory.createDocument(rkirkendPrincipalId, "TestDocumentType");
118 document.adHocToPrincipal(ActionRequestType.APPROVE, "", ewestfalPrincipalId, "", Boolean.TRUE);
119 document.route("");
120
121 document = WorkflowDocumentFactory.loadDocument(ewestfalPrincipalId, document.getDocumentId());
122 assertTrue(document.isApprovalRequested());
123
124 int emailsSent = getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ);
125 assertEquals("ewestfal should have no emails.", 0, emailsSent);
126 MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED = false;
127 MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED = false;
128
129 // let's fire up the lifecycle
130 emailReminderLifecycle = new EmailReminderLifecycle();
131 emailReminderLifecycle.start();
132
133 // sleep for 10 seconds
134 Thread.sleep(10000);
135
136 // send weekly reminder should have now been called
137 assertTrue("weekly reminder should have been called.", MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED);
138 assertFalse("daily reminder should NOT have been called.", MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED);
139
140 emailReminderLifecycle.stop();
141
142 // setting cron to empty so job will cease
143 ConfigContext.getCurrentContextConfig().putProperty(KewApiConstants.WEEKLY_EMAIL_CRON_EXPRESSION, DEFAULT_EMAIL_CRON_WEEKLY);
144
145 // try restarting to verify rescheduling of tasks
146 emailReminderLifecycle.start();
147 emailReminderLifecycle.stop();
148 }
149
150 // /**
151 // * Verify that no more messages are put in the queue if there are already weekly and daily reminders in the
152 // * queue
153 // * @throws Exception
154 // */
155 // @Test
156 // public void testEmailMessagesInQueue() throws Exception {
157 //
158 // setUpConfigForEmail();
159 //
160 // PersistedMessageBO dailyMessage = getMockDailyMessage();
161 // PersistedMessageBO weeklyMessage = getMockWeeklyMessage();
162 // KEWServiceLocator.getRouteQueueService().save(dailyMessage);
163 // KEWServiceLocator.getRouteQueueService().save(weeklyMessage);
164 //
165 // Collection messages = KEWServiceLocator.getRouteQueueService().findAll();
166 // assertEquals("Should only be 2 items present in queue", 2, messages.size());
167 //
168 // emailReminderLifecycle.start();
169 //
170 // messages = KEWServiceLocator.getRouteQueueService().findAll();
171 // assertEquals("Should only be 2 items present in queue", 2, messages.size());
172 //
173 // PersistedMessageBO fetchedDaily = null;
174 // PersistedMessageBO fetchedWeekly = null;
175 //
176 // for (Iterator iter = messages.iterator(); iter.hasNext();) {
177 // PersistedMessageBO fetchedMessage = (PersistedMessageBO) iter.next();
178 // if (fetchedMessage.getMethodName().equals("sendDailyReminder")) {
179 // fetchedDaily = fetchedMessage;
180 // } else {
181 // fetchedWeekly = fetchedMessage;
182 // }
183 // }
184 // assertEquals("Daily message was re-inserted or removed when it should have been allowed to stay in queue for later processing", dailyMessage.getRouteQueueId(), fetchedDaily.getRouteQueueId());
185 // assertEquals("Weekly message was re-inserted or removed when it should have been allowed to stay in queue for later processing", weeklyMessage.getRouteQueueId(), fetchedWeekly.getRouteQueueId());
186 // assertTrue("Lifecycle should report itself as started", emailReminderLifecycle.isStarted());
187 // }
188 //
189 // /**
190 // * If only a daily is in the queue then the other reminder should be put in the queue
191 // *
192 // * @throws Exception
193 // */
194 // @Test public void testOnlyDailyReminderInQueue() throws Exception {
195 //
196 // setUpConfigForEmail();
197 //
198 // PersistedMessageBO dailyMessage = getMockDailyMessage();
199 // KEWServiceLocator.getRouteQueueService().save(dailyMessage);
200 //
201 // Collection messages = KEWServiceLocator.getRouteQueueService().findAll();
202 // assertEquals("Should only be 1 items present in queue", 1, messages.size());
203 //
204 // emailReminderLifecycle.start();
205 //
206 // messages = KEWServiceLocator.getRouteQueueService().findAll();
207 // assertEquals("Should only be 2 items present in queue", 2, messages.size());
208 //
209 // PersistedMessageBO fetchedDaily = null;
210 // PersistedMessageBO fetchedWeekly = null;
211 //
212 // for (Iterator iter = messages.iterator(); iter.hasNext();) {
213 // PersistedMessageBO fetchedMessage = (PersistedMessageBO) iter.next();
214 // if (fetchedMessage.getMethodName().equals("sendDailyReminder")) {
215 // fetchedDaily = fetchedMessage;
216 // } else {
217 // fetchedWeekly = fetchedMessage;
218 // }
219 // }
220 // assertEquals("Daily message was re-inserted or removed when it should have been allowed to stay in queue for later processing", dailyMessage.getRouteQueueId(), fetchedDaily.getRouteQueueId());
221 // assertTrue(fetchedWeekly != null);
222 // assertTrue("Lifecycle should report itself as started", emailReminderLifecycle.isStarted());
223 // }
224 //
225 // /**
226 // * If only a weekly reminder is in the queue then the other reminder should be put in the queue
227 // *
228 // * @throws Exception
229 // */
230 // @Test public void testOnlyWeeklyReminderInQueue() throws Exception {
231 //
232 // setUpConfigForEmail();
233 //
234 // PersistedMessageBO weeklyMessage = getMockWeeklyMessage();
235 // KEWServiceLocator.getRouteQueueService().save(weeklyMessage);
236 //
237 // Collection messages = KEWServiceLocator.getRouteQueueService().findAll();
238 // assertEquals("Should only be 1 items present in queue", 1, messages.size());
239 //
240 // emailReminderLifecycle.start();
241 //
242 // messages = KEWServiceLocator.getRouteQueueService().findAll();
243 // assertEquals("Should only be 2 items present in queue", 2, messages.size());
244 //
245 // PersistedMessageBO fetchedDaily = null;
246 // PersistedMessageBO fetchedWeekly = null;
247 //
248 // for (Iterator iter = messages.iterator(); iter.hasNext();) {
249 // PersistedMessageBO fetchedMessage = (PersistedMessageBO) iter.next();
250 // if (fetchedMessage.getMethodName().equals("sendDailyReminder")) {
251 // fetchedDaily = fetchedMessage;
252 // } else {
253 // fetchedWeekly = fetchedMessage;
254 // }
255 // }
256 // assertEquals("Weekly message was re-inserted or removed when it should have been allowed to stay in queue for later processing", weeklyMessage.getRouteQueueId(), fetchedWeekly.getRouteQueueId());
257 // assertTrue("Daily message not sent", fetchedDaily != null);
258 // assertTrue("Lifecycle should report itself as started", emailReminderLifecycle.isStarted());
259 //
260 // }
261 //
262 // /**
263 // * Tests that email reminder calls are sent to the queue when none are present. New messages should
264 // * be set for the proper delay.
265 // *
266 // * @throws Exception
267 // */
268 // @Test public void testNoEmailRemindersInQueue() throws Exception {
269 //
270 // setUpConfigForEmail();
271 //
272 // emailReminderLifecycle.start();
273 // Collection messages = KEWServiceLocator.getRouteQueueService().findAll();
274 // assertEquals("Should only be 2 items present in queue", 2, messages.size());
275 // PersistedMessageBO fetchedDaily = null;
276 // PersistedMessageBO fetchedWeekly = null;
277 //
278 // for (Iterator iter = messages.iterator(); iter.hasNext();) {
279 // PersistedMessageBO fetchedMessage = (PersistedMessageBO) iter.next();
280 // if (fetchedMessage.getMethodName().equals("sendDailyReminder")) {
281 // fetchedDaily = fetchedMessage;
282 // } else {
283 // fetchedWeekly = fetchedMessage;
284 // }
285 // }
286 // assertNotNull("No daily message sent", fetchedDaily);
287 // assertNotNull("No weekly message sent", fetchedWeekly);
288 //
289 // assertTrue("Daily message not sent", fetchedDaily != null);
290 // assertTrue("Weekly message not sent", fetchedWeekly != null);
291 // assertTrue("Lifecycle should report itself as started", emailReminderLifecycle.isStarted());
292 //
293 //
294 // AsynchronousCall methodCall = (AsynchronousCall)KSBServiceLocator.getMessageHelper().deserializeObject(fetchedWeekly.getPayload());
295 // assertEquals("Weekly email not on a weekly delay", EmailReminderLifecycle.WEEKLY_DELAY, methodCall.getRepeatCallTimeIncrement().longValue());
296 //
297 // methodCall = (AsynchronousCall)KSBServiceLocator.getMessageHelper().deserializeObject(fetchedDaily.getPayload());
298 // assertEquals("Weekly email not on a weekly delay", EmailReminderLifecycle.DAILY_DELAY, methodCall.getRepeatCallTimeIncrement().longValue());
299 // }
300 //
301 // /**
302 // * the lifecycle should not blow up if this ip is the designated emailer but no email options are sent. it should
303 // * do nothing and report started.
304 // *
305 // * @throws Exception
306 // */
307 // @Test public void testNoEmailDatesInConfig() throws Exception {
308 // KEWServiceLocator.getApplicationConstantsService().save(new ApplicationConstant(KewApiConstants.APP_CONST_EMAIL_FIRST_SEND_IP_KEY, Utilities.getIpNumber()));
309 //
310 // Config config = ConfigContext.getCurrentContextConfig();
311 // config.getProperties().remove(Config.FIRST_DAILY_EMAIL_DELIVERY_DATE);
312 // config.getProperties().remove(Config.FIRST_WEEKLY_EMAIL_DELIVERY_DATE);
313 // emailReminderLifecycle.start();
314 // Collection messages = KEWServiceLocator.getRouteQueueService().findAll();
315 // assertEquals("Should not be items present in queue", 0, messages.size());
316 //
317 // assertTrue("Lifecycle should report itself as started", emailReminderLifecycle.isStarted());
318 // emailReminderLifecycle.stop();
319 // assertFalse("Lifecycle should not report itself as started", emailReminderLifecycle.isStarted());
320 // }
321 //
322 // /**
323 // * Keep the threadpool on and synchronous. Start the lifecycle and verify that
324 // * the action list email service got called.
325 // * @throws Exception
326 // */
327 // @Test public void testActionListEmailServiceBeingCalled() throws Exception {
328 // KEWServiceLocator.getApplicationConstantsService().save(new ApplicationConstant(KewApiConstants.APP_CONST_EMAIL_FIRST_SEND_IP_KEY, Utilities.getIpNumber()));
329 // Config config = ConfigContext.getCurrentContextConfig();
330 // config.overrideProperty(Config.FIRST_DAILY_EMAIL_DELIVERY_DATE, DAILY_REMINDER_DATE);
331 // config.overrideProperty(Config.FIRST_WEEKLY_EMAIL_DELIVERY_DATE, WEEKLY_REMINDER_DATE);
332 // emailReminderLifecycle.start();
333 // assertTrue("Send daily not called on email notification service", MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED);
334 // assertTrue("Send weekly not called on email notification service", MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED);
335 // }
336 //
337 // private void setUpConfigForEmail() throws Exception {
338 // KEWServiceLocator.getApplicationConstantsService().save(new ApplicationConstant(KewApiConstants.APP_CONST_EMAIL_FIRST_SEND_IP_KEY, Utilities.getIpNumber()));
339 //
340 // Config config = ConfigContext.getCurrentContextConfig();
341 // config.overrideProperty(Config.FIRST_DAILY_EMAIL_DELIVERY_DATE, DAILY_REMINDER_DATE);
342 // config.overrideProperty(Config.FIRST_WEEKLY_EMAIL_DELIVERY_DATE, WEEKLY_REMINDER_DATE);
343 //
344 // }
345 //
346 //
347 // private PersistedMessageBO getMockDailyMessage() throws Exception {
348 // PersistedMessageBO dailyMessage = new PersistedMessageBO();
349 // dailyMessage.setServiceName(emailReminderLifecycle.getEmailServiceName().toString());
350 // dailyMessage.setMethodName("sendDailyReminder");
351 // dailyMessage.setQueueDate(new Timestamp(System.currentTimeMillis()));
352 // dailyMessage.setQueuePriority(1);
353 // dailyMessage.setQueueStatus("Q");
354 // dailyMessage.setRetryCount(1);
355 // dailyMessage.setIpNumber(Utilities.getIpNumber());
356 // dailyMessage.setApplicationId("KEW");
357 // dailyMessage.setPayload(KSBServiceLocator.getMessageHelper().serializeObject("payload"));
358 // return dailyMessage;
359 // }
360 //
361 // private PersistedMessageBO getMockWeeklyMessage() throws Exception {
362 // PersistedMessageBO weeklyMessage = new PersistedMessageBO();
363 // weeklyMessage.setServiceName(emailReminderLifecycle.getEmailServiceName().toString());
364 // weeklyMessage.setQueueDate(new Timestamp(System.currentTimeMillis()));
365 // weeklyMessage.setMethodName("sendWeeklyReminder");
366 // weeklyMessage.setQueuePriority(1);
367 // weeklyMessage.setQueueStatus("Q");
368 // weeklyMessage.setRetryCount(1);
369 // weeklyMessage.setIpNumber(Utilities.getIpNumber());
370 // weeklyMessage.setApplicationId("KEW");
371 // weeklyMessage.setPayload(KSBServiceLocator.getMessageHelper().serializeObject("payload"));
372 // return weeklyMessage;
373 // }
374
375 private MockEmailNotificationService getMockEmailService() {
376 return (MockEmailNotificationService)KEWServiceLocator.getActionListEmailService();
377 }
378
379 }