Upgrade SDC from Titan to Janus Graph
[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 org.junit.Assert;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
40 import org.openecomp.sdc.be.components.impl.policy.PolicyTargetsUpdateHandler;
41 import org.openecomp.sdc.be.components.validation.AccessValidations;
42 import org.openecomp.sdc.be.config.Configuration;
43 import org.openecomp.sdc.be.config.ConfigurationManager;
44 import org.openecomp.sdc.be.dao.api.ActionStatus;
45 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
46 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
47 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
48 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
49 import org.openecomp.sdc.be.impl.ComponentsUtils;
50 import org.openecomp.sdc.be.model.Component;
51 import org.openecomp.sdc.be.model.Resource;
52 import org.openecomp.sdc.be.model.GroupTypeDefinition;
53 import org.openecomp.sdc.be.model.DataTypeDefinition;
54 import org.openecomp.sdc.be.model.GroupDefinition;
55 import org.openecomp.sdc.be.model.GroupInstance;
56 import org.openecomp.sdc.be.model.GroupInstanceProperty;
57 import org.openecomp.sdc.be.model.PropertyDefinition;
58 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
59 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
60 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation;
61 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
62 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
63 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
64 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
65 import org.openecomp.sdc.be.model.tosca.ToscaType;
66 import org.openecomp.sdc.common.api.ConfigurationSource;
67 import org.openecomp.sdc.common.api.Constants;
68 import org.openecomp.sdc.common.impl.ExternalConfiguration;
69 import org.openecomp.sdc.common.impl.FSConfigurationSource;
70 import org.openecomp.sdc.exception.ResponseFormat;
71 import fj.data.Either;
72
73 import javax.servlet.ServletContext;
74 import java.util.ArrayList;
75 import java.util.List;
76 import java.util.Map;
77 import java.util.HashMap;
78 import java.util.LinkedList;
79 import java.util.Set;
80
81 import static java.util.Arrays.asList;
82 import static org.assertj.core.api.Assertions.assertThat;
83 import static org.mockito.ArgumentMatchers.any;
84 import static org.mockito.ArgumentMatchers.anyMap;
85 import static org.mockito.ArgumentMatchers.anyString;
86 import static org.mockito.ArgumentMatchers.anyList;
87 import static org.mockito.ArgumentMatchers.anyObject;
88 import static org.mockito.Mockito.when;
89
90
91 @RunWith(MockitoJUnitRunner.class)
92 public class GroupBusinessLogicTest {
93
94     @InjectMocks
95     private GroupBusinessLogic test;
96
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     PolicyTargetsUpdateHandler policyTargetsUpdateHandler;
115
116     private final static ServletContext servletContext = Mockito.mock(ServletContext.class);
117     private final static ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class);
118     private final static Configuration configuration = Mockito.mock(Configuration.class);
119     static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
120
121     @Test
122     public void testCreateGroups_NoDataType() {
123         Either<List<GroupDefinition>, ResponseFormat> result;
124         Component component = new Resource();
125         List<GroupDefinition> groupDefinitions = new ArrayList<>();
126         GroupDefinition groupDefinition = new GroupDefinition();
127         groupDefinitions.add(groupDefinition);
128         when(dataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
129         result = test.createGroups(component, groupDefinitions, true);
130         Assert.assertTrue(result.isRight());
131     }
132
133     @Test
134     public void testCreateGroups() {
135         Either<List<GroupDefinition>, ResponseFormat> result;
136         Component component = new Resource();
137         component.setUniqueId("id");
138         List<GroupDefinition> groupDefinitions = new ArrayList<>();
139         GroupDefinition groupDefinition = new GroupDefinition();
140         groupDefinition.setName("name");
141         groupDefinitions.add(groupDefinition);
142         groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
143         GroupTypeDefinition groupTypeDefinition = new GroupTypeDefinition();
144         Map<String, DataTypeDefinition> map = new HashMap<>();
145         when(dataTypeCache.getAll()).thenReturn(Either.left(map));
146         when(groupTypeOperation.getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, true)).thenReturn(Either.left(groupTypeDefinition));
147         when(groupsOperation.createGroups(any(Component.class), anyMap())).thenReturn(Either.left(groupDefinitions));
148         when(groupsOperation.addCalculatedCapabilitiesWithProperties(anyString(), anyMap(), anyMap())).thenReturn(StorageOperationStatus.OK);
149         result = test.createGroups(component, groupDefinitions, true);
150         Assert.assertTrue(result.isLeft());
151     }
152
153     @Test
154     public void testValidUpdateVfGrpNameOnGraph() {
155         Either<List<GroupDefinition>, ResponseFormat> result;
156         Component component = new Resource();
157         component.setSystemName("name");
158
159         List<GroupDefinition> groupDefinitions = new ArrayList<>();
160         GroupDefinition groupDefinition = new GroupDefinition();
161         groupDefinition.setName("grp_one-1. ::module-1");
162         groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
163         groupDefinition.setDescription("desc");
164         groupDefinitions.add(groupDefinition);
165
166         result = test.validateUpdateVfGroupNamesOnGraph(groupDefinitions, component);
167         Assert.assertTrue(result.isLeft());
168     }
169
170     @Test
171     public void testValidAndUpdateGrpInstancePropValues_fail() {
172         Either<GroupInstance, ResponseFormat> result;
173         String componentId = "id";
174         String instanceId = "id";
175         GroupInstance oldGroupInstance = new GroupInstance();
176         List<GroupInstanceProperty> newProperties = new ArrayList<>();
177         List<PropertyDataDefinition> properties = new LinkedList<>();
178         properties.add(new PropertyDataDefinition());
179         oldGroupInstance.setProperties(properties);
180         result = test.validateAndUpdateGroupInstancePropertyValues(componentId, instanceId, oldGroupInstance, newProperties);
181         Assert.assertTrue(result.isRight());
182     }
183
184     @Test
185     public void testCreateGroup() {
186         GroupDefinition result;
187         String componentId = "id";
188         String grpType = "grpType";
189         String userId = "userId";
190         ComponentTypeEnum compTypeEnum = ComponentTypeEnum.RESOURCE;
191         Component component = new Resource();
192         component.setName("name");
193         component.setUniqueId(componentId);
194         component.setToscaType(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
195         List<GroupDefinition> groupDefList = new ArrayList<>();
196         Map<String, Set<String>> excludedGroupTypesMap = new HashMap<>();
197         GroupTypeDefinition groupTypeDefinition = new GroupTypeDefinition();
198         Map<String, DataTypeDefinition> map = new HashMap<>();
199         when(dataTypeCache.getAll()).thenReturn(Either.left(map));
200         when(accessValidations.validateUserCanWorkOnComponent(componentId, compTypeEnum, userId, "CreateGroup")).thenReturn(component);
201
202         ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
203         configurationManager.setConfiguration(new Configuration());
204         configurationManager.getConfiguration().setExcludedGroupTypesMapping(excludedGroupTypesMap);
205
206         List<PropertyDefinition> properties = asList(
207                 buildProperty("network_collection_type", "l3-network", "network collection type, defined with default value"));
208         groupTypeDefinition.setProperties(properties);
209         when(groupTypeOperation.getLatestGroupTypeByType(grpType, false)).thenReturn(Either.left(groupTypeDefinition));
210         when(toscaOperationFacade.canAddGroups(componentId)).thenReturn(true);
211         when(groupTypeOperation.getLatestGroupTypeByType(grpType, true)).thenReturn(Either.left(groupTypeDefinition));
212         when(propertyOperation.checkInnerType(any(PropertyDefinition.class))).thenReturn(Either.left("ok"));
213         when(propertyOperation.validateAndUpdatePropertyValue("string", null, "ok", map)).thenReturn(Either.left(component));
214         when(groupsOperation.addGroups(any(Resource.class), any())).thenReturn(Either.left(groupDefList));
215         when(groupsOperation.addCalculatedCapabilitiesWithProperties(anyString(), anyMap(), anyMap())).thenReturn(StorageOperationStatus.OK);
216         result = test.createGroup(componentId, compTypeEnum, grpType, userId);
217         assertThat(result.getClass().isInstance(GroupDefinition.class));
218     }
219
220     private PropertyDefinition buildProperty(String name, String defaultValue, String description) {
221         PropertyDefinition property = new PropertyDefinition();
222         property.setName(name);
223         property.setDefaultValue(defaultValue);
224         property.setRequired(true);
225         property.setDescription(description);
226         property.setType(ToscaType.STRING.name().toLowerCase());
227         return property;
228     }
229
230     @Test
231     public void testUpdateGroup() throws Exception {
232
233         Component component= new Resource();
234         GroupDefinition updatedGroup = new GroupDefinition();
235         List<GroupDefinition> grpdefList = new ArrayList<>();
236         updatedGroup.setName("GRP.01");
237         grpdefList.add(updatedGroup);
238         component.setUniqueId("GRP.01");
239         component.setGroups(grpdefList);
240         updatedGroup.setUniqueId("GRP.01");
241         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "UpdateGroup")).thenReturn(component);
242         when(groupsOperation.updateGroup(component, updatedGroup)).thenReturn(Either.left(updatedGroup));
243         GroupDefinition Gdefinition = test.updateGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
244                 "USR01", updatedGroup);
245         Assert.assertEquals(Gdefinition,updatedGroup);
246     }
247
248
249     @Test(expected = ComponentException.class)
250     public void testUpdateGroup_Invalidname() throws Exception {
251
252         Component component= new Resource();
253         GroupDefinition updatedGroup = new GroupDefinition();
254         List<GroupDefinition> grpdefList = new ArrayList<>();
255         updatedGroup.setName("GRP~01");
256         updatedGroup.setUniqueId("GRP.01");
257         grpdefList.add(updatedGroup);
258         component.setUniqueId("GRP.01");
259         component.setGroups(grpdefList);
260         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "UpdateGroup")).thenReturn(component);
261         GroupDefinition Gdefinition = test.updateGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
262                 "USR01", updatedGroup);
263
264     }
265
266     @Test(expected = ComponentException.class)
267     public void testDeleteGroup_exception() throws Exception {
268
269         Component component= new Resource();
270         GroupDefinition updatedGroup = new GroupDefinition();
271         List<GroupDefinition> grpdefList = new ArrayList<>();
272         updatedGroup.setName("GRP~01");
273         updatedGroup.setUniqueId("GRP.01");
274         grpdefList.add(updatedGroup);
275         component.setUniqueId("GRP.01");
276         component.setGroups(grpdefList);
277         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "DeleteGroup")).thenReturn(component);
278         when(groupsOperation.deleteGroups(anyObject(),anyList())).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
279
280         when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
281         GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
282                 "USR01");
283     }
284
285     @Test
286     public void testDeleteGroup() throws Exception {
287
288         Component component= new Resource();
289         GroupDefinition updatedGroup = new GroupDefinition();
290         List<GroupDefinition> grpdefList = new ArrayList<>();
291         updatedGroup.setName("GRP~01");
292         updatedGroup.setUniqueId("GRP.01");
293         grpdefList.add(updatedGroup);
294         component.setUniqueId("GRP.01");
295         component.setGroups(grpdefList);
296         when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "DeleteGroup")).thenReturn(component);
297         when(groupsOperation.deleteGroups(anyObject(),anyList())).thenReturn(Either.left(grpdefList));
298         when(groupsOperation.deleteCalculatedCapabilitiesWithProperties(anyString(), anyObject())).thenReturn(StorageOperationStatus.OK);
299         when(policyTargetsUpdateHandler.removePoliciesTargets(anyObject(),anyString(),anyObject())).thenReturn(ActionStatus.OK);
300
301         GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
302                 "USR01");
303         Assert.assertEquals(Gdefinition,updatedGroup);
304     }
305 }