SO refactor - extract Workflow type Service
[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.mockito.ArgumentMatchers.anyString;
57 import static org.mockito.Mockito.mock;
58
59 public class UserParamsServiceTraversalTest extends BaseTaskTest {
60
61     private static final String MACRO_ASSIGN_JSON = "Macro/ServiceMacroAssign.json";
62     private static final String MACRO_ASSIGN_PNF_JSON = "Macro/ServiceMacroAssignPnf.json";
63     private static final String NETWORK_COLLECTION_JSON = "Macro/CreateNetworkCollection.json";
64     private static final String serviceInstanceId = "123";
65     private DelegateExecution execution;
66     private CatalogDbClient mockCatalogDbClient;
67     private UserParamsServiceTraversal userParamsServiceTraversal;
68     private String requestAction;
69
70     @Before
71     public void before() throws Exception {
72         execution = new DelegateExecutionFake();
73         mockCatalogDbClient = mock(CatalogDbClient.class);
74         userParamsServiceTraversal = new UserParamsServiceTraversal(mockCatalogDbClient, mock(ExceptionBuilder.class));
75         requestAction = "assignInstance";
76     }
77
78     @Test
79     public void getResourceListFromUserParamsForVnfs() throws Exception {
80         initExecution(requestAction, readBpmnRequestFromFile(MACRO_ASSIGN_JSON), false);
81         Mockito.doReturn(getVfModuleCustomization()).when(mockCatalogDbClient)
82                 .getVfModuleCustomizationByModelCuztomizationUUID("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f");
83         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
84                 anyString(), anyString());
85
86         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
87                 getUserParams(), serviceInstanceId, requestAction);
88         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.VNF, WorkflowType.VOLUMEGROUP,
89                 WorkflowType.VFMODULE, WorkflowType.CONFIGURATION);
90         List<WorkflowType> result =
91                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
92
93         assertEquals(5, resourceListFromUserParams.size());
94         assertThat(expected, is(result));
95     }
96
97     @Test
98     public void getResourceListFromUserParamsForPnfs() throws Exception {
99         initExecution(requestAction, readBpmnRequestFromFile(MACRO_ASSIGN_PNF_JSON), false);
100         Mockito.doReturn(getVfModuleCustomization()).when(mockCatalogDbClient)
101                 .getVfModuleCustomizationByModelCuztomizationUUID("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f");
102         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
103                 anyString(), anyString());
104
105         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
106                 getUserParams(), serviceInstanceId, requestAction);
107         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.PNF);
108         List<WorkflowType> result =
109                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
110
111         assertEquals(2, resourceListFromUserParams.size());
112         assertThat(expected, is(result));
113     }
114
115     @Test
116     public void getResourceListFromUserParamsForNetworks() throws Exception {
117         requestAction = "createInstance";
118         initExecution(requestAction, readBpmnRequestFromFile(NETWORK_COLLECTION_JSON), false);
119         Mockito.doReturn(getVfModuleCustomization()).when(mockCatalogDbClient)
120                 .getVfModuleCustomizationByModelCuztomizationUUID("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f");
121         Mockito.doReturn(getCvnfcCustomizations()).when(mockCatalogDbClient).getCvnfcCustomization(anyString(),
122                 anyString(), anyString());
123         Mockito.doReturn(getService()).when(mockCatalogDbClient).getServiceByID(anyString());
124         Mockito.doReturn(new NetworkCollectionResourceCustomization()).when(mockCatalogDbClient)
125                 .getNetworkCollectionResourceCustomizationByID(anyString());
126
127         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
128                 getUserParams(), serviceInstanceId, requestAction);
129         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.NETWORK, WorkflowType.NETWORK,
130                 WorkflowType.NETWORKCOLLECTION);
131         List<WorkflowType> result =
132                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
133
134         assertEquals(4, resourceListFromUserParams.size());
135         assertThat(expected, is(result));
136     }
137
138     @Test
139     public void getResourceListFromUserParamsBuildAndThrowExceptionWhenVfModuleAreEmpty() throws Exception {
140         initExecution(requestAction, readBpmnRequestFromFile(MACRO_ASSIGN_JSON), false);
141         VfModuleCustomization vfModuleCustomization = new VfModuleCustomization();
142         vfModuleCustomization.setVfModule(null);
143         Mockito.doReturn(vfModuleCustomization).when(mockCatalogDbClient)
144                 .getVfModuleCustomizationByModelCuztomizationUUID("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f");
145
146         List<Resource> resourceListFromUserParams = userParamsServiceTraversal.getResourceListFromUserParams(execution,
147                 getUserParams(), serviceInstanceId, requestAction);
148         List<WorkflowType> expected = List.of(WorkflowType.SERVICE, WorkflowType.VNF);
149         List<WorkflowType> result =
150                 resourceListFromUserParams.stream().map(Resource::getResourceType).collect(Collectors.toList());
151
152         assertEquals(2, resourceListFromUserParams.size());
153         assertThat(expected, is(result));
154     }
155
156     private List<Map<String, Object>> getUserParams() throws IOException {
157         String bpmnRequest = (String) execution.getVariable(BBConstants.G_BPMN_REQUEST);
158         ServiceInstancesRequest sIRequest = new ObjectMapper().readValue(bpmnRequest, ServiceInstancesRequest.class);
159         return sIRequest.getRequestDetails().getRequestParameters().getUserParams();
160     }
161
162     @Test
163     public void getResourceListFromUserParamsWhenUserParamsAreNull() throws Exception {
164         List<Resource> expectedResourceList = new ArrayList<>();
165         List<Resource> resultResourceList = userParamsServiceTraversal.getResourceListFromUserParams(execution, null,
166                 serviceInstanceId, requestAction);
167
168         assertEquals(expectedResourceList, resultResourceList);
169     }
170
171     private String readBpmnRequestFromFile(String fileName) throws IOException {
172         return new String(Files.readAllBytes(Paths.get("src/test/resources/__files/" + fileName)));
173     }
174
175     private void initExecution(String gAction, String bpmnRequest, boolean isAlaCarte) {
176         execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
177         execution.setVariable("requestAction", gAction);
178         execution.setVariable("bpmnRequest", bpmnRequest);
179         execution.setVariable("aLaCarte", isAlaCarte);
180         execution.setVariable("apiVersion", "7");
181     }
182
183     private Service getService() {
184         Service service = new Service();
185         List<CollectionResourceCustomization> collectionResourceCustomizations = new ArrayList<>();
186         CollectionResourceCustomization collectionResourceCustomization = new CollectionResourceCustomization();
187         collectionResourceCustomization.setModelCustomizationUUID("123");
188         collectionResourceCustomizations.add(collectionResourceCustomization);
189         service.setCollectionResourceCustomizations(collectionResourceCustomizations);
190         return service;
191     }
192
193     private VfModuleCustomization getVfModuleCustomization() {
194         VfModuleCustomization vfModuleCustomization = new VfModuleCustomization();
195         vfModuleCustomization.setVolumeHeatEnv(new HeatEnvironment());
196         vfModuleCustomization.setModelCustomizationUUID("a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f");
197         VfModule vfModule = new VfModule();
198         vfModule.setVolumeHeatTemplate(new HeatTemplate());
199         vfModule.setModelName("helm");
200         vfModule.setModuleHeatTemplate(new HeatTemplate());
201         vfModuleCustomization.setVfModule(vfModule);
202         return vfModuleCustomization;
203     }
204
205     private List<CvnfcCustomization> getCvnfcCustomizations() {
206         ConfigurationResource configurationResource = new ConfigurationResource();
207         configurationResource.setToscaNodeType("FabricConfiguration");
208
209         CvnfcConfigurationCustomization cvnfcConfigurationCustomization = new CvnfcConfigurationCustomization();
210         cvnfcConfigurationCustomization.setConfigurationResource(configurationResource);
211         CvnfcCustomization cvnfcCustomization = new CvnfcCustomization();
212
213         List<CvnfcConfigurationCustomization> cvnfcConfigurationCustomizations = new ArrayList<>();
214         cvnfcConfigurationCustomizations.add(cvnfcConfigurationCustomization);
215         cvnfcCustomization.setCvnfcConfigurationCustomization(cvnfcConfigurationCustomizations);
216
217         List<CvnfcCustomization> cvnfcCustomizations = new ArrayList<>();
218         cvnfcCustomizations.add(cvnfcCustomization);
219         return cvnfcCustomizations;
220     }
221 }