2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components;
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
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.jsongraph.JanusGraphDao;
35 import org.openecomp.sdc.be.impl.ComponentsUtils;
36 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
37 import org.openecomp.sdc.be.model.*;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
39 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
40 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.be.user.Role;
43 import org.openecomp.sdc.be.user.UserBusinessLogic;
44 import org.openecomp.sdc.common.api.ConfigurationSource;
45 import org.openecomp.sdc.common.api.Constants;
46 import org.openecomp.sdc.common.impl.ExternalConfiguration;
47 import org.openecomp.sdc.common.impl.FSConfigurationSource;
48 import org.openecomp.sdc.exception.ResponseFormat;
49 import org.openecomp.sdc.test.utils.InterfaceOperationTestUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.web.context.WebApplicationContext;
54 import javax.servlet.ServletContext;
55 import java.lang.reflect.Field;
58 import static org.junit.Assert.*;
59 import static org.mockito.ArgumentMatchers.*;
60 import static org.mockito.Mockito.when;
62 public class PropertyBusinessLogicTest {
64 private static final Logger log = LoggerFactory.getLogger(PropertyBusinessLogicTest.class);
66 private ServletContext servletContext;
68 private IPropertyOperation propertyOperation;
70 private WebAppContextWrapper webAppContextWrapper;
72 private UserBusinessLogic mockUserAdmin;
74 private WebApplicationContext webAppContext;
76 private ComponentsUtils componentsUtils;
78 private ToscaOperationFacade toscaOperationFacade;
80 private UserValidations userValidations;
82 IGraphLockOperation graphLockOperation;
84 JanusGraphDao janusGraphDao;
87 private PropertyBusinessLogic bl = new PropertyBusinessLogic();
88 private User user = null;
89 private String resourceId = "resourceforproperty.0.1";
90 private String serviceId = "serviceForProperty.0.1";
91 private static final String interfaceType = "interfaceType";
92 private static final String operationType = "operationType";
93 private static final String operationId = "operationId";
94 private static final String operationId2 = "operationId2";
98 MockitoAnnotations.initMocks(this);
99 ExternalConfiguration.setAppName("catalog-be");
101 // init Configuration
102 String appConfigDir = "src/test/resources/config/catalog-be";
103 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
104 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
106 // User data and management
108 user.setUserId("jh003");
109 user.setFirstName("Jimmi");
110 user.setLastName("Hendrix");
111 user.setRole(Role.ADMIN.name());
113 Either<User, ActionStatus> eitherGetUser = Either.left(user);
114 when(mockUserAdmin.getUser("jh003", false)).thenReturn(eitherGetUser);
115 when(userValidations.validateUserExists(eq("jh003"), anyString(), eq(false))).thenReturn(user);
117 // Servlet Context attributes
118 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
119 when(servletContext.getAttribute(Constants.PROPERTY_OPERATION_MANAGER)).thenReturn(propertyOperation);
120 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
121 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
127 public void getProperty_propertyNotFound() throws Exception {
128 Resource resource = new Resource();
129 PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
130 PropertyDefinition property2 = createPropertyObject("someProperty2", "myResource");
131 resource.setProperties(Arrays.asList(property1, property2));
132 String resourceId = "myResource";
133 resource.setUniqueId(resourceId);
135 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
136 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> nonExistingProperty = bl.getComponentProperty(resourceId, "NonExistingProperty", user.getUserId());
137 assertTrue(nonExistingProperty.isRight());
138 Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
142 public void getProperty_propertyNotBelongsToResource() throws Exception {
143 Resource resource = new Resource();
144 PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
145 resource.setProperties(Arrays.asList(property1));
146 String resourceId = "myResource";
147 resource.setUniqueId(resourceId);
149 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
150 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> notFoundProperty = bl.getComponentProperty(resourceId, "invalidId", user.getUserId());
151 assertTrue(notFoundProperty.isRight());
152 Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
156 public void getProperty() throws Exception {
157 Resource resource = new Resource();
158 resource.setUniqueId(resourceId);
159 PropertyDefinition property1 = createPropertyObject("someProperty", null);
160 resource.setProperties(Arrays.asList(property1));
162 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
163 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> foundProperty = bl.getComponentProperty(resourceId, property1.getUniqueId(), user.getUserId());
164 assertTrue(foundProperty.isLeft());
165 assertEquals(foundProperty.left().value().getValue().getUniqueId(), property1.getUniqueId());
169 public void testGetPropertyFromService() {
170 Service service = new Service();
171 service.setUniqueId(serviceId);
173 PropertyDefinition property1 = createPropertyObject("someProperty", null);
174 service.setProperties(Arrays.asList(property1));
176 Mockito.when(toscaOperationFacade.getToscaElement(serviceId)).thenReturn(Either.left(service));
177 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> serviceProperty =
178 bl.getComponentProperty(serviceId, property1.getUniqueId(), user.getUserId());
180 assertTrue(serviceProperty.isLeft());
181 assertEquals(serviceProperty.left().value().getValue().getUniqueId(), property1.getUniqueId());
185 public void testPropertyNotFoundOnService() {
186 Service service = new Service();
187 service.setUniqueId(serviceId);
189 PropertyDefinition property1 = createPropertyObject("someProperty", null);
190 service.setProperties(Arrays.asList(property1));
192 Mockito.when(toscaOperationFacade.getToscaElement(serviceId)).thenReturn(Either.left(service));
193 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> serviceProperty =
194 bl.getComponentProperty(serviceId, "notExistingPropId", user.getUserId());
196 assertTrue(serviceProperty.isRight());
200 public void isPropertyUsedByComponentInterface(){
201 Service service = new Service();
202 service.setUniqueId(serviceId);
203 service.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType));
205 PropertyDefinition propDef1 = new PropertyDefinition();
206 propDef1.setUniqueId("ComponentInput1_uniqueId");
207 assertTrue(bl.isPropertyUsedByOperation(service, propDef1));
209 PropertyDefinition propDef2 = new PropertyDefinition();
210 propDef1.setUniqueId("inputId2");
211 Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>()));
212 assertFalse(bl.isPropertyUsedByOperation(service, propDef2));
216 public void isPropertyUsedByComponentInstanceInterface(){
217 Map<String, InterfaceDefinition> newInterfaceDefinition = InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType);
218 ComponentInstanceInterface componentInstanceInterface = new ComponentInstanceInterface(interfaceType, newInterfaceDefinition.get(interfaceType));
220 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>();
221 componentInstanceInterfaces.put("Test", Arrays.asList(componentInstanceInterface));
223 Service service = new Service();
224 service.setUniqueId(serviceId);
225 service.setComponentInstancesInterfaces(componentInstanceInterfaces);
227 PropertyDefinition propDef1 = new PropertyDefinition();
228 propDef1.setUniqueId("ComponentInput1_uniqueId");
229 assertTrue(bl.isPropertyUsedByOperation(service, propDef1));
231 PropertyDefinition propDef2 = new PropertyDefinition();
232 propDef1.setUniqueId("inputId2");
233 Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>()));
234 assertFalse(bl.isPropertyUsedByOperation(service, propDef2));
238 public void isPropertyUsedByComponentParentComponentInstanceInterface(){
239 Map<String, InterfaceDefinition> newInterfaceDefinition = InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType);
240 ComponentInstanceInterface componentInstanceInterface = new ComponentInstanceInterface(interfaceType, newInterfaceDefinition.get(interfaceType));
242 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>();
243 componentInstanceInterfaces.put("Test", Arrays.asList(componentInstanceInterface));
245 Service parentService = new Service();
246 parentService.setComponentInstancesInterfaces(componentInstanceInterfaces);
247 Service childService = new Service();
248 childService.setUniqueId(serviceId);
250 PropertyDefinition propDef1 = new PropertyDefinition();
251 propDef1.setUniqueId("ComponentInput1_uniqueId");
252 Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(Arrays.asList(parentService)));
253 assertTrue(bl.isPropertyUsedByOperation(childService, propDef1));
255 PropertyDefinition propDef2 = new PropertyDefinition();
256 propDef1.setUniqueId("inputId2");
257 Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>()));
258 assertFalse(bl.isPropertyUsedByOperation(childService, propDef2));
262 private PropertyDefinition createPropertyObject(String propertyName, String resourceId) {
263 PropertyDefinition pd = new PropertyDefinition();
264 pd.setConstraints(null);
265 pd.setDefaultValue("100");
266 pd.setDescription("Size of thasdasdasdasde local disk, in Gigabytes (GB), available to applications running on the Compute node");
267 pd.setPassword(false);
268 pd.setRequired(true);
269 pd.setType("Integer");
270 pd.setOwnerId(resourceId);
271 pd.setUniqueId(resourceId + "." + propertyName);
276 public void deleteProperty_CONNECTION_FAILURE() {
277 StorageOperationStatus lockResult = StorageOperationStatus.CONNECTION_FAILURE;
278 when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
279 when(toscaOperationFacade.getToscaElement(anyString())).thenReturn(Either.left(new Resource()));
280 assertTrue(bl.deletePropertyFromComponent("resourceforproperty.0.1", "someProperty","i726").isRight());
284 public void deleteProperty_RESOURCE_NOT_FOUND() throws Exception {
286 Resource resource = new Resource();
287 PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
289 resource.setProperties(Arrays.asList(property1));
290 String resourceId = "myResource";
291 resource.setUniqueId(resourceId);
293 Field baseBusinessLogic3;
294 baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao");
295 baseBusinessLogic3.setAccessible(true);
296 baseBusinessLogic3.set(bl, janusGraphDao);
299 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
301 StorageOperationStatus lockResult = StorageOperationStatus.OK;
302 when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
303 //doNothing().when(janusGraphDao).commit();
305 Either<PropertyDefinition, ResponseFormat> result;
307 Component resourcereturn= new Resource();
308 resourcereturn.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
309 resourcereturn.setIsDeleted(false);
310 resourcereturn.setLastUpdaterUserId("USR01");
312 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
313 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
316 assertTrue(bl.deletePropertyFromComponent("RES01", "someProperty","i726").isRight());
320 public void deleteProperty_RESTRICTED_OPERATION() throws Exception {
322 Resource resource = new Resource();
323 PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
325 resource.setProperties(Arrays.asList(property1));
326 String resourceId = "myResource";
327 resource.setUniqueId(resourceId);
329 Field baseBusinessLogic3;
330 baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao");
331 baseBusinessLogic3.setAccessible(true);
332 baseBusinessLogic3.set(bl, janusGraphDao);
335 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
337 StorageOperationStatus lockResult = StorageOperationStatus.OK;
338 when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
339 //doNothing().when(janusGraphDao).commit();
341 Either<PropertyDefinition, ResponseFormat> result;
343 Component resourcereturn= new Resource();
344 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
345 resource.setIsDeleted(false);
346 resource.setLastUpdaterUserId("USR01");
348 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
349 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
352 assertTrue(bl.deletePropertyFromComponent("RES01", "someProperty","i726").isRight());
356 public void deleteProperty_RESTRICTED_() throws Exception {
358 Resource resource = new Resource();
359 PropertyDefinition property1 = createPropertyObject("PROP", "RES01");
360 property1.setUniqueId("PROP");
361 resource.setProperties(Arrays.asList(property1));
362 String resourceId = "myResource";
363 resource.setUniqueId(resourceId);
365 Field baseBusinessLogic3;
366 baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao");
367 baseBusinessLogic3.setAccessible(true);
368 baseBusinessLogic3.set(bl, janusGraphDao);
371 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
373 StorageOperationStatus lockResult = StorageOperationStatus.OK;
374 when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
375 //doNothing().when(janusGraphDao).commit();
377 Either<PropertyDefinition, ResponseFormat> result;
379 Component resourcereturn= new Resource();
380 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
381 resource.setIsDeleted(false);
382 resource.setLastUpdaterUserId("USR01");
384 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
385 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
386 when(toscaOperationFacade.deletePropertyOfComponent(anyObject(),anyString())).thenReturn(StorageOperationStatus.OK);
387 when(toscaOperationFacade.getParentComponents(anyString())).thenReturn(Either.left(new ArrayList<>()));
389 assertTrue(bl.deletePropertyFromComponent("RES01", "PROP","USR01").isRight());