3 * Copyright (c) 2018 AT&T Intellectual Property.
7 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
27 * limitations under the License.
30 package org.openecomp.sdc.be.components.impl;
32 import fj.data.Either;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
41 import org.openecomp.sdc.be.components.impl.policy.PolicyTargetsUpdateHandler;
42 import org.openecomp.sdc.be.components.validation.AccessValidations;
43 import org.openecomp.sdc.be.config.Configuration;
44 import org.openecomp.sdc.be.config.ConfigurationManager;
45 import org.openecomp.sdc.be.dao.api.ActionStatus;
46 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
47 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
48 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
49 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
50 import org.openecomp.sdc.be.impl.ComponentsUtils;
51 import org.openecomp.sdc.be.model.Component;
52 import org.openecomp.sdc.be.model.DataTypeDefinition;
53 import org.openecomp.sdc.be.model.GroupDefinition;
54 import org.openecomp.sdc.be.model.GroupInstance;
55 import org.openecomp.sdc.be.model.GroupInstanceProperty;
56 import org.openecomp.sdc.be.model.GroupTypeDefinition;
57 import org.openecomp.sdc.be.model.PropertyDefinition;
58 import org.openecomp.sdc.be.model.Resource;
59 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
60 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
61 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation;
62 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
63 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
64 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
65 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
66 import org.openecomp.sdc.be.model.tosca.ToscaType;
67 import org.openecomp.sdc.common.api.ConfigurationSource;
68 import org.openecomp.sdc.common.api.Constants;
69 import org.openecomp.sdc.common.impl.ExternalConfiguration;
70 import org.openecomp.sdc.common.impl.FSConfigurationSource;
71 import org.openecomp.sdc.exception.ResponseFormat;
73 import java.util.ArrayList;
74 import java.util.HashMap;
75 import java.util.LinkedList;
76 import java.util.List;
80 import static java.util.Arrays.asList;
81 import static org.assertj.core.api.Assertions.assertThat;
82 import static org.mockito.ArgumentMatchers.any;
83 import static org.mockito.ArgumentMatchers.anyList;
84 import static org.mockito.ArgumentMatchers.anyMap;
85 import static org.mockito.ArgumentMatchers.anyObject;
86 import static org.mockito.ArgumentMatchers.anyString;
87 import static org.mockito.Mockito.when;
90 @RunWith(MockitoJUnitRunner.class)
91 public class GroupBusinessLogicTest {
94 private GroupBusinessLogic test;
97 private ApplicationDataTypeCache dataTypeCache;
99 private ComponentsUtils componentsUtils;
101 private IGroupTypeOperation groupTypeOperation;
103 private GroupsOperation groupsOperation;
105 private AccessValidations accessValidations;
107 private ToscaOperationFacade toscaOperationFacade;
109 private PropertyOperation propertyOperation;
111 private JanusGraphDao janusGraphDao;
113 PolicyTargetsUpdateHandler policyTargetsUpdateHandler;
115 static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
116 "src/test/resources/config/catalog-be");
117 static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
120 public void setUp() throws Exception {
121 test.setDataTypeCache(dataTypeCache);
122 test.setToscaOperationFacade(toscaOperationFacade);
123 test.setPropertyOperation(propertyOperation);
124 test.setComponentsUtils(componentsUtils);
125 test.setJanusGraphDao(janusGraphDao);
129 public void testCreateGroups_NoDataType() {
130 Either<List<GroupDefinition>, ResponseFormat> result;
131 Component component = new Resource();
132 List<GroupDefinition> groupDefinitions = new ArrayList<>();
133 GroupDefinition groupDefinition = new GroupDefinition();
134 groupDefinitions.add(groupDefinition);
135 when(dataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
136 result = test.createGroups(component, groupDefinitions, true);
137 Assert.assertTrue(result.isRight());
141 public void testCreateGroups() {
142 Either<List<GroupDefinition>, ResponseFormat> result;
143 Component component = new Resource();
144 component.setUniqueId("id");
145 List<GroupDefinition> groupDefinitions = new ArrayList<>();
146 GroupDefinition groupDefinition = new GroupDefinition();
147 groupDefinition.setName("name");
148 groupDefinitions.add(groupDefinition);
149 groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
150 GroupTypeDefinition groupTypeDefinition = new GroupTypeDefinition();
151 Map<String, DataTypeDefinition> map = new HashMap<>();
152 when(dataTypeCache.getAll()).thenReturn(Either.left(map));
153 when(groupTypeOperation.getLatestGroupTypeByType(Constants.DEFAULT_GROUP_VF_MODULE, true)).thenReturn(Either.left(groupTypeDefinition));
154 when(groupsOperation.createGroups(any(Component.class), anyMap())).thenReturn(Either.left(groupDefinitions));
155 when(groupsOperation.addCalculatedCapabilitiesWithProperties(anyString(), anyMap(), anyMap())).thenReturn(StorageOperationStatus.OK);
156 result = test.createGroups(component, groupDefinitions, true);
157 Assert.assertTrue(result.isLeft());
161 public void testValidUpdateVfGrpNameOnGraph() {
162 Either<List<GroupDefinition>, ResponseFormat> result;
163 Component component = new Resource();
164 component.setSystemName("name");
166 List<GroupDefinition> groupDefinitions = new ArrayList<>();
167 GroupDefinition groupDefinition = new GroupDefinition();
168 groupDefinition.setName("grp_one-1. ::module-1");
169 groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
170 groupDefinition.setDescription("desc");
171 groupDefinitions.add(groupDefinition);
173 result = test.validateUpdateVfGroupNamesOnGraph(groupDefinitions, component);
174 Assert.assertTrue(result.isLeft());
178 public void testValidAndUpdateGrpInstancePropValues_fail() {
179 Either<GroupInstance, ResponseFormat> result;
180 String componentId = "id";
181 String instanceId = "id";
182 GroupInstance oldGroupInstance = new GroupInstance();
183 List<GroupInstanceProperty> newProperties = new ArrayList<>();
184 List<PropertyDataDefinition> properties = new LinkedList<>();
185 properties.add(new PropertyDataDefinition());
186 oldGroupInstance.setProperties(properties);
188 result = test.validateAndUpdateGroupInstancePropertyValues(componentId, instanceId, oldGroupInstance, newProperties);
189 }catch (ComponentException e){
190 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.GENERAL_ERROR);
195 public void testCreateGroup() {
196 GroupDefinition result;
197 String componentId = "id";
198 String grpType = "grpType";
199 String userId = "userId";
200 ComponentTypeEnum compTypeEnum = ComponentTypeEnum.RESOURCE;
201 Component component = new Resource();
202 component.setName("name");
203 component.setUniqueId(componentId);
204 component.setToscaType(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
205 List<GroupDefinition> groupDefList = new ArrayList<>();
206 Map<String, Set<String>> excludedGroupTypesMap = new HashMap<>();
207 GroupTypeDefinition groupTypeDefinition = new GroupTypeDefinition();
208 Map<String, DataTypeDefinition> map = new HashMap<>();
209 when(dataTypeCache.getAll()).thenReturn(Either.left(map));
210 when(accessValidations.validateUserCanWorkOnComponent(componentId, compTypeEnum, userId, "CreateGroup")).thenReturn(component);
212 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
213 configurationManager.setConfiguration(new Configuration());
214 configurationManager.getConfiguration().setExcludedGroupTypesMapping(excludedGroupTypesMap);
216 List<PropertyDefinition> properties = asList(
217 buildProperty("network_collection_type", "l3-network", "network collection type, defined with default value"));
218 groupTypeDefinition.setProperties(properties);
219 when(groupTypeOperation.getLatestGroupTypeByType(grpType, false)).thenReturn(Either.left(groupTypeDefinition));
220 when(toscaOperationFacade.canAddGroups(componentId)).thenReturn(true);
221 when(groupTypeOperation.getLatestGroupTypeByType(grpType, true)).thenReturn(Either.left(groupTypeDefinition));
222 when(propertyOperation.checkInnerType(any(PropertyDefinition.class))).thenReturn(Either.left("ok"));
223 when(propertyOperation.validateAndUpdatePropertyValue("string", null, "ok", map)).thenReturn(Either.left(component));
224 when(groupsOperation.addGroups(any(Resource.class), any())).thenReturn(Either.left(groupDefList));
225 when(groupsOperation.addCalculatedCapabilitiesWithProperties(anyString(), anyMap(), anyMap())).thenReturn(StorageOperationStatus.OK);
226 result = test.createGroup(componentId, compTypeEnum, grpType, userId);
227 assertThat(result.getClass().isInstance(GroupDefinition.class));
230 private PropertyDefinition buildProperty(String name, String defaultValue, String description) {
231 PropertyDefinition property = new PropertyDefinition();
232 property.setName(name);
233 property.setDefaultValue(defaultValue);
234 property.setRequired(true);
235 property.setDescription(description);
236 property.setType(ToscaType.STRING.name().toLowerCase());
241 public void testUpdateGroup() throws Exception {
243 Component component= new Resource();
244 GroupDefinition updatedGroup = new GroupDefinition();
245 List<GroupDefinition> grpdefList = new ArrayList<>();
246 updatedGroup.setName("GRP.01");
247 grpdefList.add(updatedGroup);
248 component.setUniqueId("GRP.01");
249 component.setGroups(grpdefList);
250 updatedGroup.setUniqueId("GRP.01");
251 when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "UpdateGroup")).thenReturn(component);
252 when(groupsOperation.updateGroup(component, updatedGroup)).thenReturn(Either.left(updatedGroup));
253 GroupDefinition Gdefinition = test.updateGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
254 "USR01", updatedGroup);
255 Assert.assertEquals(Gdefinition,updatedGroup);
259 @Test(expected = ComponentException.class)
260 public void testUpdateGroup_Invalidname() throws Exception {
262 Component component= new Resource();
263 GroupDefinition updatedGroup = new GroupDefinition();
264 List<GroupDefinition> grpdefList = new ArrayList<>();
265 updatedGroup.setName("GRP~01");
266 updatedGroup.setUniqueId("GRP.01");
267 grpdefList.add(updatedGroup);
268 component.setUniqueId("GRP.01");
269 component.setGroups(grpdefList);
270 when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "UpdateGroup")).thenReturn(component);
271 GroupDefinition Gdefinition = test.updateGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
272 "USR01", updatedGroup);
276 @Test(expected = ComponentException.class)
277 public void testDeleteGroup_exception() throws Exception {
279 Component component= new Resource();
280 GroupDefinition updatedGroup = new GroupDefinition();
281 List<GroupDefinition> grpdefList = new ArrayList<>();
282 updatedGroup.setName("GRP~01");
283 updatedGroup.setUniqueId("GRP.01");
284 grpdefList.add(updatedGroup);
285 component.setUniqueId("GRP.01");
286 component.setGroups(grpdefList);
287 when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "DeleteGroup")).thenReturn(component);
288 when(groupsOperation.deleteGroups(anyObject(),anyList())).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
290 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
291 GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
296 public void testDeleteGroup(){
298 Component component= new Resource();
299 List<GroupDefinition> groupDefList = new ArrayList<>();
300 GroupDefinition updatedGroup = new GroupDefinition();
301 updatedGroup.setName("GRP~01");
302 updatedGroup.setUniqueId("GRP.01");
303 groupDefList.add(updatedGroup);
304 component.setUniqueId("GRP.01");
305 component.setGroups(groupDefList);
306 List<GroupDefinition> groupDefListCopy = new ArrayList<>();
307 groupDefListCopy.add(updatedGroup);
308 when(accessValidations.validateUserCanWorkOnComponent("compid", ComponentTypeEnum.SERVICE, "USR01", "DeleteGroup")).thenReturn(component);
309 when(groupsOperation.deleteGroups(anyObject(),anyList())).thenReturn(Either.left(groupDefListCopy));
310 when(groupsOperation.deleteCalculatedCapabilitiesWithProperties(anyString(), anyObject())).thenReturn(StorageOperationStatus.OK);
311 when(policyTargetsUpdateHandler.removePoliciesTargets(anyObject(),anyString(),anyObject())).thenReturn(ActionStatus.OK);
313 GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
315 Assert.assertEquals(Gdefinition,updatedGroup);