Created new BB for so-etsi
[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
33 import java.io.Serializable;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.UUID;
38
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;
54
55 import com.google.common.base.Optional;
56
57
58 /**
59  * @author waqas.ikram@est.tech
60  */
61 public class VnfmAdapterCreateVnfTaskTest extends BaseTaskTest {
62
63     private static final String MODEL_INSTANCE_NAME = "MODEL_INSTANCE_NAME";
64
65     private static final String CLOUD_OWNER = "CLOUD_OWNER";
66
67     private static final String LCP_CLOUD_REGIONID = "RegionOnce";
68
69     private static final String TENANT_ID = UUID.randomUUID().toString();
70
71     private static final String VNF_ID = UUID.randomUUID().toString();
72
73     private static final String VNF_NAME = "VNF_NAME";
74
75     private static final String JOB_ID = UUID.randomUUID().toString();
76
77     @Mock
78     private VnfmAdapterServiceProvider mockedVnfmAdapterServiceProvider;
79
80     private final BuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution();
81
82     @Test
83     public void testBuildCreateVnfRequest_withValidValues_storesRequestInExecution() throws Exception {
84
85         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
86
87         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
88         objUnderTest.buildCreateVnfRequest(stubbedxecution);
89
90         final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
91         assertNotNull(actual);
92         assertEquals(VNF_NAME + "." + MODEL_INSTANCE_NAME, actual.getName());
93
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());
98
99     }
100
101     @Test
102     public void testBuildCreateVnfRequest_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
103
104         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
105
106         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
107
108         objUnderTest.buildCreateVnfRequest(stubbedxecution);
109
110         final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
111
112         assertNull(actual);
113         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1200),
114                 any(Exception.class));
115
116     }
117
118     @Test
119     public void testInvokeVnfmAdapter_validValues_storesResponseInExecution() throws Exception {
120
121         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
122
123         stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
124
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());
128
129         objUnderTest.invokeVnfmAdapter(stubbedxecution);
130
131         assertNotNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
132     }
133
134     @Test
135     public void testInvokeVnfmAdapter_invalidValues_storesResponseInExecution() throws Exception {
136
137         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
138
139         stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
140
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());
144
145         objUnderTest.invokeVnfmAdapter(stubbedxecution);
146
147         assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
148         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
149                 any(Exception.class));
150     }
151
152
153     @Test
154     public void testInvokeVnfmAdapter_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
155
156         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
157
158         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
159
160         objUnderTest.invokeVnfmAdapter(stubbedxecution);
161
162         assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
163         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
164                 any(Exception.class));
165
166     }
167
168     private Optional<CreateVnfResponse> getCreateVnfResponse() {
169         final CreateVnfResponse response = new CreateVnfResponse();
170         response.setJobId(JOB_ID);
171         return Optional.of(response);
172     }
173
174     private GenericVnf getGenericVnf() {
175         final GenericVnf genericVnf = new GenericVnf();
176         genericVnf.setVnfId(VNF_ID);
177         genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
178         genericVnf.setVnfName(VNF_NAME);
179         return genericVnf;
180     }
181
182     private ModelInfoGenericVnf getModelInfoGenericVnf() {
183         final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
184         modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
185         return modelInfoGenericVnf;
186     }
187
188     private VnfmAdapterCreateVnfTask getEtsiVnfInstantiateTask() {
189         return new VnfmAdapterCreateVnfTask(exceptionUtil, extractPojosForBB, mockedVnfmAdapterServiceProvider);
190     }
191
192     private class StubbedBuildingBlockExecution implements BuildingBlockExecution {
193
194         private final Map<String, Serializable> execution = new HashMap<>();
195         private final GeneralBuildingBlock generalBuildingBlock;
196
197         StubbedBuildingBlockExecution() {
198             generalBuildingBlock = getGeneralBuildingBlockValue();
199         }
200
201         @Override
202         public GeneralBuildingBlock getGeneralBuildingBlock() {
203             return generalBuildingBlock;
204         }
205
206         @SuppressWarnings("unchecked")
207         @Override
208         public <T> T getVariable(final String key) {
209             return (T) execution.get(key);
210         }
211
212         @Override
213         public <T> T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception {
214             return null;
215         }
216
217         @Override
218         public void setVariable(final String key, final Serializable value) {
219             execution.put(key, value);
220         }
221
222         @Override
223         public Map<ResourceKey, String> getLookupMap() {
224             return Collections.emptyMap();
225         }
226
227         @Override
228         public String getFlowToBeCalled() {
229             return null;
230         }
231
232         private GeneralBuildingBlock getGeneralBuildingBlockValue() {
233             final GeneralBuildingBlock buildingBlock = new GeneralBuildingBlock();
234             buildingBlock.setCloudRegion(getCloudRegion());
235             return buildingBlock;
236         }
237
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);
243             return cloudRegion;
244         }
245
246     }
247
248 }