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.assertFalse;
 
  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.List;
 
  32 import java.util.concurrent.CompletableFuture;
 
  33 import javax.ws.rs.client.InvocationCallback;
 
  34 import org.junit.AfterClass;
 
  35 import org.junit.Before;
 
  36 import org.junit.BeforeClass;
 
  37 import org.junit.Test;
 
  38 import org.onap.policy.aai.AaiConstants;
 
  39 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
 
  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.OperationOutcome;
 
  43 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
 
  44 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
 
  45 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
 
  46 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
 
  48 public class AaiGetTenantOperationTest extends BasicAaiOperation {
 
  49     private static final String INPUT_FIELD = "input";
 
  50     private static final String TEXT = "my-text";
 
  52     private AaiGetTenantOperation oper;
 
  54     public AaiGetTenantOperationTest() {
 
  55         super(AaiConstants.ACTOR_NAME, AaiGetTenantOperation.NAME);
 
  59     public static void setUpBeforeClass() throws Exception {
 
  64     public static void tearDownAfterClass() {
 
  72     public void setUp() throws Exception {
 
  74         oper = new AaiGetTenantOperation(params, config);
 
  75         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
 
  79     public void testConstructor() {
 
  80         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
 
  81         assertEquals(AaiGetTenantOperation.NAME, oper.getName());
 
  85     public void testGetPropertyNames() {
 
  86         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
 
  90      * Tests "success" case with simulator.
 
  93     public void testSuccess() throws Exception {
 
  94         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
 
  95         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
 
  97         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
 
  98         oper = new AaiGetTenantOperation(params, config);
 
  99         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "OzVServer");
 
 101         outcome = oper.start().get();
 
 102         assertEquals(OperationResult.SUCCESS, outcome.getResult());
 
 103         assertTrue(outcome.getResponse() instanceof StandardCoderObject);
 
 107      * Tests "failure" case with simulator.
 
 110     public void testFailure() throws Exception {
 
 111         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
 
 112         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
 
 114         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
 
 115         oper = new AaiGetTenantOperation(params, config);
 
 116         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "failedVserver");
 
 118         outcome = oper.start().get();
 
 119         assertEquals(OperationResult.FAILURE, outcome.getResult());
 
 123     @SuppressWarnings("unchecked")
 
 124     public void testStartOperationAsync_testStartQueryAsync() throws Exception {
 
 126         // return a map in the reply
 
 127         Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
 
 128         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
 
 130         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
 
 132         oper.generateSubRequestId(1);
 
 133         outcome.setSubRequestId(oper.getSubRequestId());
 
 135         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
 
 136         assertFalse(future2.isDone());
 
 138         executor.runAll(100);
 
 139         assertTrue(future2.isDone());
 
 141         assertEquals(OperationResult.SUCCESS, future2.get().getResult());
 
 143         assertEquals("1", future2.get().getSubRequestId());
 
 147      * Tests startOperationAsync() when there's a failure.
 
 150     @SuppressWarnings("unchecked")
 
 151     public void testStartOperationAsyncFailure() throws Exception {
 
 153         when(rawResponse.getStatus()).thenReturn(500);
 
 154         when(rawResponse.readEntity(String.class)).thenReturn("");
 
 156         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
 
 158         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
 
 159         assertFalse(future2.isDone());
 
 161         executor.runAll(100);
 
 162         assertTrue(future2.isDone());
 
 164         assertEquals(OperationResult.FAILURE, future2.get().getResult());
 
 168     public void testGetKey() {
 
 169         assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetTenantOperation.getKey(TARGET_ENTITY));