Add junit coverage for 2 RequestContext classes
[appc.git] / appc-adapters / appc-rest-adapter / appc-rest-adapter-bundle / src / test / java / org / onap / appc / adapter / rest / 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.rest.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.rest.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 by forcing the retry delay and limit to small values for the test
51      * and setting up the request context object.
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(2);
59     }
60
61     /**
62      * Ensure that we set up the property correctly
63      */
64     @Test
65     public void testRetryDelayProperty() {
66         assertEquals(1, rc.getRetryDelay());
67     }
68
69     /**
70      * Ensure that we set up the property correctly
71      */
72     @Test
73     public void testRetryLimitProperty() {
74         assertEquals(3, rc.getRetryLimit());
75     }
76
77     /**
78      * This test ensures that the retry attempt counter is zero on a new context
79      */
80     @Test
81     public void testRetryCountNoRetries() {
82         assertEquals(0, rc.getAttempts());
83     }
84
85     /**
86      * Test that the delay is accurate
87      */
88     @Test
89     public void testDelay() {
90         long future = System.currentTimeMillis() + (rc.getRetryDelay() * 1000L);
91
92         rc.delay();
93
94         assertTrue(System.currentTimeMillis() >= future);
95     }
96
97     /**
98      * The RequestContext tracks the number of retry attempts against the limit.
99      * This unannotated test verifies that the maximum number of retries can be attempted.
100      * With argument testPastLimit set to true, it demonstrates going beyond the limit fails.
101      */
102     private void internalTestCanRetry(boolean testPastLimit) {
103         assertEquals(0, rc.getAttempts());
104         assertTrue(rc.attempt());
105         assertFalse(rc.isFailed());
106         assertEquals(1, rc.getAttempts());
107         assertTrue(rc.attempt());
108         assertFalse(rc.isFailed());
109         assertEquals(2, rc.getAttempts());
110         assertTrue(rc.attempt());
111         assertFalse(rc.isFailed());
112         assertEquals(3, rc.getAttempts());
113         if (testPastLimit) {
114             assertFalse(rc.attempt());
115             assertTrue(rc.isFailed());
116             assertEquals(3, rc.getAttempts());
117             assertFalse(rc.attempt());
118             assertTrue(rc.isFailed());
119             assertEquals(3, rc.getAttempts());
120         }
121     }
122
123     /**
124      * The RequestContext tracks the number of retry attempts against the limit. This test verifies
125      * that tracking logic works correctly.
126      */
127     @Test
128     public void testCanRetry() {
129         internalTestCanRetry(true);
130     }
131
132     /**
133      * The same RequestContext is used throughout the processing, and retries need to be reset once
134      * successfully connected so that any earlier (successful) recoveries are not considered when
135      * performing any new future recoveries. This test ensures that a reset clears the retry counter
136      * and that we can attempt retries again up to the limit.
137      */
138     @Test
139     public void testResetAndCanRetry() {
140         internalTestCanRetry(false);
141         rc.reset();
142         internalTestCanRetry(true);
143     }
144
145     /**
146      * This test is used to test tracking of time to live for the request context. Because time is
147      * inexact, the assertions can only be ranges of values, such as at least some value or greater.
148      * The total duration tracking in the request context is only updated on each call to
149      * {@link RequestContext#isAlive()}. Also, durations are NOT affected by calls to reset.
150      */
151     @Test
152     public void testTimeToLive() {
153         assertTrue(rc.getTotalDuration() == 0L);
154         assertTrue(rc.isAlive());
155         rc.reset();
156         rc.delay();
157         assertTrue(rc.isAlive());
158         assertTrue(rc.getTotalDuration() >= 1000L);
159         rc.reset();
160         rc.delay();
161         rc.isAlive();
162         assertTrue(rc.getTotalDuration() >= 2000L);
163         rc.reset();
164         rc.delay();
165         assertFalse(rc.isAlive());
166         assertTrue(rc.getTotalDuration() >= 3000L);
167     }
168
169     /**
170      * Demonstrate setSvcLogicContext/getSvcLogicContext work as expected
171      */
172     @Test
173     public void testGetSvcLogicContext() {
174         assertNull(rc.getSvcLogicContext());
175         SvcLogicContext slc = new SvcLogicContext();
176         rc.setSvcLogicContext(slc);
177         assertEquals(slc, rc.getSvcLogicContext());
178     }
179 }