cfa5aaa4b88f395edc81e69a4c47f709b634d519
[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,
76                 new InputParameter(Collections.emptyMap(), Collections.emptyList()));
77
78         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
79         objUnderTest.buildCreateVnfRequest(stubbedxecution);
80
81         final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
82         assertNotNull(actual);
83         assertEquals(VNF_NAME + "." + MODEL_INSTANCE_NAME, actual.getName());
84
85         final Tenant actualTenant = actual.getTenant();
86         assertEquals(CLOUD_OWNER, actualTenant.getCloudOwner());
87         assertEquals(LCP_CLOUD_REGIONID, actualTenant.getRegionName());
88         assertEquals(StubbedBuildingBlockExecution.getTenantId(), actualTenant.getTenantId());
89
90     }
91
92     @Test
93     public void testBuildCreateVnfRequest_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
94
95         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
96
97         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
98
99         objUnderTest.buildCreateVnfRequest(stubbedxecution);
100
101         final CreateVnfRequest actual = stubbedxecution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
102
103         assertNull(actual);
104         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1200),
105                 any(Exception.class));
106
107     }
108
109     @Test
110     public void testInvokeVnfmAdapter_validValues_storesResponseInExecution() throws Exception {
111
112         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
113
114         stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
115
116         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
117         when(mockedVnfmAdapterServiceProvider.invokeCreateInstantiationRequest(eq(VNF_ID), any(CreateVnfRequest.class)))
118                 .thenReturn(getCreateVnfResponse());
119
120         objUnderTest.invokeVnfmAdapter(stubbedxecution);
121
122         assertNotNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
123     }
124
125     @Test
126     public void testInvokeVnfmAdapter_invalidValues_storesResponseInExecution() throws Exception {
127
128         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
129
130         stubbedxecution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, new CreateVnfRequest());
131
132         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(getGenericVnf());
133         when(mockedVnfmAdapterServiceProvider.invokeCreateInstantiationRequest(eq(VNF_ID), any(CreateVnfRequest.class)))
134                 .thenReturn(Optional.absent());
135
136         objUnderTest.invokeVnfmAdapter(stubbedxecution);
137
138         assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
139         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
140                 any(Exception.class));
141     }
142
143
144     @Test
145     public void testInvokeVnfmAdapter_extractPojosForBBThrowsException_exceptionBuilderCalled() throws Exception {
146
147         final VnfmAdapterCreateVnfTask objUnderTest = getEtsiVnfInstantiateTask();
148
149         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
150
151         objUnderTest.invokeVnfmAdapter(stubbedxecution);
152
153         assertNull(stubbedxecution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME));
154         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1202),
155                 any(Exception.class));
156
157     }
158
159     private Optional<CreateVnfResponse> getCreateVnfResponse() {
160         final CreateVnfResponse response = new CreateVnfResponse();
161         response.setJobId(JOB_ID);
162         return Optional.of(response);
163     }
164
165     private GenericVnf getGenericVnf() {
166         final GenericVnf genericVnf = new GenericVnf();
167         genericVnf.setVnfId(VNF_ID);
168         genericVnf.setModelInfoGenericVnf(getModelInfoGenericVnf());
169         genericVnf.setVnfName(VNF_NAME);
170         return genericVnf;
171     }
172
173     private ModelInfoGenericVnf getModelInfoGenericVnf() {
174         final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
175         modelInfoGenericVnf.setModelInstanceName(MODEL_INSTANCE_NAME);
176         return modelInfoGenericVnf;
177     }
178
179     private VnfmAdapterCreateVnfTask getEtsiVnfInstantiateTask() {
180         return new VnfmAdapterCreateVnfTask(exceptionUtil, extractPojosForBB, mockedVnfmAdapterServiceProvider);
181     }
182 }