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 fj.data.Either;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.openecomp.sdc.ElementOperationMock;
27 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
28 import org.openecomp.sdc.be.components.InterfaceOperationTestUtils;
29 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
30 import org.openecomp.sdc.be.components.validation.InterfaceOperationValidation;
31 import org.openecomp.sdc.be.components.validation.UserValidations;
32 import org.openecomp.sdc.be.config.ConfigurationManager;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
35 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
36 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
37 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
38 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
39 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
40 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
41 import org.openecomp.sdc.be.impl.ComponentsUtils;
42 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
43 import org.openecomp.sdc.be.model.Component;
44 import org.openecomp.sdc.be.model.DataTypeDefinition;
45 import org.openecomp.sdc.be.model.InputDefinition;
46 import org.openecomp.sdc.be.model.InterfaceDefinition;
47 import org.openecomp.sdc.be.model.LifecycleStateEnum;
48 import org.openecomp.sdc.be.model.Operation;
49 import org.openecomp.sdc.be.model.Resource;
50 import org.openecomp.sdc.be.model.User;
51 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
52 import org.openecomp.sdc.be.model.jsontitan.operations.InterfaceOperation;
53 import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation;
54 import org.openecomp.sdc.be.model.jsontitan.operations.NodeTypeOperation;
55 import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
56 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
57 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
58 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
59 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
60 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
61 import org.openecomp.sdc.be.user.Role;
62 import org.openecomp.sdc.be.user.UserBusinessLogic;
63 import org.openecomp.sdc.common.api.ConfigurationSource;
64 import org.openecomp.sdc.common.api.Constants;
65 import org.openecomp.sdc.common.impl.ExternalConfiguration;
66 import org.openecomp.sdc.common.impl.FSConfigurationSource;
67 import org.openecomp.sdc.exception.ResponseFormat;
68 import org.springframework.web.context.WebApplicationContext;
70 import javax.servlet.ServletContext;
71 import java.util.ArrayList;
72 import java.util.HashMap;
73 import java.util.HashSet;
74 import java.util.List;
77 import java.util.stream.Collectors;
78 import java.util.stream.Stream;
80 import static org.mockito.ArgumentMatchers.anyCollection;
81 import static org.mockito.ArgumentMatchers.anyBoolean;
82 import static org.mockito.ArgumentMatchers.anyObject;
83 import static org.mockito.ArgumentMatchers.any;
84 import static org.mockito.Matchers.anyString;
85 import static org.mockito.Matchers.eq;
86 import static org.mockito.Mockito.when;
88 public class InterfaceOperationBusinessLogicTest implements InterfaceOperationTestUtils{
90 public static final String RESOURCE_CATEGORY1 = "Network Layer 2-3";
91 public static final String RESOURCE_SUBCATEGORY = "Router";
94 private String resourceId = "resourceId1";
95 private String operationId = "uniqueId1";
96 Resource resourceUpdate;
98 public static final String RESOURCE_NAME = "My-Resource_Name with space";
100 final ServletContext servletContext = Mockito.mock(ServletContext.class);
101 IElementOperation mockElementDao;
102 TitanDao mockTitanDao = Mockito.mock(TitanDao.class);
103 UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
104 ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
105 NodeTypeOperation nodeTypeOperation = Mockito.mock(NodeTypeOperation.class);
106 NodeTemplateOperation nodeTemplateOperation = Mockito.mock(NodeTemplateOperation.class);
107 TopologyTemplateOperation topologyTemplateOperation = Mockito.mock(TopologyTemplateOperation.class);
108 final IPropertyOperation propertyOperation = Mockito.mock(IPropertyOperation.class);
109 final ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class);
110 WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
111 UserValidations userValidations = Mockito.mock(UserValidations.class);
112 WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
113 ArtifactCassandraDao artifactCassandraDao = Mockito.mock(ArtifactCassandraDao.class);
114 InterfaceOperation interfaceOperation = Mockito.mock(InterfaceOperation.class);
115 InterfaceOperationValidation operationValidator = Mockito.mock(InterfaceOperationValidation.class);
117 ResponseFormatManager responseManager = null;
118 GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
120 Resource resourceResponse = null;
121 ComponentsUtils componentsUtils;
122 ArtifactsBusinessLogic artifactManager = new ArtifactsBusinessLogic();
123 private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
127 InterfaceOperationBusinessLogic bl = new InterfaceOperationBusinessLogic();
130 public void setup() {
131 MockitoAnnotations.initMocks(this);
132 Mockito.reset(propertyOperation);
134 ExternalConfiguration.setAppName("catalog-be");
136 // init Configuration
137 String appConfigDir = "src/test/resources/config/catalog-be";
138 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
139 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
140 componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
143 mockElementDao = new ElementOperationMock();
145 // User data and management
147 user.setUserId("jh0003");
148 user.setFirstName("Jimmi");
149 user.setLastName("Hendrix");
150 user.setRole(Role.ADMIN.name());
152 Either<User, ActionStatus> eitherGetUser = Either.left(user);
153 when(mockUserAdmin.getUser("jh0003", false)).thenReturn(eitherGetUser);
154 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), eq(false))).thenReturn(Either.left(user));
155 when(userValidations.validateUserNotEmpty(eq(user), anyString())).thenReturn(Either.left(user));
156 // Servlet Context attributes
157 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
158 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
159 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
160 when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
162 Either<Boolean, StorageOperationStatus> eitherFalse = Either.left(true);
163 when(toscaOperationFacade.validateComponentNameExists("Root", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE)).thenReturn(eitherFalse);
165 Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true);
166 when(toscaOperationFacade.validateComponentNameExists("alreadyExists", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE)).thenReturn(eitherCountExist);
168 Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false);
169 when(toscaOperationFacade.validateComponentNameExists(eq(RESOURCE_NAME), any(ResourceTypeEnum.class), eq(ComponentTypeEnum.RESOURCE))).thenReturn(eitherCount);
171 Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
172 when(toscaOperationFacade.validateToscaResourceNameExists("Root")).thenReturn(validateDerivedExists);
174 Either<Boolean, StorageOperationStatus> validateDerivedNotExists = Either.left(false);
175 when(toscaOperationFacade.validateToscaResourceNameExists("kuku")).thenReturn(validateDerivedNotExists);
176 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
177 when(graphLockOperation.lockComponentByName(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
180 resourceResponse = createResourceObject(true);
181 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(resourceResponse);
182 Either<Integer, StorageOperationStatus> eitherValidate = Either.left(null);
183 when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
184 when(toscaOperationFacade.validateCsarUuidUniqueness(Mockito.anyString())).thenReturn(eitherValidate);
185 Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<String, DataTypeDefinition>();
186 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(emptyDataTypes));
190 when(operationValidator.validateInterfaceOperations(anyCollection(), anyString(), anyBoolean())).thenReturn(Either.left(true));
191 when(interfaceOperation.addInterface(anyString(), anyObject())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
192 when(interfaceOperation.updateInterface(anyString(), anyObject())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
193 when(interfaceOperation.deleteInterface(anyObject(), anyObject())).thenReturn(Either.left(new HashSet<>()));
194 when(interfaceOperation.deleteInterface(any(),any())).thenReturn(Either.left(new HashSet<>()));
195 when(interfaceOperation.updateInterface(any(),any())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
196 when(mockTitanDao.commit()).thenReturn(TitanOperationStatus.OK);
199 artifactManager.setNodeTemplateOperation(nodeTemplateOperation);
200 bl = new InterfaceOperationBusinessLogic();
202 bl.setUserAdmin(mockUserAdmin);
203 bl.setComponentsUtils(componentsUtils);
204 bl.setGraphLockOperation(graphLockOperation);
205 bl.setTitanGenericDao(mockTitanDao);
206 bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
207 toscaOperationFacade.setNodeTypeOperation(nodeTypeOperation);
208 toscaOperationFacade.setTopologyTemplateOperation(topologyTemplateOperation);
209 bl.setToscaOperationFacade(toscaOperationFacade);
210 bl.setUserValidations(userValidations);
211 bl.setInterfaceOperation(interfaceOperation);
212 bl.setInterfaceOperationValidation(operationValidator);
213 bl.setArtifactCassandraDao(artifactCassandraDao);
214 Resource resourceCsar = createResourceObjectCsar(true);
215 setCanWorkOnResource(resourceCsar);
216 Either<Component, StorageOperationStatus> oldResourceRes = Either.left(resourceCsar);
217 when(toscaOperationFacade.getToscaFullElement(resourceCsar.getUniqueId())).thenReturn(oldResourceRes);
218 responseManager = ResponseFormatManager.getInstance();
223 public void createInterfaceOperationTest() {
224 validateUserRoles(Role.ADMIN, Role.DESIGNER);
225 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createMockResourceForAddInterface()));
226 resourceUpdate = setUpResourceMock();
227 Either<Resource, ResponseFormat> interfaceOperation = bl.createInterfaceOperation(resourceId, resourceUpdate, user, true);
228 Assert.assertTrue(interfaceOperation.isLeft());
233 public void updateInterfaceOperationTest() {
234 validateUserRoles(Role.ADMIN, Role.DESIGNER);
235 resourceUpdate = setUpResourceMock();
236 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createResourceForInterfaceOperation()));
237 Either<Resource, ResponseFormat> interfaceOperation = bl.updateInterfaceOperation(resourceId, resourceUpdate, user, true);
238 Assert.assertTrue(interfaceOperation.isLeft());
243 public void deleteInterfaceOperationTest() {
244 validateUserRoles(Role.ADMIN, Role.DESIGNER);
245 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createResourceForInterfaceOperation()));
246 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
247 Set<String> idsToDelete = new HashSet<>();
248 idsToDelete.add(operationId);
250 Either<Resource, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, idsToDelete, user, true);
251 Assert.assertTrue(deleteResourceResponseFormatEither.isLeft());
255 public void deleteInterfaceOperationTestShouldFailWrongId() {
256 validateUserRoles(Role.ADMIN, Role.DESIGNER);
257 Set<String> idsToDelete = new HashSet<>();
258 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createResourceForInterfaceOperation()));
259 idsToDelete.add(resourceId);
260 Either<Resource, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, idsToDelete, user, true);
261 Assert.assertFalse(deleteResourceResponseFormatEither.isLeft());
266 public void deleteInterfaceOperationFailToDeleteArtifactTest() {
267 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createResourceForInterfaceOperation()));
268 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
269 validateUserRoles(Role.ADMIN, Role.DESIGNER);
270 Set<String> idsToDelete = new HashSet<>();
271 idsToDelete.add(operationId);
273 Either<Resource, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, idsToDelete, user, true);
274 Assert.assertTrue(deleteResourceResponseFormatEither.isRight());
278 public void interfaceOperationFailedScenarioTest() {
279 validateUserRoles(Role.ADMIN, Role.DESIGNER);
280 Resource resourceWithoutInterface = new Resource();
281 resourceWithoutInterface.setUniqueId(resourceId);
282 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resourceWithoutInterface));
283 Either<Resource, ResponseFormat> interfaceOperation = bl.updateInterfaceOperation(resourceId, resourceWithoutInterface, user, true);
284 Assert.assertTrue(interfaceOperation.isRight());
287 private void validateUserRoles(Role... roles) {
288 List<Role> listOfRoles = Stream.of(roles).collect(Collectors.toList());
289 when(userValidations.validateUserRole(user, listOfRoles)).thenReturn(Either.left(true));
291 private Resource createMockResourceForAddInterface () {
292 Resource resource = new Resource();
293 resource.setUniqueId(resourceId);
294 resource.setName(RESOURCE_NAME);
295 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
296 resource.setDescription("My short description");
298 Map<String, Operation> operationMap = new HashMap<>();
299 Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
300 interfaceDefinitionMap.put("int1", createInterface("int1", "Interface 1",
301 "lifecycle", "org.openecomp.interfaces.node.lifecycle." + RESOURCE_NAME, operationMap));
302 resource.setInterfaces(interfaceDefinitionMap);
303 List<InputDefinition> inputDefinitionList = new ArrayList<>();
304 inputDefinitionList.add(createInputDefinition("uniqueId1"));
305 resource.setInputs(inputDefinitionList);
310 private Resource setCanWorkOnResource(Resource resource) {
311 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
312 resource.setLastUpdaterUserId(user.getUserId());
316 private Resource setUpResourceMock(){
317 Resource resource = new Resource();
318 resource.setUniqueId(resourceId);
319 resource.setName(RESOURCE_NAME);
320 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
321 resource.setDescription("My short description");
322 resource.setInterfaces(createMockInterfaceDefinition(RESOURCE_NAME));
324 List<InputDefinition> inputDefinitionList = new ArrayList<>();
325 inputDefinitionList.add(createInputDefinition("uniqueId1"));
326 resource.setInputs(inputDefinitionList);
331 private InputDefinition createInputDefinition(String inputId) {
332 InputDefinition inputDefinition = new InputDefinition();
333 inputDefinition.setInputId(inputId);
334 inputDefinition.setDescription("Input Description");
336 return inputDefinition;
339 private Resource createResourceForInterfaceOperation() {
340 Resource resource = new Resource();
341 resource.setUniqueId(resourceId);
342 resource.setName(RESOURCE_NAME);
343 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
344 resource.setDescription("Resource name for response");
345 resource.setInterfaces(createMockInterfaceDefinition(RESOURCE_NAME));
349 private Resource createResourceObjectCsar(boolean afterCreate) {
350 Resource resource = new Resource();
351 resource.setName(RESOURCE_NAME);
352 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
353 resource.setDescription("My short description");
354 List<String> tgs = new ArrayList<String>();
356 tgs.add(resource.getName());
357 resource.setTags(tgs);
358 List<String> template = new ArrayList<String>();
359 template.add("Root");
360 resource.setDerivedFrom(template);
361 resource.setVendorName("Motorola");
362 resource.setVendorRelease("1.0.0");
363 resource.setResourceVendorModelNumber("");
364 resource.setContactId("ya5467");
365 resource.setIcon("MyIcon");
366 resource.setCsarUUID("valid_vf.csar");
367 resource.setCsarVersion("1");
370 resource.setName(resource.getName());
371 resource.setVersion("0.1");
373 resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
374 resource.setCreatorUserId(user.getUserId());
375 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
376 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
381 private Resource createResourceObject(boolean afterCreate) {
382 Resource resource = new Resource();
383 resource.setName(RESOURCE_NAME);
384 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
385 resource.setDescription("My short description");
386 List<String> tgs = new ArrayList<String>();
388 tgs.add(resource.getName());
389 resource.setTags(tgs);
390 List<String> template = new ArrayList<String>();
391 template.add("Root");
392 resource.setDerivedFrom(template);
393 resource.setVendorName("Motorola");
394 resource.setVendorRelease("1.0.0");
395 resource.setContactId("ya5467");
396 resource.setIcon("MyIcon");
399 resource.setName(resource.getName());
400 resource.setVersion("0.1");
401 resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
402 resource.setCreatorUserId(user.getUserId());
403 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
404 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);