22c4c15079938b85bd1c4ca01924c19484120833
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / VnfmAdapterCreateVnfTaskTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
22
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;
47
48
49 /**
50  * @author waqas.ikram@est.tech
51  */
52 public class VnfmAdapterCreateVnfTaskTest extends BaseTaskTest {
53
54   private static final String MODEL_INSTANCE_NAME = "MODEL_INSTANCE_NAME";
55
56   private static final String CLOUD_OWNER = "CLOUD_OWNER";
57
58   private static final String LCP_CLOUD_REGIONID = "RegionOnce";
59
60   private static final String VNF_ID = UUID.randomUUID().toString();
61
62   private static final String VNF_NAME = "VNF_NAME";
63
64   private static final String JOB_ID = UUID.randomUUID().toString();
65
66   @Mock
67   private VnfmAdapterServiceProvider mockedVnfmAdapterServiceProvider;
68
69   private final BuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution();
70
71   @Test
72   public void testBuildCreateVnfRequest_withValidValues_storesRequestInExecution() throws Exception {
73
74     final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
75     stubbedxecution.setVariable(INPUT_PARAMETER, new InputParameter(Collections.emptyMap(), Collections.emptyList()));
76
77     when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
78     objUnderTest.buildCreateVnfRequest(stubbedxecution);
79
80     final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
81     assertNotNull(actual);
82     assertEquals(VNF_NAME + "." + MODEL_INSTANCE_NAME, actual.getName());
83
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());
88
89   }
90
91   @Test
92   public void testBuildCreateVnfRequest_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
93
94     final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
95
96     when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
97
98     objUnderTest.buildCreateVnfRequest(stubbedxecution);
99
100     final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
101
102     assertNull(actual);
103     verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1200),
104         any(Exception.class));
105
106   }
107
108   @Test
109   public void testInvokeVnfmAdapter_validValues_storesResponseInExecution() throws Exception {
110
111     final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
112
113     stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
114
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());
118
119     objUnderTest.invokeVnfmAdapter(stubbedxecution);
120
121     assertNotNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
122   }
123
124   @Test
125   public void testInvokeVnfmAdapter_invalidValues_storesResponseInExecution() throws Exception {
126
127     final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
128
129     stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
130
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());
134
135     objUnderTest.invokeVnfmAdapter(stubbedxecution);
136
137     assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
138     verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
139         any(Exception.class));
140   }
141
142
143   @Test
144   public void testInvokeVnfmAdapter_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
145
146     final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
147
148     when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
149
150     objUnderTest.invokeVnfmAdapter(stubbedxecution);
151
152     assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
153     verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
154         any(Exception.class));
155
156   }
157
158   private Optional<CreateVnfResponse> getCreateVnfResponse() {
159     final CreateVnfResponse response = new CreateVnfResponse();
160     response.setJobId(JOB_ID);
161     return Optional.of(response);
162   }
163
164   private GenericVnf getGenericVnf() {
165     final GenericVnf genericVnf = new GenericVnf();
166     genericVnf.setVnfId(VNF_ID);
167     genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
168     genericVnf.setVnfName(VNF_NAME);
169     return genericVnf;
170   }
171
172   private ModelInfoGenericVnf getModelInfoGenericVnf() {
173     final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
174     modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
175     return modelInfoGenericVnf;
176   }
177
178   private VnfmAdapterCreateVnfTask getEtsiVnfInstantiateTask() {
179     return new VnfmAdapterCreateVnfTask(exceptionUtil, extractPojosForBB, mockedVnfmAdapterServiceProvider);
180   }
181 }