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.core.api.criteria.QueryByCriteria;
20 import org.kuali.rice.ken.bo.NotificationBo;
21 import org.kuali.rice.ken.bo.NotificationMessageDelivery;
22 import org.kuali.rice.ken.service.NotificationMessageDeliveryAutoRemovalService;
23 import org.kuali.rice.ken.service.ProcessingResult;
24 import org.kuali.rice.ken.test.KENTestCase;
25 import org.kuali.rice.ken.util.NotificationConstants;
26 import org.kuali.rice.krad.service.KRADServiceLocator;
27 import org.kuali.rice.test.BaselineTestCase;
28
29 import java.util.Collection;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertTrue;
33
34 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
35 import static org.kuali.rice.core.api.criteria.PredicateFactory.isNotNull;
36
37
38
39
40
41 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
42 public class NotificationMessageDeliveryAutoRemovalServiceImplTest extends KENTestCase {
43
44 private static final int EXPECTED_SUCCESSES = 6;
45
46 protected void assertProcessResults() {
47
48 Collection<NotificationMessageDelivery> lockedDeliveries = services.getNotificationMessegDeliveryDao().getLockedDeliveries(NotificationBo.class, KRADServiceLocator.getDataObjectService());
49
50 assertEquals(0, lockedDeliveries.size());
51
52
53 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
54 criteria.setPredicates(equal(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS,
55 NotificationConstants.MESSAGE_DELIVERY_STATUS.AUTO_REMOVED));
56 Collection<NotificationMessageDelivery> unprocessedDeliveries =
57 KRADServiceLocator.getDataObjectService().findMatching(NotificationMessageDelivery.class,
58 criteria.build()).getResults();
59
60 assertEquals(EXPECTED_SUCCESSES, unprocessedDeliveries.size());
61 }
62
63
64
65
66 @Test
67 public void testAutoRemovedNotificationMessageDeliveries() {
68 NotificationMessageDeliveryAutoRemovalService nSvc = services.getNotificationMessageDeliveryAutoRemovalService();
69
70 services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
71
72 ProcessingResult result = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
73
74 assertEquals(0, result.getFailures().size());
75 assertEquals(EXPECTED_SUCCESSES, result.getSuccesses().size());
76
77 assertProcessResults();
78 }
79
80
81
82
83 @Test
84 public void testAutoRemovalConcurrency() throws InterruptedException {
85 final NotificationMessageDeliveryAutoRemovalService nSvc = services.getNotificationMessageDeliveryAutoRemovalService();
86
87 services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
88
89 final ProcessingResult[] results = new ProcessingResult[2];
90 Thread t1 = new Thread(new Runnable() {
91 public void run() {
92 results[0] = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
93 }
94 });
95 Thread t2 = new Thread(new Runnable() {
96 public void run() {
97 results[1] = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
98 }
99 });
100
101 t1.start();
102 t2.start();
103
104 t1.join();
105 t2.join();
106
107
108 LOG.info("Results of thread #1: " + results[0]);
109 LOG.info("Results of thread #2: " + results[1]);
110 assertTrue((results[0].getSuccesses().size() == EXPECTED_SUCCESSES && results[0].getFailures().size() == 0 &&
111 results[1].getSuccesses().size() == 0 && results[1].getFailures().size() == 0) ||
112 (results[1].getSuccesses().size() == EXPECTED_SUCCESSES && results[1].getFailures().size() == 0 &&
113 results[0].getSuccesses().size() == 0 && results[0].getFailures().size() == 0));
114
115 assertProcessResults();
116
117 }
118
119 }