Reduce unit test duration from 40s to 4s
[appc.git] / appc-adapters / appc-rest-healthcheck-adapter / appc-rest-healthcheck-adapter-bundle / src / test / java / org / onap / appc / adapter / restHealthcheck / impl / RequestContextTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Ericsson
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23
24 package org.onap.appc.adapter.restHealthcheck.impl;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 // import org.onap.appc.Constants;
34 import org.onap.appc.adapter.restHealthcheck.impl.RequestContext;
35 // import org.onap.appc.configuration.Configuration;
36 // import org.onap.appc.configuration.ConfigurationFactory;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38 import org.powermock.reflect.Whitebox;
39
40 /**
41  * Test the RequestContext object
42  * <p>
43  * The request context is used to track retries, recovery attempts, and time to live of the
44  * processing of a request.
45  * </p>
46  */
47
48 public class RequestContextTest {
49
50     private RequestContext rc;
51     // private Configuration config = ConfigurationFactory.getConfiguration();
52
53     /**
54      * Set up the test environment.  Unfortunately, there is no means of overriding the wired-in retry delay
55      * and retry limit to small values for the test.
56      */
57     @Before
58     public void setup() {
59         // config.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
60         // config.setProperty(Constants.PROPERTY_RETRY_LIMIT, "3");
61         rc = Mockito.spy(new RequestContext(null));
62         Whitebox.setInternalState(rc, "retryDelay", 1);
63         rc.setTimeToLiveSeconds(rc.getRetryDelay() * 3);
64         // rc.setTimeToLiveMS(rc.getRetryDelay() * 3000L - 1L); // not quite adequate for 3 retries
65     }
66
67     /**
68      * Ensure that the retryDelay is as expected
69      */
70     @Test
71     public void testRetryDelayProperty() {
72         assertEquals(10, new RequestContext(null).getRetryDelay());
73     }
74
75     /**
76      * Ensure that the retryLimit is as expected
77      */
78     @Test
79     public void testRetryLimitProperty() {
80         assertEquals(10, rc.getRetryLimit());
81     }
82
83     /**
84      * This test ensures that the retry attempt counter is zero on a new context
85      */
86     @Test
87     public void testRetryCountNoRetries() {
88         assertEquals(0, rc.getAttempts());
89     }
90
91     /**
92      * Test that the delay is accurate
93      */
94     @Test
95     public void testDelay() {
96         long future = System.currentTimeMillis() + (rc.getRetryDelay() * 1000L);
97         rc.delay();
98         assertTrue(System.currentTimeMillis() >= future);
99     }
100
101     /**
102      * The RequestContext tracks the number of retry attempts against the limit.
103      * This unannotated test verifies that the maximum number of retries can be attempted.
104      * With argument testPastLimit set to true, it demonstrates going beyond the limit fails.
105      */
106     private void internalTestCanRetry(boolean testPastLimit) {
107         int i;
108         for (i = 0; i < rc.getRetryLimit(); ++i) {
109             assertEquals(i, rc.getAttempts());
110             assertTrue(rc.attempt());
111             assertFalse(rc.isFailed());
112         }
113         assertEquals(i, rc.getAttempts());
114         if (testPastLimit) {
115             assertFalse(rc.attempt());
116             assertTrue(rc.isFailed());
117             assertEquals(i, rc.getAttempts());
118             assertFalse(rc.attempt());
119             assertTrue(rc.isFailed());
120             assertEquals(i, rc.getAttempts());
121         }
122     }
123
124     /**
125      * The RequestContext tracks the number of retry attempts against the limit.
126      * This test verifies that tracking logic works correctly.
127      */
128     @Test
129     public void testCanRetry() {
130         internalTestCanRetry(true);
131     }
132
133     /**
134      * The same RequestContext is used throughout the processing, and retries need to be reset once
135      * successfully connected so that any earlier (successful) recoveries are not considered when
136      * performing any new future recoveries. This test ensures that a reset clears the retry counter
137      * and that we can attempt retries again up to the limit.
138      */
139     @Test
140     public void testResetAndCanRetry() {
141         internalTestCanRetry(false);
142         rc.reset();
143         internalTestCanRetry(true);
144     }
145
146     /**
147      * This test is used to test tracking of time to live for the request context. Because time is
148      * inexact, the assertions can only be ranges of values, such as at least some value or greater.
149      * The total duration tracking in the request context is only updated on each call to
150      * {@link RequestContext#isAlive()}. Also, durations are NOT affected by calls to reset.
151      */
152     @Test
153     public void testTimeToLive() {
154         assertTrue(rc.getTotalDuration() == 0L);
155         assertTrue(rc.isAlive());
156         long delay = rc.getRetryDelay() * 1000L;
157         rc.reset();
158         rc.delay();
159         assertTrue(rc.isAlive());
160         assertTrue(rc.getTotalDuration() >= 1 * delay);
161         rc.reset();
162         rc.delay();
163         rc.isAlive();
164         assertTrue(rc.getTotalDuration() >= 2 * delay);
165         rc.reset();
166         rc.delay();
167         // Ensure exceeded timeToLive
168         try {
169             Thread.sleep(1L);
170         } catch (Throwable e) {
171             // Even if interrupted, assume enough delay has occurred
172         }
173         assertFalse(rc.isAlive());
174         assertTrue(rc.getTotalDuration() >= 3 * delay);
175     }
176
177     /**
178      * Demonstrate setSvcLogicContext/getSvcLogicContext work as expected
179      */
180     @Test
181     public void testGetSvcLogicContext() {
182         assertNull(rc.getSvcLogicContext());
183         SvcLogicContext slc = new SvcLogicContext();
184         rc.setSvcLogicContext(slc);
185         assertEquals(slc, rc.getSvcLogicContext());
186     }
187 }