2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 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.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.lenient;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import java.util.Arrays;
35 import java.util.List;
37 import java.util.TreeMap;
38 import java.util.concurrent.CompletableFuture;
39 import java.util.concurrent.ExecutionException;
40 import java.util.concurrent.TimeoutException;
41 import javax.ws.rs.client.InvocationCallback;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.junit.MockitoJUnitRunner;
48 import org.onap.policy.aai.AaiConstants;
49 import org.onap.policy.aai.AaiCqResponse;
50 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
51 import org.onap.policy.common.utils.coder.StandardCoder;
52 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
53 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
54 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
55 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
56 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
58 @RunWith(MockitoJUnitRunner.class)
59 public class AaiCustomQueryOperationTest extends BasicAaiOperation {
60 private static final StandardCoder coder = new StandardCoder();
62 private static final String MY_LINK = "my-link";
64 private AaiCustomQueryOperation oper;
66 public AaiCustomQueryOperationTest() {
67 super(AaiConstants.ACTOR_NAME, AaiCustomQueryOperation.NAME);
71 public static void setUpBeforeClass() throws Exception {
76 public static void tearDownAfterClass() {
84 public void setUp() throws Exception {
87 oper = new AaiCustomQueryOperation(params, config);
88 oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
92 * Tests "success" case with simulator.
95 public void testSuccess() throws Exception {
96 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/query").build();
97 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
99 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
100 oper = new AaiCustomQueryOperation(params, config);
102 oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
104 outcome = oper.start().get();
105 assertEquals(OperationResult.SUCCESS, outcome.getResult());
107 assertNotNull(outcome.getResponse());
111 public void testConstructor() {
112 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
113 assertEquals(AaiCustomQueryOperation.NAME, oper.getName());
117 public void testGetPropertyNames() {
118 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_VSERVER_LINK));
122 public void testGenerateSubRequestId() {
123 oper.generateSubRequestId(3);
124 assertEquals("3", oper.getSubRequestId());
128 @SuppressWarnings("unchecked")
129 public void testStartOperationAsync_testMakeRequest() throws Exception {
130 // need two responses
131 when(rawResponse.readEntity(String.class)).thenReturn(makeTenantReply()).thenReturn(makeCqReply());
132 lenient().when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
133 when(webAsync.put(any(), any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse, 1));
135 CompletableFuture<OperationOutcome> future2 = oper.start();
137 assertEquals(OperationResult.SUCCESS, getResult(future2));
139 assertEquals("1", future2.get().getSubRequestId());
143 public void testMakeHeaders() {
144 verifyHeaders(oper.makeHeaders());
148 @SuppressWarnings("unchecked")
149 public void testMakeRequest_testGetVserverLink() throws Exception {
150 when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply());
151 when(webAsync.put(any(), any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse, 1));
154 executor.runAll(100);
156 verify(webAsync).put(requestCaptor.capture(), any(InvocationCallback.class));
158 String reqText = requestCaptor.getValue().getEntity();
159 Map<String, String> reqMap = coder.decode(reqText, Map.class);
161 // sort the request fields so they match the order in cq.json
162 Map<String, String> request = new TreeMap<>(reqMap);
164 verifyRequest("cq.json", request);
168 public void testGetVserverLink() throws Exception {
169 oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
170 assertEquals(MY_LINK, oper.getVserverLink());
174 public void testSetOutcome() {
175 outcome = oper.setOutcome(params.makeOutcome(), OperationResult.SUCCESS, null, null);
176 assertNull(outcome.getResponse());
178 outcome = oper.setOutcome(params.makeOutcome(), OperationResult.SUCCESS, null, "{}");
179 assertTrue(outcome.getResponse() instanceof AaiCqResponse);
182 private String makeTenantReply() throws Exception {
183 Map<String, String> links = Map.of(AaiCustomQueryOperation.RESOURCE_LINK, MY_LINK);
184 List<Map<String, String>> data = Arrays.asList(links);
186 Map<String, Object> reply = Map.of(AaiCustomQueryOperation.RESULT_DATA, data);
187 return coder.encode(reply);
190 private String makeCqReply() {
195 private OperationResult getResult(CompletableFuture<OperationOutcome> future2)
196 throws InterruptedException, ExecutionException, TimeoutException {
198 executor.runAll(100);
199 assertTrue(future2.isDone());
201 return future2.get().getResult();