1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ken.services.impl;
17
18 import org.junit.Test;
19 import org.kuali.rice.ken.bo.NotificationMessageDelivery;
20 import org.kuali.rice.ken.service.NotificationMessageDeliveryAutoRemovalService;
21 import org.kuali.rice.ken.service.ProcessingResult;
22 import org.kuali.rice.ken.test.KENTestCase;
23 import org.kuali.rice.ken.util.NotificationConstants;
24
25 import java.util.Collection;
26 import java.util.HashMap;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertTrue;
30
31
32
33
34
35
36
37
38 public class NotificationMessageDeliveryAutoRemovalServiceImplTest extends KENTestCase {
39
40 private static final int EXPECTED_SUCCESSES = 6;
41
42 protected void assertProcessResults() {
43
44 Collection<NotificationMessageDelivery> lockedDeliveries = services.getNotificationMessegDeliveryDao().getLockedDeliveries(NotificationMessageDelivery.class, services.getGenericDao());
45
46 assertEquals(0, lockedDeliveries.size());
47
48
49 HashMap<String, String> queryCriteria = new HashMap<String, String>();
50 queryCriteria.put(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.AUTO_REMOVED);
51 Collection<NotificationMessageDelivery> unprocessedDeliveries = services.getGenericDao().findMatching(NotificationMessageDelivery.class, queryCriteria);
52 assertEquals(EXPECTED_SUCCESSES, unprocessedDeliveries.size());
53 }
54
55
56
57
58 @Test
59 public void testAutoRemovedNotificationMessageDeliveries() {
60 NotificationMessageDeliveryAutoRemovalService nSvc = services.getNotificationMessageDeliveryAutoRemovalService();
61
62 services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
63
64 ProcessingResult result = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
65
66 assertEquals(0, result.getFailures().size());
67 assertEquals(EXPECTED_SUCCESSES, result.getSuccesses().size());
68
69 assertProcessResults();
70 }
71
72
73
74
75 @Test
76 public void testAutoRemovalConcurrency() throws InterruptedException {
77 final NotificationMessageDeliveryAutoRemovalService nSvc = services.getNotificationMessageDeliveryAutoRemovalService();
78
79 services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
80
81 final ProcessingResult[] results = new ProcessingResult[2];
82 Thread t1 = new Thread(new Runnable() {
83 public void run() {
84 results[0] = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
85 }
86 });
87 Thread t2 = new Thread(new Runnable() {
88 public void run() {
89 results[1] = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
90 }
91 });
92
93 t1.start();
94 t2.start();
95
96 t1.join();
97 t2.join();
98
99
100 LOG.info("Results of thread #1: " + results[0]);
101 LOG.info("Results of thread #2: " + results[1]);
102 assertTrue((results[0].getSuccesses().size() == EXPECTED_SUCCESSES && results[0].getFailures().size() == 0 && results[1].getSuccesses().size() == 0 && results[1].getFailures().size() == 0) ||
103 (results[1].getSuccesses().size() == EXPECTED_SUCCESSES && results[1].getFailures().size() == 0 && results[0].getSuccesses().size() == 0 && results[0].getFailures().size() == 0));
104
105 assertProcessResults();
106 }
107 }