View Javadoc
1   /**
2    * Copyright 2005-2014 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.ken.services.impl;
17  
18  import org.junit.Ignore;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.criteria.QueryByCriteria;
21  import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
22  import org.kuali.rice.kcb.service.MessageService;
23  import org.kuali.rice.ken.bo.NotificationBo;
24  import org.kuali.rice.ken.bo.NotificationMessageDelivery;
25  import org.kuali.rice.ken.service.NotificationMessageDeliveryResolverService;
26  import org.kuali.rice.ken.service.NotificationRecipientService;
27  import org.kuali.rice.ken.service.NotificationService;
28  import org.kuali.rice.ken.service.ProcessingResult;
29  import org.kuali.rice.ken.service.UserPreferenceService;
30  import org.kuali.rice.ken.service.impl.NotificationMessageDeliveryResolverServiceImpl;
31  import org.kuali.rice.ken.test.KENTestCase;
32  import org.kuali.rice.ken.util.NotificationConstants;
33  import org.kuali.rice.krad.data.DataObjectService;
34  import org.kuali.rice.krad.service.KRADServiceLocator;
35  import org.kuali.rice.test.data.PerTestUnitTestData;
36  import org.kuali.rice.test.data.UnitTestData;
37  import org.kuali.rice.test.data.UnitTestSql;
38  import org.springframework.transaction.PlatformTransactionManager;
39  
40  import java.util.Collection;
41  import java.util.concurrent.ExecutorService;
42  import java.util.concurrent.Executors;
43  
44  import static org.junit.Assert.*;
45  
46  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
47  
48  //import org.kuali.rice.core.jpa.criteria.Criteria;
49  
50  /**
51   * Tests NotificationMessageDeliveryResolverServiceImpl
52   * @author Kuali Rice Team (rice.collab@kuali.org)
53   */
54  // Make sure KCB has some deliverers configured for the test users, so message deliveries get created and the messages aren't removed
55  @PerTestUnitTestData(
56  		@UnitTestData(
57  				order = { UnitTestData.Type.SQL_STATEMENTS },
58  				sqlStatements = {
59  						@UnitTestSql("insert into KREN_RECIP_DELIV_T (RECIP_DELIV_ID, RECIP_ID, CHNL, NM, VER_NBR) values (1, 'testuser6', 'KEW', 'mock', 0)"),
60  						@UnitTestSql("insert into KREN_RECIP_DELIV_T (RECIP_DELIV_ID, RECIP_ID, CHNL, NM, VER_NBR) values (2, 'testuser1', 'KEW', 'mock', 0)"),
61  						@UnitTestSql("insert into KREN_RECIP_DELIV_T (RECIP_DELIV_ID, RECIP_ID, CHNL, NM, VER_NBR) values (3, 'testuser2', 'KEW', 'mock', 0)"),
62  						@UnitTestSql("insert into KREN_RECIP_DELIV_T (RECIP_DELIV_ID, RECIP_ID, CHNL, NM, VER_NBR) values (4, 'quickstart', 'KEW', 'mock', 0)"),
63  						@UnitTestSql("insert into KREN_RECIP_DELIV_T (RECIP_DELIV_ID, RECIP_ID, CHNL, NM, VER_NBR) values (5, 'testuser5', 'KEW', 'mock', 0)"),
64  						@UnitTestSql("insert into KREN_RECIP_DELIV_T (RECIP_DELIV_ID, RECIP_ID, CHNL, NM, VER_NBR) values (6, 'testuser4', 'KEW', 'mock', 0)")
65  				}
66  		)
67  )
68  @Ignore // deadlocks are detected during clear database lifecycle (even when select for update is commented out...)
69  public class NotificationMessageDeliveryResolverServiceImplTest extends KENTestCase {
70      // NOTE: this value is HIGHLY dependent on the test data, make sure that it reflects the results
71      // expected from the test data
72      private static final int EXPECTED_SUCCESSES = 6;
73  
74      /**
75       * Id of notification for which we will intentionally generate an exception during processing
76       */
77      private static final long BAD_NOTIFICATION_ID = 3L;
78  
79      private static class TestNotificationMessageDeliveryResolverService extends NotificationMessageDeliveryResolverServiceImpl {
80          public TestNotificationMessageDeliveryResolverService(NotificationService notificationService, NotificationRecipientService notificationRecipientService,
81                  DataObjectService dataObjectService, PlatformTransactionManager txManager, ExecutorService executor) {
82              super(notificationService, notificationRecipientService, dataObjectService, txManager, executor);
83          }
84  
85          @Override
86          protected Collection<Object> processWorkItems(Collection<NotificationBo> notifications) {
87              for (NotificationBo notification: notifications) {
88                  if (notification.getId().longValue() == BAD_NOTIFICATION_ID) {
89                      throw new RuntimeException("Intentional heinous exception");
90                  }
91              }
92              return super.processWorkItems(notifications);
93          }
94      }
95  
96      protected TestNotificationMessageDeliveryResolverService getResolverService() {
97          return new TestNotificationMessageDeliveryResolverService(services.getNotificationService(), services.getNotificationRecipientService(),
98                  KRADServiceLocator.getDataObjectService(), transactionManager,
99          	Executors.newFixedThreadPool(5));
100     }
101 
102     //this is the one need to tweek on Criteria
103     protected void assertProcessResults() {
104         // one error should have occurred and the delivery should have been marked unlocked again
105     	Collection<NotificationMessageDelivery> lockedDeliveries = services.getNotificationMessegDeliveryDao().getLockedDeliveries(NotificationBo.class, KRADServiceLocator.getDataObjectService());
106     	assertEquals(0, lockedDeliveries.size());
107 
108         // should be 1 unprocessed delivery (the one that had an error)
109         QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
110         criteria.setPredicates(equal(NotificationConstants.BO_PROPERTY_NAMES.PROCESSING_FLAG, NotificationConstants.PROCESSING_FLAGS.UNRESOLVED));
111         Collection<NotificationBo> unprocessedDeliveries = KRADServiceLocator.getDataObjectService().findMatching(NotificationBo.class, criteria.build()).getResults();
112 
113         assertEquals(1, unprocessedDeliveries.size());
114         NotificationBo n = unprocessedDeliveries.iterator().next();
115         // #3 is the bad one
116         assertEquals(BAD_NOTIFICATION_ID, n.getId().longValue());
117     }
118 
119     /**
120      * Test resolution of notifications
121      * This test resolves UNRESOLVED notification ids #3 and #4 in the test data.  An artificial exception is generated for notification #3.
122      * For notification #4, the recipients are defined to be the Rice Team and testuser1.  This results in 8 recipient resolutions, two of which
123      * are Email deliveries for jaf30 and ag266.
124      * If you change the test data this test should be updated to reflect the expected results.
125      */
126     @Test
127     public void testResolveNotificationMessageDeliveries() throws Exception {
128         NotificationMessageDeliveryResolverService nSvc = getResolverService();
129 
130         ProcessingResult result = nSvc.resolveNotificationMessageDeliveries();
131 
132         Thread.sleep(20000);
133 
134         assertEquals(EXPECTED_SUCCESSES, result.getSuccesses().size());
135 
136         MessageService ms = (MessageService) GlobalKCBServiceLocator.getInstance().getMessageService();
137         assertEquals(result.getSuccesses().size(), ms.getAllMessages().size());
138 
139         assertProcessResults();
140     }
141 
142 
143     /**
144      * Test concurrent resolution of notifications
145      */
146     @Test
147     public void testResolverConcurrency() throws InterruptedException {
148         final NotificationMessageDeliveryResolverService nSvc = getResolverService();
149 
150         final ProcessingResult[] results = new ProcessingResult[2];
151         Thread t1 = new Thread(new Runnable() {
152             public void run() {
153                 try {
154                     results[0] = nSvc.resolveNotificationMessageDeliveries();
155                 } catch (Exception e) {
156                     System.err.println("Error resolving notification message deliveries");
157                     e.printStackTrace();
158                 }
159             }
160         });
161         Thread t2 = new Thread(new Runnable() {
162             public void run() {
163                 try {
164                     results[1] = nSvc.resolveNotificationMessageDeliveries();
165                 } catch (Exception e) {
166                     System.err.println("Error resolving notification message deliveries");
167                     e.printStackTrace();
168                 }
169             }
170         });
171 
172         t1.start();
173         t2.start();
174 
175         t1.join();
176         t2.join();
177 
178         // assert that ONE of the resolvers got all the items, and the other got NONE of the items
179         LOG.info("Results of thread #1: " + results[0]);
180         LOG.info("Results of thread #2: " + results[1]);
181         assertNotNull(results[0]);
182         assertNotNull(results[1]);
183         assertTrue((results[0].getSuccesses().size() == EXPECTED_SUCCESSES && results[0].getFailures().size() == 1 && results[1].getSuccesses().size() == 0 && results[1].getFailures().size() == 0) ||
184                    (results[1].getSuccesses().size() == EXPECTED_SUCCESSES && results[1].getFailures().size() == 1 && results[0].getSuccesses().size() == 0 && results[0].getFailures().size() == 0));
185 
186         assertProcessResults();
187     }
188 }