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.assertFalse;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import org.junit.Test;
24  import org.kuali.rice.core.api.config.property.ConfigContext;
25  import org.kuali.rice.ksb.api.bus.support.JavaServiceConfiguration;
26  import org.kuali.rice.ksb.api.messaging.AsynchronousCall;
27  import org.kuali.rice.ksb.messaging.PersistedMessageBO;
28  import org.kuali.rice.ksb.messaging.PersistedMessagePayload;
29  import org.kuali.rice.ksb.test.KSBTestCase;
30  import org.kuali.rice.ksb.util.KSBConstants;
31  
32  
33  public class DefaultMessageExceptionHandlerTest extends KSBTestCase {
34      
35      private PersistedMessageBO setupMessage(Integer retriesAttempted, Integer serviceMaxRetries) throws Exception {
36          PersistedMessageBO message = new PersistedMessageBO();
37          message.setRetryCount(retriesAttempted);
38          //ServiceInfo serviceInfo = new ServiceInfo();
39          //serviceInfo.setServiceDefinition(new JavaServiceDefinition());
40          //serviceInfo.getServiceDefinition().setRetryAttempts(serviceMaxRetries);
41          JavaServiceConfiguration.Builder builder = JavaServiceConfiguration.Builder.create();
42          builder.setRetryAttempts(serviceMaxRetries);
43          AsynchronousCall methodCall = new AsynchronousCall(new Class[0], new Object[0], builder.build(), "", null, null);
44          message.setPayload(new PersistedMessagePayload(methodCall, message));
45          message.setMethodCall(methodCall);
46          return message;
47      }
48      
49      private void setMaxRetries(String maxRetries) {
50      	ConfigContext.getCurrentContextConfig().putProperty(KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_OVERRIDE_KEY, maxRetries);
51      }
52      
53      @Test public void testGetGlobalMaxRetryAttempts() throws Exception {
54          DefaultMessageExceptionHandler exceptionHandler = new DefaultMessageExceptionHandler();
55          
56          this.setMaxRetries("0");
57          
58          
59          //  test non-numeric
60          this.setMaxRetries("B");
61          assertNull("Method should return null if app constant is non-numeric.", exceptionHandler.getGlobalMaxRetryAttempts());
62          
63          //  test large negative number
64          this.setMaxRetries("-10");
65          assertNull("Method should return null if app constant is negative number.", exceptionHandler.getGlobalMaxRetryAttempts());
66          
67          //  test -1
68          this.setMaxRetries("-1");
69          assertNull("Method should return null if app constant is negative number.", exceptionHandler.getGlobalMaxRetryAttempts());
70          
71          //  test 0
72          this.setMaxRetries("0");
73          assertEquals("Method should return app constant value if app constant is numeric and greater than or equal to zero.", new Integer(0), exceptionHandler.getGlobalMaxRetryAttempts());
74          
75          //  test 1
76          this.setMaxRetries("1");
77          assertEquals("Method should return app constant value if app constant is numeric and greater than or equal to zero.", new Integer(1), exceptionHandler.getGlobalMaxRetryAttempts());
78          
79          //  test 5
80          this.setMaxRetries("5");
81          assertEquals("Method should return app constant value if app constant is numeric and greater than or equal to zero.", new Integer(5), exceptionHandler.getGlobalMaxRetryAttempts());
82      }
83      
84      @Test public void testIsInException() throws Exception {
85          DefaultMessageExceptionHandler exceptionHandler = new DefaultMessageExceptionHandler();
86          PersistedMessageBO message = null;
87          
88          this.setMaxRetries("-10");
89  
90          message = setupMessage(0, 1);
91          assertFalse(exceptionHandler.isInException(message));
92          
93          message.setRetryCount(1);
94          assertTrue(exceptionHandler.isInException(message));
95          
96          message.setRetryCount(2);
97          assertTrue(exceptionHandler.isInException(message));
98          
99          this.setMaxRetries("5");
100         
101         message.setRetryCount(4);
102         assertFalse(exceptionHandler.isInException(message));
103         
104         message.setRetryCount(5);
105         assertTrue(exceptionHandler.isInException(message));
106         
107         message.setRetryCount(6);
108         assertTrue(exceptionHandler.isInException(message));
109         
110         this.setMaxRetries("0");
111         
112         message.setRetryCount(0);
113         assertTrue(exceptionHandler.isInException(message));
114         
115         message.setRetryCount(1);
116         assertTrue(exceptionHandler.isInException(message));
117         
118         this.setMaxRetries("-1");
119         
120         message.setRetryCount(0);
121         assertFalse(exceptionHandler.isInException(message));
122         
123         message = setupMessage(1, 5);
124         assertFalse(exceptionHandler.isInException(message));
125         
126         this.setMaxRetries("B");
127         
128         message.setRetryCount(0);
129         assertFalse(exceptionHandler.isInException(message));
130     }
131 
132 }