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;
33 import java.io.Serializable;
34 import java.util.Collections;
35 import java.util.HashMap;
37 import java.util.UUID;
39 import org.junit.Test;
40 import org.mockito.Mock;
41 import org.onap.so.bpmn.BaseTaskTest;
42 import org.onap.so.bpmn.common.BuildingBlockExecution;
43 import org.onap.so.bpmn.common.exceptions.RequiredExecutionVariableExeception;
44 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.VnfmAdapterCreateVnfTask;
45 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.VnfmAdapterServiceProvider;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
48 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
49 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
50 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
51 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
52 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
53 import org.onap.vnfmadapter.v1.model.Tenant;
55 import com.google.common.base.Optional;
59 * @author waqas.ikram@est.tech
61 public class VnfmAdapterCreateVnfTaskTest extends BaseTaskTest {
63 private static final String MODEL_INSTANCE_NAME = "MODEL_INSTANCE_NAME";
65 private static final String CLOUD_OWNER = "CLOUD_OWNER";
67 private static final String LCP_CLOUD_REGIONID = "RegionOnce";
69 private static final String TENANT_ID = UUID.randomUUID().toString();
71 private static final String VNF_ID = UUID.randomUUID().toString();
73 private static final String VNF_NAME = "VNF_NAME";
75 private static final String JOB_ID = UUID.randomUUID().toString();
78 private VnfmAdapterServiceProvider mockedVnfmAdapterServiceProvider;
80 private final BuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution();
83 public void testBuildCreateVnfRequest_withValidValues_storesRequestInExecution() throws Exception {
85 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
87 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
88 objUnderTest.buildCreateVnfRequest(stubbedxecution);
90 final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
91 assertNotNull(actual);
92 assertEquals(VNF_NAME + "." + MODEL_INSTANCE_NAME, actual.getName());
94 final Tenant actualTenant = actual.getTenant();
95 assertEquals(CLOUD_OWNER, actualTenant.getCloudOwner());
96 assertEquals(LCP_CLOUD_REGIONID, actualTenant.getRegionName());
97 assertEquals(TENANT_ID, actualTenant.getTenantId());
102 public void testBuildCreateVnfRequest_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
104 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
106 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
108 objUnderTest.buildCreateVnfRequest(stubbedxecution);
110 final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
113 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1200),
114 any(Exception.class));
119 public void testInvokeVnfmAdapter_validValues_storesResponseInExecution() throws Exception {
121 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
123 stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
125 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
126 when(mockedVnfmAdapterServiceProvider.invokeCreateInstantiationRequest(eq(VNF_ID), any(CreateVnfRequest.class)))
127 .thenReturn(getCreateVnfResponse());
129 objUnderTest.invokeVnfmAdapter(stubbedxecution);
131 assertNotNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
135 public void testInvokeVnfmAdapter_invalidValues_storesResponseInExecution() throws Exception {
137 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
139 stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
141 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
142 when(mockedVnfmAdapterServiceProvider.invokeCreateInstantiationRequest(eq(VNF_ID), any(CreateVnfRequest.class)))
143 .thenReturn(Optional.absent());
145 objUnderTest.invokeVnfmAdapter(stubbedxecution);
147 assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
148 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
149 any(Exception.class));
154 public void testInvokeVnfmAdapter_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
156 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
158 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
160 objUnderTest.invokeVnfmAdapter(stubbedxecution);
162 assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
163 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
164 any(Exception.class));
168 private Optional<CreateVnfResponse> getCreateVnfResponse() {
169 final CreateVnfResponse response = new CreateVnfResponse();
170 response.setJobId(JOB_ID);
171 return Optional.of(response);
174 private GenericVnf getGenericVnf() {
175 final GenericVnf genericVnf = new GenericVnf();
176 genericVnf.setVnfId(VNF_ID);
177 genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
178 genericVnf.setVnfName(VNF_NAME);
182 private ModelInfoGenericVnf getModelInfoGenericVnf() {
183 final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
184 modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
185 return modelInfoGenericVnf;
188 private VnfmAdapterCreateVnfTask getEtsiVnfInstantiateTask() {
189 return new VnfmAdapterCreateVnfTask(exceptionUtil, extractPojosForBB, mockedVnfmAdapterServiceProvider);
192 private class StubbedBuildingBlockExecution implements BuildingBlockExecution {
194 private final Map<String, Serializable> execution = new HashMap<>();
195 private final GeneralBuildingBlock generalBuildingBlock;
197 StubbedBuildingBlockExecution() {
198 generalBuildingBlock = getGeneralBuildingBlockValue();
202 public GeneralBuildingBlock getGeneralBuildingBlock() {
203 return generalBuildingBlock;
206 @SuppressWarnings("unchecked")
208 public <T> T getVariable(final String key) {
209 return (T) execution.get(key);
213 public <T> T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception {
218 public void setVariable(final String key, final Serializable value) {
219 execution.put(key, value);
223 public Map<ResourceKey, String> getLookupMap() {
224 return Collections.emptyMap();
228 public String getFlowToBeCalled() {
232 private GeneralBuildingBlock getGeneralBuildingBlockValue() {
233 final GeneralBuildingBlock buildingBlock = new GeneralBuildingBlock();
234 buildingBlock.setCloudRegion(getCloudRegion());
235 return buildingBlock;
238 private CloudRegion getCloudRegion() {
239 final CloudRegion cloudRegion = new CloudRegion();
240 cloudRegion.setCloudOwner(CLOUD_OWNER);
241 cloudRegion.setLcpCloudRegionId(LCP_CLOUD_REGIONID);
242 cloudRegion.setTenantId(TENANT_ID);