2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2019 Bell Canada.
 
   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=========================================================
 
  20 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
 
  22 import org.camunda.bpm.engine.delegate.DelegateExecution;
 
  23 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
 
  24 import org.junit.Before;
 
  25 import org.junit.Test;
 
  26 import org.mockito.InjectMocks;
 
  27 import org.mockito.Mock;
 
  28 import org.onap.so.bpmn.BaseTaskTest;
 
  29 import org.onap.so.bpmn.common.BuildingBlockExecution;
 
  30 import org.onap.so.bpmn.common.DelegateExecutionImpl;
 
  31 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerContext;
 
  32 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerRunnable;
 
  33 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
 
  34 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
 
  35 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
 
  36 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
 
  37 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
 
  38 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
 
  39 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
 
  40 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
 
  41 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
 
  42 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
 
  43 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
 
  44 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoVfModule;
 
  45 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
 
  46 import org.onap.so.client.cds.*;
 
  47 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
 
  48 import org.onap.so.client.exception.ExceptionBuilder;
 
  49 import org.onap.so.serviceinstancebeans.ModelInfo;
 
  50 import org.onap.so.serviceinstancebeans.Resources;
 
  51 import org.onap.so.serviceinstancebeans.Service;
 
  52 import org.onap.so.serviceinstancebeans.Vnfs;
 
  53 import org.springframework.beans.factory.annotation.Autowired;
 
  54 import java.util.ArrayList;
 
  55 import java.util.HashMap;
 
  56 import java.util.List;
 
  58 import static org.assertj.core.api.Assertions.assertThat;
 
  59 import static org.junit.Assert.assertEquals;
 
  60 import static org.junit.Assert.assertNotNull;
 
  61 import static org.mockito.Mockito.*;
 
  63 public class GenericCDSProcessingBBTest extends BaseTaskTest {
 
  65     private static final String VNF_SCOPE = "vnf";
 
  66     private static final String TEST_VNF_MODEL_CUSTOMIZATION_UUID = "23ce9ac4-e5dd-11e9-81b4-2a2ae2dbcce4";
 
  67     private static final String DEPLOY_ACTION_FOR_CDS = "configDeploy";
 
  68     private static final String GENERAL_BLOCK_EXECUTION_MAP_KEY = "gBBInput";
 
  69     private static final String BUILDING_BLOCK = "buildingBlock";
 
  70     private static final String TEST_MSO_REQUEST_ID = "ff874603-4222-11e7-9252-005056850d2e";
 
  71     private static final String EXECUTION_OBJECT = "executionObject";
 
  72     private static final String BLUEPRINT_NAME = "test";
 
  73     private static final String BLUEPRINT_VERSION = "1.0.0";
 
  76     private GenericCDSProcessingBB controllerRunnable;
 
  79     private AbstractCDSProcessingBBUtils cdsDispather;
 
  82     private GeneratePayloadForCds generatePayloadForCds;
 
  84     private BuildingBlockExecution buildingBlockExecution;
 
  86     private ExecuteBuildingBlock executeBuildingBlock;
 
  90         buildingBlockExecution = createBuildingBlockExecution();
 
  91         executeBuildingBlock = new ExecuteBuildingBlock();
 
  95     public void testExecutionObjectCreationForVnf() throws Exception {
 
  97         ControllerContext<BuildingBlockExecution> controllerContext = new ControllerContext<>();
 
  98         controllerContext.setExecution(buildingBlockExecution);
 
  99         controllerContext.setControllerActor("CDS");
 
 100         controllerContext.setControllerScope("vnf");
 
 101         setScopeAndAction(VNF_SCOPE, DEPLOY_ACTION_FOR_CDS);
 
 102         AbstractCDSPropertiesBean cdsBean = prepareCDSBean();
 
 104         doReturn(cdsBean).when(generatePayloadForCds).buildCdsPropertiesBean(buildingBlockExecution);
 
 105         doNothing().when(cdsDispather).constructExecutionServiceInputObject(buildingBlockExecution);
 
 106         doNothing().when(cdsDispather).sendRequestToCDSClient(buildingBlockExecution);
 
 109         Boolean isUnderstandable = controllerRunnable.understand(controllerContext);
 
 110         Boolean isReady = controllerRunnable.ready(controllerContext);
 
 111         controllerRunnable.prepare(controllerContext);
 
 112         controllerRunnable.run(controllerContext);
 
 115         assertEquals(isUnderstandable, true);
 
 116         assertEquals(isReady, true);
 
 117         AbstractCDSPropertiesBean executionObject = buildingBlockExecution.getVariable(EXECUTION_OBJECT);
 
 118         assertNotNull(executionObject);
 
 119         assertThat(executionObject).isInstanceOf(AbstractCDSPropertiesBean.class);
 
 120         assertEquals(BLUEPRINT_NAME, executionObject.getBlueprintName());
 
 121         assertEquals(BLUEPRINT_VERSION, executionObject.getBlueprintVersion());
 
 122         assertEquals(TEST_MSO_REQUEST_ID, executionObject.getRequestId());
 
 123         assertNotNull(executionObject.getRequestObject());
 
 126     private AbstractCDSPropertiesBean prepareCDSBean() {
 
 127         AbstractCDSPropertiesBean cdsBean = new AbstractCDSPropertiesBean();
 
 128         cdsBean.setBlueprintName(BLUEPRINT_NAME);
 
 129         cdsBean.setBlueprintVersion(BLUEPRINT_VERSION);
 
 130         cdsBean.setRequestId(TEST_MSO_REQUEST_ID);
 
 131         cdsBean.setRequestObject("requestObject");
 
 136     private GeneralBuildingBlock createGeneralBuildingBlock() {
 
 137         GeneralBuildingBlock generalBuildingBlock = new GeneralBuildingBlock();
 
 138         RequestContext requestContext = new RequestContext();
 
 139         RequestParameters requestParameters = new RequestParameters();
 
 140         requestParameters.setUserParams(createRequestParameters());
 
 141         requestContext.setRequestParameters(requestParameters);
 
 142         requestContext.setMsoRequestId(TEST_MSO_REQUEST_ID);
 
 143         generalBuildingBlock.setRequestContext(requestContext);
 
 144         return generalBuildingBlock;
 
 147     private List<Map<String, Object>> createRequestParameters() {
 
 148         List<Map<String, Object>> userParams = new ArrayList<>();
 
 149         Map<String, Object> userParamMap = new HashMap<>();
 
 150         userParamMap.put("service", getUserParams());
 
 151         userParams.add(userParamMap);
 
 155     private Service getUserParams() {
 
 156         Service service = new Service();
 
 157         Resources resources = new Resources();
 
 158         resources.setVnfs(createVnfList());
 
 159         service.setResources(resources);
 
 163     private List<Vnfs> createVnfList() {
 
 164         List<Map<String, String>> instanceParamsListSearchedVnf = new ArrayList<>();
 
 165         Map<String, String> instanceParam = new HashMap<>();
 
 166         instanceParam.put("sec_group", "sec_group");
 
 167         instanceParam.put("net_id", "acl-cloud-region");
 
 168         instanceParamsListSearchedVnf.add(instanceParam);
 
 169         Vnfs searchedVnf = createVnf(instanceParamsListSearchedVnf);
 
 170         List<Vnfs> vnfList = new ArrayList<>();
 
 171         vnfList.add(searchedVnf);
 
 175     private Vnfs createVnf(List<Map<String, String>> instanceParamsList) {
 
 176         Vnfs vnf = new Vnfs();
 
 177         ModelInfo modelInfo = new ModelInfo();
 
 178         modelInfo.setModelCustomizationId(TEST_VNF_MODEL_CUSTOMIZATION_UUID);
 
 179         vnf.setModelInfo(modelInfo);
 
 180         vnf.setInstanceParams(instanceParamsList);
 
 184     private BuildingBlockExecution createBuildingBlockExecution() {
 
 185         DelegateExecution execution = new DelegateExecutionFake();
 
 186         execution.setVariable(GENERAL_BLOCK_EXECUTION_MAP_KEY, createGeneralBuildingBlock());
 
 187         return new DelegateExecutionImpl(execution);
 
 190     private void setScopeAndAction(String scope, String action) {
 
 191         BuildingBlock buildingBlock = new BuildingBlock();
 
 192         buildingBlock.setBpmnScope(scope);
 
 193         buildingBlock.setBpmnAction(action);
 
 194         executeBuildingBlock.setBuildingBlock(buildingBlock);
 
 195         buildingBlockExecution.setVariable(BUILDING_BLOCK, executeBuildingBlock);