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.assertNotNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.when;
30 import java.util.Arrays;
31 import java.util.List;
33 import java.util.concurrent.CompletableFuture;
34 import java.util.concurrent.ExecutionException;
35 import java.util.concurrent.TimeoutException;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.onap.policy.aai.AaiConstants;
40 import org.onap.policy.aai.AaiCqResponse;
41 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.coder.StandardCoderObject;
44 import org.onap.policy.controlloop.actorserviceprovider.Operation;
45 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
46 import org.onap.policy.controlloop.actorserviceprovider.Util;
47 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
48 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
49 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
50 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
51 import org.onap.policy.controlloop.policy.PolicyResult;
53 public class AaiCustomQueryOperationTest extends BasicAaiOperation<Map<String, String>> {
54 private static final StandardCoder coder = new StandardCoder();
56 private static final String MY_LINK = "my-link";
59 private Actor tenantActor;
61 private AaiCustomQueryOperation oper;
63 public AaiCustomQueryOperationTest() {
64 super(AaiConstants.ACTOR_NAME, AaiCustomQueryOperation.NAME);
71 public void setUp() throws Exception {
74 MyTenantOperator tenantOperator = new MyTenantOperator();
76 when(service.getActor(AaiConstants.ACTOR_NAME)).thenReturn(tenantActor);
77 when(tenantActor.getOperator(AaiGetOperation.TENANT)).thenReturn(tenantOperator);
79 oper = new AaiCustomQueryOperation(params, operator);
83 public void testAaiCustomQueryOperation() {
84 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
85 assertEquals(AaiCustomQueryOperation.NAME, oper.getName());
89 public void testStartOperationAsync_testStartPreprocessorAsync_testMakeRequest_testPostProcess() throws Exception {
91 when(rawResponse.readEntity(String.class)).thenReturn(makeTenantReply()).thenReturn(makeCqReply());
92 when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
93 when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
95 CompletableFuture<OperationOutcome> future2 = oper.start();
97 assertEquals(PolicyResult.SUCCESS, getResult(future2));
99 // tenant response should have been cached within the context
100 assertNotNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)));
102 // custom query response should have been cached within the context
103 AaiCqResponse cqData = context.getProperty(AaiCqResponse.CONTEXT_KEY);
104 assertNotNull(cqData);
108 * Tests when preprocessor step is not needed.
111 public void testStartOperationAsync_testStartPreprocessorAsyncNotNeeded() throws Exception {
112 // pre-load the tenant data
113 final StandardCoderObject data = preloadTenantData();
115 // only need one response
116 when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply());
117 when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
119 CompletableFuture<OperationOutcome> future2 = oper.start();
121 assertEquals(PolicyResult.SUCCESS, getResult(future2));
123 // should not have replaced tenant response
124 assertSame(data, context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)));
126 // custom query response should have been cached within the context
127 AaiCqResponse cqData = context.getProperty(AaiCqResponse.CONTEXT_KEY);
128 assertNotNull(cqData);
132 public void testMakeHeaders() {
133 verifyHeaders(oper.makeHeaders());
137 public void testMakeRequestNoResourceLink() throws Exception {
138 // pre-load EMPTY tenant data
139 preloadTenantData(new StandardCoderObject());
141 when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply());
142 when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
144 CompletableFuture<OperationOutcome> future2 = oper.start();
146 assertEquals(PolicyResult.FAILURE_EXCEPTION, getResult(future2));
149 private String makeTenantReply() throws Exception {
150 Map<String, String> links = Map.of(AaiCustomQueryOperation.RESOURCE_LINK, MY_LINK);
151 List<Map<String, String>> data = Arrays.asList(links);
153 Map<String, Object> reply = Map.of(AaiCustomQueryOperation.RESULT_DATA, data);
154 return coder.encode(reply);
157 private String makeCqReply() {
161 private StandardCoderObject preloadTenantData() throws Exception {
162 StandardCoderObject data = coder.decode(makeTenantReply(), StandardCoderObject.class);
163 preloadTenantData(data);
167 private void preloadTenantData(StandardCoderObject data) {
168 context.setProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY), data);
171 private PolicyResult getResult(CompletableFuture<OperationOutcome> future2)
172 throws InterruptedException, ExecutionException, TimeoutException {
174 executor.runAll(100);
175 assertTrue(future2.isDone());
177 return future2.get().getResult();
180 protected class MyTenantOperator extends HttpOperator {
181 public MyTenantOperator() {
182 super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT);
184 HttpParams http = HttpParams.builder().clientName(MY_CLIENT).path(PATH).timeoutSec(1).build();
186 configure(Util.translateToMap(AaiGetOperation.TENANT, http));
191 public Operation buildOperation(ControlLoopOperationParams params) {
192 return new AaiGetOperation(params, this);
196 protected HttpClientFactory getClientFactory() {