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 fj.data.Either;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34 import javax.servlet.ServletContext;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.openecomp.sdc.ElementOperationMock;
42 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
43 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
44 import org.openecomp.sdc.be.components.validation.InterfaceOperationValidation;
45 import org.openecomp.sdc.be.components.validation.UserValidations;
46 import org.openecomp.sdc.be.config.ConfigurationManager;
47 import org.openecomp.sdc.be.dao.api.ActionStatus;
48 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
49 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
50 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
51 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
52 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
53 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
54 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
55 import org.openecomp.sdc.be.impl.ComponentsUtils;
56 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
57 import org.openecomp.sdc.be.model.Component;
58 import org.openecomp.sdc.be.model.DataTypeDefinition;
59 import org.openecomp.sdc.be.model.InputDefinition;
60 import org.openecomp.sdc.be.model.LifecycleStateEnum;
61 import org.openecomp.sdc.be.model.Operation;
62 import org.openecomp.sdc.be.model.Resource;
63 import org.openecomp.sdc.be.model.User;
64 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
65 import org.openecomp.sdc.be.model.jsontitan.operations.InterfaceOperation;
66 import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation;
67 import org.openecomp.sdc.be.model.jsontitan.operations.NodeTypeOperation;
68 import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
69 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
70 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
71 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
72 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
73 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
74 import org.openecomp.sdc.be.user.Role;
75 import org.openecomp.sdc.be.user.UserBusinessLogic;
76 import org.openecomp.sdc.common.api.ConfigurationSource;
77 import org.openecomp.sdc.common.api.Constants;
78 import org.openecomp.sdc.common.impl.ExternalConfiguration;
79 import org.openecomp.sdc.common.impl.FSConfigurationSource;
80 import org.openecomp.sdc.exception.ResponseFormat;
81 import org.openecomp.sdc.test.utils.InterfaceOperationTestUtils;
82 import org.springframework.web.context.WebApplicationContext;
84 public class InterfaceOperationBusinessLogicTest {
86 public static final String RESOURCE_CATEGORY1 = "Network Layer 2-3";
87 public static final String RESOURCE_SUBCATEGORY = "Router";
89 private String resourceId = "resourceId1";
90 private String operationId = "uniqueId1";
91 Resource resourceUpdate;
94 public static final String RESOURCE_NAME = "My-Resource_Name with space";
96 final ServletContext servletContext = Mockito.mock(ServletContext.class);
97 IElementOperation mockElementDao;
98 TitanDao mockTitanDao = Mockito.mock(TitanDao.class);
99 UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
100 ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
101 NodeTypeOperation nodeTypeOperation = Mockito.mock(NodeTypeOperation.class);
102 NodeTemplateOperation nodeTemplateOperation = Mockito.mock(NodeTemplateOperation.class);
103 TopologyTemplateOperation topologyTemplateOperation = Mockito.mock(TopologyTemplateOperation.class);
104 final IPropertyOperation propertyOperation = Mockito.mock(IPropertyOperation.class);
105 final ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class);
106 WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
107 UserValidations userValidations = Mockito.mock(UserValidations.class);
108 WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
109 ArtifactCassandraDao artifactCassandraDao = Mockito.mock(ArtifactCassandraDao.class);
110 InterfaceOperation interfaceOperation = Mockito.mock(InterfaceOperation.class);
111 InterfaceOperationValidation operationValidator = Mockito.mock(InterfaceOperationValidation.class);
113 ResponseFormatManager responseManager = null;
114 GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
116 Resource resourceResponse = null;
117 ComponentsUtils componentsUtils;
118 ArtifactsBusinessLogic artifactManager = new ArtifactsBusinessLogic();
119 private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
122 InterfaceOperationBusinessLogic bl = new InterfaceOperationBusinessLogic();
125 public void setup() {
126 MockitoAnnotations.initMocks(this);
127 Mockito.reset(propertyOperation);
129 ExternalConfiguration.setAppName("catalog-be");
131 // init Configuration
132 String appConfigDir = "src/test/resources/config/catalog-be";
133 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
134 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
135 componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
138 mockElementDao = new ElementOperationMock();
140 // User data and management
142 user.setUserId("jh0003");
143 user.setFirstName("Jimmi");
144 user.setLastName("Hendrix");
145 user.setRole(Role.ADMIN.name());
147 Either<User, ActionStatus> eitherGetUser = Either.left(user);
148 when(mockUserAdmin.getUser("jh0003", false)).thenReturn(eitherGetUser);
149 when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), eq(false))).thenReturn(user);
150 when(userValidations.validateUserNotEmpty(eq(user), anyString())).thenReturn(user);
151 // Servlet Context attributes
152 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
153 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
154 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
155 when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
157 Either<Boolean, StorageOperationStatus> eitherFalse = Either.left(true);
158 when(toscaOperationFacade.validateComponentNameExists("Root", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE)).thenReturn(eitherFalse);
160 Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true);
161 when(toscaOperationFacade.validateComponentNameExists("alreadyExists", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE)).thenReturn(eitherCountExist);
163 Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false);
164 when(toscaOperationFacade.validateComponentNameExists(eq(RESOURCE_NAME), any(ResourceTypeEnum.class), eq(ComponentTypeEnum.RESOURCE))).thenReturn(eitherCount);
166 Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
167 when(toscaOperationFacade.validateToscaResourceNameExists("Root")).thenReturn(validateDerivedExists);
169 Either<Boolean, StorageOperationStatus> validateDerivedNotExists = Either.left(false);
170 when(toscaOperationFacade.validateToscaResourceNameExists("kuku")).thenReturn(validateDerivedNotExists);
171 when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
172 when(graphLockOperation.lockComponentByName(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
175 resourceResponse = createResourceObject(true);
176 Either<Resource, StorageOperationStatus> eitherCreate = Either.left(resourceResponse);
177 Either<Integer, StorageOperationStatus> eitherValidate = Either.left(null);
178 when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
179 //TODO Remove if passes
180 /*when(toscaOperationFacade.validateCsarUuidUniqueness(Mockito.anyString())).thenReturn(eitherValidate);*/
181 Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<String, DataTypeDefinition>();
182 when(applicationDataTypeCache.getAll()).thenReturn(Either.left(emptyDataTypes));
185 when(operationValidator.validateInterfaceOperations(anyCollection(), anyObject(), anyBoolean())).thenReturn(Either.left(true));
186 when(interfaceOperation.addInterface(anyString(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
187 when(interfaceOperation.updateInterface(anyString(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
188 when(interfaceOperation.addInterfaceOperation(anyObject(), anyObject(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockOperationToReturn(RESOURCE_NAME)));
189 when(interfaceOperation.updateInterfaceOperation(anyObject(), anyObject(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockOperationToReturn(RESOURCE_NAME)));
190 when(interfaceOperation.deleteInterfaceOperation(anyObject(), anyObject(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockOperationToReturn(RESOURCE_NAME)));
191 when(interfaceOperation.deleteInterfaceOperation(any(),any(), any())).thenReturn(Either.left(InterfaceOperationTestUtils.mockOperationToReturn(RESOURCE_NAME)));
192 when(interfaceOperation.updateInterface(any(),any())).thenReturn(Either.left(InterfaceOperationTestUtils.mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
193 when(mockTitanDao.commit()).thenReturn(TitanOperationStatus.OK);
196 artifactManager.setNodeTemplateOperation(nodeTemplateOperation);
197 bl = new InterfaceOperationBusinessLogic();
199 bl.setUserAdmin(mockUserAdmin);
200 bl.setComponentsUtils(componentsUtils);
201 bl.setGraphLockOperation(graphLockOperation);
202 bl.setTitanGenericDao(mockTitanDao);
203 toscaOperationFacade.setNodeTypeOperation(nodeTypeOperation);
204 toscaOperationFacade.setTopologyTemplateOperation(topologyTemplateOperation);
205 bl.setToscaOperationFacade(toscaOperationFacade);
206 bl.setUserValidations(userValidations);
207 bl.setInterfaceOperation(interfaceOperation);
208 bl.setInterfaceOperationValidation(operationValidator);
209 Resource resourceCsar = createResourceObjectCsar(true);
210 setCanWorkOnResource(resourceCsar);
211 Either<Component, StorageOperationStatus> oldResourceRes = Either.left(resourceCsar);
212 when(toscaOperationFacade.getToscaFullElement(resourceCsar.getUniqueId())).thenReturn(oldResourceRes);
213 responseManager = ResponseFormatManager.getInstance();
217 public void createInterfaceOperationTest() {
218 Resource resource = createResourceForInterfaceOperation();
219 resource.setComponentType(ComponentTypeEnum.RESOURCE);
220 validateUserRoles(Role.ADMIN, Role.DESIGNER);
221 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
222 operation = InterfaceOperationTestUtils.createMockOperation();
223 Either<Operation, ResponseFormat> interfaceOperation = bl.createInterfaceOperation(resourceId, operation, user, true);
224 Assert.assertTrue(interfaceOperation.isLeft());
225 Assert.assertNotNull(interfaceOperation.left().value().getWorkflowId());
226 Assert.assertNotNull(interfaceOperation.left().value().getWorkflowVersionId());
230 public void updateInterfaceOperationTest() {
231 validateUserRoles(Role.ADMIN, Role.DESIGNER);
232 operation = InterfaceOperationTestUtils.createMockOperation();
233 Resource resource = createResourceForInterfaceOperation();
234 resource.setComponentType(ComponentTypeEnum.RESOURCE);
235 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
236 Either<Operation, ResponseFormat> interfaceOperation = bl.updateInterfaceOperation(resourceId, operation, user, true);
237 Assert.assertTrue(interfaceOperation.isLeft());
241 public void deleteInterfaceOperationTest() {
242 Resource resource = createResourceForInterfaceOperation();
243 resource.setComponentType(ComponentTypeEnum.RESOURCE);
244 validateUserRoles(Role.ADMIN, Role.DESIGNER);
245 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
246 when(artifactCassandraDao.deleteArtifact(any(String.class))).thenReturn(CassandraOperationStatus.OK);
247 Either<Operation, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, operationId, user, true);
248 Assert.assertTrue(deleteResourceResponseFormatEither.isLeft());
252 public void getInterfaceOperationTest() {
253 Resource resource = createResourceForInterfaceOperation();
254 resource.setComponentType(ComponentTypeEnum.RESOURCE);
255 validateUserRoles(Role.ADMIN, Role.DESIGNER);
256 when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
257 Either<Operation, ResponseFormat> getResourceResponseFormatEither = bl.getInterfaceOperation(resourceId, operationId, user, true);
258 Assert.assertTrue(getResourceResponseFormatEither.isLeft());
261 private void validateUserRoles(Role... roles) {
262 List<Role> listOfRoles = Stream.of(roles).collect(Collectors.toList());
265 private Resource setCanWorkOnResource(Resource resource) {
266 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
267 resource.setLastUpdaterUserId(user.getUserId());
271 private Resource setUpResourceMock(){
272 Resource resource = new Resource();
273 resource.setUniqueId(resourceId);
274 resource.setName(RESOURCE_NAME);
275 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
276 resource.setDescription("My short description");
277 resource.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinition(RESOURCE_NAME));
279 List<InputDefinition> inputDefinitionList = new ArrayList<>();
280 inputDefinitionList.add(createInputDefinition("uniqueId1"));
281 resource.setInputs(inputDefinitionList);
286 private InputDefinition createInputDefinition(String inputId) {
287 InputDefinition inputDefinition = new InputDefinition();
288 inputDefinition.setInputId(inputId);
289 inputDefinition.setDescription("Input Description");
291 return inputDefinition;
294 private Resource createResourceForInterfaceOperation() {
295 Resource resource = new Resource();
296 resource.setUniqueId(resourceId);
297 resource.setName(RESOURCE_NAME);
298 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
299 resource.setDescription("Resource name for response");
300 resource.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinition(RESOURCE_NAME));
304 private Resource createResourceObjectCsar(boolean afterCreate) {
305 Resource resource = new Resource();
306 resource.setName(RESOURCE_NAME);
307 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
308 resource.setDescription("My short description");
309 List<String> tgs = new ArrayList<String>();
311 tgs.add(resource.getName());
312 resource.setTags(tgs);
313 List<String> template = new ArrayList<String>();
314 template.add("Root");
315 resource.setDerivedFrom(template);
316 resource.setVendorName("Motorola");
317 resource.setVendorRelease("1.0.0");
318 resource.setResourceVendorModelNumber("");
319 resource.setContactId("ya5467");
320 resource.setIcon("MyIcon");
321 resource.setCsarUUID("valid_vf.csar");
322 resource.setCsarVersion("1");
325 resource.setName(resource.getName());
326 resource.setVersion("0.1");
328 resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
329 resource.setCreatorUserId(user.getUserId());
330 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
331 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
336 private Resource createResourceObject(boolean afterCreate) {
337 Resource resource = new Resource();
338 resource.setName(RESOURCE_NAME);
339 resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
340 resource.setDescription("My short description");
341 List<String> tgs = new ArrayList<String>();
343 tgs.add(resource.getName());
344 resource.setTags(tgs);
345 List<String> template = new ArrayList<String>();
346 template.add("Root");
347 resource.setDerivedFrom(template);
348 resource.setVendorName("Motorola");
349 resource.setVendorRelease("1.0.0");
350 resource.setContactId("ya5467");
351 resource.setIcon("MyIcon");
354 resource.setName(resource.getName());
355 resource.setVersion("0.1");
356 resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
357 resource.setCreatorUserId(user.getUserId());
358 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
359 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);