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.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
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.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.Arrays;
33 import java.util.List;
35 import java.util.TreeMap;
36 import java.util.concurrent.CompletableFuture;
37 import java.util.concurrent.ExecutionException;
38 import java.util.concurrent.TimeoutException;
39 import javax.ws.rs.client.InvocationCallback;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.onap.policy.aai.AaiConstants;
45 import org.onap.policy.aai.AaiCqResponse;
46 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
47 import org.onap.policy.common.utils.coder.StandardCoder;
48 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
49 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
50 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
51 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
52 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
54 public class AaiCustomQueryOperationTest extends BasicAaiOperation {
55 private static final StandardCoder coder = new StandardCoder();
57 private static final String MY_LINK = "my-link";
59 private AaiCustomQueryOperation oper;
61 public AaiCustomQueryOperationTest() {
62 super(AaiConstants.ACTOR_NAME, AaiCustomQueryOperation.NAME);
66 public static void setUpBeforeClass() throws Exception {
71 public static void tearDownAfterClass() {
79 public void setUp() throws Exception {
82 oper = new AaiCustomQueryOperation(params, config);
83 oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
87 * Tests "success" case with simulator.
90 public void testSuccess() throws Exception {
91 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/query").build();
92 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
94 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).preprocessed(true).build();
95 oper = new AaiCustomQueryOperation(params, config);
97 oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
99 outcome = oper.start().get();
100 assertEquals(OperationResult.SUCCESS, outcome.getResult());
102 assertNotNull(outcome.getResponse());
106 public void testConstructor() {
107 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
108 assertEquals(AaiCustomQueryOperation.NAME, oper.getName());
112 public void testGetPropertyNames() {
113 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_VSERVER_LINK));
117 public void testGenerateSubRequestId() {
118 oper.generateSubRequestId(3);
119 assertEquals("3", oper.getSubRequestId());
123 @SuppressWarnings("unchecked")
124 public void testStartOperationAsync_testMakeRequest() throws Exception {
125 // need two responses
126 when(rawResponse.readEntity(String.class)).thenReturn(makeTenantReply()).thenReturn(makeCqReply());
127 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
128 when(webAsync.put(any(), any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse, 1));
130 CompletableFuture<OperationOutcome> future2 = oper.start();
132 assertEquals(OperationResult.SUCCESS, getResult(future2));
134 assertEquals("1", future2.get().getSubRequestId());
138 public void testMakeHeaders() {
139 verifyHeaders(oper.makeHeaders());
143 @SuppressWarnings("unchecked")
144 public void testMakeRequest_testGetVserverLink() throws Exception {
145 when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply());
146 when(webAsync.put(any(), any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse, 1));
149 executor.runAll(100);
151 verify(webAsync).put(requestCaptor.capture(), any(InvocationCallback.class));
153 String reqText = requestCaptor.getValue().getEntity();
154 Map<String, String> reqMap = coder.decode(reqText, Map.class);
156 // sort the request fields so they match the order in cq.json
157 Map<String, String> request = new TreeMap<>(reqMap);
159 verifyRequest("cq.json", request);
163 public void testGetVserverLink() throws Exception {
164 oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
165 assertEquals(MY_LINK, oper.getVserverLink());
169 public void testSetOutcome() {
170 outcome = oper.setOutcome(params.makeOutcome(null), OperationResult.SUCCESS, null, null);
171 assertNull(outcome.getResponse());
173 outcome = oper.setOutcome(params.makeOutcome(null), OperationResult.SUCCESS, null, "{}");
174 assertTrue(outcome.getResponse() instanceof AaiCqResponse);
177 private String makeTenantReply() throws Exception {
178 Map<String, String> links = Map.of(AaiCustomQueryOperation.RESOURCE_LINK, MY_LINK);
179 List<Map<String, String>> data = Arrays.asList(links);
181 Map<String, Object> reply = Map.of(AaiCustomQueryOperation.RESULT_DATA, data);
182 return coder.encode(reply);
185 private String makeCqReply() {
190 private OperationResult getResult(CompletableFuture<OperationOutcome> future2)
191 throws InterruptedException, ExecutionException, TimeoutException {
193 executor.runAll(100);
194 assertTrue(future2.isDone());
196 return future2.get().getResult();