2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Ericsson
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.restHealthcheck.impl;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 // import org.onap.appc.Constants;
34 import org.onap.appc.adapter.restHealthcheck.impl.RequestContext;
35 // import org.onap.appc.configuration.Configuration;
36 // import org.onap.appc.configuration.ConfigurationFactory;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38 import org.powermock.reflect.Whitebox;
41 * Test the RequestContext object
43 * The request context is used to track retries, recovery attempts, and time to live of the
44 * processing of a request.
48 public class RequestContextTest {
50 private RequestContext rc;
51 // private Configuration config = ConfigurationFactory.getConfiguration();
54 * Set up the test environment. Unfortunately, there is no means of overriding the wired-in retry delay
55 * and retry limit to small values for the test.
59 // config.setProperty(Constants.PROPERTY_RETRY_DELAY, "1");
60 // config.setProperty(Constants.PROPERTY_RETRY_LIMIT, "3");
61 rc = Mockito.spy(new RequestContext(null));
62 Whitebox.setInternalState(rc, "retryDelay", 1);
63 rc.setTimeToLiveSeconds(rc.getRetryDelay() * 3);
64 // rc.setTimeToLiveMS(rc.getRetryDelay() * 3000L - 1L); // not quite adequate for 3 retries
68 * Ensure that the retryDelay is as expected
71 public void testRetryDelayProperty() {
72 assertEquals(10, new RequestContext(null).getRetryDelay());
76 * Ensure that the retryLimit is as expected
79 public void testRetryLimitProperty() {
80 assertEquals(10, rc.getRetryLimit());
84 * This test ensures that the retry attempt counter is zero on a new context
87 public void testRetryCountNoRetries() {
88 assertEquals(0, rc.getAttempts());
92 * Test that the delay is accurate
95 public void testDelay() {
96 long future = System.currentTimeMillis() + (rc.getRetryDelay() * 1000L);
98 assertTrue(System.currentTimeMillis() >= future);
102 * The RequestContext tracks the number of retry attempts against the limit.
103 * This unannotated test verifies that the maximum number of retries can be attempted.
104 * With argument testPastLimit set to true, it demonstrates going beyond the limit fails.
106 private void internalTestCanRetry(boolean testPastLimit) {
108 for (i = 0; i < rc.getRetryLimit(); ++i) {
109 assertEquals(i, rc.getAttempts());
110 assertTrue(rc.attempt());
111 assertFalse(rc.isFailed());
113 assertEquals(i, rc.getAttempts());
115 assertFalse(rc.attempt());
116 assertTrue(rc.isFailed());
117 assertEquals(i, rc.getAttempts());
118 assertFalse(rc.attempt());
119 assertTrue(rc.isFailed());
120 assertEquals(i, rc.getAttempts());
125 * The RequestContext tracks the number of retry attempts against the limit.
126 * This test verifies that tracking logic works correctly.
129 public void testCanRetry() {
130 internalTestCanRetry(true);
134 * The same RequestContext is used throughout the processing, and retries need to be reset once
135 * successfully connected so that any earlier (successful) recoveries are not considered when
136 * performing any new future recoveries. This test ensures that a reset clears the retry counter
137 * and that we can attempt retries again up to the limit.
140 public void testResetAndCanRetry() {
141 internalTestCanRetry(false);
143 internalTestCanRetry(true);
147 * This test is used to test tracking of time to live for the request context. Because time is
148 * inexact, the assertions can only be ranges of values, such as at least some value or greater.
149 * The total duration tracking in the request context is only updated on each call to
150 * {@link RequestContext#isAlive()}. Also, durations are NOT affected by calls to reset.
153 public void testTimeToLive() {
154 assertTrue(rc.getTotalDuration() == 0L);
155 assertTrue(rc.isAlive());
156 long delay = rc.getRetryDelay() * 1000L;
159 assertTrue(rc.isAlive());
160 assertTrue(rc.getTotalDuration() >= 1 * delay);
164 assertTrue(rc.getTotalDuration() >= 2 * delay);
167 // Ensure exceeded timeToLive
170 } catch (Throwable e) {
171 // Even if interrupted, assume enough delay has occurred
173 assertFalse(rc.isAlive());
174 assertTrue(rc.getTotalDuration() >= 3 * delay);
178 * Demonstrate setSvcLogicContext/getSvcLogicContext work as expected
181 public void testGetSvcLogicContext() {
182 assertNull(rc.getSvcLogicContext());
183 SvcLogicContext slc = new SvcLogicContext();
184 rc.setSvcLogicContext(slc);
185 assertEquals(slc, rc.getSvcLogicContext());