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.*;
44 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
45 import org.openecomp.sdc.be.model.jsontitan.operations.*;
46 import org.openecomp.sdc.be.model.jsontitan.utils.InterfaceUtils;
47 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
48 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
49 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
50 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
51 import org.openecomp.sdc.be.user.Role;
52 import org.openecomp.sdc.be.user.UserBusinessLogic;
53 import org.openecomp.sdc.common.api.ConfigurationSource;
54 import org.openecomp.sdc.common.api.Constants;
55 import org.openecomp.sdc.common.impl.ExternalConfiguration;
56 import org.openecomp.sdc.common.impl.FSConfigurationSource;
57 import org.openecomp.sdc.exception.ResponseFormat;
58 import org.springframework.web.context.WebApplicationContext;
60 import javax.servlet.ServletContext;
62 import java.util.stream.Collectors;
63 import java.util.stream.Stream;
65 import static org.mockito.ArgumentMatchers.*;
66 import static org.mockito.Mockito.when;
68 public class InterfaceOperationBusinessLogicTest implements InterfaceOperationTestUtils{
70 public static final String RESOURCE_CATEGORY1 = "Network Layer 2-3";
71 public static final String RESOURCE_SUBCATEGORY = "Router";
74 private String resourceId = "resourceId1";
75 private String operationId = "uniqueId1";
76 Resource resourceUpdate;
78 public static final String RESOURCE_NAME = "My-Resource_Name with space";
80 final ServletContext servletContext = Mockito.mock(ServletContext.class);
81 IElementOperation mockElementDao;
82 TitanDao mockTitanDao = Mockito.mock(TitanDao.class);
83 UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
84 ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
85 NodeTypeOperation nodeTypeOperation = Mockito.mock(NodeTypeOperation.class);
86 NodeTemplateOperation nodeTemplateOperation = Mockito.mock(NodeTemplateOperation.class);
87 TopologyTemplateOperation topologyTemplateOperation = Mockito.mock(TopologyTemplateOperation.class);
88 final IPropertyOperation propertyOperation = Mockito.mock(IPropertyOperation.class);
89 final ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class);
90 WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
91 UserValidations userValidations = Mockito.mock(UserValidations.class);
92 WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
93 ArtifactCassandraDao artifactCassandraDao = Mockito.mock(ArtifactCassandraDao.class);
94 InterfaceOperation interfaceOperation = Mockito.mock(InterfaceOperation.class);
95 InterfaceOperationValidation operationValidator = Mockito.mock(InterfaceOperationValidation.class);
97 ResponseFormatManager responseManager = null;
98 GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
100 Resource resourceResponse = null;
101 ComponentsUtils componentsUtils;
102 ArtifactsBusinessLogic artifactManager = new ArtifactsBusinessLogic();
103 private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
107 InterfaceOperationBusinessLogic bl = new InterfaceOperationBusinessLogic();
110 public void setup() {
111 MockitoAnnotations.initMocks(this);
112 Mockito.reset(propertyOperation);
114 ExternalConfiguration.setAppName("catalog-be");
116 // init Configuration
117 String appConfigDir = "src/test/resources/config/catalog-be";
118 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
119 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
120 componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
123 mockElementDao = new ElementOperationMock();
125 // User data and management
127 user.setUserId("jh0003");
128 user.setFirstName("Jimmi");
129 user.setLastName("Hendrix");
130 user.setRole(Role.ADMIN.name());
132 Either<User, ActionStatus> eitherGetUser = Either.left(user);
133 when(mockUserAdmin.getUser("jh0003", false)).thenReturn(eitherGetUser);
134 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), eq(false))).thenReturn(user);
135 when(userValidations.validateUserNotEmpty(eq(user), anyString())).thenReturn(user);
136 // Servlet Context attributes
137 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
138 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
139 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
140 when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
142 Either<Boolean, StorageOperationStatus> eitherFalse = Either.left(true);
143 when(toscaOperationFacade.validateComponentNameExists("Root", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE)).thenReturn(eitherFalse);
145 Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true);
146 when(toscaOperationFacade.validateComponentNameExists("alreadyExists", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE)).thenReturn(eitherCountExist);
148 Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false);
149 when(toscaOperationFacade.validateComponentNameExists(eq(RESOURCE_NAME), any(ResourceTypeEnum.class), eq(ComponentTypeEnum.RESOURCE))).thenReturn(eitherCount);
151 Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
152 when(toscaOperationFacade.validateToscaResourceNameExists("Root")).thenReturn(validateDerivedExists);
154 Either<Boolean, StorageOperationStatus> validateDerivedNotExists = Either.left(false);
155 when(toscaOperationFacade.validateToscaResourceNameExists("kuku")).thenReturn(validateDerivedNotExists);
156 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
157 when(graphLockOperation.lockComponentByName(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
160 resourceResponse = createResourceObject(true);
161 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(resourceResponse);
162 Either<Integer, StorageOperationStatus> eitherValidate = Either.left(null);
163 when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
164 //TODO Remove if passes
165 /*when(toscaOperationFacade.validateCsarUuidUniqueness(Mockito.anyString())).thenReturn(eitherValidate);*/
166 Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<String, DataTypeDefinition>();
167 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(emptyDataTypes));
171 when(operationValidator.validateInterfaceOperations(anyCollection(), anyString(), anyBoolean())).thenReturn(Either.left(true));
172 when(interfaceOperation.addInterface(anyString(), anyObject())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
173 when(interfaceOperation.updateInterface(anyString(), anyObject())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
174 when(interfaceOperation.deleteInterface(anyObject(), anyObject())).thenReturn(Either.left(new HashSet<>()));
175 when(interfaceOperation.deleteInterface(any(),any())).thenReturn(Either.left(new HashSet<>()));
176 when(interfaceOperation.updateInterface(any(),any())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
177 when(mockTitanDao.commit()).thenReturn(TitanOperationStatus.OK);
180 artifactManager.setNodeTemplateOperation(nodeTemplateOperation);
181 bl = new InterfaceOperationBusinessLogic();
183 bl.setUserAdmin(mockUserAdmin);
184 bl.setComponentsUtils(componentsUtils);
185 bl.setGraphLockOperation(graphLockOperation);
186 bl.setTitanGenericDao(mockTitanDao);
187 bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
188 toscaOperationFacade.setNodeTypeOperation(nodeTypeOperation);
189 toscaOperationFacade.setTopologyTemplateOperation(topologyTemplateOperation);
190 bl.setToscaOperationFacade(toscaOperationFacade);
191 bl.setUserValidations(userValidations);
192 bl.setInterfaceOperation(interfaceOperation);
193 bl.setInterfaceOperationValidation(operationValidator);
194 bl.setArtifactCassandraDao(artifactCassandraDao);
195 Resource resourceCsar = createResourceObjectCsar(true);
196 setCanWorkOnResource(resourceCsar);
197 Either<Component, StorageOperationStatus> oldResourceRes = Either.left(resourceCsar);
198 when(toscaOperationFacade.getToscaFullElement(resourceCsar.getUniqueId())).thenReturn(oldResourceRes);
199 responseManager = ResponseFormatManager.getInstance();
204 public void createInterfaceOperationTest() {
205 Resource resource = createResourceForInterfaceOperation();
206 resource.setComponentType(ComponentTypeEnum.RESOURCE);
207 validateUserRoles(Role.ADMIN, Role.DESIGNER);
208 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
209 resourceUpdate = setUpResourceMock();
210 Either<Resource, ResponseFormat> interfaceOperation = bl.createInterfaceOperation(resourceId, resourceUpdate, user, true);
211 Assert.assertTrue(interfaceOperation.isLeft());
212 Map<String, Operation> interfaceOperationsFromInterfaces = InterfaceUtils
213 .getInterfaceOperationsFromInterfaces(interfaceOperation.left().value().getInterfaces(),
214 interfaceOperation.left().value());
215 for(Operation operation : interfaceOperationsFromInterfaces.values()) {
216 Assert.assertNotNull(operation.getWorkflowId());
217 Assert.assertNotNull(operation.getWorkflowVersionId());
222 public void updateInterfaceOperationTest() {
223 validateUserRoles(Role.ADMIN, Role.DESIGNER);
224 resourceUpdate = setUpResourceMock();
225 Resource resource = createResourceForInterfaceOperation();
226 resource.setComponentType(ComponentTypeEnum.RESOURCE);
227 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
228 Either<Resource, ResponseFormat> interfaceOperation = bl.updateInterfaceOperation(resourceId, resourceUpdate, user, true);
229 Assert.assertTrue(interfaceOperation.isLeft());
234 public void deleteInterfaceOperationTest() {
235 Resource resource = createResourceForInterfaceOperation();
236 resource.setComponentType(ComponentTypeEnum.RESOURCE);
237 validateUserRoles(Role.ADMIN, Role.DESIGNER);
238 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
239 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
240 Set<String> idsToDelete = new HashSet<>();
241 idsToDelete.add(operationId);
243 Either<Resource, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, idsToDelete, user, true);
244 Assert.assertTrue(deleteResourceResponseFormatEither.isLeft());
248 public void deleteInterfaceOperationTestShouldFailWrongId() {
249 validateUserRoles(Role.ADMIN, Role.DESIGNER);
250 Set<String> idsToDelete = new HashSet<>();
251 Resource resource = createResourceForInterfaceOperation();
252 resource.setComponentType(ComponentTypeEnum.RESOURCE);
253 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
254 idsToDelete.add(resourceId);
255 Either<Resource, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, idsToDelete, user, true);
256 Assert.assertFalse(deleteResourceResponseFormatEither.isLeft());
261 public void deleteInterfaceOperationFailToDeleteArtifactTest() {
262 Resource resource = createResourceForInterfaceOperation();
263 resource.setComponentType(ComponentTypeEnum.RESOURCE);
264 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
265 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.GENERAL_ERROR);
266 validateUserRoles(Role.ADMIN, Role.DESIGNER);
267 Set<String> idsToDelete = new HashSet<>();
268 idsToDelete.add(operationId);
270 Either<Resource, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, idsToDelete, user, true);
271 Assert.assertTrue(deleteResourceResponseFormatEither.isRight());
275 public void interfaceOperationFailedScenarioTest() {
276 validateUserRoles(Role.ADMIN, Role.DESIGNER);
277 Resource resourceWithoutInterface = new Resource();
278 resourceWithoutInterface.setUniqueId(resourceId);
279 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resourceWithoutInterface));
280 Either<Resource, ResponseFormat> interfaceOperation = bl.updateInterfaceOperation(resourceId, resourceWithoutInterface, user, true);
281 Assert.assertTrue(interfaceOperation.isRight());
284 private void validateUserRoles(Role... roles) {
285 List<Role> listOfRoles = Stream.of(roles).collect(Collectors.toList());
287 private Resource createMockResourceForAddInterface () {
288 Resource resource = new Resource();
289 resource.setUniqueId(resourceId);
290 resource.setName(RESOURCE_NAME);
291 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
292 resource.setDescription("My short description");
294 Map<String, Operation> operationMap = new HashMap<>();
295 Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
296 interfaceDefinitionMap.put("int1", createInterface("int1", "Interface 1",
297 "lifecycle", "org.openecomp.interfaces.node.lifecycle." + RESOURCE_NAME, operationMap));
298 resource.setInterfaces(interfaceDefinitionMap);
299 List<InputDefinition> inputDefinitionList = new ArrayList<>();
300 inputDefinitionList.add(createInputDefinition("uniqueId1"));
301 resource.setInputs(inputDefinitionList);
306 private Resource setCanWorkOnResource(Resource resource) {
307 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
308 resource.setLastUpdaterUserId(user.getUserId());
312 private Resource setUpResourceMock(){
313 Resource resource = new Resource();
314 resource.setUniqueId(resourceId);
315 resource.setName(RESOURCE_NAME);
316 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
317 resource.setDescription("My short description");
318 resource.setInterfaces(createMockInterfaceDefinition(RESOURCE_NAME));
320 List<InputDefinition> inputDefinitionList = new ArrayList<>();
321 inputDefinitionList.add(createInputDefinition("uniqueId1"));
322 resource.setInputs(inputDefinitionList);
327 private InputDefinition createInputDefinition(String inputId) {
328 InputDefinition inputDefinition = new InputDefinition();
329 inputDefinition.setInputId(inputId);
330 inputDefinition.setDescription("Input Description");
332 return inputDefinition;
335 private Resource createResourceForInterfaceOperation() {
336 Resource resource = new Resource();
337 resource.setUniqueId(resourceId);
338 resource.setName(RESOURCE_NAME);
339 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
340 resource.setDescription("Resource name for response");
341 resource.setInterfaces(createMockInterfaceDefinition(RESOURCE_NAME));
345 private Resource createResourceObjectCsar(boolean afterCreate) {
346 Resource resource = new Resource();
347 resource.setName(RESOURCE_NAME);
348 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
349 resource.setDescription("My short description");
350 List<String> tgs = new ArrayList<String>();
352 tgs.add(resource.getName());
353 resource.setTags(tgs);
354 List<String> template = new ArrayList<String>();
355 template.add("Root");
356 resource.setDerivedFrom(template);
357 resource.setVendorName("Motorola");
358 resource.setVendorRelease("1.0.0");
359 resource.setResourceVendorModelNumber("");
360 resource.setContactId("ya5467");
361 resource.setIcon("MyIcon");
362 resource.setCsarUUID("valid_vf.csar");
363 resource.setCsarVersion("1");
366 resource.setName(resource.getName());
367 resource.setVersion("0.1");
369 resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
370 resource.setCreatorUserId(user.getUserId());
371 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
372 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
377 private Resource createResourceObject(boolean afterCreate) {
378 Resource resource = new Resource();
379 resource.setName(RESOURCE_NAME);
380 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
381 resource.setDescription("My short description");
382 List<String> tgs = new ArrayList<String>();
384 tgs.add(resource.getName());
385 resource.setTags(tgs);
386 List<String> template = new ArrayList<String>();
387 template.add("Root");
388 resource.setDerivedFrom(template);
389 resource.setVendorName("Motorola");
390 resource.setVendorRelease("1.0.0");
391 resource.setContactId("ya5467");
392 resource.setIcon("MyIcon");
395 resource.setName(resource.getName());
396 resource.setVersion("0.1");
397 resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
398 resource.setCreatorUserId(user.getUserId());
399 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
400 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);