cac7f872b3eda7d2c985c9cf41141a921ebcae10
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / workflow / tasks / ebb / loader / UserParamsServiceTraversalTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Modifications Copyright (c) 2020 Nokia
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.workflow.tasks.ebb.loader;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.onap.so.bpmn.BaseTaskTest;
30 import org.onap.so.bpmn.common.BBConstants;
31 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
32 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
33 import org.onap.so.client.exception.ExceptionBuilder;
34 import org.onap.so.db.catalog.beans.ConfigurationResource;
35 import org.onap.so.db.catalog.beans.CvnfcCustomization;
36 import org.onap.so.db.catalog.beans.HeatTemplate;
37 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
38 import org.onap.so.db.catalog.beans.VfModuleCustomization;
39 import org.onap.so.db.catalog.beans.Service;
40 import org.onap.so.db.catalog.beans.VfModule;
41 import org.onap.so.db.catalog.beans.CollectionResourceCustomization;
42 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
43 import org.onap.so.db.catalog.beans.HeatEnvironment;
44 import org.onap.so.db.catalog.client.CatalogDbClient;
45 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
46 import java.io.IOException;
47 import java.nio.file.Files;
48 import java.nio.file.Paths;
49 import java.util.ArrayList;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.stream.Collectors;
53 import static org.hamcrest.MatcherAssert.assertThat;
54 import static org.hamcrest.Matchers.is;
55 import static org.junit.Assert.assertEquals;
56 import static org.junit.Assert.assertTrue;
57 import static org.mockito.ArgumentMatchers.anyString;
58 import static org.mockito.Mockito.mock;
59
60 public class UserParamsServiceTraversalTest extends BaseTaskTest {
61
62     private static final String MACRO_ASSIGN_JSON = "Macro/ServiceMacroAssign.json";
63     private static final String MACRO_CREATE_JSON = "Macro/ServiceMacroAssignVnfAndPnf.json";
64     private static final String MACRO_ASSIGN_PNF_JSON = "Macro/ServiceMacroAssignPnf.json";
65     private static final String NETWORK_COLLECTION_JSON = "Macro/CreateNetworkCollection.json";
66     private static final String MACRO_CREATE_WITHOUT_RESOURCES_JSON = "Macro/ServiceMacroCreateWithoutResources.json";
67     private static final String MACRO_CREATE_SVC_SAME_MODEL_VNF_VFMODULE =
68             "Macro/ServiceMacroCreateMultipleSameModelVnfsAndVfModules.json";
69     private static final String serviceInstanceId = "123";
70     private DelegateExecution execution;
71     private CatalogDbClient mockCatalogDbClient;
72     private UserParamsServiceTraversal userParamsServiceTraversal;
73     private String requestAction;
74
75     @Before
76     public void before() throws Exception {
77         execution = new DelegateExecutionFake();
78         mockCatalogDbClient = mock(CatalogDbClient.class);
79         userParamsServiceTraversal = new UserParamsServiceTraversal(mockCatalogDbClient, mock(ExceptionBuilder.class));
80         requestAction = "assignInstance";
81     }
82
83     @Test
84     public void getResourceListFromUserParams() throws Exception {
85         initExecution(requestAction, readBpmnRequestFromFile(MACRO_CREATE_WITHOUT_RESOURCES_JSON), false);
86         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
87                 anyString(), anyString());
88
89         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
90                 getUserParams(), serviceInstanceId, requestAction);
91         List<WorkflowType> expected = List.of(WorkflowType.SERVICE);
92         List<WorkflowType> result =
93                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
94
95         assertEquals(1, resourceListFromUserParams.size());
96         assertThat(expected, is(result));
97     }
98
99     @Test
100     public void getResourceListFromUserParamsMultipleSameModelVnfVfModule() throws Exception {
101         initExecution("createInstance", readBpmnRequestFromFile(MACRO_CREATE_SVC_SAME_MODEL_VNF_VFMODULE), false);
102         Mockito.doReturn(getVfModuleCustomization("3bd19000-6d21-49f1-9eb3-ea76a6eac5e0", false))
103                 .when(mockCatalogDbClient)
104                 .getVfModuleCustomizationByModelCuztomizationUUID("3bd19000-6d21-49f1-9eb3-ea76a6eac5e0");
105         Mockito.doReturn(getVfModuleCustomization("83677d89-428a-407b-b4ec-738e68275d84", false))
106                 .when(mockCatalogDbClient)
107                 .getVfModuleCustomizationByModelCuztomizationUUID("83677d89-428a-407b-b4ec-738e68275d84");
108
109         List<Resource> resources = userParamsServiceTraversal.getResourceListFromUserParams(execution, getUserParams(),
110                 serviceInstanceId, requestAction);
111
112         assertEquals(7, resources.size());
113
114         Resource service = resources.get(0);
115         assertTrue(service.getResourceType() == WorkflowType.SERVICE);
116         assertEquals(2, service.getChildren().size());
117
118         Resource vnf1 = service.getChildren().get(0);
119         assertEquals(service, vnf1.getParent());
120         assertEquals("vnf-instanceName-1", vnf1.getInstanceName());
121         assertEquals("0d0ba1ee-6b7f-47fe-8266-2967993b2c08", vnf1.getResourceId());
122         assertEquals(2, vnf1.getChildren().size());
123
124         Resource vnf2 = service.getChildren().get(1);
125         assertEquals(service, vnf2.getParent());
126         assertEquals("vnf-instanceName-2", vnf2.getInstanceName());
127         assertEquals("0d0ba1ee-6b7f-47fe-8266-2967993b2c08", vnf2.getResourceId());
128         assertEquals(2, vnf2.getChildren().size());
129
130         Resource vfmodule1 = vnf1.getChildren().get(0);
131         assertEquals(vnf1, vfmodule1.getParent());
132         assertEquals("demo-network-1", vfmodule1.getInstanceName());
133         assertEquals("3bd19000-6d21-49f1-9eb3-ea76a6eac5e0", vfmodule1.getResourceId());
134
135         Resource vfmodule2 = vnf1.getChildren().get(1);
136         assertEquals(vnf1, vfmodule2.getParent());
137         assertEquals("demo-1", vfmodule2.getInstanceName());
138         assertEquals("83677d89-428a-407b-b4ec-738e68275d84", vfmodule2.getResourceId());
139
140         Resource vfmodule3 = vnf2.getChildren().get(0);
141         assertEquals(vnf2, vfmodule3.getParent());
142         assertEquals("demo-2", vfmodule3.getInstanceName());
143         assertEquals("83677d89-428a-407b-b4ec-738e68275d84", vfmodule3.getResourceId());
144
145         Resource vfmodule4 = vnf2.getChildren().get(1);
146         assertEquals(vnf2, vfmodule4.getParent());
147         assertEquals("demo-3", vfmodule4.getInstanceName());
148         assertEquals("83677d89-428a-407b-b4ec-738e68275d84", vfmodule4.getResourceId());
149     }
150
151     @Test
152     public void getResourceListFromUserParamsForVnfs() throws Exception {
153         initExecution(requestAction, readBpmnRequestFromFile(MACRO_ASSIGN_JSON), false);
154         Mockito.doReturn(getVfModuleCustomization("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f", true))
155                 .when(mockCatalogDbClient)
156                 .getVfModuleCustomizationByModelCuztomizationUUID("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f");
157         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
158                 anyString(), anyString());
159
160         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
161                 getUserParams(), serviceInstanceId, requestAction);
162         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.VNF, WorkflowType.VOLUMEGROUP,
163                 WorkflowType.VFMODULE, WorkflowType.CONFIGURATION);
164         List<WorkflowType> result =
165                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
166
167         assertEquals(5, resourceListFromUserParams.size());
168         assertThat(expected, is(result));
169     }
170
171     @Test
172     public void getResourceListFromUserParamsForVnfsWithPriorities() throws Exception {
173         initExecution(requestAction, readBpmnRequestFromFile(MACRO_CREATE_JSON), false);
174         Mockito.doReturn(getVfModuleCustomization("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f", true))
175                 .when(mockCatalogDbClient)
176                 .getVfModuleCustomizationByModelCuztomizationUUID("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f");
177         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
178                 anyString(), anyString());
179
180         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
181                 getUserParams(), serviceInstanceId, requestAction);
182         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.VNF, WorkflowType.VOLUMEGROUP,
183                 WorkflowType.VFMODULE, WorkflowType.CONFIGURATION, WorkflowType.PNF);
184         List<WorkflowType> result =
185                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
186
187         assertEquals(6, resourceListFromUserParams.size());
188         assertThat(expected, is(result));
189         assertEquals(2, resourceListFromUserParams.get(1).getChildren().get(1).getProcessingPriority());
190     }
191
192     @Test
193     public void getResourceListFromUserParamsForPnfs() throws Exception {
194         initExecution(requestAction, readBpmnRequestFromFile(MACRO_ASSIGN_PNF_JSON), false);
195         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
196                 anyString(), anyString());
197
198         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
199                 getUserParams(), serviceInstanceId, requestAction);
200         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.PNF);
201         List<WorkflowType> result =
202                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
203
204         assertEquals(2, resourceListFromUserParams.size());
205         assertThat(expected, is(result));
206     }
207
208     @Test
209     public void getResourceListFromUserParamsForNetworks() throws Exception {
210         requestAction = "createInstance";
211         initExecution(requestAction, readBpmnRequestFromFile(NETWORK_COLLECTION_JSON), false);
212         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
213                 anyString(), anyString());
214         Mockito.doReturn(getService()).when(mockCatalogDbClient).getServiceByID(anyString());
215         Mockito.doReturn(new NetworkCollectionResourceCustomization()).when(mockCatalogDbClient)
216                 .getNetworkCollectionResourceCustomizationByID(anyString());
217
218         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
219                 getUserParams(), serviceInstanceId, requestAction);
220         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.NETWORK, WorkflowType.NETWORK,
221                 WorkflowType.NETWORKCOLLECTION);
222         List<WorkflowType> result =
223                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
224
225         assertEquals(4, resourceListFromUserParams.size());
226         assertThat(expected, is(result));
227     }
228
229     @Test
230     public void getResourceListFromUserParamsBuildAndThrowExceptionWhenVfModuleAreEmpty() throws Exception {
231         initExecution(requestAction, readBpmnRequestFromFile(MACRO_ASSIGN_JSON), false);
232         VfModuleCustomization vfModuleCustomization = new VfModuleCustomization();
233         vfModuleCustomization.setVfModule(null);
234         Mockito.doReturn(vfModuleCustomization).when(mockCatalogDbClient)
235                 .getVfModuleCustomizationByModelCuztomizationUUID("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f");
236
237         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
238                 getUserParams(), serviceInstanceId, requestAction);
239         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.VNF);
240         List<WorkflowType> result =
241                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
242
243         assertEquals(2, resourceListFromUserParams.size());
244         assertThat(expected, is(result));
245     }
246
247     private List<Map<String, Object>> getUserParams() throws IOException {
248         String bpmnRequest = (String) execution.getVariable(BBConstants.G_BPMN_REQUEST);
249         ServiceInstancesRequest sIRequest = new ObjectMapper().readValue(bpmnRequest, ServiceInstancesRequest.class);
250         return sIRequest.getRequestDetails().getRequestParameters().getUserParams();
251     }
252
253     @Test
254     public void getResourceListFromUserParamsWhenUserParamsAreNull() throws Exception {
255         List<Resource> expectedResourceList = new ArrayList<>();
256         List<Resource> resultResourceList = userParamsServiceTraversal.getResourceListFromUserParams(execution, null,
257                 serviceInstanceId, requestAction);
258
259         assertEquals(expectedResourceList, resultResourceList);
260     }
261
262     private String readBpmnRequestFromFile(String fileName) throws IOException {
263         return new String(Files.readAllBytes(Paths.get("src/test/resources/__files/" + fileName)));
264     }
265
266     private void initExecution(String gAction, String bpmnRequest, boolean isAlaCarte) {
267         execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
268         execution.setVariable("requestAction", gAction);
269         execution.setVariable("bpmnRequest", bpmnRequest);
270         execution.setVariable("aLaCarte", isAlaCarte);
271         execution.setVariable("apiVersion", "7");
272     }
273
274     private Service getService() {
275         Service service = new Service();
276         List<CollectionResourceCustomization> collectionResourceCustomizations = new ArrayList<>();
277         CollectionResourceCustomization collectionResourceCustomization = new CollectionResourceCustomization();
278         collectionResourceCustomization.setModelCustomizationUUID("123");
279         collectionResourceCustomizations.add(collectionResourceCustomization);
280         service.setCollectionResourceCustomizations(collectionResourceCustomizations);
281         return service;
282     }
283
284     private VfModuleCustomization getVfModuleCustomization(String modelCustomizationUUID, boolean includeVolumeGroup) {
285         VfModuleCustomization vfModuleCustomization = new VfModuleCustomization();
286         vfModuleCustomization.setModelCustomizationUUID(modelCustomizationUUID);
287         VfModule vfModule = new VfModule();
288         if (includeVolumeGroup) {
289             vfModuleCustomization.setVolumeHeatEnv(new HeatEnvironment());
290             vfModule.setVolumeHeatTemplate(new HeatTemplate());
291             vfModule.setModelName("helm");
292         } else {
293             vfModuleCustomization.setHeatEnvironment(new HeatEnvironment());
294         }
295         vfModule.setModuleHeatTemplate(new HeatTemplate());
296         vfModuleCustomization.setVfModule(vfModule);
297         return vfModuleCustomization;
298     }
299
300     private List<CvnfcCustomization> getCvnfcCustomizations() {
301         ConfigurationResource configurationResource = new ConfigurationResource();
302         configurationResource.setToscaNodeType("FabricConfiguration");
303
304         CvnfcConfigurationCustomization cvnfcConfigurationCustomization = new CvnfcConfigurationCustomization();
305         cvnfcConfigurationCustomization.setConfigurationResource(configurationResource);
306         CvnfcCustomization cvnfcCustomization = new CvnfcCustomization();
307
308         List<CvnfcConfigurationCustomization> cvnfcConfigurationCustomizations = new ArrayList<>();
309         cvnfcConfigurationCustomizations.add(cvnfcConfigurationCustomization);
310         cvnfcCustomization.setCvnfcConfigurationCustomization(cvnfcConfigurationCustomizations);
311
312         List<CvnfcCustomization> cvnfcCustomizations = new ArrayList<>();
313         cvnfcCustomizations.add(cvnfcCustomization);
314         return cvnfcCustomizations;
315     }
316 }