Merge ":Sonar issues review, fix weak cryptography problem. Reformat code"
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / workflow / tasks / ebb / loader / ServiceEBBLoaderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * 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.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.camunda.bpm.engine.delegate.DelegateExecution;
26 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
27 import org.javatuples.Pair;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.mockito.Mock;
33 import org.onap.aai.domain.yang.Relationship;
34 import org.onap.aai.domain.yang.RelationshipList;
35 import org.onap.aai.domain.yang.ServiceInstance;
36 import org.onap.aaiclient.client.aai.entities.Relationships;
37 import org.onap.so.bpmn.BaseTaskTest;
38 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
39 import org.onap.so.bpmn.infrastructure.workflow.tasks.VrfBondingServiceException;
40 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
42 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
43 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
44 import org.onap.so.client.exception.ExceptionBuilder;
45 import org.onap.so.client.orchestration.AAIConfigurationResources;
46 import org.onap.so.db.catalog.beans.ConfigurationResourceCustomization;
47 import org.onap.so.db.catalog.beans.Service;
48 import org.onap.so.db.catalog.beans.CollectionResourceCustomization;
49 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
50 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
51 import org.onap.so.db.catalog.beans.CollectionResource;
52 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
53 import org.onap.so.db.catalog.beans.InstanceGroup;
54 import org.onap.so.db.catalog.client.CatalogDbClient;
55 import org.onap.so.serviceinstancebeans.RelatedInstance;
56 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
57 import java.io.IOException;
58 import java.nio.file.Files;
59 import java.nio.file.Paths;
60 import java.util.ArrayList;
61 import java.util.List;
62 import java.util.Optional;
63 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
64 import static org.junit.Assert.assertEquals;
65 import static org.junit.Assert.assertNull;
66 import static org.junit.Assert.assertTrue;
67 import static org.junit.Assert.assertFalse;
68 import static org.junit.Assert.assertNotNull;
69 import static org.junit.Assert.assertThat;
70 import static org.junit.Assert.fail;
71 import static org.mockito.Mockito.mock;
72 import static org.mockito.Mockito.doReturn;
73 import static org.mockito.Mockito.any;
74 import static org.mockito.Mockito.anyList;
75 import static org.mockito.Mockito.anyString;
76
77 public class ServiceEBBLoaderTest extends BaseTaskTest {
78
79     private static final String MACRO_ACTIVATE_DELETE_UNASSIGN_JSON = "Macro/ServiceMacroActivateDeleteUnassign.json";
80     private static final String MACRO_ASSIGN_JSON = "Macro/ServiceMacroAssign.json";
81
82     @Rule
83     public ExpectedException thrown = ExpectedException.none();
84
85     @Mock
86     protected Relationships relationships;
87
88     private DelegateExecution execution;
89     private ServiceEBBLoader serviceEBBLoader;
90     private UserParamsServiceTraversal mockUserParamsServiceTraversal;
91     private CatalogDbClient mockCatalogDbClient;
92     private VrfValidation mockVrfValidation;
93     private AAIConfigurationResources mockAaiConfigurationResources;
94     private WorkflowActionExtractResourcesAAI mockWorkflowActionExtractResourcesAAI;
95     private BBInputSetupUtils mockBbInputSetupUtils;
96     private BBInputSetup mockBbInputSetup;
97
98     @Before
99     public void before() throws Exception {
100         execution = new DelegateExecutionFake();
101         mockUserParamsServiceTraversal = mock(UserParamsServiceTraversal.class);
102         mockCatalogDbClient = mock(CatalogDbClient.class);
103         mockVrfValidation = mock(VrfValidation.class);
104         mockAaiConfigurationResources = mock(AAIConfigurationResources.class);
105         mockWorkflowActionExtractResourcesAAI = mock(WorkflowActionExtractResourcesAAI.class);
106         mockBbInputSetupUtils = mock(BBInputSetupUtils.class);
107         mockBbInputSetup = mock(BBInputSetup.class);
108         serviceEBBLoader = new ServiceEBBLoader(mockUserParamsServiceTraversal, mockCatalogDbClient, mockVrfValidation,
109                 mockAaiConfigurationResources, mockWorkflowActionExtractResourcesAAI, mockBbInputSetupUtils,
110                 mockBbInputSetup, mock(ExceptionBuilder.class));
111     }
112
113
114     @Test
115     public void getResourceListForServiceWithRequestActionAssignInstance()
116             throws IOException, VrfBondingServiceException {
117         String bpmnRequest = readBpmnRequestFromFile(MACRO_ASSIGN_JSON);
118         ObjectMapper mapper = new ObjectMapper();
119         ServiceInstancesRequest sIRequest = mapper.readValue(bpmnRequest, ServiceInstancesRequest.class);
120         String requestAction = "assignInstance";
121         String serviceInstanceId = "123";
122         String resourceId = "si0";
123         List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
124         doReturn(prepareListWithResources()).when(mockUserParamsServiceTraversal).getResourceListFromUserParams(any(),
125                 anyList(), anyString(), anyString());
126         List<Resource> resources = serviceEBBLoader.getResourceListForService(sIRequest, requestAction, execution,
127                 serviceInstanceId, resourceId, aaiResourceIds);
128         assertNotNull(resources);
129         assertEquals(resources.size(), 6);
130     }
131
132     @Test
133     public void findCatalogNetworkCollectionTest() {
134         Service service = new Service();
135         NetworkCollectionResourceCustomization networkCustomization = new NetworkCollectionResourceCustomization();
136         networkCustomization.setModelCustomizationUUID("123");
137         service.getCollectionResourceCustomizations().add(networkCustomization);
138         doReturn(networkCustomization).when(mockCatalogDbClient).getNetworkCollectionResourceCustomizationByID("123");
139         CollectionResourceCustomization customization =
140                 serviceEBBLoader.findCatalogNetworkCollection(execution, service);
141         assertNotNull(customization);
142     }
143
144     @Test
145     public void findCatalogNetworkCollectionEmptyTest() {
146         Service service = new Service();
147         NetworkCollectionResourceCustomization networkCustomization = new NetworkCollectionResourceCustomization();
148         networkCustomization.setModelCustomizationUUID("123");
149         service.getCollectionResourceCustomizations().add(networkCustomization);
150         CollectionResourceCustomization customization =
151                 serviceEBBLoader.findCatalogNetworkCollection(execution, service);
152         assertNull(customization);
153     }
154
155     @Test
156     public void findCatalogNetworkCollectionMoreThanOneTest() {
157         Service service = new Service();
158         NetworkCollectionResourceCustomization networkCustomization1 = new NetworkCollectionResourceCustomization();
159         networkCustomization1.setModelCustomizationUUID("123");
160         NetworkCollectionResourceCustomization networkCustomization2 = new NetworkCollectionResourceCustomization();
161         networkCustomization2.setModelCustomizationUUID("321");
162         service.getCollectionResourceCustomizations().add(networkCustomization1);
163         service.getCollectionResourceCustomizations().add(networkCustomization2);
164         doReturn(networkCustomization1).when(mockCatalogDbClient).getNetworkCollectionResourceCustomizationByID("123");
165         doReturn(networkCustomization2).when(mockCatalogDbClient).getNetworkCollectionResourceCustomizationByID("321");
166         serviceEBBLoader.findCatalogNetworkCollection(execution, service);
167         assertEquals("Found multiple Network Collections in the Service model, only one per Service is supported.",
168                 execution.getVariable("WorkflowActionErrorMessage"));
169     }
170
171     @Test
172     public void foundRelatedTest() {
173         List<Resource> resourceList = new ArrayList<>();
174         resourceList.add(new Resource(WorkflowType.PNF, "model customization id", false));
175         resourceList.add(new Resource(WorkflowType.VNF, "model customization id", false));
176         resourceList.add(new Resource(WorkflowType.NETWORK, "model customization id", false));
177         resourceList.add(new Resource(WorkflowType.NETWORKCOLLECTION, "model customization id", false));
178
179         assertTrue(serviceEBBLoader.foundRelated(resourceList));
180     }
181
182     @Test
183     public void containsWorkflowTypeTest() {
184         List<Resource> resourceList = new ArrayList<>();
185         resourceList.add(new Resource(WorkflowType.PNF, "resource id", false));
186         resourceList.add(new Resource(WorkflowType.VNF, "model customization id", false));
187         resourceList.add(new Resource(WorkflowType.NETWORK, "model customization id", false));
188         resourceList.add(new Resource(WorkflowType.NETWORKCOLLECTION, "model customization id", false));
189
190         assertTrue(serviceEBBLoader.containsWorkflowType(resourceList, WorkflowType.PNF));
191         assertTrue(serviceEBBLoader.containsWorkflowType(resourceList, WorkflowType.VNF));
192         assertTrue(serviceEBBLoader.containsWorkflowType(resourceList, WorkflowType.NETWORK));
193         assertTrue(serviceEBBLoader.containsWorkflowType(resourceList, WorkflowType.NETWORKCOLLECTION));
194         assertFalse(serviceEBBLoader.containsWorkflowType(resourceList, WorkflowType.CONFIGURATION));
195     }
196
197     @Test
198     public void traverseAAIServiceTest() {
199         List<Resource> resourceCounter = new ArrayList<>();
200         String resourceId = "si0";
201         List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
202
203         ServiceInstance serviceInstanceAAI = new ServiceInstance();
204         serviceInstanceAAI.setServiceInstanceId(resourceId);
205
206         org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstance = setServiceInstance();
207         setGenericVnf();
208         setVfModule(true);
209         setVolumeGroup();
210         setL3Network();
211         setCollection();
212         setConfiguration();
213
214         Configuration config = new Configuration();
215         config.setConfigurationId("testConfigurationId2");
216         serviceInstance.getConfigurations().add(config);
217
218         Relationship relationship1 = new Relationship();
219         relationship1.setRelatedTo("vnfc");
220         RelationshipList relationshipList1 = new RelationshipList();
221         relationshipList1.getRelationship().add(relationship1);
222
223         Relationship relationship2 = new Relationship();
224         relationship2.setRelatedTo("vpn-binding");
225         RelationshipList relationshipList2 = new RelationshipList();
226         relationshipList2.getRelationship().add(relationship2);
227
228         org.onap.aai.domain.yang.Configuration aaiConfiguration1 = new org.onap.aai.domain.yang.Configuration();
229         aaiConfiguration1.setConfigurationId("testConfigurationId");
230         aaiConfiguration1.setRelationshipList(relationshipList1);
231
232         org.onap.aai.domain.yang.Configuration aaiConfiguration2 = new org.onap.aai.domain.yang.Configuration();
233         aaiConfiguration2.setConfigurationId("testConfigurationId2");
234         aaiConfiguration2.setRelationshipList(relationshipList1);
235
236         try {
237             doReturn(serviceInstanceAAI).when(mockBbInputSetupUtils).getAAIServiceInstanceById(resourceId);
238             doReturn(serviceInstance).when(mockBbInputSetup).getExistingServiceInstance(serviceInstanceAAI);
239             doReturn(Optional.of(aaiConfiguration1)).when(mockAaiConfigurationResources)
240                     .getConfiguration("testConfigurationId");
241             doReturn(Optional.of(aaiConfiguration2)).when(mockAaiConfigurationResources)
242                     .getConfiguration("testConfigurationId2");
243             serviceEBBLoader.traverseAAIService(execution, resourceCounter, resourceId, aaiResourceIds);
244             assertEquals(8, resourceCounter.size());
245             assertTrue(resourceCounter.get(2).isBaseVfModule());
246             assertThat(aaiResourceIds, sameBeanAs(getExpectedResourceIds()));
247         } catch (Exception e) {
248             fail("Unexpected exception was thrown.");
249         }
250     }
251
252     @Test
253     public void traverseVrfConfigurationTest() throws VrfBondingServiceException, JsonProcessingException {
254         List<Resource> resource = new ArrayList<>();
255         List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
256
257         Service service = new Service();
258         List<ConfigurationResourceCustomization> resourceCustomizations = new ArrayList<>();
259
260         ConfigurationResourceCustomization configuration = new ConfigurationResourceCustomization();
261         configuration.setModelCustomizationUUID("123");
262         resourceCustomizations.add(configuration);
263         service.setConfigurationCustomizations(resourceCustomizations);
264
265         Relationship relationship = new Relationship();
266         relationship.setRelatedTo("vpn-binding");
267
268         RelationshipList relationshipList = new RelationshipList();
269         relationshipList.getRelationship().add(relationship);
270
271         org.onap.aai.domain.yang.L3Network aaiLocalNetwork = new org.onap.aai.domain.yang.L3Network();
272         aaiLocalNetwork.setNetworkId("localNetworkId");
273         aaiLocalNetwork.setRelationshipList(relationshipList);
274
275         RelatedInstance relatedVpnBinding = new RelatedInstance();
276         relatedVpnBinding.setInstanceId("vpnBindingInstanceId");
277         RelatedInstance relatedLocalNetwork = new RelatedInstance();
278         relatedLocalNetwork.setInstanceId("localNetworkInstanceId");
279
280
281         doReturn(aaiLocalNetwork).when(mockBbInputSetupUtils).getAAIL3Network("localNetworkInstanceId");
282
283         serviceEBBLoader.traverseVrfConfiguration(aaiResourceIds, resource, service, relatedVpnBinding,
284                 relatedLocalNetwork);
285         assertEquals(resource.size(), 1);
286         assertEquals(aaiResourceIds.size(), 0);
287     }
288
289     private List<Pair<WorkflowType, String>> getExpectedResourceIds() {
290         List<Pair<WorkflowType, String>> resourceIds = new ArrayList<>();
291         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VNF, "testVnfId1"));
292         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VFMODULE, "testVfModuleId1"));
293         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VOLUMEGROUP, "testVolumeGroupId1"));
294         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.NETWORK, "testNetworkId1"));
295         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.NETWORKCOLLECTION, "testId"));
296         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.CONFIGURATION, "testConfigurationId"));
297         resourceIds.add(new Pair<WorkflowType, String>(WorkflowType.CONFIGURATION, "testConfigurationId2"));
298         return resourceIds;
299     }
300
301     @Test
302     public void traverseCatalogDbServiceMultipleNetworkTest() throws IOException, VrfBondingServiceException {
303         execution.setVariable("testProcessKey", "testProcessKeyValue");
304         Service service = new Service();
305         List<NetworkResourceCustomization> networkCustomizations = new ArrayList<>();
306         NetworkResourceCustomization networkCust = new NetworkResourceCustomization();
307         networkCust.setModelCustomizationUUID("123");
308         networkCustomizations.add(networkCust);
309         service.setNetworkCustomizations(networkCustomizations);
310         NetworkCollectionResourceCustomization collectionResourceCustomization =
311                 new NetworkCollectionResourceCustomization();
312         collectionResourceCustomization.setModelCustomizationUUID("123");
313         CollectionResource collectionResource = new CollectionResource();
314         collectionResource.setToscaNodeType("NetworkCollection");
315         InstanceGroup instanceGroup = new InstanceGroup();
316         List<CollectionResourceInstanceGroupCustomization> collectionInstanceGroupCustomizations = new ArrayList<>();
317         CollectionResourceInstanceGroupCustomization collectionInstanceGroupCustomization =
318                 new CollectionResourceInstanceGroupCustomization();
319         collectionInstanceGroupCustomization.setSubInterfaceNetworkQuantity(3);
320         collectionInstanceGroupCustomizations.add(collectionInstanceGroupCustomization);
321         instanceGroup.setCollectionInstanceGroupCustomizations(collectionInstanceGroupCustomizations);
322         collectionResource.setInstanceGroup(instanceGroup);
323         collectionResourceCustomization.setCollectionResource(collectionResource);;
324         service.setModelUUID("abc");
325         service.getCollectionResourceCustomizations().add(collectionResourceCustomization);
326         service.getCollectionResourceCustomizations().add(collectionResourceCustomization);
327
328
329         doReturn(service).when(mockCatalogDbClient).getServiceByID("3c40d244-808e-42ca-b09a-256d83d19d0a");
330         doReturn(collectionResourceCustomization).when(mockCatalogDbClient)
331                 .getNetworkCollectionResourceCustomizationByID("123");
332         String bpmnRequest = readBpmnRequestFromFile(MACRO_ACTIVATE_DELETE_UNASSIGN_JSON);
333         ObjectMapper mapper = new ObjectMapper();
334         ServiceInstancesRequest sIRequest = mapper.readValue(bpmnRequest, ServiceInstancesRequest.class);
335
336         List<Resource> resource = new ArrayList<>();
337         List<Pair<WorkflowType, String>> aaiResourceIds = new ArrayList<>();
338
339         serviceEBBLoader.traverseCatalogDbService(execution, sIRequest, resource, aaiResourceIds);
340         assertEquals(resource.size(), 2);
341     }
342
343     private String readBpmnRequestFromFile(String fileName) throws IOException {
344         return new String(Files.readAllBytes(Paths.get("src/test/resources/__files/" + fileName)));
345     }
346
347     private List<Resource> prepareListWithResources() {
348         List<Resource> resourceList = new ArrayList<>();
349         resourceList.add(new Resource(WorkflowType.SERVICE, "3c40d244-808e-42ca-b09a-256d83d19d0a", false));
350         resourceList.add(new Resource(WorkflowType.VNF, "ab153b6e-c364-44c0-bef6-1f2982117f04", false));
351         resourceList.add(new Resource(WorkflowType.VOLUMEGROUP, "a25e8e8c-58b8-4eec-810c-97dcc1f5cb7f", false));
352         resourceList.add(new Resource(WorkflowType.VFMODULE, "72d9d1cd-f46d-447a-abdb-451d6fb05fa8", false));
353         resourceList.add(new Resource(WorkflowType.VFMODULE, "3c40d244-808e-42ca-b09a-256d83d19d0a", false));
354         resourceList.add(new Resource(WorkflowType.VFMODULE, "72d9d1cd-f46d-447a-abdb-451d6fb05fa8", false));
355         return resourceList;
356     }
357 }