Remove usage of outdated library JMockit (catalog-dao)
[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.janusgraph.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 applicationDataTypeCache;
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.setApplicationDataTypeCache(applicationDataTypeCache);
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         result = test.createGroups(component, groupDefinitions, true);
133         assertThat(result.isRight()).isTrue();
134     }
135
136     @Test
137     void testCreateGroups() {
138         Either<List<GroupDefinition>, ResponseFormat> result;
139         Component component = new Resource();
140         component.setUniqueId("id");
141         List<GroupDefinition> groupDefinitions = new ArrayList<>();
142         GroupDefinition groupDefinition = new GroupDefinition();
143         groupDefinition.setName("name");
144         groupDefinitions.add(groupDefinition);
145         groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
146         GroupTypeDefinition groupTypeDefinition = new GroupTypeDefinition();
147         Map<String, DataTypeDefinition> map = new HashMap<>();
148         when(groupTypeOperation.getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, component.getModel(), true))
149             .thenReturn(Either.left(groupTypeDefinition));
150         when(groupsOperation.createGroups(any(Component.class), anyMap())).thenReturn(Either.left(groupDefinitions));
151         when(groupsOperation.addCalculatedCapabilitiesWithProperties(anyString(), anyMap(), anyMap())).thenReturn(StorageOperationStatus.OK);
152         result = test.createGroups(component, groupDefinitions, true);
153         assertThat(result.isLeft()).isTrue();
154     }
155
156     @Test
157     void testValidUpdateVfGrpNameOnGraph() {
158         Either<List<GroupDefinition>, ResponseFormat> result;
159         Component component = new Resource();
160         component.setSystemName("name");
161
162         List<GroupDefinition> groupDefinitions = new ArrayList<>();
163         GroupDefinition groupDefinition = new GroupDefinition();
164         groupDefinition.setName("grp_one-1. ::module-1");
165         groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
166         groupDefinition.setDescription("desc");
167         groupDefinitions.add(groupDefinition);
168
169         result = test.validateUpdateVfGroupNamesOnGraph(groupDefinitions, component);
170         assertThat(result.isLeft()).isTrue();
171     }
172
173     @Test
174     void testValidAndUpdateGrpInstancePropValues_fail() {
175         Either<GroupInstance, ResponseFormat> result;
176         String componentId = "id";
177         String instanceId = "id";
178         GroupInstance oldGroupInstance = new GroupInstance();
179         List<GroupInstanceProperty> newProperties = new ArrayList<>();
180         List<PropertyDataDefinition> properties = new LinkedList<>();
181         properties.add(new PropertyDataDefinition());
182         oldGroupInstance.setProperties(properties);
183         try {
184             result = test.validateAndUpdateGroupInstancePropertyValues(componentId, instanceId, oldGroupInstance, newProperties);
185         } catch (ComponentException e) {
186             assertThat(e.getActionStatus()).isEqualTo(ActionStatus.GENERAL_ERROR);
187         }
188     }
189
190     @Test
191     void testCreateGroup() {
192         GroupDefinition result;
193         String componentId = "id";
194         String grpType = "grpType";
195         String userId = "userId";
196         ComponentTypeEnum compTypeEnum = ComponentTypeEnum.RESOURCE;
197         Component component = new Resource();
198         component.setName("name");
199         component.setUniqueId(componentId);
200         component.setToscaType(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
201         List<GroupDefinition> groupDefList = new ArrayList<>();
202         Map<String, Set<String>> excludedGroupTypesMap = new HashMap<>();
203         GroupTypeDefinition groupTypeDefinition = new GroupTypeDefinition();
204         Map<String, DataTypeDefinition> map = new HashMap<>();
205         when(accessValidations.validateUserCanWorkOnComponent(componentId, compTypeEnum, userId, "CreateGroup")).thenReturn(component);
206
207         ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
208         configurationManager.setConfiguration(new Configuration());
209         configurationManager.getConfiguration().setExcludedGroupTypesMapping(excludedGroupTypesMap);
210
211         List<PropertyDefinition> properties = asList(
212             buildProperty("network_collection_type", "l3-network", "network collection type, defined with default value"));
213         groupTypeDefinition.setProperties(properties);
214         when(groupTypeOperation.getLatestGroupTypeByType(grpType, component.getModel(), false)).thenReturn(Either.left(groupTypeDefinition));
215         when(toscaOperationFacade.canAddGroups(componentId)).thenReturn(true);
216         when(groupTypeOperation.getLatestGroupTypeByType(grpType, component.getModel(), true)).thenReturn(Either.left(groupTypeDefinition));
217         when(propertyOperation.checkInnerType(any(PropertyDefinition.class))).thenReturn(Either.left("ok"));
218         when(propertyOperation.validateAndUpdatePropertyValue("string", null, "ok", map)).thenReturn(Either.left(component));
219         when(groupsOperation.addGroups(any(Resource.class), any())).thenReturn(Either.left(groupDefList));
220         when(groupsOperation.addCalculatedCapabilitiesWithProperties(anyString(), anyMap(), anyMap())).thenReturn(StorageOperationStatus.OK);
221         result = test.createGroup(componentId, compTypeEnum, grpType, userId);
222         assertThat(result.getClass()).isAssignableFrom(GroupDefinition.class);
223     }
224
225     private PropertyDefinition buildProperty(String name, String defaultValue, String description) {
226         PropertyDefinition property = new PropertyDefinition();
227         property.setName(name);
228         property.setDefaultValue(defaultValue);
229         property.setRequired(true);
230         property.setDescription(description);
231         property.setType(ToscaType.STRING.name().toLowerCase());
232         return property;
233     }
234
235     @Test
236     void testUpdateGroup() throws Exception {
237
238         Component component = new Resource();
239         GroupDefinition updatedGroup = new GroupDefinition();
240         List<GroupDefinition> grpdefList = new ArrayList<>();
241         updatedGroup.setName("GRP.01");
242         grpdefList.add(updatedGroup);
243         component.setUniqueId("GRP.01");
244         component.setGroups(grpdefList);
245         updatedGroup.setUniqueId("GRP.01");
246         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "UpdateGroup")).thenReturn(component);
247         when(groupsOperation.updateGroup(component, updatedGroup)).thenReturn(Either.left(updatedGroup));
248         GroupDefinition Gdefinition = test.updateGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
249             "USR01", updatedGroup);
250         Assert.assertEquals(Gdefinition, updatedGroup);
251     }
252
253     @Test
254     void testUpdateGroup_Invalidname() throws Exception {
255
256         Component component = new Resource();
257         GroupDefinition updatedGroup = new GroupDefinition();
258         List<GroupDefinition> grpdefList = new ArrayList<>();
259         updatedGroup.setName("GRP~01");
260         updatedGroup.setUniqueId("GRP.01");
261         grpdefList.add(updatedGroup);
262         component.setUniqueId("GRP.01");
263         component.setGroups(grpdefList);
264         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "UpdateGroup")).thenReturn(component);
265         assertThrows(ComponentException.class, () -> {
266             GroupDefinition Gdefinition = test.updateGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
267                 "USR01", updatedGroup);
268         });
269     }
270
271     @Test
272     void testDeleteGroup_exception() throws Exception {
273
274         Component component = new Resource();
275         GroupDefinition updatedGroup = new GroupDefinition();
276         List<GroupDefinition> grpdefList = new ArrayList<>();
277         updatedGroup.setName("GRP~01");
278         updatedGroup.setUniqueId("GRP.01");
279         grpdefList.add(updatedGroup);
280         component.setUniqueId("GRP.01");
281         component.setGroups(grpdefList);
282         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "DeleteGroup")).thenReturn(component);
283         when(groupsOperation.deleteGroups(anyObject(), anyList())).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
284
285         when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
286         assertThrows(ComponentException.class, () -> {
287             GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
288                 "USR01");
289         });
290     }
291
292     @Test
293     void testDeleteGroup() {
294
295         Component component = new Resource();
296         List<GroupDefinition> groupDefList = new ArrayList<>();
297         GroupDefinition updatedGroup = new GroupDefinition();
298         updatedGroup.setName("GRP~01");
299         updatedGroup.setUniqueId("GRP.01");
300         groupDefList.add(updatedGroup);
301         component.setUniqueId("GRP.01");
302         component.setGroups(groupDefList);
303         List<GroupDefinition> groupDefListCopy = new ArrayList<>();
304         groupDefListCopy.add(updatedGroup);
305         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "DeleteGroup")).thenReturn(component);
306         when(groupsOperation.deleteGroups(anyObject(), anyList())).thenReturn(Either.left(groupDefListCopy));
307         when(groupsOperation.deleteCalculatedCapabilitiesWithProperties(anyString(), anyObject())).thenReturn(StorageOperationStatus.OK);
308         when(policyTargetsUpdateHandler.removePoliciesTargets(anyObject(), anyString(), anyObject())).thenReturn(ActionStatus.OK);
309
310         GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
311             "USR01");
312         Assert.assertEquals(Gdefinition, updatedGroup);
313     }
314 }