2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.appc.adapter.rest.impl;
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;
37 * Test the RequestContext object
39 * The request context is used to track retries, recovery attempts, and time to live of the
40 * processing of a request.
44 public class RequestContextTest {
46 private RequestContext rc;
47 private Configuration config = ConfigurationFactory.getConfiguration();
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.
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);
62 * Ensure that we set up the property correctly
65 public void testRetryDelayProperty() {
66 assertEquals(1, rc.getRetryDelay());
70 * Ensure that we set up the property correctly
73 public void testRetryLimitProperty() {
74 assertEquals(3, rc.getRetryLimit());
78 * This test ensures that the retry attempt counter is zero on a new context
81 public void testRetryCountNoRetries() {
82 assertEquals(0, rc.getAttempts());
86 * Test that the delay is accurate
89 public void testDelay() {
90 long future = System.currentTimeMillis() + (rc.getRetryDelay() * 1000L);
94 assertTrue(System.currentTimeMillis() >= future);
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.
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());
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());
124 * The RequestContext tracks the number of retry attempts against the limit. This test verifies
125 * that tracking logic works correctly.
128 public void testCanRetry() {
129 internalTestCanRetry(true);
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.
139 public void testResetAndCanRetry() {
140 internalTestCanRetry(false);
142 internalTestCanRetry(true);
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.
152 public void testTimeToLive() {
153 assertTrue(rc.getTotalDuration() == 0L);
154 assertTrue(rc.isAlive());
157 assertTrue(rc.isAlive());
158 assertTrue(rc.getTotalDuration() >= 1000L);
162 assertTrue(rc.getTotalDuration() >= 2000L);
165 assertFalse(rc.isAlive());
166 assertTrue(rc.getTotalDuration() >= 3000L);
170 * Demonstrate setSvcLogicContext/getSvcLogicContext work as expected
173 public void testGetSvcLogicContext() {
174 assertNull(rc.getSvcLogicContext());
175 SvcLogicContext slc = new SvcLogicContext();
176 rc.setSvcLogicContext(slc);
177 assertEquals(slc, rc.getSvcLogicContext());