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.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   * Tests NotificationMessageDeliveryAutoRemovalServiceImpl
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
42  public class NotificationMessageDeliveryAutoRemovalServiceImplTest extends KENTestCase {
43  	// NOTE: this value is highly dependent on test data 
44  	private static final int EXPECTED_SUCCESSES = 6;
45  
46  	protected void assertProcessResults() {
47  		// one error should have occurred and the delivery should have been marked unlocked again
48  		Collection<NotificationMessageDelivery> lockedDeliveries = services.getNotificationMessegDeliveryDao().getLockedDeliveries(NotificationBo.class, KRADServiceLocator.getDataObjectService());
49  
50  		assertEquals(0, lockedDeliveries.size());
51  
52  		// should be 1 autoremoved delivery
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  	 * Test auto-removal message deliveries
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  	 * Test concurrent auto-removal of message deliveries
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 		// assert that ONE of the autoremovers got all the items, and the other got NONE of the items
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 }