001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.mail;
018:
019: import mocks.MockEmailNotificationService;
020: import mocks.MockEmailNotificationServiceImpl;
021:
022: import org.junit.Test;
023: import org.kuali.rice.core.Core;
024: import org.kuali.workflow.test.WorkflowTestCase;
025:
026: import edu.iu.uis.eden.EdenConstants;
027: import edu.iu.uis.eden.KEWServiceLocator;
028: import edu.iu.uis.eden.clientapp.WorkflowDocument;
029: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
030: import edu.iu.uis.eden.preferences.Preferences;
031: import edu.iu.uis.eden.user.AuthenticationUserId;
032: import edu.iu.uis.eden.user.WorkflowUser;
033:
034: public class EmailReminderLifecycleTest extends WorkflowTestCase {
035:
036: private EmailReminderLifecycle emailReminderLifecycle;
037:
038: @Override
039: public void setUp() throws Exception {
040: super .setUp();
041:
042: }
043:
044: @Test
045: public void testDailyEmails() throws Exception {
046: // fire daily every 2 seconds
047: Core.getCurrentContextConfig().overrideProperty(
048: EdenConstants.DAILY_EMAIL_CRON_EXPRESSION,
049: "0/2 * * * * ?");
050: // turn daily on and weekly off
051: Core.getCurrentContextConfig().overrideProperty(
052: EdenConstants.DAILY_EMAIL_ACTIVE, "true");
053: Core.getCurrentContextConfig().overrideProperty(
054: EdenConstants.WEEKLY_EMAIL_ACTIVE, "false");
055:
056: // setup ewestfal to recieve daily emails
057: WorkflowUser ewestfal = KEWServiceLocator.getUserService()
058: .getWorkflowUser(new AuthenticationUserId("ewestfal"));
059: Preferences prefs = KEWServiceLocator.getPreferencesService()
060: .getPreferences(ewestfal);
061: prefs.setEmailNotification(EdenConstants.DAILY);
062: KEWServiceLocator.getPreferencesService().savePreferences(
063: ewestfal, prefs);
064:
065: WorkflowDocument document = new WorkflowDocument(
066: new NetworkIdVO("rkirkend"), "TestDocumentType");
067: document.appSpecificRouteDocumentToUser(
068: EdenConstants.ACTION_REQUEST_APPROVE_REQ, "",
069: new NetworkIdVO("ewestfal"), "", Boolean.TRUE);
070: document.routeDocument("");
071:
072: document = new WorkflowDocument(new NetworkIdVO("ewestfal"),
073: document.getRouteHeaderId());
074: assertTrue(document.isApprovalRequested());
075:
076: int emailsSent = getMockEmailService().emailsSent("ewestfal",
077: document.getRouteHeaderId(),
078: EdenConstants.ACTION_REQUEST_APPROVE_REQ);
079: assertEquals("ewestfal should have no emails.", 0, emailsSent);
080: MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED = false;
081: MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED = false;
082:
083: // let's fire up the lifecycle
084: emailReminderLifecycle = new EmailReminderLifecycle();
085: emailReminderLifecycle.start();
086:
087: // sleep for 10 seconds
088: Thread.sleep(10000);
089:
090: // send daily reminder should have now been called
091: assertTrue(
092: "daily reminder should have been called.",
093: MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED);
094: assertFalse(
095: "weekly reminder should NOT have been called.",
096: MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED);
097:
098: emailReminderLifecycle.stop();
099:
100: // try restarting to verify rescheduling of tasks
101: emailReminderLifecycle.start();
102: emailReminderLifecycle.stop();
103:
104: }
105:
106: @Test
107: public void testWeeklyEmails() throws Exception {
108: // fire daily every 2 seconds
109: Core.getCurrentContextConfig().overrideProperty(
110: EdenConstants.WEEKLY_EMAIL_CRON_EXPRESSION,
111: "0/2 * * * * ?");
112: // turn weekly on and daily off
113: Core.getCurrentContextConfig().overrideProperty(
114: EdenConstants.WEEKLY_EMAIL_ACTIVE, "true");
115: Core.getCurrentContextConfig().overrideProperty(
116: EdenConstants.DAILY_EMAIL_ACTIVE, "false");
117:
118: // setup ewestfal to recieve weekly emails
119: WorkflowUser ewestfal = KEWServiceLocator.getUserService()
120: .getWorkflowUser(new AuthenticationUserId("ewestfal"));
121: Preferences prefs = KEWServiceLocator.getPreferencesService()
122: .getPreferences(ewestfal);
123: prefs.setEmailNotification(EdenConstants.WEEKLY);
124: KEWServiceLocator.getPreferencesService().savePreferences(
125: ewestfal, prefs);
126:
127: WorkflowDocument document = new WorkflowDocument(
128: new NetworkIdVO("rkirkend"), "TestDocumentType");
129: document.appSpecificRouteDocumentToUser(
130: EdenConstants.ACTION_REQUEST_APPROVE_REQ, "",
131: new NetworkIdVO("ewestfal"), "", Boolean.TRUE);
132: document.routeDocument("");
133:
134: document = new WorkflowDocument(new NetworkIdVO("ewestfal"),
135: document.getRouteHeaderId());
136: assertTrue(document.isApprovalRequested());
137:
138: int emailsSent = getMockEmailService().emailsSent("ewestfal",
139: document.getRouteHeaderId(),
140: EdenConstants.ACTION_REQUEST_APPROVE_REQ);
141: assertEquals("ewestfal should have no emails.", 0, emailsSent);
142: MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED = false;
143: MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED = false;
144:
145: // let's fire up the lifecycle
146: emailReminderLifecycle = new EmailReminderLifecycle();
147: emailReminderLifecycle.start();
148:
149: // sleep for 10 seconds
150: Thread.sleep(10000);
151:
152: // send weekly reminder should have now been called
153: assertTrue(
154: "weekly reminder should have been called.",
155: MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED);
156: assertFalse(
157: "daily reminder should NOT have been called.",
158: MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED);
159:
160: emailReminderLifecycle.stop();
161:
162: // try restarting to verify rescheduling of tasks
163: emailReminderLifecycle.start();
164: }
165:
166: // /**
167: // * Verify that no more messages are put in the queue if there are already weekly and daily reminders in the
168: // * queue
169: // * @throws Exception
170: // */
171: // @Test
172: // public void testEmailMessagesInQueue() throws Exception {
173: //
174: // setUpConfigForEmail();
175: //
176: // PersistedMessage dailyMessage = getMockDailyMessage();
177: // PersistedMessage weeklyMessage = getMockWeeklyMessage();
178: // KEWServiceLocator.getRouteQueueService().save(dailyMessage);
179: // KEWServiceLocator.getRouteQueueService().save(weeklyMessage);
180: //
181: // Collection messages = KEWServiceLocator.getRouteQueueService().findAll();
182: // assertEquals("Should only be 2 items present in queue", 2, messages.size());
183: //
184: // emailReminderLifecycle.start();
185: //
186: // messages = KEWServiceLocator.getRouteQueueService().findAll();
187: // assertEquals("Should only be 2 items present in queue", 2, messages.size());
188: //
189: // PersistedMessage fetchedDaily = null;
190: // PersistedMessage fetchedWeekly = null;
191: //
192: // for (Iterator iter = messages.iterator(); iter.hasNext();) {
193: // PersistedMessage fetchedMessage = (PersistedMessage) iter.next();
194: // if (fetchedMessage.getMethodName().equals("sendDailyReminder")) {
195: // fetchedDaily = fetchedMessage;
196: // } else {
197: // fetchedWeekly = fetchedMessage;
198: // }
199: // }
200: // 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());
201: // 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());
202: // assertTrue("Lifecycle should report itself as started", emailReminderLifecycle.isStarted());
203: // }
204: //
205: // /**
206: // * If only a daily is in the queue then the other reminder should be put in the queue
207: // *
208: // * @throws Exception
209: // */
210: // @Test public void testOnlyDailyReminderInQueue() throws Exception {
211: //
212: // setUpConfigForEmail();
213: //
214: // PersistedMessage dailyMessage = getMockDailyMessage();
215: // KEWServiceLocator.getRouteQueueService().save(dailyMessage);
216: //
217: // Collection messages = KEWServiceLocator.getRouteQueueService().findAll();
218: // assertEquals("Should only be 1 items present in queue", 1, messages.size());
219: //
220: // emailReminderLifecycle.start();
221: //
222: // messages = KEWServiceLocator.getRouteQueueService().findAll();
223: // assertEquals("Should only be 2 items present in queue", 2, messages.size());
224: //
225: // PersistedMessage fetchedDaily = null;
226: // PersistedMessage fetchedWeekly = null;
227: //
228: // for (Iterator iter = messages.iterator(); iter.hasNext();) {
229: // PersistedMessage fetchedMessage = (PersistedMessage) iter.next();
230: // if (fetchedMessage.getMethodName().equals("sendDailyReminder")) {
231: // fetchedDaily = fetchedMessage;
232: // } else {
233: // fetchedWeekly = fetchedMessage;
234: // }
235: // }
236: // 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());
237: // assertTrue(fetchedWeekly != null);
238: // assertTrue("Lifecycle should report itself as started", emailReminderLifecycle.isStarted());
239: // }
240: //
241: // /**
242: // * If only a weekly reminder is in the queue then the other reminder should be put in the queue
243: // *
244: // * @throws Exception
245: // */
246: // @Test public void testOnlyWeeklyReminderInQueue() throws Exception {
247: //
248: // setUpConfigForEmail();
249: //
250: // PersistedMessage weeklyMessage = getMockWeeklyMessage();
251: // KEWServiceLocator.getRouteQueueService().save(weeklyMessage);
252: //
253: // Collection messages = KEWServiceLocator.getRouteQueueService().findAll();
254: // assertEquals("Should only be 1 items present in queue", 1, messages.size());
255: //
256: // emailReminderLifecycle.start();
257: //
258: // messages = KEWServiceLocator.getRouteQueueService().findAll();
259: // assertEquals("Should only be 2 items present in queue", 2, messages.size());
260: //
261: // PersistedMessage fetchedDaily = null;
262: // PersistedMessage fetchedWeekly = null;
263: //
264: // for (Iterator iter = messages.iterator(); iter.hasNext();) {
265: // PersistedMessage fetchedMessage = (PersistedMessage) iter.next();
266: // if (fetchedMessage.getMethodName().equals("sendDailyReminder")) {
267: // fetchedDaily = fetchedMessage;
268: // } else {
269: // fetchedWeekly = fetchedMessage;
270: // }
271: // }
272: // 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());
273: // assertTrue("Daily message not sent", fetchedDaily != null);
274: // assertTrue("Lifecycle should report itself as started", emailReminderLifecycle.isStarted());
275: //
276: // }
277: //
278: // /**
279: // * Tests that email reminder calls are sent to the queue when none are present. New messages should
280: // * be set for the proper delay.
281: // *
282: // * @throws Exception
283: // */
284: // @Test public void testNoEmailRemindersInQueue() throws Exception {
285: //
286: // setUpConfigForEmail();
287: //
288: // emailReminderLifecycle.start();
289: // Collection messages = KEWServiceLocator.getRouteQueueService().findAll();
290: // assertEquals("Should only be 2 items present in queue", 2, messages.size());
291: // PersistedMessage fetchedDaily = null;
292: // PersistedMessage fetchedWeekly = null;
293: //
294: // for (Iterator iter = messages.iterator(); iter.hasNext();) {
295: // PersistedMessage fetchedMessage = (PersistedMessage) iter.next();
296: // if (fetchedMessage.getMethodName().equals("sendDailyReminder")) {
297: // fetchedDaily = fetchedMessage;
298: // } else {
299: // fetchedWeekly = fetchedMessage;
300: // }
301: // }
302: // assertNotNull("No daily message sent", fetchedDaily);
303: // assertNotNull("No weekly message sent", fetchedWeekly);
304: //
305: // assertTrue("Daily message not sent", fetchedDaily != null);
306: // assertTrue("Weekly message not sent", fetchedWeekly != null);
307: // assertTrue("Lifecycle should report itself as started", emailReminderLifecycle.isStarted());
308: //
309: //
310: // AsynchronousCall methodCall = (AsynchronousCall)KSBServiceLocator.getMessageHelper().deserializeObject(fetchedWeekly.getPayload());
311: // assertEquals("Weekly email not on a weekly delay", EmailReminderLifecycle.WEEKLY_DELAY, methodCall.getRepeatCallTimeIncrement().longValue());
312: //
313: // methodCall = (AsynchronousCall)KSBServiceLocator.getMessageHelper().deserializeObject(fetchedDaily.getPayload());
314: // assertEquals("Weekly email not on a weekly delay", EmailReminderLifecycle.DAILY_DELAY, methodCall.getRepeatCallTimeIncrement().longValue());
315: // }
316: //
317: // /**
318: // * the lifecycle should not blow up if this ip is the designated emailer but no email options are sent. it should
319: // * do nothing and report started.
320: // *
321: // * @throws Exception
322: // */
323: // @Test public void testNoEmailDatesInConfig() throws Exception {
324: // KEWServiceLocator.getApplicationConstantsService().save(new ApplicationConstant(EdenConstants.APP_CONST_EMAIL_FIRST_SEND_IP_KEY, Utilities.getIpNumber()));
325: //
326: // Config config = Core.getCurrentContextConfig();
327: // config.getProperties().remove(Config.FIRST_DAILY_EMAIL_DELIVERY_DATE);
328: // config.getProperties().remove(Config.FIRST_WEEKLY_EMAIL_DELIVERY_DATE);
329: // emailReminderLifecycle.start();
330: // Collection messages = KEWServiceLocator.getRouteQueueService().findAll();
331: // assertEquals("Should not be items present in queue", 0, messages.size());
332: //
333: // assertTrue("Lifecycle should report itself as started", emailReminderLifecycle.isStarted());
334: // emailReminderLifecycle.stop();
335: // assertFalse("Lifecycle should not report itself as started", emailReminderLifecycle.isStarted());
336: // }
337: //
338: // /**
339: // * Keep the threadpool on and synchronous. Start the lifecycle and verify that
340: // * the action list email service got called.
341: // * @throws Exception
342: // */
343: // @Test public void testActionListEmailServiceBeingCalled() throws Exception {
344: // KEWServiceLocator.getApplicationConstantsService().save(new ApplicationConstant(EdenConstants.APP_CONST_EMAIL_FIRST_SEND_IP_KEY, Utilities.getIpNumber()));
345: // Config config = Core.getCurrentContextConfig();
346: // config.overrideProperty(Config.FIRST_DAILY_EMAIL_DELIVERY_DATE, DAILY_REMINDER_DATE);
347: // config.overrideProperty(Config.FIRST_WEEKLY_EMAIL_DELIVERY_DATE, WEEKLY_REMINDER_DATE);
348: // emailReminderLifecycle.start();
349: // assertTrue("Send daily not called on email notification service", MockEmailNotificationServiceImpl.SEND_DAILY_REMINDER_CALLED);
350: // assertTrue("Send weekly not called on email notification service", MockEmailNotificationServiceImpl.SEND_WEEKLY_REMINDER_CALLED);
351: // }
352: //
353: // private void setUpConfigForEmail() throws Exception {
354: // KEWServiceLocator.getApplicationConstantsService().save(new ApplicationConstant(EdenConstants.APP_CONST_EMAIL_FIRST_SEND_IP_KEY, Utilities.getIpNumber()));
355: //
356: // Config config = Core.getCurrentContextConfig();
357: // config.overrideProperty(Config.FIRST_DAILY_EMAIL_DELIVERY_DATE, DAILY_REMINDER_DATE);
358: // config.overrideProperty(Config.FIRST_WEEKLY_EMAIL_DELIVERY_DATE, WEEKLY_REMINDER_DATE);
359: //
360: // }
361: //
362: //
363: // private PersistedMessage getMockDailyMessage() throws Exception {
364: // PersistedMessage dailyMessage = new PersistedMessage();
365: // dailyMessage.setServiceName(emailReminderLifecycle.getEmailServiceName().toString());
366: // dailyMessage.setMethodName("sendDailyReminder");
367: // dailyMessage.setQueueDate(new Timestamp(System.currentTimeMillis()));
368: // dailyMessage.setQueuePriority(1);
369: // dailyMessage.setQueueStatus("Q");
370: // dailyMessage.setRetryCount(1);
371: // dailyMessage.setIpNumber(Utilities.getIpNumber());
372: // dailyMessage.setMessageEntity("KEW");
373: // dailyMessage.setPayload(KSBServiceLocator.getMessageHelper().serializeObject("payload"));
374: // return dailyMessage;
375: // }
376: //
377: // private PersistedMessage getMockWeeklyMessage() throws Exception {
378: // PersistedMessage weeklyMessage = new PersistedMessage();
379: // weeklyMessage.setServiceName(emailReminderLifecycle.getEmailServiceName().toString());
380: // weeklyMessage.setQueueDate(new Timestamp(System.currentTimeMillis()));
381: // weeklyMessage.setMethodName("sendWeeklyReminder");
382: // weeklyMessage.setQueuePriority(1);
383: // weeklyMessage.setQueueStatus("Q");
384: // weeklyMessage.setRetryCount(1);
385: // weeklyMessage.setIpNumber(Utilities.getIpNumber());
386: // weeklyMessage.setMessageEntity("KEW");
387: // weeklyMessage.setPayload(KSBServiceLocator.getMessageHelper().serializeObject("payload"));
388: // return weeklyMessage;
389: // }
390:
391: private MockEmailNotificationService getMockEmailService() {
392: return (MockEmailNotificationService) KEWServiceLocator
393: .getActionListEmailService();
394: }
395:
396: }
|