Fix locally failing TCs in catalog-be
[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.HashMap;
45 import java.util.LinkedList;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Set;
49 import org.junit.Assert;
50 import org.junit.jupiter.api.BeforeEach;
51 import org.junit.jupiter.api.Test;
52 import org.junit.jupiter.api.extension.ExtendWith;
53 import org.mockito.InjectMocks;
54 import org.mockito.Mock;
55 import org.mockito.junit.jupiter.MockitoExtension;
56 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
57 import org.openecomp.sdc.be.components.impl.policy.PolicyTargetsUpdateHandler;
58 import org.openecomp.sdc.be.components.validation.AccessValidations;
59 import org.openecomp.sdc.be.config.Configuration;
60 import org.openecomp.sdc.be.config.ConfigurationManager;
61 import org.openecomp.sdc.be.dao.api.ActionStatus;
62 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
63 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
64 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
65 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
66 import org.openecomp.sdc.be.impl.ComponentsUtils;
67 import org.openecomp.sdc.be.model.Component;
68 import org.openecomp.sdc.be.model.DataTypeDefinition;
69 import org.openecomp.sdc.be.model.GroupDefinition;
70 import org.openecomp.sdc.be.model.GroupInstance;
71 import org.openecomp.sdc.be.model.GroupInstanceProperty;
72 import org.openecomp.sdc.be.model.GroupTypeDefinition;
73 import org.openecomp.sdc.be.model.PropertyDefinition;
74 import org.openecomp.sdc.be.model.Resource;
75 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
76 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
77 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation;
78 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
79 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
80 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
81 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
82 import org.openecomp.sdc.be.model.tosca.ToscaType;
83 import org.openecomp.sdc.common.api.ConfigurationSource;
84 import org.openecomp.sdc.common.api.Constants;
85 import org.openecomp.sdc.common.impl.ExternalConfiguration;
86 import org.openecomp.sdc.common.impl.FSConfigurationSource;
87 import org.openecomp.sdc.exception.ResponseFormat;
88
89 @ExtendWith(MockitoExtension.class)
90 class GroupBusinessLogicTest {
91
92     static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
93         "src/test/resources/config/catalog-be");
94     static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
95     @InjectMocks
96     private GroupBusinessLogic test;
97     @Mock
98     private ApplicationDataTypeCache dataTypeCache;
99     @Mock
100     private ComponentsUtils componentsUtils;
101     @Mock
102     private IGroupTypeOperation groupTypeOperation;
103     @Mock
104     private GroupsOperation groupsOperation;
105     @Mock
106     private AccessValidations accessValidations;
107     @Mock
108     private ToscaOperationFacade toscaOperationFacade;
109     @Mock
110     private PropertyOperation propertyOperation;
111     @Mock
112     private JanusGraphDao janusGraphDao;
113     @Mock
114     private PolicyTargetsUpdateHandler policyTargetsUpdateHandler;
115
116     @BeforeEach
117     public void setUp() throws Exception {
118         test.setDataTypeCache(dataTypeCache);
119         test.setToscaOperationFacade(toscaOperationFacade);
120         test.setPropertyOperation(propertyOperation);
121         test.setComponentsUtils(componentsUtils);
122         test.setJanusGraphDao(janusGraphDao);
123     }
124
125     @Test
126     void testCreateGroups_NoDataType() {
127         Either<List<GroupDefinition>, ResponseFormat> result;
128         Component component = new Resource();
129         List<GroupDefinition> groupDefinitions = new ArrayList<>();
130         GroupDefinition groupDefinition = new GroupDefinition();
131         groupDefinitions.add(groupDefinition);
132         when(dataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
133         result = test.createGroups(component, groupDefinitions, true);
134         assertThat(result.isRight()).isTrue();
135     }
136
137     @Test
138     void testCreateGroups() {
139         Either<List<GroupDefinition>, ResponseFormat> result;
140         Component component = new Resource();
141         component.setUniqueId("id");
142         List<GroupDefinition> groupDefinitions = new ArrayList<>();
143         GroupDefinition groupDefinition = new GroupDefinition();
144         groupDefinition.setName("name");
145         groupDefinitions.add(groupDefinition);
146         groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
147         GroupTypeDefinition groupTypeDefinition = new GroupTypeDefinition();
148         Map<String, DataTypeDefinition> map = new HashMap<>();
149         when(dataTypeCache.getAll()).thenReturn(Either.left(map));
150         when(groupTypeOperation.getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, true)).thenReturn(Either.left(groupTypeDefinition));
151         when(groupsOperation.createGroups(any(Component.class), anyMap())).thenReturn(Either.left(groupDefinitions));
152         when(groupsOperation.addCalculatedCapabilitiesWithProperties(anyString(), anyMap(), anyMap())).thenReturn(StorageOperationStatus.OK);
153         result = test.createGroups(component, groupDefinitions, true);
154         assertThat(result.isLeft()).isTrue();
155     }
156
157     @Test
158     void testValidUpdateVfGrpNameOnGraph() {
159         Either<List<GroupDefinition>, ResponseFormat> result;
160         Component component = new Resource();
161         component.setSystemName("name");
162
163         List<GroupDefinition> groupDefinitions = new ArrayList<>();
164         GroupDefinition groupDefinition = new GroupDefinition();
165         groupDefinition.setName("grp_one-1. ::module-1");
166         groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
167         groupDefinition.setDescription("desc");
168         groupDefinitions.add(groupDefinition);
169
170         result = test.validateUpdateVfGroupNamesOnGraph(groupDefinitions, component);
171         assertThat(result.isLeft()).isTrue();
172     }
173
174     @Test
175     void testValidAndUpdateGrpInstancePropValues_fail() {
176         Either<GroupInstance, ResponseFormat> result;
177         String componentId = "id";
178         String instanceId = "id";
179         GroupInstance oldGroupInstance = new GroupInstance();
180         List<GroupInstanceProperty> newProperties = new ArrayList<>();
181         List<PropertyDataDefinition> properties = new LinkedList<>();
182         properties.add(new PropertyDataDefinition());
183         oldGroupInstance.setProperties(properties);
184         try {
185             result = test.validateAndUpdateGroupInstancePropertyValues(componentId, instanceId, oldGroupInstance, newProperties);
186         } catch (ComponentException e) {
187             assertThat(e.getActionStatus()).isEqualTo(ActionStatus.GENERAL_ERROR);
188         }
189     }
190
191     @Test
192     void testCreateGroup() {
193         GroupDefinition result;
194         String componentId = "id";
195         String grpType = "grpType";
196         String userId = "userId";
197         ComponentTypeEnum compTypeEnum = ComponentTypeEnum.RESOURCE;
198         Component component = new Resource();
199         component.setName("name");
200         component.setUniqueId(componentId);
201         component.setToscaType(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
202         List<GroupDefinition> groupDefList = new ArrayList<>();
203         Map<String, Set<String>> excludedGroupTypesMap = new HashMap<>();
204         GroupTypeDefinition groupTypeDefinition = new GroupTypeDefinition();
205         Map<String, DataTypeDefinition> map = new HashMap<>();
206         when(dataTypeCache.getAll()).thenReturn(Either.left(map));
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, false)).thenReturn(Either.left(groupTypeDefinition));
217         when(toscaOperationFacade.canAddGroups(componentId)).thenReturn(true);
218         when(groupTypeOperation.getLatestGroupTypeByType(grpType, 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         Assert.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         Assert.assertEquals(Gdefinition, updatedGroup);
315     }
316 }