Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / AttributeBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.impl;
22
23 import java.lang.reflect.Field;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import junit.framework.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.openecomp.sdc.be.components.validation.UserValidations;
34 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
35 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
36 import org.openecomp.sdc.be.impl.ComponentsUtils;
37 import org.openecomp.sdc.be.model.*;
38 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
40 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
43 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
44 import org.openecomp.sdc.exception.ResponseFormat;
45 import static org.mockito.ArgumentMatchers.any;
46 import static org.mockito.ArgumentMatchers.anyObject;
47 import static org.mockito.Mockito.when;
48
49 import fj.data.Either;
50
51
52 public class AttributeBusinessLogicTest {
53
54         private AttributeBusinessLogic createTestSubject() {
55                 return new AttributeBusinessLogic();
56         }
57
58         UserValidations userValidations  = Mockito.mock(UserValidations.class);
59         ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class);
60         JanusGraphDao janusGraphDao = Mockito.mock(JanusGraphDao.class);
61         ToscaOperationFacade toscaOperationFacade  = Mockito.mock(ToscaOperationFacade.class);
62         ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class);
63         PropertyOperation propertyOperation = Mockito.mock(PropertyOperation.class);
64         Field baseBusinessLogic;
65         Field baseBusinessLogic1;
66         Field baseBusinessLogic2;
67         Field baseBusinessLogic3;
68         AttributeBusinessLogic attributeBusinessLogic=createTestSubject();
69         IGraphLockOperation igraphLockOperation = Mockito.mock(IGraphLockOperation.class);
70
71
72         @Before
73         public void setup() throws Exception{
74
75                 baseBusinessLogic = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("graphLockOperation");
76                 baseBusinessLogic.setAccessible(true);
77                 baseBusinessLogic.set(attributeBusinessLogic, igraphLockOperation);
78
79                 baseBusinessLogic1 = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("userValidations");
80                 baseBusinessLogic1.setAccessible(true);
81                 baseBusinessLogic1.set(attributeBusinessLogic, userValidations);
82
83                 baseBusinessLogic2 = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("componentsUtils");
84                 baseBusinessLogic2.setAccessible(true);
85                 baseBusinessLogic2.set(attributeBusinessLogic, componentsUtils);
86
87                 baseBusinessLogic3 = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("janusGraphDao");
88                 baseBusinessLogic3.setAccessible(true);
89                 baseBusinessLogic3.set(attributeBusinessLogic, janusGraphDao);
90
91                 baseBusinessLogic = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("toscaOperationFacade");
92                 baseBusinessLogic.setAccessible(true);
93                 baseBusinessLogic.set(attributeBusinessLogic, toscaOperationFacade);
94
95                 baseBusinessLogic = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("applicationDataTypeCache");
96                 baseBusinessLogic.setAccessible(true);
97                 baseBusinessLogic.set(attributeBusinessLogic, applicationDataTypeCache);
98
99                 baseBusinessLogic = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("propertyOperation");
100                 baseBusinessLogic.setAccessible(true);
101                 baseBusinessLogic.set(attributeBusinessLogic, propertyOperation);
102         }
103
104
105         @Test
106         public void testCreateAttribute() throws Exception {
107                 AttributeBusinessLogic testSubject;
108                 String resourceId = "";
109                 PropertyDefinition newAttributeDef = null;
110                 String userId = "";
111                 Either<PropertyDefinition, ResponseFormat> result;
112
113                 // default test
114                 testSubject = createTestSubject();
115         }
116
117
118         @Test
119         public void testIsAttributeExist() throws Exception {
120                 AttributeBusinessLogic testSubject;List<PropertyDefinition> attributes = null;
121                 String resourceUid = "";
122                 String propertyName = "";
123                 boolean result;
124
125                 // test 1
126                 testSubject=createTestSubject();attributes = null;
127         }
128
129
130         @Test
131         public void testGetAttribute() throws Exception {
132                 AttributeBusinessLogic testSubject;
133                 String resourceId = "";
134                 String attributeId = "";
135                 String userId = "";
136                 Either<PropertyDefinition, ResponseFormat> result;
137
138                 // default test
139                 testSubject = createTestSubject();
140         }
141
142
143         @Test
144         public void testUpdateAttribute() throws Exception {
145                 AttributeBusinessLogic testSubject;
146                 String resourceId = "";
147                 String attributeId = "";
148                 PropertyDefinition newAttDef = null;
149                 String userId = "";
150                 Either<PropertyDefinition, ResponseFormat> result;
151
152                 // default test
153                 testSubject = createTestSubject();
154         }
155
156
157         @Test
158         public void testDeleteAttribute() throws Exception {
159                 AttributeBusinessLogic testSubject;
160                 String resourceId = "";
161                 String attributeId = "";
162                 String userId = "";
163                 Either<PropertyDefinition, ResponseFormat> result;
164
165                 // default test
166                 testSubject = createTestSubject();
167         }
168
169
170         @Test
171         public void createAttribute_lockfail() throws Exception {
172                 Either<PropertyDefinition, ResponseFormat> response;
173                 PropertyDefinition prop= new PropertyDefinition();
174
175                 response = attributeBusinessLogic.createAttribute("RES01", prop, "USR01");
176
177                 Assert.assertEquals(true,response.isRight());
178
179         }
180
181         @Test
182         public void createAttribute_Success() throws Exception {
183
184                 Component resource= new Resource();
185                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
186                 resource.setIsDeleted(false);
187                 resource.setLastUpdaterUserId("USR01");
188
189                 PropertyDefinition prop= new PropertyDefinition();
190                 prop.setType(ToscaPropertyType.STRING.getType());
191
192                 when(igraphLockOperation.lockComponent(any(),any())).thenReturn(StorageOperationStatus.OK);
193
194                 //Either<Component, StorageOperationStatus> toscastatus=Either.right(StorageOperationStatus.INVALID_PROPERTY);
195                 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
196                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
197                 PropertyDefinition propertyDefinition = new PropertyDefinition();
198                 Either<PropertyDefinition, StorageOperationStatus> either = Either.left(propertyDefinition);
199                 when(toscaOperationFacade.addAttributeOfResource(anyObject(),anyObject())).thenReturn(either);
200
201                 when(propertyOperation.isPropertyTypeValid(anyObject())).thenReturn(true);
202
203                 Map<String,DataTypeDefinition> data=new HashMap<>();
204                 data.put("ONE",new DataTypeDefinition());
205                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = Either.left(data);
206                 when(applicationDataTypeCache.getAll()).thenReturn(allDataTypes);
207
208                 when(propertyOperation.isPropertyDefaultValueValid(anyObject(),anyObject())).thenReturn(true);
209                 Either<PropertyDefinition, ResponseFormat> response;
210
211                 response = attributeBusinessLogic.createAttribute("RES01", prop, "USR01");
212
213                 Assert.assertEquals(true,response.isLeft());
214
215         }
216
217         @Test
218         public void createAttribute_failtogettoscaelement() throws Exception {
219
220                 Component resource= new Resource();
221                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
222                 resource.setIsDeleted(false);
223                 resource.setLastUpdaterUserId("USR01");
224
225                 when(igraphLockOperation.lockComponent(any(),any())).thenReturn(StorageOperationStatus.OK);
226
227                 Either<PropertyDefinition, ResponseFormat> response;
228                 PropertyDefinition prop= new PropertyDefinition();
229
230                 baseBusinessLogic = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("toscaOperationFacade");
231                 baseBusinessLogic.setAccessible(true);
232                 baseBusinessLogic.set(attributeBusinessLogic, toscaOperationFacade);
233                 //Either<Component, StorageOperationStatus> toscastatus=Either.right(StorageOperationStatus.INVALID_PROPERTY);
234                 Either<Component, StorageOperationStatus> toscastatus=Either.right(StorageOperationStatus.GENERAL_ERROR);
235                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
236
237                 response = attributeBusinessLogic.createAttribute("RES01", prop, "USR01");
238
239                 Assert.assertEquals(true,response.isRight());
240
241         }
242
243         @Test
244         public void createAttribute_componentvalidationfails() throws Exception {
245
246                 Component resource= new Resource();
247                 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
248                 resource.setIsDeleted(false);
249                 resource.setLastUpdaterUserId("USR02");
250
251                 when(igraphLockOperation.lockComponent(any(),any())).thenReturn(StorageOperationStatus.OK);
252
253                 Either<PropertyDefinition, ResponseFormat> response;
254                 PropertyDefinition prop= new PropertyDefinition();
255
256                 //Either<Component, StorageOperationStatus> toscastatus=Either.right(StorageOperationStatus.INVALID_PROPERTY);
257                 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
258                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
259
260                 response = attributeBusinessLogic.createAttribute("RES01", prop, "USR01");
261
262                 Assert.assertEquals(true,response.isRight());
263
264         }
265
266         @Test
267         public void createAttribute_componentalreadyexist_fails() throws Exception {
268
269                 Either<PropertyDefinition, ResponseFormat> response;
270                 PropertyDefinition prop= new PropertyDefinition();
271                 prop.setName("RES01");
272                 prop.setParentUniqueId("RES01");
273
274                 List<PropertyDefinition> attributes = new ArrayList<>();
275                 attributes.add(prop);
276
277                 Component resource= new Resource();
278                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
279                 resource.setIsDeleted(false);
280                 resource.setLastUpdaterUserId("USR01");
281                 ((Resource) resource).setAttributes(attributes);
282
283                 when(igraphLockOperation.lockComponent(any(),any())).thenReturn(StorageOperationStatus.OK);
284
285                 //Either<Component, StorageOperationStatus> toscastatus=Either.right(StorageOperationStatus.INVALID_PROPERTY);
286                 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
287                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
288
289                 response = attributeBusinessLogic.createAttribute("RES01", prop, "USR01");
290
291                 Assert.assertEquals(true,response.isRight());
292
293         }
294
295
296         @Test
297         public void createAttribute_addresourcetostoragefails() throws Exception {
298
299                 Component resource= new Resource();
300                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
301                 resource.setIsDeleted(false);
302                 resource.setLastUpdaterUserId("USR01");
303
304                 PropertyDefinition prop= new PropertyDefinition();
305                 prop.setType(ToscaPropertyType.STRING.getType());
306
307                 IGraphLockOperation igraphLockOperation = Mockito.mock(IGraphLockOperation.class);
308                 when(igraphLockOperation.lockComponent(any(),any())).thenReturn(StorageOperationStatus.OK);
309
310                 //Either<Component, StorageOperationStatus> toscastatus=Either.right(StorageOperationStatus.INVALID_PROPERTY);
311                 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
312                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
313                 PropertyDefinition propertyDefinition = new PropertyDefinition();
314                 Either<PropertyDefinition, StorageOperationStatus> either = Either.right(StorageOperationStatus.CONNECTION_FAILURE);
315                 when(toscaOperationFacade.addAttributeOfResource(anyObject(),anyObject())).thenReturn(either);
316
317                 when(propertyOperation.isPropertyTypeValid(anyObject())).thenReturn(true);
318
319                 Map<String,DataTypeDefinition> data=new HashMap<>();
320                 data.put("ONE",new DataTypeDefinition());
321                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = Either.left(data);
322                 when(applicationDataTypeCache.getAll()).thenReturn(allDataTypes);
323
324                 when(propertyOperation.isPropertyDefaultValueValid(anyObject(),anyObject())).thenReturn(true);
325                 Either<PropertyDefinition, ResponseFormat> response;
326
327                 response = attributeBusinessLogic.createAttribute("RES01", prop, "USR01");
328
329                 Assert.assertEquals(true,response.isRight());
330
331         }
332
333         @Test
334         public void testgetAttribute_ATTRIBUTE_NOT_FOUND() throws Exception {
335                 Either<PropertyDefinition, ResponseFormat> result;
336
337                 Component resource= new Resource();
338                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
339                 resource.setIsDeleted(false);
340                 resource.setLastUpdaterUserId("USR01");
341                 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
342                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
343
344                 result=attributeBusinessLogic.getAttribute("RES01","ATTR01", "USR01");
345                 Assert.assertEquals(true,result.isRight());
346         }
347
348         @Test
349         public void testgetAttribute_success() throws Exception {
350                 Either<PropertyDefinition, ResponseFormat> result;
351
352                 Component resource= new Resource();
353                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
354                 resource.setIsDeleted(false);
355                 resource.setLastUpdaterUserId("USR01");
356
357                 PropertyDefinition prop= new PropertyDefinition();
358                 prop.setUniqueId("ATTR01");
359                 prop.setParentUniqueId("RES01");
360
361                 List<PropertyDefinition> attr = new ArrayList<>();
362                 attr.add(prop);
363
364                 ((Resource) resource).setAttributes(attr);
365                 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
366                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
367
368                 result=attributeBusinessLogic.getAttribute("RES01","ATTR01", "USR01");
369                 Assert.assertEquals(true,result.isLeft());
370         }
371
372         @Test
373         public void testgetAttribute_RESOURCE_NOT_FOUND() throws Exception {
374                 Either<PropertyDefinition, ResponseFormat> result;
375
376                 Either<Component, StorageOperationStatus> toscastatus=Either.right(StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND);
377                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
378
379                 result=attributeBusinessLogic.getAttribute("RES01","ATTR01", "USR01");
380                 Assert.assertEquals(true,result.isRight());
381         }
382
383         @Test
384         public void testdeleteAttribute_FAILED_TO_LOCK_COMPONENT() throws Exception {
385                 Either<PropertyDefinition, ResponseFormat> result;
386
387                 result=attributeBusinessLogic.deleteAttribute("RES01","ATTR01", "USR01");
388                 Assert.assertEquals(true,result.isRight());
389         }
390
391         @Test
392         public void testdeleteAttribute_get_RESOURCE_from_DB_failed() throws Exception {
393                 Either<PropertyDefinition, ResponseFormat> result;
394
395                 Either<Component, StorageOperationStatus> toscastatus=Either.right(StorageOperationStatus.CONNECTION_FAILURE);
396                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
397
398                 result=attributeBusinessLogic.deleteAttribute("RES01","ATTR01", "USR01");
399                 Assert.assertEquals(true,result.isRight());
400         }
401
402         @Test
403         public void testdeleteAttribute_get_RESOURCE_verification_failed() throws Exception {
404                 Either<PropertyDefinition, ResponseFormat> result;
405
406
407                 Component resource= new Resource();
408                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
409                 resource.setIsDeleted(true);
410                 resource.setLastUpdaterUserId("USR01");
411
412
413                 when(igraphLockOperation.lockComponent(any(),any())).thenReturn(StorageOperationStatus.OK);
414
415
416
417
418                 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
419                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
420
421                 result=attributeBusinessLogic.deleteAttribute("RES01","ATTR01", "USR01");
422                 Assert.assertEquals(true,result.isRight());
423         }
424
425         @Test
426         public void testdeleteAttribute_nonexistingresource() throws Exception {
427                 Either<PropertyDefinition, ResponseFormat> result;
428
429
430                 Component resource= new Resource();
431                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
432                 resource.setIsDeleted(false);
433                 resource.setLastUpdaterUserId("USR01");
434
435                 when(igraphLockOperation.lockComponent(any(),any())).thenReturn(StorageOperationStatus.OK);
436
437                 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
438                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
439
440                 result=attributeBusinessLogic.deleteAttribute("RES01","ATTR01", "USR01");
441                 Assert.assertEquals(true,result.isRight());
442         }
443
444         @Test
445         public void testdeleteAttribute_success() throws Exception {
446                 Either<PropertyDefinition, ResponseFormat> result;
447
448
449                 Component resource= new Resource();
450                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
451                 resource.setIsDeleted(false);
452                 resource.setLastUpdaterUserId("USR01");
453
454
455                 when(igraphLockOperation.lockComponent(any(),any())).thenReturn(StorageOperationStatus.OK);
456
457                 Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
458                 when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
459                 when(toscaOperationFacade.deleteAttributeOfResource(any(),any())).thenReturn(StorageOperationStatus.OK);
460
461                 PropertyDefinition prop= new PropertyDefinition();
462                 prop.setUniqueId("ATTR01");
463                 prop.setParentUniqueId("RES01");
464                 List<PropertyDefinition> attributes = new ArrayList<>();
465                 attributes.add(prop);
466                 ((Resource) resource).setAttributes(attributes);
467
468                 result=attributeBusinessLogic.deleteAttribute("RES01","ATTR01", "USR01");
469                 Assert.assertEquals(true,result.isLeft());
470         }
471
472
473 }