View Javadoc

1   /**
2    * Copyright 2005-2013 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.ksb.messaging.exceptionhandling;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertTrue;
20  
21  import java.util.List;
22  
23  import javax.xml.namespace.QName;
24  
25  import org.junit.Test;
26  import org.kuali.rice.ksb.api.KsbApiServiceLocator;
27  import org.kuali.rice.ksb.messaging.GlobalCallbackRegistry;
28  import org.kuali.rice.ksb.messaging.PersistedMessageBO;
29  import org.kuali.rice.ksb.messaging.TestCallback;
30  import org.kuali.rice.ksb.messaging.remotedservices.TesetHarnessExplodingQueue;
31  import org.kuali.rice.ksb.messaging.service.KSBJavaService;
32  import org.kuali.rice.ksb.service.KSBServiceLocator;
33  import org.kuali.rice.ksb.test.KSBTestCase;
34  import org.kuali.rice.ksb.util.KSBConstants;
35  import org.kuali.rice.test.TestUtilities;
36  
37  
38  /**
39   * Tests various exception messaging cases
40   *
41   * Millis to live - that a message with no home is still sending messages while it's time to live hasn't expired
42   * Retry count - that a message configured with a retry count will send x number of messages before being marked exception
43   * Being marked as exception - that a message in exception is in the route log and marked with a status of 'E'
44   * Defuault retry count - that a message configured with no retry or time to live is retry the default number of times as
45   * 	noted in an app constant and a class default if that constant is not a number or doesn't exist
46   * App Constant to determine the default time increment works (we need this to effectively test anyway)
47   * Things work without the timeincrement constant in place
48   *
49   * @author Kuali Rice Team (rice.collab@kuali.org)
50   *
51   */
52  public class ExceptionMessagingTest extends KSBTestCase {
53  
54  
55  	private QName queueTimeToLiveServiceName = new QName("KEW", "explodingQueueTimeLimit");
56  	private TestCallback callback = new TestCallback();
57  
58  	@Override
59  	public void setUp() throws Exception {
60  		System.setProperty(KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY, "500");
61  		System.setProperty(KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_KEY, "5");
62  		super.setUp();
63  		GlobalCallbackRegistry.getCallbacks().clear();
64  		GlobalCallbackRegistry.getCallbacks().add(this.callback);
65  		TestCallback.clearCallbacks();
66  		TesetHarnessExplodingQueue.NUM_CALLS = 0;
67  	}
68  
69  	@Override
70  	public void tearDown() throws Exception {
71  	    try {
72  		KSBServiceLocator.getScheduler().shutdown();
73  	    } finally {
74  		super.tearDown();
75  	    }
76  	}
77  
78  
79  
80  	/**
81  	 * test that service is in queue marked 'E' when the time to live is expired.
82  	 * @throws Exception
83  	 */
84  	@Test public void testTimeToLive() throws Exception {
85  
86  		KSBJavaService explodingQueue = (KSBJavaService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(this.queueTimeToLiveServiceName);
87  		explodingQueue.invoke("");
88  		TestUtilities.waitForExceptionRouting();
89  		//this service is on a 3 second wait the queue is on a 1 sec.
90  		Thread.sleep(10000);
91  
92  		//verify the entry is in exception routing
93  		List<PersistedMessageBO> messagesQueued = KSBServiceLocator.getMessageQueueService().findByServiceName(this.queueTimeToLiveServiceName, "invoke");
94  		PersistedMessageBO message = messagesQueued.get(0);
95  		assertEquals("Message should be in exception status", KSBConstants.ROUTE_QUEUE_EXCEPTION, message.getQueueStatus());
96  		assertTrue("Message expiration date should be equal to or earlier than last queue date", message.getExpirationDate().getTime() <= message.getQueueDate().getTime());
97  	}
98  
99  
100 }