Updating licenses in 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  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23
24 package org.openecomp.appc.adapter.iaas.impl;
25
26 import static org.junit.Assert.*;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.openecomp.appc.Constants;
31 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
32 import org.openecomp.appc.configuration.Configuration;
33 import org.openecomp.appc.configuration.ConfigurationFactory;
34
35 /**
36  * Test the RequestContext object
37  * <p>
38  * The request context is used to track retries, recovery attempts, and time to live of the processing of a request.
39  * </p>
40  */
41
42 public class TestRequestContext {
43
44     private RequestContext rc;
45     private Configuration config = ConfigurationFactory.getConfiguration();
46
47     /**
48      * Set up the test environment by forcing the retry delay and limit to small values for the test and setting up the
49      * request context object.
50      */
51     @Before
52     public void setup() {
53         config.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
54         config.setProperty(Constants.PROPERTY_RETRY_LIMIT, "3");
55         rc = new RequestContext(null);
56         rc.setTimeToLiveSeconds(2);
57     }
58
59     /**
60      * Ensure that we set up the property correctly
61      */
62     @Test
63     public void testRetryDelayProperty() {
64         assertEquals(1, rc.getRetryDelay());
65     }
66
67     /**
68      * Ensure that we set up the property correctly
69      */
70     @Test
71     public void testRetryLimitProperty() {
72         assertEquals(3, rc.getRetryLimit());
73     }
74
75     /**
76      * This test ensures that the retry attempt counter is zero on a new context
77      */
78     @Test
79     public void testRetryCountNoRetries() {
80         assertEquals(0, rc.getAttempts());
81     }
82
83     /**
84      * Test that the delay is accurate
85      */
86     @Test
87     public void testDelay() {
88         long future = System.currentTimeMillis() + (rc.getRetryDelay() * 1000L);
89
90         rc.delay();
91
92         assertTrue(System.currentTimeMillis() >= future);
93     }
94
95     /**
96      * The RequestContext tracks the number of retry attempts against the limit. This test verifies that tracking logic
97      * works correctly.
98      */
99     @Test
100     public void testCanRetry() {
101         assertEquals(0, rc.getAttempts());
102         assertTrue(rc.attempt());
103         assertEquals(1, rc.getAttempts());
104         assertTrue(rc.attempt());
105         assertEquals(2, rc.getAttempts());
106         assertTrue(rc.attempt());
107         assertEquals(3, rc.getAttempts());
108         assertFalse(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     }
115
116     /**
117      * The same RequestContext is used throughout the processing, and retries need to be reset once successfully
118      * connected so that any earlier (successful) recoveries are not considered when performing any new future
119      * recoveries. This test ensures that a reset clears the retry counter and that we can attempt retries again up to
120      * the limit.
121      */
122     @Test
123     public void testResetAndCanRetry() {
124         assertTrue(rc.attempt());
125         assertTrue(rc.attempt());
126         assertTrue(rc.attempt());
127         rc.reset();
128
129         assertTrue(rc.attempt());
130         assertTrue(rc.attempt());
131         assertTrue(rc.attempt());
132         assertFalse(rc.attempt());
133         assertFalse(rc.attempt());
134         assertFalse(rc.attempt());
135     }
136
137     /**
138      * This test is used to test tracking of time to live for the request context. Because time is inexact, the
139      * assertions can only be ranges of values, such as at least some value or greater. The total duration tracking in
140      * the request context is only updated on each call to {@link RequestContext#isAlive()}. Also, durations are NOT
141      * affected by calls to reset.
142      */
143     @Test
144     public void testTimeToLive() {
145         assertTrue(rc.getTotalDuration() == 0L);
146         assertTrue(rc.isAlive());
147         rc.reset();
148         rc.delay();
149         assertTrue(rc.isAlive());
150         assertTrue(rc.getTotalDuration() >= 1000L);
151         rc.reset();
152         rc.delay();
153         rc.isAlive();
154         assertTrue(rc.getTotalDuration() >= 2000L);
155         rc.reset();
156         rc.delay();
157         assertFalse(rc.isAlive());
158         assertTrue(rc.getTotalDuration() >= 3000L);
159     }
160 }