Change nexus values to properties
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / test / java / org / openecomp / appc / adapter / iaas / impl / TestRequestContext.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22
23 package org.openecomp.appc.adapter.iaas.impl;
24
25 import static org.junit.Assert.*;
26
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.openecomp.appc.Constants;
30 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
31 import org.openecomp.appc.configuration.Configuration;
32 import org.openecomp.appc.configuration.ConfigurationFactory;
33
34 /**
35  * Test the RequestContext object
36  * <p>
37  * The request context is used to track retries, recovery attempts, and time to live of the processing of a request.
38  * </p>
39  */
40
41 public class TestRequestContext {
42
43     private RequestContext rc;
44     private Configuration config = ConfigurationFactory.getConfiguration();
45
46     /**
47      * Set up the test environment by forcing the retry delay and limit to small values for the test and setting up the
48      * request context object.
49      */
50     @Before
51     public void setup() {
52         config.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
53         config.setProperty(Constants.PROPERTY_RETRY_LIMIT, "3");
54         rc = new RequestContext(null);
55         rc.setTimeToLiveSeconds(2);
56     }
57
58     /**
59      * Ensure that we set up the property correctly
60      */
61     @Test
62     public void testRetryDelayProperty() {
63         assertEquals(1, rc.getRetryDelay());
64     }
65
66     /**
67      * Ensure that we set up the property correctly
68      */
69     @Test
70     public void testRetryLimitProperty() {
71         assertEquals(3, rc.getRetryLimit());
72     }
73
74     /**
75      * This test ensures that the retry attempt counter is zero on a new context
76      */
77     @Test
78     public void testRetryCountNoRetries() {
79         assertEquals(0, rc.getAttempts());
80     }
81
82     /**
83      * Test that the delay is accurate
84      */
85     @Test
86     public void testDelay() {
87         long future = System.currentTimeMillis() + (rc.getRetryDelay() * 1000L);
88
89         rc.delay();
90
91         assertTrue(System.currentTimeMillis() >= future);
92     }
93
94     /**
95      * The RequestContext tracks the number of retry attempts against the limit. This test verifies that tracking logic
96      * works correctly.
97      */
98     @Test
99     public void testCanRetry() {
100         assertEquals(0, rc.getAttempts());
101         assertTrue(rc.attempt());
102         assertEquals(1, rc.getAttempts());
103         assertTrue(rc.attempt());
104         assertEquals(2, rc.getAttempts());
105         assertTrue(rc.attempt());
106         assertEquals(3, rc.getAttempts());
107         assertFalse(rc.attempt());
108         assertEquals(3, rc.getAttempts());
109         assertFalse(rc.attempt());
110         assertEquals(3, rc.getAttempts());
111         assertFalse(rc.attempt());
112         assertEquals(3, rc.getAttempts());
113     }
114
115     /**
116      * The same RequestContext is used throughout the processing, and retries need to be reset once successfully
117      * connected so that any earlier (successful) recoveries are not considered when performing any new future
118      * recoveries. This test ensures that a reset clears the retry counter and that we can attempt retries again up to
119      * the limit.
120      */
121     @Test
122     public void testResetAndCanRetry() {
123         assertTrue(rc.attempt());
124         assertTrue(rc.attempt());
125         assertTrue(rc.attempt());
126         rc.reset();
127
128         assertTrue(rc.attempt());
129         assertTrue(rc.attempt());
130         assertTrue(rc.attempt());
131         assertFalse(rc.attempt());
132         assertFalse(rc.attempt());
133         assertFalse(rc.attempt());
134     }
135
136     /**
137      * This test is used to test tracking of time to live for the request context. Because time is inexact, the
138      * assertions can only be ranges of values, such as at least some value or greater. The total duration tracking in
139      * the request context is only updated on each call to {@link RequestContext#isAlive()}. Also, durations are NOT
140      * affected by calls to reset.
141      */
142     @Test
143     public void testTimeToLive() {
144         assertTrue(rc.getTotalDuration() == 0L);
145         assertTrue(rc.isAlive());
146         rc.reset();
147         rc.delay();
148         assertTrue(rc.isAlive());
149         assertTrue(rc.getTotalDuration() >= 1000L);
150         rc.reset();
151         rc.delay();
152         rc.isAlive();
153         assertTrue(rc.getTotalDuration() >= 2000L);
154         rc.reset();
155         rc.delay();
156         assertFalse(rc.isAlive());
157         assertTrue(rc.getTotalDuration() >= 3000L);
158     }
159 }