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.assertFalse;
 
  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.when;
 
  32 import java.util.concurrent.CompletableFuture;
 
  33 import org.junit.Before;
 
  34 import org.junit.Test;
 
  35 import org.onap.policy.aai.AaiConstants;
 
  36 import org.onap.policy.common.utils.coder.StandardCoder;
 
  37 import org.onap.policy.common.utils.coder.StandardCoderObject;
 
  38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
 
  39 import org.onap.policy.controlloop.policy.PolicyResult;
 
  41 public class AaiGetOperationTest extends BasicAaiOperation<Void> {
 
  43     private static final String INPUT_FIELD = "input";
 
  44     private static final String TEXT = "my-text";
 
  46     private AaiGetOperation oper;
 
  48     public AaiGetOperationTest() {
 
  49         super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT);
 
  56     public void setUp() throws Exception {
 
  58         oper = new AaiGetOperation(params, operator);
 
  62     public void testGetRetry() {
 
  63         // use default if null retry
 
  64         assertEquals(AaiGetOperation.DEFAULT_RETRY, oper.getRetry(null));
 
  66         // otherwise, use specified value
 
  67         assertEquals(0, oper.getRetry(0));
 
  68         assertEquals(10, oper.getRetry(10));
 
  72     public void testStartOperationAsync_testStartQueryAsync_testPostProcessResponse() throws Exception {
 
  74         // return a map in the reply
 
  75         Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
 
  76         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
 
  78         when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
 
  80         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
 
  81         assertFalse(future2.isDone());
 
  84         assertTrue(future2.isDone());
 
  86         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
 
  88         // data should have been cached within the context
 
  89         StandardCoderObject data = context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY));
 
  91         assertEquals(TEXT, data.getString(INPUT_FIELD));
 
  95      * Tests startOperationAsync() when there's a failure.
 
  98     public void testStartOperationAsyncFailure() throws Exception {
 
 100         when(rawResponse.getStatus()).thenReturn(500);
 
 101         when(rawResponse.readEntity(String.class)).thenReturn("");
 
 103         when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
 
 105         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
 
 106         assertFalse(future2.isDone());
 
 108         executor.runAll(100);
 
 109         assertTrue(future2.isDone());
 
 111         assertEquals(PolicyResult.FAILURE, future2.get().getResult());
 
 113         // data should NOT have been cached within the context
 
 114         assertNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)));
 
 118     public void testMakeHeaders() {
 
 119         verifyHeaders(oper.makeHeaders());
 
 123     public void testMakePath() {
 
 124         assertEquals(PATH + "/" + TARGET_ENTITY, oper.makePath());
 
 128     public void testAaiGetOperator() {
 
 129         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
 
 130         assertEquals(AaiGetOperation.TENANT, oper.getName());
 
 134     public void testGetTenantKey() {
 
 135         assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetOperation.getTenantKey(TARGET_ENTITY));