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.mockito.ArgumentMatchers.any;
 
  27 import static org.mockito.Mockito.when;
 
  29 import java.util.Arrays;
 
  30 import java.util.List;
 
  32 import java.util.concurrent.CompletableFuture;
 
  33 import java.util.concurrent.TimeUnit;
 
  34 import org.junit.Before;
 
  35 import org.junit.Test;
 
  36 import org.mockito.Mock;
 
  37 import org.onap.policy.aai.AaiConstants;
 
  38 import org.onap.policy.aai.AaiCqResponse;
 
  39 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
 
  40 import org.onap.policy.common.utils.coder.StandardCoder;
 
  41 import org.onap.policy.common.utils.coder.StandardCoderObject;
 
  42 import org.onap.policy.controlloop.actorserviceprovider.Operation;
 
  43 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
 
  44 import org.onap.policy.controlloop.actorserviceprovider.Util;
 
  45 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
 
  46 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 
  47 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
 
  48 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
 
  49 import org.onap.policy.controlloop.policy.PolicyResult;
 
  51 public class AaiCustomQueryOperationTest extends BasicAaiOperator<Map<String, String>> {
 
  52     private static final StandardCoder coder = new StandardCoder();
 
  54     private static final String MY_LINK = "my-link";
 
  57     private Actor tenantActor;
 
  59     private AaiCustomQueryOperation oper;
 
  61     public AaiCustomQueryOperationTest() {
 
  62         super(AaiConstants.ACTOR_NAME, AaiCustomQueryOperation.NAME);
 
  69     public void setUp() throws Exception {
 
  72         MyTenantOperator tenantOperator = new MyTenantOperator();
 
  74         when(service.getActor(AaiConstants.ACTOR_NAME)).thenReturn(tenantActor);
 
  75         when(tenantActor.getOperator(AaiGetOperation.TENANT)).thenReturn(tenantOperator);
 
  77         oper = new AaiCustomQueryOperation(params, operator);
 
  81     public void testAaiCustomQueryOperation() {
 
  82         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
 
  83         assertEquals(AaiCustomQueryOperation.NAME, oper.getName());
 
  87     public void testStartOperationAsync_testStartPreprocessorAsync_testMakeRequest_testPostProcess() throws Exception {
 
  89         when(rawResponse.readEntity(String.class)).thenReturn(makeTenantReply()).thenReturn(makeCqReply());
 
  90         when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
 
  91         when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
 
  93         CompletableFuture<OperationOutcome> future2 = oper.start();
 
  95         assertEquals(PolicyResult.SUCCESS, future2.get(5, TimeUnit.SECONDS).getResult());
 
  97         // tenant response should have been cached within the context
 
  98         assertNotNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)));
 
 100         // custom query response should have been cached within the context
 
 101         AaiCqResponse cqData = context.getProperty(AaiCqResponse.CONTEXT_KEY);
 
 102         assertNotNull(cqData);
 
 106      * Tests when preprocessor step is not needed.
 
 109     public void testStartOperationAsync_testStartPreprocessorAsyncNotNeeded() throws Exception {
 
 110         // pre-load the tenant data
 
 111         final StandardCoderObject data = preloadTenantData();
 
 113         // only need one response
 
 114         when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply());
 
 115         when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
 
 117         CompletableFuture<OperationOutcome> future2 = oper.start();
 
 119         assertEquals(PolicyResult.SUCCESS, future2.get(5, TimeUnit.SECONDS).getResult());
 
 121         // should not have replaced tenant response
 
 122         assertSame(data, context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)));
 
 124         // custom query response should have been cached within the context
 
 125         AaiCqResponse cqData = context.getProperty(AaiCqResponse.CONTEXT_KEY);
 
 126         assertNotNull(cqData);
 
 130     public void testMakeHeaders() {
 
 131         verifyHeaders(oper.makeHeaders());
 
 135     public void testMakeRequestNoResourceLink() throws Exception {
 
 136         // pre-load EMPTY tenant data
 
 137         preloadTenantData(new StandardCoderObject());
 
 139         when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply());
 
 140         when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
 
 142         CompletableFuture<OperationOutcome> future2 = oper.start();
 
 144         assertEquals(PolicyResult.FAILURE_EXCEPTION, future2.get(5, TimeUnit.SECONDS).getResult());
 
 147     private String makeTenantReply() throws Exception {
 
 148         Map<String, String> links = Map.of(AaiCustomQueryOperation.RESOURCE_LINK, MY_LINK);
 
 149         List<Map<String, String>> data = Arrays.asList(links);
 
 151         Map<String, Object> reply = Map.of(AaiCustomQueryOperation.RESULT_DATA, data);
 
 152         return coder.encode(reply);
 
 155     private String makeCqReply() {
 
 159     private StandardCoderObject preloadTenantData() throws Exception {
 
 160         StandardCoderObject data = coder.decode(makeTenantReply(), StandardCoderObject.class);
 
 161         preloadTenantData(data);
 
 165     private void preloadTenantData(StandardCoderObject data) {
 
 166         context.setProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY), data);
 
 169     protected class MyTenantOperator extends HttpOperator {
 
 170         public MyTenantOperator() {
 
 171             super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT);
 
 173             HttpParams http = HttpParams.builder().clientName(MY_CLIENT).path(PATH).build();
 
 175             configure(Util.translateToMap(AaiGetOperation.TENANT, http));
 
 180         public Operation buildOperation(ControlLoopOperationParams params) {
 
 181             return new AaiGetOperation(params, this);
 
 185         protected HttpClientFactory getClientFactory() {