2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2019 Nordix Foundation.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.junit.Assert.assertNotNull;
 
  25 import static org.junit.Assert.assertNull;
 
  26 import static org.mockito.ArgumentMatchers.any;
 
  27 import static org.mockito.ArgumentMatchers.eq;
 
  28 import static org.mockito.Mockito.verify;
 
  29 import static org.mockito.Mockito.when;
 
  30 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CREATE_VNF_REQUEST_PARAM_NAME;
 
  31 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CREATE_VNF_RESPONSE_PARAM_NAME;
 
  32 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.INPUT_PARAMETER;
 
  33 import java.util.Collections;
 
  34 import java.util.UUID;
 
  35 import org.junit.Test;
 
  36 import org.mockito.Mock;
 
  37 import org.onap.etsi.sol003.adapter.lcm.v1.model.CreateVnfRequest;
 
  38 import org.onap.etsi.sol003.adapter.lcm.v1.model.CreateVnfResponse;
 
  39 import org.onap.etsi.sol003.adapter.lcm.v1.model.Tenant;
 
  40 import org.onap.so.bpmn.BaseTaskTest;
 
  41 import org.onap.so.bpmn.common.BuildingBlockExecution;
 
  42 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter;
 
  43 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
 
  44 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
 
  45 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
 
  46 import com.google.common.base.Optional;
 
  50  * @author waqas.ikram@est.tech
 
  52 public class VnfmAdapterCreateVnfTaskTest extends BaseTaskTest {
 
  54     private static final String MODEL_INSTANCE_NAME = "MODEL_INSTANCE_NAME";
 
  56     private static final String CLOUD_OWNER = "CLOUD_OWNER";
 
  58     private static final String LCP_CLOUD_REGIONID = "RegionOnce";
 
  60     private static final String VNF_ID = UUID.randomUUID().toString();
 
  62     private static final String VNF_NAME = "VNF_NAME";
 
  64     private static final String JOB_ID = UUID.randomUUID().toString();
 
  67     private VnfmAdapterServiceProvider mockedVnfmAdapterServiceProvider;
 
  69     private final BuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution();
 
  72     public void testBuildCreateVnfRequest_withValidValues_storesRequestInExecution() throws Exception {
 
  74         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
 
  75         stubbedxecution.setVariable(INPUT_PARAMETER,
 
  76                 new InputParameter(Collections.emptyMap(), Collections.emptyList()));
 
  78         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
 
  79         objUnderTest.buildCreateVnfRequest(stubbedxecution);
 
  81         final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
 
  82         assertNotNull(actual);
 
  83         assertEquals(VNF_NAME + "." + MODEL_INSTANCE_NAME, actual.getName());
 
  85         final Tenant actualTenant = actual.getTenant();
 
  86         assertEquals(CLOUD_OWNER, actualTenant.getCloudOwner());
 
  87         assertEquals(LCP_CLOUD_REGIONID, actualTenant.getRegionName());
 
  88         assertEquals(StubbedBuildingBlockExecution.getTenantId(), actualTenant.getTenantId());
 
  93     public void testBuildCreateVnfRequest_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
 
  95         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
 
  97         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
 
  99         objUnderTest.buildCreateVnfRequest(stubbedxecution);
 
 101         final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
 
 104         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1200),
 
 105                 any(Exception.class));
 
 110     public void testInvokeVnfmAdapter_validValues_storesResponseInExecution() throws Exception {
 
 112         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
 
 114         stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
 
 116         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
 
 117         when(mockedVnfmAdapterServiceProvider.invokeCreateInstantiationRequest(eq(VNF_ID), any(CreateVnfRequest.class)))
 
 118                 .thenReturn(getCreateVnfResponse());
 
 120         objUnderTest.invokeVnfmAdapter(stubbedxecution);
 
 122         assertNotNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
 
 126     public void testInvokeVnfmAdapter_invalidValues_storesResponseInExecution() throws Exception {
 
 128         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
 
 130         stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
 
 132         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
 
 133         when(mockedVnfmAdapterServiceProvider.invokeCreateInstantiationRequest(eq(VNF_ID), any(CreateVnfRequest.class)))
 
 134                 .thenReturn(Optional.absent());
 
 136         objUnderTest.invokeVnfmAdapter(stubbedxecution);
 
 138         assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
 
 139         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
 
 140                 any(Exception.class));
 
 145     public void testInvokeVnfmAdapter_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
 
 147         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
 
 149         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
 
 151         objUnderTest.invokeVnfmAdapter(stubbedxecution);
 
 153         assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
 
 154         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
 
 155                 any(Exception.class));
 
 159     private Optional<CreateVnfResponse> getCreateVnfResponse() {
 
 160         final CreateVnfResponse response = new CreateVnfResponse();
 
 161         response.setJobId(JOB_ID);
 
 162         return Optional.of(response);
 
 165     private GenericVnf getGenericVnf() {
 
 166         final GenericVnf genericVnf = new GenericVnf();
 
 167         genericVnf.setVnfId(VNF_ID);
 
 168         genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
 
 169         genericVnf.setVnfName(VNF_NAME);
 
 173     private ModelInfoGenericVnf getModelInfoGenericVnf() {
 
 174         final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
 
 175         modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
 
 176         return modelInfoGenericVnf;
 
 179     private VnfmAdapterCreateVnfTask getEtsiVnfInstantiateTask() {
 
 180         return new VnfmAdapterCreateVnfTask(exceptionUtil, extractPojosForBB, mockedVnfmAdapterServiceProvider);