2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023-2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.aai;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.lenient;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 import jakarta.ws.rs.client.InvocationCallback;
36 import java.util.Arrays;
37 import java.util.List;
39 import java.util.TreeMap;
40 import java.util.concurrent.CompletableFuture;
41 import java.util.concurrent.ExecutionException;
42 import java.util.concurrent.TimeoutException;
43 import org.junit.jupiter.api.AfterAll;
44 import org.junit.jupiter.api.BeforeAll;
45 import org.junit.jupiter.api.BeforeEach;
46 import org.junit.jupiter.api.Test;
47 import org.junit.jupiter.api.extension.ExtendWith;
48 import org.mockito.junit.jupiter.MockitoExtension;
49 import org.onap.policy.aai.AaiConstants;
50 import org.onap.policy.aai.AaiCqResponse;
51 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
52 import org.onap.policy.common.utils.coder.StandardCoder;
53 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
54 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
55 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
56 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
57 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
59 @ExtendWith(MockitoExtension.class)
60 class AaiCustomQueryOperationTest extends BasicAaiOperation {
61 private static final StandardCoder coder = new StandardCoder();
63 private static final String MY_LINK = "my-link";
65 private AaiCustomQueryOperation oper;
67 AaiCustomQueryOperationTest() {
68 super(AaiConstants.ACTOR_NAME, AaiCustomQueryOperation.NAME);
72 static void setUpBeforeClass() throws Exception {
77 static void tearDownAfterClass() {
88 oper = new AaiCustomQueryOperation(params, config);
89 oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
93 * Tests "success" case with simulator.
96 void testSuccess() throws Exception {
97 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/query").build();
98 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
100 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
101 oper = new AaiCustomQueryOperation(params, config);
103 oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
105 outcome = oper.start().get();
106 assertEquals(OperationResult.SUCCESS, outcome.getResult());
108 assertNotNull(outcome.getResponse());
112 void testConstructor() {
113 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
114 assertEquals(AaiCustomQueryOperation.NAME, oper.getName());
118 void testGetPropertyNames() {
119 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_VSERVER_LINK));
123 void testGenerateSubRequestId() {
124 oper.generateSubRequestId(3);
125 assertEquals("3", oper.getSubRequestId());
129 @SuppressWarnings("unchecked")
130 void testStartOperationAsync_testMakeRequest() throws Exception {
131 // need two responses
132 when(rawResponse.readEntity(String.class)).thenReturn(makeTenantReply()).thenReturn(makeCqReply());
133 lenient().when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
134 when(webAsync.put(any(), any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse, 1));
136 CompletableFuture<OperationOutcome> future2 = oper.start();
138 assertEquals(OperationResult.SUCCESS, getResult(future2));
140 assertEquals("1", future2.get().getSubRequestId());
144 void testMakeHeaders() {
145 verifyHeaders(oper.makeHeaders());
149 @SuppressWarnings("unchecked")
150 void testMakeRequest_testGetVserverLink() throws Exception {
151 when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply());
152 when(webAsync.put(any(), any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse, 1));
155 executor.runAll(100);
157 verify(webAsync).put(requestCaptor.capture(), any(InvocationCallback.class));
159 String reqText = requestCaptor.getValue().getEntity();
160 Map<String, String> reqMap = coder.decode(reqText, Map.class);
162 // sort the request fields so they match the order in cq.json
163 Map<String, String> request = new TreeMap<>(reqMap);
165 verifyRequest("cq.json", request);
169 void testGetVserverLink() {
170 oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
171 assertEquals(MY_LINK, oper.getVserverLink());
175 void testSetOutcome() {
176 outcome = oper.setOutcome(params.makeOutcome(), OperationResult.SUCCESS, null, null);
177 assertNull(outcome.getResponse());
179 outcome = oper.setOutcome(params.makeOutcome(), OperationResult.SUCCESS, null, "{}");
180 assertInstanceOf(AaiCqResponse.class, outcome.getResponse());
183 private String makeTenantReply() throws Exception {
184 Map<String, String> links = Map.of(AaiCustomQueryOperation.RESOURCE_LINK, MY_LINK);
185 List<Map<String, String>> data = Arrays.asList(links);
187 Map<String, Object> reply = Map.of(AaiCustomQueryOperation.RESULT_DATA, data);
188 return coder.encode(reply);
191 private String makeCqReply() {
196 private OperationResult getResult(CompletableFuture<OperationOutcome> future2)
197 throws InterruptedException, ExecutionException {
199 executor.runAll(100);
200 assertTrue(future2.isDone());
202 return future2.get().getResult();