Applying license changes to all files
[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  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
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  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25
26 package org.openecomp.appc.adapter.iaas.impl;
27
28 import static org.junit.Assert.*;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.openecomp.appc.Constants;
33 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
34 import org.openecomp.appc.configuration.Configuration;
35 import org.openecomp.appc.configuration.ConfigurationFactory;
36
37 /**
38  * Test the RequestContext object
39  * <p>
40  * The request context is used to track retries, recovery attempts, and time to live of the processing of a request.
41  * </p>
42  */
43
44 public class TestRequestContext {
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 and setting up the
51      * 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. This test verifies that tracking logic
99      * works correctly.
100      */
101     @Test
102     public void testCanRetry() {
103         assertEquals(0, rc.getAttempts());
104         assertTrue(rc.attempt());
105         assertEquals(1, rc.getAttempts());
106         assertTrue(rc.attempt());
107         assertEquals(2, rc.getAttempts());
108         assertTrue(rc.attempt());
109         assertEquals(3, rc.getAttempts());
110         assertFalse(rc.attempt());
111         assertEquals(3, rc.getAttempts());
112         assertFalse(rc.attempt());
113         assertEquals(3, rc.getAttempts());
114         assertFalse(rc.attempt());
115         assertEquals(3, rc.getAttempts());
116     }
117
118     /**
119      * The same RequestContext is used throughout the processing, and retries need to be reset once successfully
120      * connected so that any earlier (successful) recoveries are not considered when performing any new future
121      * recoveries. This test ensures that a reset clears the retry counter and that we can attempt retries again up to
122      * the limit.
123      */
124     @Test
125     public void testResetAndCanRetry() {
126         assertTrue(rc.attempt());
127         assertTrue(rc.attempt());
128         assertTrue(rc.attempt());
129         rc.reset();
130
131         assertTrue(rc.attempt());
132         assertTrue(rc.attempt());
133         assertTrue(rc.attempt());
134         assertFalse(rc.attempt());
135         assertFalse(rc.attempt());
136         assertFalse(rc.attempt());
137     }
138
139     /**
140      * This test is used to test tracking of time to live for the request context. Because time is inexact, the
141      * assertions can only be ranges of values, such as at least some value or greater. The total duration tracking in
142      * the request context is only updated on each call to {@link RequestContext#isAlive()}. Also, durations are NOT
143      * affected by calls to reset.
144      */
145     @Test
146     public void testTimeToLive() {
147         assertTrue(rc.getTotalDuration() == 0L);
148         assertTrue(rc.isAlive());
149         rc.reset();
150         rc.delay();
151         assertTrue(rc.isAlive());
152         assertTrue(rc.getTotalDuration() >= 1000L);
153         rc.reset();
154         rc.delay();
155         rc.isAlive();
156         assertTrue(rc.getTotalDuration() >= 2000L);
157         rc.reset();
158         rc.delay();
159         assertFalse(rc.isAlive());
160         assertTrue(rc.getTotalDuration() >= 3000L);
161     }
162 }