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