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.so.bpmn.BaseTaskTest;
38 import org.onap.so.bpmn.common.BuildingBlockExecution;
39 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
41 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
42 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
43 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
44 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
45 import org.onap.vnfmadapter.v1.model.Tenant;
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, new InputParameter(Collections.emptyMap(), Collections.emptyList()));
77 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
78 objUnderTest.buildCreateVnfRequest(stubbedxecution);
80 final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
81 assertNotNull(actual);
82 assertEquals(VNF_NAME + "." + MODEL_INSTANCE_NAME, actual.getName());
84 final Tenant actualTenant = actual.getTenant();
85 assertEquals(CLOUD_OWNER, actualTenant.getCloudOwner());
86 assertEquals(LCP_CLOUD_REGIONID, actualTenant.getRegionName());
87 assertEquals(StubbedBuildingBlockExecution.getTenantId(), actualTenant.getTenantId());
92 public void testBuildCreateVnfRequest_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
94 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
96 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
98 objUnderTest.buildCreateVnfRequest(stubbedxecution);
100 final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
103 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1200),
104 any(Exception.class));
109 public void testInvokeVnfmAdapter_validValues_storesResponseInExecution() throws Exception {
111 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
113 stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
115 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
116 when(mockedVnfmAdapterServiceProvider.invokeCreateInstantiationRequest(eq(VNF_ID), any(CreateVnfRequest.class)))
117 .thenReturn(getCreateVnfResponse());
119 objUnderTest.invokeVnfmAdapter(stubbedxecution);
121 assertNotNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
125 public void testInvokeVnfmAdapter_invalidValues_storesResponseInExecution() throws Exception {
127 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
129 stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
131 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
132 when(mockedVnfmAdapterServiceProvider.invokeCreateInstantiationRequest(eq(VNF_ID), any(CreateVnfRequest.class)))
133 .thenReturn(Optional.absent());
135 objUnderTest.invokeVnfmAdapter(stubbedxecution);
137 assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
138 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
139 any(Exception.class));
144 public void testInvokeVnfmAdapter_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
146 final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
148 when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
150 objUnderTest.invokeVnfmAdapter(stubbedxecution);
152 assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
153 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
154 any(Exception.class));
158 private Optional<CreateVnfResponse> getCreateVnfResponse() {
159 final CreateVnfResponse response = new CreateVnfResponse();
160 response.setJobId(JOB_ID);
161 return Optional.of(response);
164 private GenericVnf getGenericVnf() {
165 final GenericVnf genericVnf = new GenericVnf();
166 genericVnf.setVnfId(VNF_ID);
167 genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
168 genericVnf.setVnfName(VNF_NAME);
172 private ModelInfoGenericVnf getModelInfoGenericVnf() {
173 final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
174 modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
175 return modelInfoGenericVnf;
178 private VnfmAdapterCreateVnfTask getEtsiVnfInstantiateTask() {
179 return new VnfmAdapterCreateVnfTask(exceptionUtil, extractPojosForBB, mockedVnfmAdapterServiceProvider);