2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.be.components.impl;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyBoolean;
21 import static org.mockito.ArgumentMatchers.anyCollection;
22 import static org.mockito.ArgumentMatchers.anyObject;
23 import static org.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.when;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
36 import javax.servlet.ServletContext;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mockito;
43 import org.mockito.MockitoAnnotations;
44 import org.openecomp.sdc.ElementOperationMock;
45 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
46 import org.openecomp.sdc.be.components.InterfaceOperationTestUtils;
47 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
48 import org.openecomp.sdc.be.components.validation.InterfaceOperationValidation;
49 import org.openecomp.sdc.be.components.validation.UserValidations;
50 import org.openecomp.sdc.be.config.ConfigurationManager;
51 import org.openecomp.sdc.be.dao.api.ActionStatus;
52 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
53 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
54 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
55 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
56 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
57 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
58 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
59 import org.openecomp.sdc.be.impl.ComponentsUtils;
60 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
61 import org.openecomp.sdc.be.model.Component;
62 import org.openecomp.sdc.be.model.DataTypeDefinition;
63 import org.openecomp.sdc.be.model.InputDefinition;
64 import org.openecomp.sdc.be.model.InterfaceDefinition;
65 import org.openecomp.sdc.be.model.LifecycleStateEnum;
66 import org.openecomp.sdc.be.model.Operation;
67 import org.openecomp.sdc.be.model.Resource;
68 import org.openecomp.sdc.be.model.User;
69 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
70 import org.openecomp.sdc.be.model.jsontitan.operations.InterfaceOperation;
71 import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation;
72 import org.openecomp.sdc.be.model.jsontitan.operations.NodeTypeOperation;
73 import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
74 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
75 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
76 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
77 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
78 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
79 import org.openecomp.sdc.be.user.Role;
80 import org.openecomp.sdc.be.user.UserBusinessLogic;
81 import org.openecomp.sdc.common.api.ConfigurationSource;
82 import org.openecomp.sdc.common.api.Constants;
83 import org.openecomp.sdc.common.impl.ExternalConfiguration;
84 import org.openecomp.sdc.common.impl.FSConfigurationSource;
85 import org.openecomp.sdc.exception.ResponseFormat;
86 import org.springframework.web.context.WebApplicationContext;
88 import fj.data.Either;
90 public class InterfaceOperationBusinessLogicTest implements InterfaceOperationTestUtils{
92 public static final String RESOURCE_CATEGORY1 = "Network Layer 2-3";
93 public static final String RESOURCE_SUBCATEGORY = "Router";
96 private String resourceId = "resourceId1";
97 private String operationId = "uniqueId1";
98 Resource resourceUpdate;
100 public static final String RESOURCE_NAME = "My-Resource_Name with space";
102 final ServletContext servletContext = Mockito.mock(ServletContext.class);
103 IElementOperation mockElementDao;
104 TitanDao mockTitanDao = Mockito.mock(TitanDao.class);
105 UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
106 ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
107 NodeTypeOperation nodeTypeOperation = Mockito.mock(NodeTypeOperation.class);
108 NodeTemplateOperation nodeTemplateOperation = Mockito.mock(NodeTemplateOperation.class);
109 TopologyTemplateOperation topologyTemplateOperation = Mockito.mock(TopologyTemplateOperation.class);
110 final IPropertyOperation propertyOperation = Mockito.mock(IPropertyOperation.class);
111 final ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class);
112 WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
113 UserValidations userValidations = Mockito.mock(UserValidations.class);
114 WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
115 ArtifactCassandraDao artifactCassandraDao = Mockito.mock(ArtifactCassandraDao.class);
116 InterfaceOperation interfaceOperation = Mockito.mock(InterfaceOperation.class);
117 InterfaceOperationValidation operationValidator = Mockito.mock(InterfaceOperationValidation.class);
119 ResponseFormatManager responseManager = null;
120 GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
122 Resource resourceResponse = null;
123 ComponentsUtils componentsUtils;
124 ArtifactsBusinessLogic artifactManager = new ArtifactsBusinessLogic();
125 private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
129 InterfaceOperationBusinessLogic bl = new InterfaceOperationBusinessLogic();
132 public void setup() {
133 MockitoAnnotations.initMocks(this);
134 Mockito.reset(propertyOperation);
136 ExternalConfiguration.setAppName("catalog-be");
138 // init Configuration
139 String appConfigDir = "src/test/resources/config/catalog-be";
140 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
141 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
142 componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
145 mockElementDao = new ElementOperationMock();
147 // User data and management
149 user.setUserId("jh0003");
150 user.setFirstName("Jimmi");
151 user.setLastName("Hendrix");
152 user.setRole(Role.ADMIN.name());
154 Either<User, ActionStatus> eitherGetUser = Either.left(user);
155 when(mockUserAdmin.getUser("jh0003", false)).thenReturn(eitherGetUser);
156 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), eq(false))).thenReturn(Either.left(user));
157 when(userValidations.validateUserNotEmpty(eq(user), anyString())).thenReturn(Either.left(user));
158 // Servlet Context attributes
159 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
160 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
161 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
162 when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
164 Either<Boolean, StorageOperationStatus> eitherFalse = Either.left(true);
165 when(toscaOperationFacade.validateComponentNameExists("Root", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE)).thenReturn(eitherFalse);
167 Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true);
168 when(toscaOperationFacade.validateComponentNameExists("alreadyExists", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE)).thenReturn(eitherCountExist);
170 Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false);
171 when(toscaOperationFacade.validateComponentNameExists(eq(RESOURCE_NAME), any(ResourceTypeEnum.class), eq(ComponentTypeEnum.RESOURCE))).thenReturn(eitherCount);
173 Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
174 when(toscaOperationFacade.validateToscaResourceNameExists("Root")).thenReturn(validateDerivedExists);
176 Either<Boolean, StorageOperationStatus> validateDerivedNotExists = Either.left(false);
177 when(toscaOperationFacade.validateToscaResourceNameExists("kuku")).thenReturn(validateDerivedNotExists);
178 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
179 when(graphLockOperation.lockComponentByName(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
182 resourceResponse = createResourceObject(true);
183 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(resourceResponse);
184 Either<Integer, StorageOperationStatus> eitherValidate = Either.left(null);
185 when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
186 when(toscaOperationFacade.validateCsarUuidUniqueness(Mockito.anyString())).thenReturn(eitherValidate);
187 Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<String, DataTypeDefinition>();
188 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(emptyDataTypes));
192 when(operationValidator.validateInterfaceOperations(anyCollection(), anyString(), anyBoolean())).thenReturn(Either.left(true));
193 when(interfaceOperation.addInterface(anyString(), anyObject())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
194 when(interfaceOperation.updateInterface(anyString(), anyObject())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
195 when(interfaceOperation.deleteInterface(anyObject(), anyObject())).thenReturn(Either.left(new HashSet<>()));
196 when(interfaceOperation.deleteInterface(any(),any())).thenReturn(Either.left(new HashSet<>()));
197 when(interfaceOperation.updateInterface(any(),any())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
198 when(mockTitanDao.commit()).thenReturn(TitanOperationStatus.OK);
201 artifactManager.setNodeTemplateOperation(nodeTemplateOperation);
202 bl = new InterfaceOperationBusinessLogic();
204 bl.setUserAdmin(mockUserAdmin);
205 bl.setComponentsUtils(componentsUtils);
206 bl.setGraphLockOperation(graphLockOperation);
207 bl.setTitanGenericDao(mockTitanDao);
208 bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
209 toscaOperationFacade.setNodeTypeOperation(nodeTypeOperation);
210 toscaOperationFacade.setTopologyTemplateOperation(topologyTemplateOperation);
211 bl.setToscaOperationFacade(toscaOperationFacade);
212 bl.setUserValidations(userValidations);
213 bl.setInterfaceOperation(interfaceOperation);
214 bl.setInterfaceOperationValidation(operationValidator);
215 bl.setArtifactCassandraDao(artifactCassandraDao);
216 Resource resourceCsar = createResourceObjectCsar(true);
217 setCanWorkOnResource(resourceCsar);
218 Either<Component, StorageOperationStatus> oldResourceRes = Either.left(resourceCsar);
219 when(toscaOperationFacade.getToscaFullElement(resourceCsar.getUniqueId())).thenReturn(oldResourceRes);
220 responseManager = ResponseFormatManager.getInstance();
225 public void createInterfaceOperationTest() {
226 validateUserRoles(Role.ADMIN, Role.DESIGNER);
227 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createMockResourceForAddInterface()));
228 resourceUpdate = setUpResourceMock();
229 Either<Resource, ResponseFormat> interfaceOperation = bl.createInterfaceOperation(resourceId, resourceUpdate, user, true);
230 Assert.assertTrue(interfaceOperation.isLeft());
235 public void updateInterfaceOperationTest() {
236 validateUserRoles(Role.ADMIN, Role.DESIGNER);
237 resourceUpdate = setUpResourceMock();
238 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createResourceForInterfaceOperation()));
239 Either<Resource, ResponseFormat> interfaceOperation = bl.updateInterfaceOperation(resourceId, resourceUpdate, user, true);
240 Assert.assertTrue(interfaceOperation.isLeft());
245 public void deleteInterfaceOperationTest() {
246 validateUserRoles(Role.ADMIN, Role.DESIGNER);
247 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createResourceForInterfaceOperation()));
248 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
249 Set<String> idsToDelete = new HashSet<>();
250 idsToDelete.add(operationId);
252 Either<Resource, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, idsToDelete, user, true);
253 Assert.assertTrue(deleteResourceResponseFormatEither.isLeft());
257 public void deleteInterfaceOperationTestShouldFailWrongId() {
258 validateUserRoles(Role.ADMIN, Role.DESIGNER);
259 Set<String> idsToDelete = new HashSet<>();
260 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createResourceForInterfaceOperation()));
261 idsToDelete.add(resourceId);
262 Either<Resource, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, idsToDelete, user, true);
263 Assert.assertFalse(deleteResourceResponseFormatEither.isLeft());
268 public void deleteInterfaceOperationFailToDeleteArtifactTest() {
269 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createResourceForInterfaceOperation()));
270 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
271 validateUserRoles(Role.ADMIN, Role.DESIGNER);
272 Set<String> idsToDelete = new HashSet<>();
273 idsToDelete.add(operationId);
275 Either<Resource, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, idsToDelete, user, true);
276 Assert.assertTrue(deleteResourceResponseFormatEither.isRight());
280 public void interfaceOperationFailedScenarioTest() {
281 validateUserRoles(Role.ADMIN, Role.DESIGNER);
282 Resource resourceWithoutInterface = new Resource();
283 resourceWithoutInterface.setUniqueId(resourceId);
284 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resourceWithoutInterface));
285 Either<Resource, ResponseFormat> interfaceOperation = bl.updateInterfaceOperation(resourceId, resourceWithoutInterface, user, true);
286 Assert.assertTrue(interfaceOperation.isRight());
289 private void validateUserRoles(Role... roles) {
290 List<Role> listOfRoles = Stream.of(roles).collect(Collectors.toList());
291 when(userValidations.validateUserRole(user, listOfRoles)).thenReturn(Either.left(true));
293 private Resource createMockResourceForAddInterface () {
294 Resource resource = new Resource();
295 resource.setUniqueId(resourceId);
296 resource.setName(RESOURCE_NAME);
297 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
298 resource.setDescription("My short description");
300 Map<String, Operation> operationMap = new HashMap<>();
301 Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
302 interfaceDefinitionMap.put("int1", createInterface("int1", "Interface 1",
303 "lifecycle", "org.openecomp.interfaces.node.lifecycle." + RESOURCE_NAME, operationMap));
304 resource.setInterfaces(interfaceDefinitionMap);
305 List<InputDefinition> inputDefinitionList = new ArrayList<>();
306 inputDefinitionList.add(createInputDefinition("uniqueId1"));
307 resource.setInputs(inputDefinitionList);
312 private Resource setCanWorkOnResource(Resource resource) {
313 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
314 resource.setLastUpdaterUserId(user.getUserId());
318 private Resource setUpResourceMock(){
319 Resource resource = new Resource();
320 resource.setUniqueId(resourceId);
321 resource.setName(RESOURCE_NAME);
322 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
323 resource.setDescription("My short description");
324 resource.setInterfaces(createMockInterfaceDefinition(RESOURCE_NAME));
326 List<InputDefinition> inputDefinitionList = new ArrayList<>();
327 inputDefinitionList.add(createInputDefinition("uniqueId1"));
328 resource.setInputs(inputDefinitionList);
333 private InputDefinition createInputDefinition(String inputId) {
334 InputDefinition inputDefinition = new InputDefinition();
335 inputDefinition.setInputId(inputId);
336 inputDefinition.setDescription("Input Description");
338 return inputDefinition;
341 private Resource createResourceForInterfaceOperation() {
342 Resource resource = new Resource();
343 resource.setUniqueId(resourceId);
344 resource.setName(RESOURCE_NAME);
345 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
346 resource.setDescription("Resource name for response");
347 resource.setInterfaces(createMockInterfaceDefinition(RESOURCE_NAME));
351 private Resource createResourceObjectCsar(boolean afterCreate) {
352 Resource resource = new Resource();
353 resource.setName(RESOURCE_NAME);
354 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
355 resource.setDescription("My short description");
356 List<String> tgs = new ArrayList<String>();
358 tgs.add(resource.getName());
359 resource.setTags(tgs);
360 List<String> template = new ArrayList<String>();
361 template.add("Root");
362 resource.setDerivedFrom(template);
363 resource.setVendorName("Motorola");
364 resource.setVendorRelease("1.0.0");
365 resource.setResourceVendorModelNumber("");
366 resource.setContactId("ya5467");
367 resource.setIcon("MyIcon");
368 resource.setCsarUUID("valid_vf.csar");
369 resource.setCsarVersion("1");
372 resource.setName(resource.getName());
373 resource.setVersion("0.1");
375 resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
376 resource.setCreatorUserId(user.getUserId());
377 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
378 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
383 private Resource createResourceObject(boolean afterCreate) {
384 Resource resource = new Resource();
385 resource.setName(RESOURCE_NAME);
386 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
387 resource.setDescription("My short description");
388 List<String> tgs = new ArrayList<String>();
390 tgs.add(resource.getName());
391 resource.setTags(tgs);
392 List<String> template = new ArrayList<String>();
393 template.add("Root");
394 resource.setDerivedFrom(template);
395 resource.setVendorName("Motorola");
396 resource.setVendorRelease("1.0.0");
397 resource.setContactId("ya5467");
398 resource.setIcon("MyIcon");
401 resource.setName(resource.getName());
402 resource.setVersion("0.1");
403 resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
404 resource.setCreatorUserId(user.getUserId());
405 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
406 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);