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