Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / PropertyBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components;
22
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;
53
54 import javax.servlet.ServletContext;
55 import java.lang.reflect.Field;
56 import java.util.*;
57
58 import static org.junit.Assert.*;
59 import static org.mockito.ArgumentMatchers.*;
60 import static org.mockito.Mockito.when;
61
62 public class PropertyBusinessLogicTest {
63
64     private static final Logger log = LoggerFactory.getLogger(PropertyBusinessLogicTest.class);
65     @Mock
66     private ServletContext servletContext;
67     @Mock
68     private IPropertyOperation propertyOperation;
69     @Mock
70     private WebAppContextWrapper webAppContextWrapper;
71     @Mock
72     private UserBusinessLogic mockUserAdmin;
73     @Mock
74     private WebApplicationContext webAppContext;
75     @Mock
76     private ComponentsUtils componentsUtils;
77     @Mock
78     private ToscaOperationFacade toscaOperationFacade;
79     @Mock
80     private UserValidations userValidations;
81     @Mock
82     IGraphLockOperation graphLockOperation;
83     @Mock
84     JanusGraphDao janusGraphDao;
85
86     @InjectMocks
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";
95
96     @Before
97     public void setup() {
98         MockitoAnnotations.initMocks(this);
99         ExternalConfiguration.setAppName("catalog-be");
100
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);
105
106         // User data and management
107         user = new User();
108         user.setUserId("jh003");
109         user.setFirstName("Jimmi");
110         user.setLastName("Hendrix");
111         user.setRole(Role.ADMIN.name());
112
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);
116
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);
122
123
124     }
125
126     @Test
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);
134
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, "");
139     }
140
141     @Test
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);
148
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, "");
153     }
154
155     @Test
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));
161
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());
166     }
167
168     @Test
169     public void testGetPropertyFromService() {
170         Service service = new Service();
171         service.setUniqueId(serviceId);
172
173         PropertyDefinition property1 = createPropertyObject("someProperty", null);
174         service.setProperties(Arrays.asList(property1));
175
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());
179
180         assertTrue(serviceProperty.isLeft());
181         assertEquals(serviceProperty.left().value().getValue().getUniqueId(), property1.getUniqueId());
182     }
183
184     @Test
185     public void testPropertyNotFoundOnService() {
186         Service service = new Service();
187         service.setUniqueId(serviceId);
188
189         PropertyDefinition property1 = createPropertyObject("someProperty", null);
190         service.setProperties(Arrays.asList(property1));
191
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());
195
196         assertTrue(serviceProperty.isRight());
197     }
198
199     @Test
200     public void isPropertyUsedByComponentInterface(){
201         Service service = new Service();
202         service.setUniqueId(serviceId);
203         service.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType));
204
205         PropertyDefinition propDef1 = new PropertyDefinition();
206         propDef1.setUniqueId("ComponentInput1_uniqueId");
207         assertTrue(bl.isPropertyUsedByOperation(service, propDef1));
208
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));
213     }
214
215     @Test
216     public void isPropertyUsedByComponentInstanceInterface(){
217         Map<String, InterfaceDefinition> newInterfaceDefinition = InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType);
218         ComponentInstanceInterface componentInstanceInterface = new ComponentInstanceInterface(interfaceType, newInterfaceDefinition.get(interfaceType));
219
220         Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>();
221         componentInstanceInterfaces.put("Test", Arrays.asList(componentInstanceInterface));
222
223         Service service = new Service();
224         service.setUniqueId(serviceId);
225         service.setComponentInstancesInterfaces(componentInstanceInterfaces);
226
227         PropertyDefinition propDef1 = new PropertyDefinition();
228         propDef1.setUniqueId("ComponentInput1_uniqueId");
229         assertTrue(bl.isPropertyUsedByOperation(service, propDef1));
230
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));
235     }
236
237     @Test
238     public void isPropertyUsedByComponentParentComponentInstanceInterface(){
239         Map<String, InterfaceDefinition> newInterfaceDefinition = InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType);
240         ComponentInstanceInterface componentInstanceInterface = new ComponentInstanceInterface(interfaceType, newInterfaceDefinition.get(interfaceType));
241
242         Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>();
243         componentInstanceInterfaces.put("Test", Arrays.asList(componentInstanceInterface));
244
245         Service parentService = new Service();
246         parentService.setComponentInstancesInterfaces(componentInstanceInterfaces);
247         Service childService = new Service();
248         childService.setUniqueId(serviceId);
249
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));
254
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));
259     }
260
261
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);
272         return pd;
273     }
274
275     @Test
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());
281     }
282
283     @Test
284     public void deleteProperty_RESOURCE_NOT_FOUND() throws Exception {
285
286         Resource resource = new Resource();
287         PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
288
289         resource.setProperties(Arrays.asList(property1));
290         String resourceId = "myResource";
291         resource.setUniqueId(resourceId);
292
293         Field baseBusinessLogic3;
294         baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao");
295         baseBusinessLogic3.setAccessible(true);
296         baseBusinessLogic3.set(bl, janusGraphDao);
297
298
299         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
300
301         StorageOperationStatus lockResult = StorageOperationStatus.OK;
302         when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
303         //doNothing().when(janusGraphDao).commit();
304
305         Either<PropertyDefinition, ResponseFormat> result;
306
307         Component resourcereturn= new Resource();
308         resourcereturn.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
309         resourcereturn.setIsDeleted(false);
310         resourcereturn.setLastUpdaterUserId("USR01");
311
312         Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
313         when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
314
315
316         assertTrue(bl.deletePropertyFromComponent("RES01", "someProperty","i726").isRight());
317     }
318
319     @Test
320     public void deleteProperty_RESTRICTED_OPERATION() throws Exception {
321
322         Resource resource = new Resource();
323         PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
324
325         resource.setProperties(Arrays.asList(property1));
326         String resourceId = "myResource";
327         resource.setUniqueId(resourceId);
328
329         Field baseBusinessLogic3;
330         baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao");
331         baseBusinessLogic3.setAccessible(true);
332         baseBusinessLogic3.set(bl, janusGraphDao);
333
334
335         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
336
337         StorageOperationStatus lockResult = StorageOperationStatus.OK;
338         when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
339         //doNothing().when(janusGraphDao).commit();
340
341         Either<PropertyDefinition, ResponseFormat> result;
342
343         Component resourcereturn= new Resource();
344         resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
345         resource.setIsDeleted(false);
346         resource.setLastUpdaterUserId("USR01");
347
348         Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
349         when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
350
351
352         assertTrue(bl.deletePropertyFromComponent("RES01", "someProperty","i726").isRight());
353     }
354
355     @Test
356     public void deleteProperty_RESTRICTED_() throws Exception {
357
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);
364
365         Field baseBusinessLogic3;
366         baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao");
367         baseBusinessLogic3.setAccessible(true);
368         baseBusinessLogic3.set(bl, janusGraphDao);
369
370
371         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
372
373         StorageOperationStatus lockResult = StorageOperationStatus.OK;
374         when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
375         //doNothing().when(janusGraphDao).commit();
376
377         Either<PropertyDefinition, ResponseFormat> result;
378
379         Component resourcereturn= new Resource();
380         resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
381         resource.setIsDeleted(false);
382         resource.setLastUpdaterUserId("USR01");
383
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<>()));
388
389         assertTrue(bl.deletePropertyFromComponent("RES01", "PROP","USR01").isRight());
390     }
391 }