2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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=========================================================
21 package org.onap.policy.controlloop.actor.aai;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.when;
32 import java.util.concurrent.CompletableFuture;
33 import javax.ws.rs.client.InvocationCallback;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.aai.AaiConstants;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.coder.StandardCoderObject;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
40 import org.onap.policy.controlloop.policy.PolicyResult;
42 public class AaiGetOperationTest extends BasicAaiOperation<Void> {
44 private static final String INPUT_FIELD = "input";
45 private static final String TEXT = "my-text";
47 private AaiGetOperation oper;
49 public AaiGetOperationTest() {
50 super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT);
57 public void setUp() throws Exception {
59 oper = new AaiGetOperation(params, config);
63 public void testGetRetry() {
64 // use default if null retry
65 assertEquals(AaiGetOperation.DEFAULT_RETRY, oper.getRetry(null));
67 // otherwise, use specified value
68 assertEquals(0, oper.getRetry(0));
69 assertEquals(10, oper.getRetry(10));
73 @SuppressWarnings("unchecked")
74 public void testStartOperationAsync_testStartQueryAsync_testPostProcessResponse() throws Exception {
76 // return a map in the reply
77 Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
78 when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
80 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
82 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
83 assertFalse(future2.isDone());
86 assertTrue(future2.isDone());
88 assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
90 // data should have been cached within the context
91 StandardCoderObject data = context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY));
93 assertEquals(TEXT, data.getString(INPUT_FIELD));
95 assertEquals("1", future2.get().getSubRequestId());
99 * Tests startOperationAsync() when there's a failure.
102 @SuppressWarnings("unchecked")
103 public void testStartOperationAsyncFailure() throws Exception {
105 when(rawResponse.getStatus()).thenReturn(500);
106 when(rawResponse.readEntity(String.class)).thenReturn("");
108 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
110 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
111 assertFalse(future2.isDone());
113 executor.runAll(100);
114 assertTrue(future2.isDone());
116 assertEquals(PolicyResult.FAILURE, future2.get().getResult());
118 // data should NOT have been cached within the context
119 assertNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)));
123 public void testMakeHeaders() {
124 verifyHeaders(oper.makeHeaders());
128 public void testAaiGetOperator() {
129 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
130 assertEquals(AaiGetOperation.TENANT, oper.getName());
134 public void testGetTenantKey() {
135 assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetOperation.getTenantKey(TARGET_ENTITY));