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;
34 import java.io.Serializable;
35 import java.util.Collections;
36 import java.util.HashMap;
38 import java.util.UUID;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.onap.so.bpmn.BaseTaskTest;
43 import org.onap.so.bpmn.common.BuildingBlockExecution;
44 import org.onap.so.bpmn.common.exceptions.RequiredExecutionVariableExeception;
45 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter;
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();
86 stubbedxecution.setVariable(INPUT_PARAMETER,
87 new InputParameter(Collections.emptyMap(), Collections.emptyList()));
89 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
90 objUnderTest.buildCreateVnfRequest(stubbedxecution);
92 final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
93 assertNotNull(actual);
94 assertEquals(VNF_NAME + "." + MODEL_INSTANCE_NAME, actual.getName());
96 final Tenant actualTenant = actual.getTenant();
97 assertEquals(CLOUD_OWNER, actualTenant.getCloudOwner());
98 assertEquals(LCP_CLOUD_REGIONID, actualTenant.getRegionName());
99 assertEquals(TENANT_ID, actualTenant.getTenantId());
104 public void testBuildCreateVnfRequest_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
106 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
108 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
110 objUnderTest.buildCreateVnfRequest(stubbedxecution);
112 final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
115 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1200),
116 any(Exception.class));
121 public void testInvokeVnfmAdapter_validValues_storesResponseInExecution() throws Exception {
123 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
125 stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
127 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
128 when(mockedVnfmAdapterServiceProvider.invokeCreateInstantiationRequest(eq(VNF_ID), any(CreateVnfRequest.class)))
129 .thenReturn(getCreateVnfResponse());
131 objUnderTest.invokeVnfmAdapter(stubbedxecution);
133 assertNotNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
137 public void testInvokeVnfmAdapter_invalidValues_storesResponseInExecution() throws Exception {
139 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
141 stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
143 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
144 when(mockedVnfmAdapterServiceProvider.invokeCreateInstantiationRequest(eq(VNF_ID), any(CreateVnfRequest.class)))
145 .thenReturn(Optional.absent());
147 objUnderTest.invokeVnfmAdapter(stubbedxecution);
149 assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
150 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
151 any(Exception.class));
156 public void testInvokeVnfmAdapter_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
158 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
160 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
162 objUnderTest.invokeVnfmAdapter(stubbedxecution);
164 assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
165 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
166 any(Exception.class));
170 private Optional<CreateVnfResponse> getCreateVnfResponse() {
171 final CreateVnfResponse response = new CreateVnfResponse();
172 response.setJobId(JOB_ID);
173 return Optional.of(response);
176 private GenericVnf getGenericVnf() {
177 final GenericVnf genericVnf = new GenericVnf();
178 genericVnf.setVnfId(VNF_ID);
179 genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
180 genericVnf.setVnfName(VNF_NAME);
184 private ModelInfoGenericVnf getModelInfoGenericVnf() {
185 final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
186 modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
187 return modelInfoGenericVnf;
190 private VnfmAdapterCreateVnfTask getEtsiVnfInstantiateTask() {
191 return new VnfmAdapterCreateVnfTask(exceptionUtil, extractPojosForBB, mockedVnfmAdapterServiceProvider);
194 private class StubbedBuildingBlockExecution implements BuildingBlockExecution {
196 private final Map<String, Serializable> execution = new HashMap<>();
197 private final GeneralBuildingBlock generalBuildingBlock;
199 StubbedBuildingBlockExecution() {
200 generalBuildingBlock = getGeneralBuildingBlockValue();
204 public GeneralBuildingBlock getGeneralBuildingBlock() {
205 return generalBuildingBlock;
208 @SuppressWarnings("unchecked")
210 public <T> T getVariable(final String key) {
211 return (T) execution.get(key);
215 public <T> T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception {
220 public void setVariable(final String key, final Serializable value) {
221 execution.put(key, value);
225 public Map<ResourceKey, String> getLookupMap() {
226 return Collections.emptyMap();
230 public String getFlowToBeCalled() {
234 private GeneralBuildingBlock getGeneralBuildingBlockValue() {
235 final GeneralBuildingBlock buildingBlock = new GeneralBuildingBlock();
236 buildingBlock.setCloudRegion(getCloudRegion());
237 return buildingBlock;
240 private CloudRegion getCloudRegion() {
241 final CloudRegion cloudRegion = new CloudRegion();
242 cloudRegion.setCloudOwner(CLOUD_OWNER);
243 cloudRegion.setLcpCloudRegionId(LCP_CLOUD_REGIONID);
244 cloudRegion.setTenantId(TENANT_ID);