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.so;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.junit.Assert.assertFalse;
 
  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.ArgumentMatchers.eq;
 
  29 import static org.mockito.Mockito.mock;
 
  30 import static org.mockito.Mockito.when;
 
  32 import java.util.concurrent.CompletableFuture;
 
  33 import java.util.concurrent.ForkJoinPool;
 
  34 import java.util.concurrent.TimeUnit;
 
  35 import java.util.concurrent.atomic.AtomicBoolean;
 
  36 import org.apache.commons.lang3.tuple.Pair;
 
  37 import org.junit.Before;
 
  38 import org.junit.Test;
 
  39 import org.onap.aai.domain.yang.CloudRegion;
 
  40 import org.onap.aai.domain.yang.GenericVnf;
 
  41 import org.onap.aai.domain.yang.ModelVer;
 
  42 import org.onap.aai.domain.yang.ServiceInstance;
 
  43 import org.onap.aai.domain.yang.Tenant;
 
  44 import org.onap.policy.aai.AaiCqResponse;
 
  45 import org.onap.policy.common.utils.coder.CoderException;
 
  46 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
 
  47 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
 
  48 import org.onap.policy.controlloop.policy.PolicyResult;
 
  49 import org.onap.policy.so.SoRequest;
 
  51 public class VfModuleCreateTest extends BasicSoOperation {
 
  52     private static final String MODEL_NAME2 = "my-model-name-B";
 
  53     private static final String MODEL_VERS2 = "my-model-version-B";
 
  54     private static final String SVC_INSTANCE_ID = "my-service-instance-id";
 
  55     private static final String VNF_ID = "my-vnf-id";
 
  57     private VfModuleCreate oper;
 
  59     public VfModuleCreateTest() {
 
  60         super(DEFAULT_ACTOR, VfModuleCreate.NAME);
 
  65     public void setUp() throws Exception {
 
  67         oper = new VfModuleCreate(params, soOperator);
 
  71     public void testConstructor() {
 
  72         assertEquals(DEFAULT_ACTOR, oper.getActorName());
 
  73         assertEquals(VfModuleCreate.NAME, oper.getName());
 
  77     public void testStartPreprocessorAsync() {
 
  78         CompletableFuture<OperationOutcome> future = new CompletableFuture<>();
 
  79         context = mock(ControlLoopEventContext.class);
 
  80         when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future);
 
  81         params = params.toBuilder().context(context).build();
 
  83         AtomicBoolean guardStarted = new AtomicBoolean();
 
  85         oper = new VfModuleCreate(params, soOperator) {
 
  87             protected CompletableFuture<OperationOutcome> startGuardAsync() {
 
  88                 guardStarted.set(true);
 
  89                 return super.startGuardAsync();
 
  93         assertSame(future, oper.startPreprocessorAsync());
 
  94         assertFalse(future.isDone());
 
  95         assertTrue(guardStarted.get());
 
  99     public void testStartOperationAsync() throws Exception {
 
 100         when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
 
 102         // use a real executor
 
 103         params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
 
 105         oper = new VfModuleCreate(params, soOperator) {
 
 107             public long getWaitMsGet() {
 
 112         CompletableFuture<OperationOutcome> future2 = oper.start();
 
 114         outcome = future2.get(500, TimeUnit.SECONDS);
 
 115         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
 
 119      * Tests startOperationAsync() when "get" operations are required.
 
 122     public void testStartOperationAsyncWithGets() throws Exception {
 
 123         when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
 
 125         when(client.post(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
 
 126         when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
 
 128         // use a real executor
 
 129         params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
 
 131         oper = new VfModuleCreate(params, soOperator) {
 
 133             public long getWaitMsGet() {
 
 138         CompletableFuture<OperationOutcome> future2 = oper.start();
 
 140         outcome = future2.get(500, TimeUnit.SECONDS);
 
 141         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
 
 145     public void testMakeRequest() throws CoderException {
 
 146         Pair<String, SoRequest> pair = oper.makeRequest();
 
 150             "/serviceInstantiation/v7/serviceInstances/my-service-instance-id/vnfs/my-vnf-id/vfModules/scaleOut",
 
 154         verifyRequest("vfModuleCreate.json", pair.getRight());
 
 159     protected void makeContext() {
 
 162         AaiCqResponse cq = mock(AaiCqResponse.class);
 
 164         GenericVnf vnf = new GenericVnf();
 
 165         when(cq.getGenericVnfByVfModuleModelInvariantId(MODEL_INVAR_ID)).thenReturn(vnf);
 
 166         vnf.setVnfId(VNF_ID);
 
 168         ServiceInstance instance = new ServiceInstance();
 
 169         when(cq.getServiceInstance()).thenReturn(instance);
 
 170         instance.setServiceInstanceId(SVC_INSTANCE_ID);
 
 172         when(cq.getDefaultTenant()).thenReturn(new Tenant());
 
 173         when(cq.getDefaultCloudRegion()).thenReturn(new CloudRegion());
 
 175         ModelVer modelVers = new ModelVer();
 
 176         when(cq.getModelVerByVersionId(any())).thenReturn(modelVers);
 
 177         modelVers.setModelName(MODEL_NAME2);
 
 178         modelVers.setModelVersion(MODEL_VERS2);
 
 180         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);