ac994f0797606b4fb0b775618577b1eadddcb857
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / GroupBusinessLogicTest.java
1 /*
2
3  * Copyright (c) 2018 AT&T Intellectual Property.
4
5  *
6
7  * Licensed under the Apache License, Version 2.0 (the "License");
8
9  * you may not use this file except in compliance with the License.
10
11  * You may obtain a copy of the License at
12
13  *
14
15  *     http://www.apache.org/licenses/LICENSE-2.0
16
17  *
18
19  * Unless required by applicable law or agreed to in writing, software
20
21  * distributed under the License is distributed on an "AS IS" BASIS,
22
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
25  * See the License for the specific language governing permissions and
26
27  * limitations under the License.
28
29  */
30 package org.openecomp.sdc.be.components.impl;
31
32 import static java.util.Arrays.asList;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.junit.jupiter.api.Assertions.assertThrows;
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.ArgumentMatchers.anyList;
37 import static org.mockito.ArgumentMatchers.anyMap;
38 import static org.mockito.ArgumentMatchers.anyObject;
39 import static org.mockito.ArgumentMatchers.anyString;
40 import static org.mockito.Mockito.when;
41
42 import fj.data.Either;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.HashMap;
46 import java.util.LinkedList;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.Set;
50 import org.junit.jupiter.api.Assertions;
51 import org.junit.jupiter.api.BeforeEach;
52 import org.junit.jupiter.api.Test;
53 import org.junit.jupiter.api.extension.ExtendWith;
54 import org.mockito.InjectMocks;
55 import org.mockito.Mock;
56 import org.mockito.junit.jupiter.MockitoExtension;
57 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
58 import org.openecomp.sdc.be.components.impl.policy.PolicyTargetsUpdateHandler;
59 import org.openecomp.sdc.be.components.validation.AccessValidations;
60 import org.openecomp.sdc.be.config.Configuration;
61 import org.openecomp.sdc.be.config.ConfigurationManager;
62 import org.openecomp.sdc.be.dao.api.ActionStatus;
63 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
64 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
65 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
66 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
67 import org.openecomp.sdc.be.impl.ComponentsUtils;
68 import org.openecomp.sdc.be.info.ArtifactTemplateInfo;
69 import org.openecomp.sdc.be.model.Component;
70 import org.openecomp.sdc.be.model.DataTypeDefinition;
71 import org.openecomp.sdc.be.model.GroupDefinition;
72 import org.openecomp.sdc.be.model.GroupInstance;
73 import org.openecomp.sdc.be.model.GroupInstanceProperty;
74 import org.openecomp.sdc.be.model.GroupTypeDefinition;
75 import org.openecomp.sdc.be.model.PropertyDefinition;
76 import org.openecomp.sdc.be.model.Resource;
77 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
78 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
79 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation;
80 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
81 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
82 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
83 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
84 import org.openecomp.sdc.be.model.tosca.ToscaType;
85 import org.openecomp.sdc.common.api.ConfigurationSource;
86 import org.openecomp.sdc.common.api.Constants;
87 import org.openecomp.sdc.common.impl.ExternalConfiguration;
88 import org.openecomp.sdc.common.impl.FSConfigurationSource;
89 import org.openecomp.sdc.exception.ResponseFormat;
90
91 @ExtendWith(MockitoExtension.class)
92 class GroupBusinessLogicTest {
93
94     static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
95         "src/test/resources/config/catalog-be");
96     static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
97     @InjectMocks
98     private GroupBusinessLogic test;
99     @Mock
100     private ApplicationDataTypeCache applicationDataTypeCache;
101     @Mock
102     private ComponentsUtils componentsUtils;
103     @Mock
104     private IGroupTypeOperation groupTypeOperation;
105     @Mock
106     private GroupsOperation groupsOperation;
107     @Mock
108     private AccessValidations accessValidations;
109     @Mock
110     private ToscaOperationFacade toscaOperationFacade;
111     @Mock
112     private PropertyOperation propertyOperation;
113     @Mock
114     private JanusGraphDao janusGraphDao;
115     @Mock
116     private PolicyTargetsUpdateHandler policyTargetsUpdateHandler;
117
118     @BeforeEach
119     public void setUp() throws Exception {
120         test.setApplicationDataTypeCache(applicationDataTypeCache);
121         test.setToscaOperationFacade(toscaOperationFacade);
122         test.setPropertyOperation(propertyOperation);
123         test.setComponentsUtils(componentsUtils);
124         test.setJanusGraphDao(janusGraphDao);
125     }
126
127     @Test
128     void testCreateGroups_NoDataType() {
129         Either<List<GroupDefinition>, ResponseFormat> result;
130         Component component = new Resource();
131         List<GroupDefinition> groupDefinitions = new ArrayList<>();
132         GroupDefinition groupDefinition = new GroupDefinition();
133         groupDefinitions.add(groupDefinition);
134         result = test.createGroups(component, groupDefinitions, true);
135         assertThat(result.isRight()).isTrue();
136     }
137
138     @Test
139     void testCreateGroups() {
140         Either<List<GroupDefinition>, ResponseFormat> result;
141         Component component = new Resource();
142         component.setUniqueId("id");
143         List<GroupDefinition> groupDefinitions = new ArrayList<>();
144         GroupDefinition groupDefinition = new GroupDefinition();
145         groupDefinition.setName("name");
146         groupDefinitions.add(groupDefinition);
147         groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
148         GroupTypeDefinition groupTypeDefinition = new GroupTypeDefinition();
149         Map<String, DataTypeDefinition> map = new HashMap<>();
150         when(groupTypeOperation.getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, component.getModel(), true))
151             .thenReturn(Either.left(groupTypeDefinition));
152         when(groupsOperation.createGroups(any(Component.class), anyMap())).thenReturn(Either.left(groupDefinitions));
153         when(groupsOperation.addCalculatedCapabilitiesWithProperties(anyString(), anyMap(), anyMap())).thenReturn(StorageOperationStatus.OK);
154         result = test.createGroups(component, groupDefinitions, true);
155         assertThat(result.isLeft()).isTrue();
156     }
157
158     @Test
159     void testValidUpdateVfGrpNameOnGraph() {
160         Either<List<GroupDefinition>, ResponseFormat> result;
161         Component component = new Resource();
162         component.setSystemName("name");
163
164         List<GroupDefinition> groupDefinitions = new ArrayList<>();
165         GroupDefinition groupDefinition = new GroupDefinition();
166         groupDefinition.setName("grp_one-1. ::module-1");
167         groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
168         groupDefinition.setDescription("desc");
169         groupDefinitions.add(groupDefinition);
170
171         result = test.validateUpdateVfGroupNamesOnGraph(groupDefinitions, component);
172         assertThat(result.isLeft()).isTrue();
173     }
174
175     @Test
176     void testValidAndUpdateGrpInstancePropValues_fail() {
177         Either<GroupInstance, ResponseFormat> result;
178         String componentId = "id";
179         String instanceId = "id";
180         GroupInstance oldGroupInstance = new GroupInstance();
181         List<GroupInstanceProperty> newProperties = new ArrayList<>();
182         List<PropertyDataDefinition> properties = new LinkedList<>();
183         properties.add(new PropertyDataDefinition());
184         oldGroupInstance.setProperties(properties);
185         try {
186             result = test.validateAndUpdateGroupInstancePropertyValues(componentId, instanceId, oldGroupInstance, newProperties);
187         } catch (ComponentException e) {
188             assertThat(e.getActionStatus()).isEqualTo(ActionStatus.GENERAL_ERROR);
189         }
190     }
191
192     @Test
193     void testCreateGroup() {
194         GroupDefinition result;
195         String componentId = "id";
196         String grpType = "grpType";
197         String userId = "userId";
198         ComponentTypeEnum compTypeEnum = ComponentTypeEnum.RESOURCE;
199         Component component = new Resource();
200         component.setName("name");
201         component.setUniqueId(componentId);
202         component.setToscaType(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
203         List<GroupDefinition> groupDefList = new ArrayList<>();
204         Map<String, Set<String>> excludedGroupTypesMap = new HashMap<>();
205         GroupTypeDefinition groupTypeDefinition = new GroupTypeDefinition();
206         Map<String, DataTypeDefinition> map = new HashMap<>();
207         when(accessValidations.validateUserCanWorkOnComponent(componentId, compTypeEnum, userId, "CreateGroup")).thenReturn(component);
208
209         ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
210         configurationManager.setConfiguration(new Configuration());
211         configurationManager.getConfiguration().setExcludedGroupTypesMapping(excludedGroupTypesMap);
212
213         List<PropertyDefinition> properties = asList(
214             buildProperty("network_collection_type", "l3-network", "network collection type, defined with default value"));
215         groupTypeDefinition.setProperties(properties);
216         when(groupTypeOperation.getLatestGroupTypeByType(grpType, component.getModel(), false)).thenReturn(Either.left(groupTypeDefinition));
217         when(toscaOperationFacade.canAddGroups(componentId)).thenReturn(true);
218         when(groupTypeOperation.getLatestGroupTypeByType(grpType, component.getModel(), true)).thenReturn(Either.left(groupTypeDefinition));
219         when(propertyOperation.checkInnerType(any(PropertyDefinition.class))).thenReturn(Either.left("ok"));
220         when(propertyOperation.validateAndUpdatePropertyValue("string", null, "ok", map)).thenReturn(Either.left(component));
221         when(groupsOperation.addGroups(any(Resource.class), any())).thenReturn(Either.left(groupDefList));
222         when(groupsOperation.addCalculatedCapabilitiesWithProperties(anyString(), anyMap(), anyMap())).thenReturn(StorageOperationStatus.OK);
223         result = test.createGroup(componentId, compTypeEnum, grpType, userId);
224         assertThat(result.getClass()).isAssignableFrom(GroupDefinition.class);
225     }
226
227     private PropertyDefinition buildProperty(String name, String defaultValue, String description) {
228         PropertyDefinition property = new PropertyDefinition();
229         property.setName(name);
230         property.setDefaultValue(defaultValue);
231         property.setRequired(true);
232         property.setDescription(description);
233         property.setType(ToscaType.STRING.name().toLowerCase());
234         return property;
235     }
236
237     @Test
238     void testUpdateGroup() throws Exception {
239
240         Component component = new Resource();
241         GroupDefinition updatedGroup = new GroupDefinition();
242         List<GroupDefinition> grpdefList = new ArrayList<>();
243         updatedGroup.setName("GRP.01");
244         grpdefList.add(updatedGroup);
245         component.setUniqueId("GRP.01");
246         component.setGroups(grpdefList);
247         updatedGroup.setUniqueId("GRP.01");
248         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "UpdateGroup")).thenReturn(component);
249         when(groupsOperation.updateGroup(component, updatedGroup)).thenReturn(Either.left(updatedGroup));
250         GroupDefinition Gdefinition = test.updateGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
251             "USR01", updatedGroup);
252         Assertions.assertEquals(Gdefinition, updatedGroup);
253     }
254
255     @Test
256     void testUpdateGroup_Invalidname() throws Exception {
257
258         Component component = new Resource();
259         GroupDefinition updatedGroup = new GroupDefinition();
260         List<GroupDefinition> grpdefList = new ArrayList<>();
261         updatedGroup.setName("GRP~01");
262         updatedGroup.setUniqueId("GRP.01");
263         grpdefList.add(updatedGroup);
264         component.setUniqueId("GRP.01");
265         component.setGroups(grpdefList);
266         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "UpdateGroup")).thenReturn(component);
267         assertThrows(ComponentException.class, () -> {
268             GroupDefinition Gdefinition = test.updateGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
269                 "USR01", updatedGroup);
270         });
271     }
272
273     @Test
274     void testDeleteGroup_exception() throws Exception {
275
276         Component component = new Resource();
277         GroupDefinition updatedGroup = new GroupDefinition();
278         List<GroupDefinition> grpdefList = new ArrayList<>();
279         updatedGroup.setName("GRP~01");
280         updatedGroup.setUniqueId("GRP.01");
281         grpdefList.add(updatedGroup);
282         component.setUniqueId("GRP.01");
283         component.setGroups(grpdefList);
284         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "DeleteGroup")).thenReturn(component);
285         when(groupsOperation.deleteGroups(anyObject(), anyList())).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
286
287         when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
288         assertThrows(ComponentException.class, () -> {
289             GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
290                 "USR01");
291         });
292     }
293
294     @Test
295     void testDeleteGroup() {
296
297         Component component = new Resource();
298         List<GroupDefinition> groupDefList = new ArrayList<>();
299         GroupDefinition updatedGroup = new GroupDefinition();
300         updatedGroup.setName("GRP~01");
301         updatedGroup.setUniqueId("GRP.01");
302         groupDefList.add(updatedGroup);
303         component.setUniqueId("GRP.01");
304         component.setGroups(groupDefList);
305         List<GroupDefinition> groupDefListCopy = new ArrayList<>();
306         groupDefListCopy.add(updatedGroup);
307         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "DeleteGroup")).thenReturn(component);
308         when(groupsOperation.deleteGroups(anyObject(), anyList())).thenReturn(Either.left(groupDefListCopy));
309         when(groupsOperation.deleteCalculatedCapabilitiesWithProperties(anyString(), anyObject())).thenReturn(StorageOperationStatus.OK);
310         when(policyTargetsUpdateHandler.removePoliciesTargets(anyObject(), anyString(), anyObject())).thenReturn(ActionStatus.OK);
311
312         GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
313             "USR01");
314         Assertions.assertEquals(Gdefinition, updatedGroup);
315     }
316
317     @Test
318     void testValidateGenerateVfModuleGroupNames_pass() {
319
320         final List<ArtifactTemplateInfo> allGroups = new ArrayList<>();
321         final ArtifactTemplateInfo artifactTemplateInfo1 = new ArtifactTemplateInfo();
322         final ArtifactTemplateInfo artifactTemplateInfo2 = new ArtifactTemplateInfo();
323         artifactTemplateInfo1.setGroupName("ArtTmpInfoName1");
324         artifactTemplateInfo1.setDescription("ArtTmpInfoDesc1");
325         artifactTemplateInfo2.setGroupName("ArtTmpInfoName2");
326         artifactTemplateInfo2.setDescription("ArtTmpInfoDesc2");
327         allGroups.add(artifactTemplateInfo1);
328         allGroups.add(artifactTemplateInfo2);
329
330         final Either<Boolean, ResponseFormat> result = test.validateGenerateVfModuleGroupNames(allGroups, "resourceSystemName", 0);
331
332         Assertions.assertEquals(2, allGroups.size());
333         Assertions.assertEquals("resourceSystemName..ArtTmpInfoDesc1..module-0", allGroups.get(0).getGroupName());
334         Assertions.assertEquals("resourceSystemName..ArtTmpInfoDesc2..module-1", allGroups.get(1).getGroupName());
335         Assertions.assertTrue(result.left().value());
336     }
337
338     @Test
339     void testValidateGenerateVfModuleGroupNames_emptyArtifactDescriptionFail() {
340
341         final List<ArtifactTemplateInfo> allGroups = new ArrayList<>();
342         allGroups.add(new ArtifactTemplateInfo());
343         final ResponseFormat expectedResponse = new ResponseFormat(400);
344         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_VF_MODULE_NAME)).thenReturn(new ResponseFormat(400));
345
346         final Either<Boolean, ResponseFormat> result = test.validateGenerateVfModuleGroupNames(allGroups, "resourceSystemName", 0);
347
348         Assertions.assertEquals(1, allGroups.size());
349         Assertions.assertNull(allGroups.get(0).getGroupName());
350         Assertions.assertEquals(expectedResponse.getStatus(), result.right().value().getStatus());
351      }
352
353     @Test
354     void testValidateGenerateVfModuleGroupNames_invalidArtifactDescriptionFail() {
355
356         final List<ArtifactTemplateInfo> allGroups = new ArrayList<>();
357         final ArtifactTemplateInfo artifactTemplateInfo1 = new ArtifactTemplateInfo();
358         artifactTemplateInfo1.setGroupName("ArtTmpInfoName1");
359         artifactTemplateInfo1.setDescription("!ArtTmpInfoDesc1!");
360         allGroups.add(artifactTemplateInfo1);
361         final ResponseFormat expectedResponse = new ResponseFormat(400);
362         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_VF_MODULE_NAME)).thenReturn(new ResponseFormat(400));
363
364         final Either<Boolean, ResponseFormat> result = test.validateGenerateVfModuleGroupNames(allGroups, "resourceSystemName", 0);
365
366         Assertions.assertEquals(1, allGroups.size());
367         Assertions.assertEquals("ArtTmpInfoName1", allGroups.get(0).getGroupName());
368         Assertions.assertEquals(expectedResponse.getStatus(), result.right().value().getStatus());
369     }
370
371     @Test
372     void testGetNextVfModuleNameCounter_groupNull() {
373
374         final int resultCollection = test.getNextVfModuleNameCounter((Collection <GroupDefinition>) null);
375         final int resultMap = test.getNextVfModuleNameCounter((Map<String, GroupDefinition>) null);
376
377         Assertions.assertEquals(0, resultCollection);
378         Assertions.assertEquals(0, resultMap);
379     }
380
381     @Test
382     void testGetNextVfModuleNameCounter_groupEmpty() {
383
384         final int resultCollection = test.getNextVfModuleNameCounter(new ArrayList<>());
385         final int resultMap = test.getNextVfModuleNameCounter(new HashMap<>());
386
387         Assertions.assertEquals(0, resultCollection);
388         Assertions.assertEquals(0, resultMap);
389     }
390
391     @Test
392     void testGetNextVfModuleNameCounter_takesCollectionPass() {
393
394         final Collection<GroupDefinition> group = new ArrayList<>();
395         final GroupDefinition groupDefinition1 = new GroupDefinition();
396         final GroupDefinition groupDefinition2 = new GroupDefinition();
397         final GroupDefinition groupDefinition3 = new GroupDefinition();
398         groupDefinition1.setName("resourceSystemName..ArtTmpInfoDesc1..module-0");
399         groupDefinition2.setName("resourceSystemName..ArtTmpInfoDesc1..module-9");
400         groupDefinition3.setName("resourceSystemName..ArtTmpInfoDesc2..module-1");
401         group.add(groupDefinition1);
402         group.add(groupDefinition2);
403         group.add(groupDefinition3);
404
405         final int result = test.getNextVfModuleNameCounter(group);
406
407         Assertions.assertEquals(10, result);
408     }
409
410     @Test
411     void testGetNextVfModuleNameCounter_takesMapPass() {
412
413         final Map<String, GroupDefinition> group = new HashMap<>();
414         final GroupDefinition groupDefinition1 = new GroupDefinition();
415         final GroupDefinition groupDefinition2 = new GroupDefinition();
416         final GroupDefinition groupDefinition3 = new GroupDefinition();
417         groupDefinition1.setName("resourceSystemName..ArtTmpInfoDesc1..module-0");
418         groupDefinition2.setName("resourceSystemName..ArtTmpInfoDesc1..module-9");
419         groupDefinition3.setName("resourceSystemName..ArtTmpInfoDesc2..module-1");
420
421         group.put("first", groupDefinition1);
422         group.put("second", groupDefinition2);
423         group.put("third", groupDefinition3);
424
425         final int result = test.getNextVfModuleNameCounter(group);
426
427         Assertions.assertEquals(10, result);
428     }
429 }