View Javadoc

1   /*
2    * Copyright 2006-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  
17  package org.kuali.rice.kew.mail;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  import mocks.MockEmailNotificationService;
23  import mocks.MockEmailNotificationServiceImpl;
24  
25  import org.junit.Test;
26  import org.kuali.rice.core.api.config.property.ConfigContext;
27  import org.kuali.rice.kew.api.WorkflowDocument;
28  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
29  import org.kuali.rice.kew.api.action.ActionRequestType;
30  import org.kuali.rice.kew.preferences.Preferences;
31  import org.kuali.rice.kew.service.KEWServiceLocator;
32  import org.kuali.rice.kew.test.KEWTestCase;
33  import org.kuali.rice.kew.util.KEWConstants;
34  
35  public class EmailReminderLifecycleTest extends KEWTestCase {
36  
37      private static final String DEFAULT_EMAIL_CRON_WEEKLY = "0 0 2 ? * 2";
38      private static final String DEFAULT_EMAIL_CRON_DAILY = "0 0 1 * * ?";
39  
40  	private EmailReminderLifecycle emailReminderLifecycle;
41  
42  	/**
43  	 * This method used to reset email sending to false for both daily and weekly reminders
44  	 *
45  	 * @see org.kuali.rice.test.RiceTestCase#tearDown()
46  	 */
47  	@Override
48  	public void tearDown() throws Exception {
49          ConfigContext.getCurrentContextConfig().putProperty(KEWConstants.DAILY_EMAIL_ACTIVE, "false");
50          ConfigContext.getCurrentContextConfig().putProperty(KEWConstants.WEEKLY_EMAIL_ACTIVE, "false");
51  	    super.tearDown();
52  	}
53  
54  	@Test public void testDailyEmails() throws Exception {
55  		// fire daily every 2 seconds
56  		ConfigContext.getCurrentContextConfig().putProperty(KEWConstants.DAILY_EMAIL_CRON_EXPRESSION, "0/2 * * * * ?");
57  		// turn daily on and weekly off
58  		ConfigContext.getCurrentContextConfig().putProperty(KEWConstants.DAILY_EMAIL_ACTIVE, "true");
59  		ConfigContext.getCurrentContextConfig().putProperty(KEWConstants.WEEKLY_EMAIL_ACTIVE, "false");
60  
61  		String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
62  		String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
63  		
64  		// setup ewestfal to recieve daily emails
65  		Preferences prefs = KEWServiceLocator.getPreferencesService().getPreferences(ewestfalPrincipalId);
66  		prefs.setEmailNotification(KEWConstants.DAILY);
67  		KEWServiceLocator.getPreferencesService().savePreferences(ewestfalPrincipalId, prefs);
68  
69  		WorkflowDocument document = WorkflowDocumentFactory.createDocument(rkirkendPrincipalId, "TestDocumentType");
70  		document.adHocToPrincipal(ActionRequestType.APPROVE, "", ewestfalPrincipalId, "", Boolean.TRUE);
71  		document.route("");
72  
73  		document = WorkflowDocumentFactory.loadDocument(ewestfalPrincipalId, document.getDocumentId());
74  		assertTrue(document.isApprovalRequested());
75  
76  		int emailsSent = getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ);
77  		assertEquals("ewestfal should have no emails.", 0, emailsSent);
78  		MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED = false;
79  		MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED = false;
80  
81  		// let's fire up the lifecycle
82  		emailReminderLifecycle = new EmailReminderLifecycle();
83  		emailReminderLifecycle.start();
84  
85  		// sleep for 10 seconds
86  		Thread.sleep(10000);
87  
88  		// send daily reminder should have now been called
89  		assertTrue("daily reminder should have been called.", MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED);
90  		assertFalse("weekly reminder should NOT have been called.", MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED);
91  
92  		emailReminderLifecycle.stop();
93  
94  		// setting cron to empty so job will cease
95  		ConfigContext.getCurrentContextConfig().putProperty(KEWConstants.DAILY_EMAIL_CRON_EXPRESSION, DEFAULT_EMAIL_CRON_DAILY);
96  
97  		// try restarting to verify rescheduling of tasks
98  		emailReminderLifecycle.start();
99  		emailReminderLifecycle.stop();
100 	}
101 
102 	@Test public void testWeeklyEmails() throws Exception {
103 		// fire daily every 2 seconds
104 		ConfigContext.getCurrentContextConfig().putProperty(KEWConstants.WEEKLY_EMAIL_CRON_EXPRESSION, "0/2 * * * * ?");
105 		// turn weekly on and daily off
106 		ConfigContext.getCurrentContextConfig().putProperty(KEWConstants.WEEKLY_EMAIL_ACTIVE, "true");
107 		ConfigContext.getCurrentContextConfig().putProperty(KEWConstants.DAILY_EMAIL_ACTIVE, "false");
108 
109 		String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
110 		String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
111 		
112 		// setup ewestfal to recieve weekly emails
113 		Preferences prefs = KEWServiceLocator.getPreferencesService().getPreferences(ewestfalPrincipalId);
114 		prefs.setEmailNotification(KEWConstants.WEEKLY);
115 		KEWServiceLocator.getPreferencesService().savePreferences(ewestfalPrincipalId, prefs);
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(), KEWConstants.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(KEWConstants.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(KEWConstants.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(KEWConstants.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(KEWConstants.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 }