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.restHealthcheck.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.restHealthcheck.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. Unfortunately, there is no means of overriding the wired-in retry delay
51 * and retry limit to small values for the test.
55 // config.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
56 // config.setProperty(Constants.PROPERTY_RETRY_LIMIT, "3");
57 rc = new RequestContext(null);
58 rc.setTimeToLiveSeconds(rc.getRetryDelay() * 3);
59 // rc.setTimeToLiveMS(rc.getRetryDelay() * 3000L - 1L); // not quite adequate for 3 retries
63 * Ensure that the retryDelay is as expected
66 public void testRetryDelayProperty() {
67 assertEquals(10, rc.getRetryDelay());
71 * Ensure that the retryLimit is as expected
74 public void testRetryLimitProperty() {
75 assertEquals(10, rc.getRetryLimit());
79 * This test ensures that the retry attempt counter is zero on a new context
82 public void testRetryCountNoRetries() {
83 assertEquals(0, rc.getAttempts());
87 * Test that the delay is accurate
90 public void testDelay() {
91 long future = System.currentTimeMillis() + (rc.getRetryDelay() * 1000L);
95 assertTrue(System.currentTimeMillis() >= future);
99 * The RequestContext tracks the number of retry attempts against the limit.
100 * This unannotated test verifies that the maximum number of retries can be attempted.
101 * With argument testPastLimit set to true, it demonstrates going beyond the limit fails.
103 private void internalTestCanRetry(boolean testPastLimit) {
105 for (i = 0; i < rc.getRetryLimit(); ++i) {
106 assertEquals(i, rc.getAttempts());
107 assertTrue(rc.attempt());
108 assertFalse(rc.isFailed());
110 assertEquals(i, rc.getAttempts());
112 assertFalse(rc.attempt());
113 assertTrue(rc.isFailed());
114 assertEquals(i, rc.getAttempts());
115 assertFalse(rc.attempt());
116 assertTrue(rc.isFailed());
117 assertEquals(i, rc.getAttempts());
122 * The RequestContext tracks the number of retry attempts against the limit.
123 * This test verifies that tracking logic works correctly.
126 public void testCanRetry() {
127 internalTestCanRetry(true);
131 * The same RequestContext is used throughout the processing, and retries need to be reset once
132 * successfully connected so that any earlier (successful) recoveries are not considered when
133 * performing any new future recoveries. This test ensures that a reset clears the retry counter
134 * and that we can attempt retries again up to the limit.
137 public void testResetAndCanRetry() {
138 internalTestCanRetry(false);
140 internalTestCanRetry(true);
144 * This test is used to test tracking of time to live for the request context. Because time is
145 * inexact, the assertions can only be ranges of values, such as at least some value or greater.
146 * The total duration tracking in the request context is only updated on each call to
147 * {@link RequestContext#isAlive()}. Also, durations are NOT affected by calls to reset.
150 public void testTimeToLive() {
151 assertTrue(rc.getTotalDuration() == 0L);
152 assertTrue(rc.isAlive());
153 long delay = rc.getRetryDelay() * 1000L;
156 assertTrue(rc.isAlive());
157 assertTrue(rc.getTotalDuration() >= 1 * delay);
161 assertTrue(rc.getTotalDuration() >= 2 * delay);
164 // Ensure exceeded timeToLive
167 } catch (Throwable e) {
168 // Even if interrupted, assume enough delay has occurred
170 assertFalse(rc.isAlive());
171 assertTrue(rc.getTotalDuration() >= 3 * delay);
175 * Demonstrate setSvcLogicContext/getSvcLogicContext work as expected
178 public void testGetSvcLogicContext() {
179 assertNull(rc.getSvcLogicContext());
180 SvcLogicContext slc = new SvcLogicContext();
181 rc.setSvcLogicContext(slc);
182 assertEquals(slc, rc.getSvcLogicContext());