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 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;
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;
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;
89 @ExtendWith(MockitoExtension.class)
90 class GroupBusinessLogicTest {
92 static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
93 "src/test/resources/config/catalog-be");
94 static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
96 private GroupBusinessLogic test;
98 private ApplicationDataTypeCache applicationDataTypeCache;
100 private ComponentsUtils componentsUtils;
102 private IGroupTypeOperation groupTypeOperation;
104 private GroupsOperation groupsOperation;
106 private AccessValidations accessValidations;
108 private ToscaOperationFacade toscaOperationFacade;
110 private PropertyOperation propertyOperation;
112 private JanusGraphDao janusGraphDao;
114 private PolicyTargetsUpdateHandler policyTargetsUpdateHandler;
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);
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();
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();
157 void testValidUpdateVfGrpNameOnGraph() {
158 Either<List<GroupDefinition>, ResponseFormat> result;
159 Component component = new Resource();
160 component.setSystemName("name");
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);
169 result = test.validateUpdateVfGroupNamesOnGraph(groupDefinitions, component);
170 assertThat(result.isLeft()).isTrue();
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);
184 result = test.validateAndUpdateGroupInstancePropertyValues(componentId, instanceId, oldGroupInstance, newProperties);
185 } catch (ComponentException e) {
186 assertThat(e.getActionStatus()).isEqualTo(ActionStatus.GENERAL_ERROR);
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);
207 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
208 configurationManager.setConfiguration(new Configuration());
209 configurationManager.getConfiguration().setExcludedGroupTypesMapping(excludedGroupTypesMap);
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);
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());
236 void testUpdateGroup() throws Exception {
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);
254 void testUpdateGroup_Invalidname() throws Exception {
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);
272 void testDeleteGroup_exception() throws Exception {
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));
285 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
286 assertThrows(ComponentException.class, () -> {
287 GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
293 void testDeleteGroup() {
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);
310 GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
312 Assert.assertEquals(Gdefinition, updatedGroup);