aboutsummaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/openecomp/appc/adapter/iaas/impl/TestRequestContext.java
blob: efb40dfac9e600e4f2d143cd2d9fe88e1ab4fdc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */


package org.openecomp.appc.adapter.iaas.impl;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.openecomp.appc.Constants;
import org.openecomp.appc.adapter.iaas.impl.RequestContext;
import org.openecomp.appc.configuration.Configuration;
import org.openecomp.appc.configuration.ConfigurationFactory;

/**
 * Test the RequestContext object
 * <p>
 * The request context is used to track retries, recovery attempts, and time to live of the
 * processing of a request.
 * </p>
 */

public class TestRequestContext {

    private RequestContext rc;
    private Configuration config = ConfigurationFactory.getConfiguration();

    /**
     * Set up the test environment by forcing the retry delay and limit to small values for the test
     * and setting up the request context object.
     */
    @Before
    public void setup() {
        config.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
        config.setProperty(Constants.PROPERTY_RETRY_LIMIT, "3");
        rc = new RequestContext(null);
        rc.setTimeToLiveSeconds(2);
    }

    /**
     * Ensure that we set up the property correctly
     */
    @Test
    public void testRetryDelayProperty() {
        assertEquals(1, rc.getRetryDelay());
    }

    /**
     * Ensure that we set up the property correctly
     */
    @Test
    public void testRetryLimitProperty() {
        assertEquals(3, rc.getRetryLimit());
    }

    /**
     * This test ensures that the retry attempt counter is zero on a new context
     */
    @Test
    public void testRetryCountNoRetries() {
        assertEquals(0, rc.getAttempts());
    }

    /**
     * Test that the delay is accurate
     */
    @Test
    public void testDelay() {
        long future = System.currentTimeMillis() + (rc.getRetryDelay() * 1000L);

        rc.delay();

        assertTrue(System.currentTimeMillis() >= future);
    }

    /**
     * The RequestContext tracks the number of retry attempts against the limit. This test verifies
     * that tracking logic works correctly.
     */
    @Test
    public void testCanRetry() {
        assertEquals(0, rc.getAttempts());
        assertTrue(rc.attempt());
        assertEquals(1, rc.getAttempts());
        assertTrue(rc.attempt());
        assertEquals(2, rc.getAttempts());
        assertTrue(rc.attempt());
        assertEquals(3, rc.getAttempts());
        assertFalse(rc.attempt());
        assertEquals(3, rc.getAttempts());
        assertFalse(rc.attempt());
        assertEquals(3, rc.getAttempts());
        assertFalse(rc.attempt());
        assertEquals(3, rc.getAttempts());
    }

    /**
     * The same RequestContext is used throughout the processing, and retries need to be reset once
     * successfully connected so that any earlier (successful) recoveries are not considered when
     * performing any new future recoveries. This test ensures that a reset clears the retry counter
     * and that we can attempt retries again up to the limit.
     */
    @Test
    public void testResetAndCanRetry() {
        assertTrue(rc.attempt());
        assertTrue(rc.attempt());
        assertTrue(rc.attempt());
        rc.reset();

        assertTrue(rc.attempt());
        assertTrue(rc.attempt());
        assertTrue(rc.attempt());
        assertFalse(rc.attempt());
        assertFalse(rc.attempt());
        assertFalse(rc.attempt());
    }

    /**
     * This test is used to test tracking of time to live for the request context. Because time is
     * inexact, the assertions can only be ranges of values, such as at least some value or greater.
     * The total duration tracking in the request context is only updated on each call to
     * {@link RequestContext#isAlive()}. Also, durations are NOT affected by calls to reset.
     */
    @Test
    public void testTimeToLive() {
        assertTrue(rc.getTotalDuration() == 0L);
        assertTrue(rc.isAlive());
        rc.reset();
        rc.delay();
        assertTrue(rc.isAlive());
        assertTrue(rc.getTotalDuration() >= 1000L);
        rc.reset();
        rc.delay();
        rc.isAlive();
        assertTrue(rc.getTotalDuration() >= 2000L);
        rc.reset();
        rc.delay();
        assertFalse(rc.isAlive());
        assertTrue(rc.getTotalDuration() >= 3000L);
    }
}