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 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.jsongraph.TitanDao;
46 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
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.jsontitan.datamodel.ToscaElementTypeEnum;
60 import org.openecomp.sdc.be.model.jsontitan.operations.GroupsOperation;
61 import org.openecomp.sdc.be.model.jsontitan.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;
73 import javax.servlet.ServletContext;
74 import java.util.ArrayList;
75 import java.util.List;
77 import java.util.HashMap;
78 import java.util.LinkedList;
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;
91 @RunWith(MockitoJUnitRunner.class)
92 public class GroupBusinessLogicTest {
95 private GroupBusinessLogic test;
98 private ApplicationDataTypeCache dataTypeCache;
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 TitanDao titanDao;
114 PolicyTargetsUpdateHandler policyTargetsUpdateHandler;
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");
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(TitanOperationStatus.NOT_FOUND));
129 result = test.createGroups(component, groupDefinitions, true);
130 Assert.assertTrue(result.isRight());
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());
154 public void testValidUpdateVfGrpNameOnGraph() {
155 Either<List<GroupDefinition>, ResponseFormat> result;
156 Component component = new Resource();
157 component.setSystemName("name");
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);
166 result = test.validateUpdateVfGroupNamesOnGraph(groupDefinitions, component);
167 Assert.assertTrue(result.isLeft());
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());
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);
202 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
203 configurationManager.setConfiguration(new Configuration());
204 configurationManager.getConfiguration().setExcludedGroupTypesMapping(excludedGroupTypesMap);
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));
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());
231 public void testUpdateGroup() throws Exception {
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);
249 @Test(expected = ComponentException.class)
250 public void testUpdateGroup_Invalidname() throws Exception {
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);
266 @Test(expected = ComponentException.class)
267 public void testDeleteGroup_exception() throws Exception {
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));
280 when(titanDao.rollback()).thenReturn(TitanOperationStatus.OK);
281 GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
286 public void testDeleteGroup() throws Exception {
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);
301 GroupDefinition Gdefinition = test.deleteGroup("compid", ComponentTypeEnum.SERVICE, "GRP.01",
303 Assert.assertEquals(Gdefinition,updatedGroup);