Added input parameter loading handling from SDNC
[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
34 import java.io.Serializable;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.UUID;
39
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;
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         stubbedxecution.setVariable(INPUT_PARAMETER,
87                 new InputParameter(Collections.emptyMap(), Collections.emptyList()));
88
89         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
90         objUnderTest.buildCreateVnfRequest(stubbedxecution);
91
92         final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
93         assertNotNull(actual);
94         assertEquals(VNF_NAME + "." + MODEL_INSTANCE_NAME, actual.getName());
95
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());
100
101     }
102
103     @Test
104     public void testBuildCreateVnfRequest_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
105
106         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
107
108         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
109
110         objUnderTest.buildCreateVnfRequest(stubbedxecution);
111
112         final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
113
114         assertNull(actual);
115         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1200),
116                 any(Exception.class));
117
118     }
119
120     @Test
121     public void testInvokeVnfmAdapter_validValues_storesResponseInExecution() throws Exception {
122
123         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
124
125         stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
126
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());
130
131         objUnderTest.invokeVnfmAdapter(stubbedxecution);
132
133         assertNotNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
134     }
135
136     @Test
137     public void testInvokeVnfmAdapter_invalidValues_storesResponseInExecution() throws Exception {
138
139         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
140
141         stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
142
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());
146
147         objUnderTest.invokeVnfmAdapter(stubbedxecution);
148
149         assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
150         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
151                 any(Exception.class));
152     }
153
154
155     @Test
156     public void testInvokeVnfmAdapter_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
157
158         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
159
160         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
161
162         objUnderTest.invokeVnfmAdapter(stubbedxecution);
163
164         assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
165         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
166                 any(Exception.class));
167
168     }
169
170     private Optional<CreateVnfResponse> getCreateVnfResponse() {
171         final CreateVnfResponse response = new CreateVnfResponse();
172         response.setJobId(JOB_ID);
173         return Optional.of(response);
174     }
175
176     private GenericVnf getGenericVnf() {
177         final GenericVnf genericVnf = new GenericVnf();
178         genericVnf.setVnfId(VNF_ID);
179         genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
180         genericVnf.setVnfName(VNF_NAME);
181         return genericVnf;
182     }
183
184     private ModelInfoGenericVnf getModelInfoGenericVnf() {
185         final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
186         modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
187         return modelInfoGenericVnf;
188     }
189
190     private VnfmAdapterCreateVnfTask getEtsiVnfInstantiateTask() {
191         return new VnfmAdapterCreateVnfTask(exceptionUtil, extractPojosForBB, mockedVnfmAdapterServiceProvider);
192     }
193
194     private class StubbedBuildingBlockExecution implements BuildingBlockExecution {
195
196         private final Map<String, Serializable> execution = new HashMap<>();
197         private final GeneralBuildingBlock generalBuildingBlock;
198
199         StubbedBuildingBlockExecution() {
200             generalBuildingBlock = getGeneralBuildingBlockValue();
201         }
202
203         @Override
204         public GeneralBuildingBlock getGeneralBuildingBlock() {
205             return generalBuildingBlock;
206         }
207
208         @SuppressWarnings("unchecked")
209         @Override
210         public <T> T getVariable(final String key) {
211             return (T) execution.get(key);
212         }
213
214         @Override
215         public <T> T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception {
216             return null;
217         }
218
219         @Override
220         public void setVariable(final String key, final Serializable value) {
221             execution.put(key, value);
222         }
223
224         @Override
225         public Map<ResourceKey, String> getLookupMap() {
226             return Collections.emptyMap();
227         }
228
229         @Override
230         public String getFlowToBeCalled() {
231             return null;
232         }
233
234         private GeneralBuildingBlock getGeneralBuildingBlockValue() {
235             final GeneralBuildingBlock buildingBlock = new GeneralBuildingBlock();
236             buildingBlock.setCloudRegion(getCloudRegion());
237             return buildingBlock;
238         }
239
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);
245             return cloudRegion;
246         }
247
248     }
249
250 }