Added oparent to sdc main
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / cache / ComponentCacheTest.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.model.cache;
22
23 import fj.data.Either;
24 import mockit.Deencapsulation;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.apache.commons.lang3.tuple.ImmutableTriple;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
36 import org.openecomp.sdc.be.dao.cassandra.ComponentCassandraDao;
37 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
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.model.Component;
42 import org.openecomp.sdc.be.model.Product;
43 import org.openecomp.sdc.be.model.Resource;
44 import org.openecomp.sdc.be.model.Service;
45 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
46 import org.openecomp.sdc.be.resources.data.ComponentCacheData;
47 import org.openecomp.sdc.be.unittests.utils.ModelConfDependentTest;
48
49 import java.util.*;
50 import java.util.function.Function;
51
52 public class ComponentCacheTest extends ModelConfDependentTest {
53
54     @InjectMocks
55     ComponentCache testSubject;
56
57     @Mock
58     ComponentCassandraDao componentCassandraDao;
59
60     @Mock
61     ToscaOperationFacade toscaOperationFacade;
62
63     @Before
64     public void setUpMocks() throws Exception {
65         MockitoAnnotations.initMocks(this);
66     }
67
68     @Test
69     public void testInit() throws Exception {
70         // default test
71         testSubject.init();
72     }
73
74     @Test
75     public void testIsEnabled() throws Exception {
76
77         boolean result;
78
79         // default test
80
81         result = testSubject.isEnabled();
82     }
83
84     @Test
85     public void testSetEnabled() throws Exception {
86
87         boolean enabled = false;
88
89         // default test
90
91         testSubject.setEnabled(enabled);
92     }
93
94     @Test
95     public void testGetComponentNotFound() throws Exception {
96
97         String componentUid = "mock";
98         Long lastModificationTime = null;
99         Function<Component, Component> filterFieldsFunc = null;
100         Either<Component, ActionStatus> result;
101
102         Mockito.when(componentCassandraDao.getComponent("mock"))
103                 .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
104         // default test
105         result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
106     }
107
108     @Test
109     public void testGetComponentInvalidDate() throws Exception {
110
111         String componentUid = "mock";
112         Long lastModificationTime = 0L;
113         Function<Component, Component> filterFieldsFunc = null;
114         Either<Component, ActionStatus> result;
115
116         ComponentCacheData a = new ComponentCacheData();
117         a.setModificationTime(new Date());
118         Mockito.when(componentCassandraDao.getComponent("mock")).thenReturn(Either.left(a));
119         // default test
120         result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
121     }
122
123     @Test
124     public void testGetComponentDeserializeError() throws Exception {
125
126         String componentUid = "mock";
127         Long lastModificationTime = 0L;
128         Function<Component, Component> filterFieldsFunc = null;
129         Either<Component, ActionStatus> result;
130
131         ComponentCacheData a = new ComponentCacheData();
132         a.setModificationTime(new Date(0L));
133         a.setType(NodeTypeEnum.Resource.getName());
134         Mockito.when(componentCassandraDao.getComponent("mock")).thenReturn(Either.left(a));
135         // default test
136         result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
137     }
138
139     @Test
140     public void testGetAllComponentIdTimeAndType() throws Exception {
141
142         Either<List<ComponentCacheData>, ActionStatus> result;
143
144         // default test
145
146         result = testSubject.getAllComponentIdTimeAndType();
147         testSubject.setEnabled(false);
148         result = testSubject.getAllComponentIdTimeAndType();
149     }
150
151     @Test
152     public void testUpdateCatalogInMemoryCacheWithCertified() throws Exception {
153
154         List<Component> foundComponents = new LinkedList<>();
155
156         // default test
157         testSubject.init();
158         Deencapsulation.invoke(testSubject, "updateCatalogInMemoryCacheWithCertified", foundComponents,
159                 ComponentTypeEnum.RESOURCE);
160     }
161
162     @Test
163     public void testGetDataFromInMemoryCache() throws Exception {
164
165         Set<String> components = new HashSet<>();
166         components.add("mock");
167         ComponentTypeEnum componentTypeEnum = null;
168         List<Component> result;
169
170         // default test
171         testSubject.init();
172         result = Deencapsulation.invoke(testSubject, "getDataFromInMemoryCache", components,
173                 ComponentTypeEnum.RESOURCE);
174     }
175
176     @Test
177     public void testGetComponents() throws Exception {
178
179         Set<String> components = new HashSet<>();
180         Function<List<Component>, List<Component>> filterFieldsFunc = new Function<List<Component>, List<Component>>() {
181
182             @Override
183             public List<Component> apply(List<Component> t) {
184                 return t;
185             }
186         };
187         Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
188
189         List<ComponentCacheData> list = new LinkedList<>();
190         Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(list));
191
192         // default test
193         testSubject.init();
194         result = testSubject.getComponents(components, filterFieldsFunc);
195     }
196
197     @Test
198     public void testGetComponentsNotAllowed() throws Exception {
199
200         Set<String> components = new HashSet<>();
201         Function<List<Component>, List<Component>> filterFieldsFunc = null;
202
203         Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
204
205         // default test
206         testSubject.setEnabled(false);
207         result = testSubject.getComponents(components, filterFieldsFunc);
208     }
209
210     @Test
211     public void testGetComponentsCassndraError() throws Exception {
212
213         Set<String> components = new HashSet<>();
214         Function<List<Component>, List<Component>> filterFieldsFunc = null;
215         Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
216
217         Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class)))
218                 .thenReturn(Either.right(ActionStatus.GENERAL_ERROR));
219
220         // default test
221         testSubject.init();
222         result = testSubject.getComponents(components, filterFieldsFunc);
223     }
224
225     @Test
226     public void testGetComponentsForLeftPanel() throws Exception {
227
228         ComponentTypeEnum componentTypeEnum = null;
229         String internalComponentType = "mock";
230         Set<String> filteredResources = new HashSet<>();
231         Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
232
233         List<ComponentCacheData> list = new LinkedList<>();
234         Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(list));
235
236         // default test
237         result = testSubject.getComponentsForLeftPanel(ComponentTypeEnum.RESOURCE, internalComponentType,
238                 filteredResources);
239     }
240
241     @Test
242     public void testFilterForLeftPanel() throws Exception {
243
244         List<Component> components = new LinkedList<>();
245         List<Component> result;
246
247         // test 1
248
249         result = Deencapsulation.invoke(testSubject, "filterForLeftPanel", components);
250         Assert.assertNotEquals(null, result);
251     }
252
253     @Test
254     public void testFilterForCatalog() throws Exception {
255
256         List<Component> components = new LinkedList<>();
257         List<Component> result;
258
259         // test 1
260         result = Deencapsulation.invoke(testSubject, "filterForCatalog", components);
261         Assert.assertNotEquals(null, result);
262     }
263
264     @Test
265     public void testFilterFieldsForLeftPanel() throws Exception {
266         Component result;
267
268         // default test
269         Resource resource = new Resource();
270         resource.setComponentType(ComponentTypeEnum.RESOURCE);
271         result = Deencapsulation.invoke(testSubject, "filterFieldsForLeftPanel", resource);
272         Service service = new Service();
273         service.setComponentType(ComponentTypeEnum.SERVICE);
274         result = Deencapsulation.invoke(testSubject, "filterFieldsForLeftPanel", service);
275     }
276
277     @Test
278     public void testFilterFieldsForCatalog() throws Exception {
279         Component result;
280
281         // default test
282
283         Resource resource = new Resource();
284         resource.setComponentType(ComponentTypeEnum.RESOURCE);
285         result = Deencapsulation.invoke(testSubject, "filterFieldsForCatalog", resource);
286         Service service = new Service();
287         service.setComponentType(ComponentTypeEnum.SERVICE);
288         result = Deencapsulation.invoke(testSubject, "filterFieldsForCatalog", service);
289         Product product = new Product();
290         product.setComponentType(ComponentTypeEnum.PRODUCT);
291         result = Deencapsulation.invoke(testSubject, "filterFieldsForCatalog", product);
292     }
293
294     @Test
295     public void testCopyFieldsForLeftPanel() throws Exception {
296
297         Component component = new Resource();
298         Component filteredComponent = new Resource();
299         ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition())
300                 .setResourceType(ResourceTypeEnum.VL);
301         // default test
302
303         Deencapsulation.invoke(testSubject, "copyFieldsForLeftPanel", component, filteredComponent);
304     }
305
306     @Test
307     public void testGetComponentsFullDisabled() throws Exception {
308
309         Set<String> filteredResources = null;
310         Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
311
312         // default test
313         testSubject.setEnabled(false);
314         result = Deencapsulation.invoke(testSubject, "getComponentsFull", Set.class);
315     }
316
317
318     @Test
319     public void testGetComponentsFullDesirializeError() throws Exception {
320
321         Set<String> filteredResources = new HashSet<>();
322         filteredResources.add("mock");
323         Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
324
325         List<ComponentCacheData> a = new LinkedList<>();
326         ComponentCacheData e = new ComponentCacheData();
327         e.setId("mock");
328         e.setType(NodeTypeEnum.Resource.getName());
329         a.add(e);
330         Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(a));
331
332         // default test
333
334         result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
335     }
336
337
338     @Test
339     public void testGetComponent_1() throws Exception {
340
341         String componentUid = "mock";
342         Either<Component, ActionStatus> result;
343
344         Mockito.when(componentCassandraDao.getComponent("mock"))
345                 .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
346
347         // default test
348         result = testSubject.getComponent(componentUid);
349     }
350
351     @Test
352     public void testGetComponent_2() throws Exception {
353
354         String componentUid = "mock";
355         Long lastModificationTime = null;
356         Either<Component, ActionStatus> result;
357
358         Mockito.when(componentCassandraDao.getComponent("mock"))
359                 .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
360
361         // default test
362         Function<Component, Component> filterFieldsFunc = new Function<Component, Component>() {
363             @Override
364             public Component apply(Component component) {
365                 return new Resource();
366             }
367         };
368         result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
369     }
370
371     @Test
372     public void testSaveComponent() throws Exception {
373
374         String componentUid = "";
375         Component component = new Resource();
376         boolean result;
377
378         // default test
379         Mockito.when(componentCassandraDao.saveComponent(Mockito.any(ComponentCacheData.class)))
380                 .thenReturn(CassandraOperationStatus.OK);
381
382         result = Deencapsulation.invoke(testSubject, "saveComponent", componentUid, 0L, NodeTypeEnum.Resource,
383                 component);
384     }
385
386     @Test
387     public void testSetComponent_1Disabled() throws Exception {
388
389         Component component = new Resource();
390         component.setLastUpdateDate(0L);
391         boolean result;
392
393         // default test
394         testSubject.setEnabled(false);
395         result = testSubject.setComponent(component, NodeTypeEnum.Resource);
396     }
397
398     @Test
399     public void testSetComponent_1() throws Exception {
400
401         Component component = new Resource();
402         component.setLastUpdateDate(0L);
403         boolean result;
404
405         // default test
406
407         result = testSubject.setComponent(component, NodeTypeEnum.Resource);
408     }
409
410
411     @Test
412     public void testGetComponentsFull_1CannotDeserialize() throws Exception {
413         Map<String, Long> filteredResources = new HashMap<>();
414         Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
415
416         // default test
417         LinkedList<ComponentCacheData> left = new LinkedList<>();
418         ComponentCacheData e = new ComponentCacheData();
419         e.setType(NodeTypeEnum.Resource.getName());
420         left.add(e);
421         ImmutablePair<List<ComponentCacheData>, Set<String>> immutablePair = ImmutablePair.of(left, new HashSet<>());
422         Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.left(immutablePair));
423
424         result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
425     }
426
427     @Test
428     public void testGetComponentsFull_1Disabled() throws Exception {
429         Map<String, Long> filteredResources = new HashMap<>();
430         Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
431
432         // default test
433         testSubject.setEnabled(false);
434         result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
435     }
436
437     @Test
438     public void testGetComponentsFull_1NotFound() throws Exception {
439         Map<String, Long> filteredResources = new HashMap<>();
440         Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
441
442         // default test
443         Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
444
445         result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
446     }
447
448     @Test
449     public void testGetComponentsForCatalog_1Disabled() throws Exception {
450
451         Map<String, Long> components = null;
452         Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
453
454         // default test
455         testSubject.setEnabled(false);
456         result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE);
457     }
458
459     @Test
460     public void testGetComponentsForCatalog_1() throws Exception {
461         Map<String, Long> components = new HashMap<>();
462         Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
463
464         // default test
465         ImmutablePair<List<ComponentCacheData>, Set<String>> value = ImmutablePair.of(new LinkedList<>(), new HashSet<>());
466         Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.left(value));
467         testSubject.init();
468         result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE);
469     }
470
471     @Test
472     public void testGetComponentsForCatalog_1Error() throws Exception {
473         Map<String, Long> components = new HashMap<>();
474         Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
475
476         // default test
477         Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.right(ActionStatus.COMPONENT_NOT_FOUND));
478
479         result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE);
480     }
481
482     @Test
483     public void testGetComponents_1Disabled() throws Exception {
484
485         Map<String, Long> components = null;
486         Function<List<Component>, List<Component>> filterFieldsFunc = null;
487         Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
488
489         // default test
490         testSubject.setEnabled(false);
491         result = testSubject.getComponents(components, filterFieldsFunc);
492     }
493
494     @Test
495     public void testGetComponentAndTimeNotFound() throws Exception {
496
497         String componentUid = "";
498         Function<Component, Component> filterFieldsFunc = null;
499         Either<ImmutablePair<Component, Long>, ActionStatus> result;
500
501         // default test
502         Mockito.when(componentCassandraDao.getComponent(Mockito.anyString())).thenReturn(Either.right(ActionStatus.API_RESOURCE_NOT_FOUND));
503
504         result = testSubject.getComponentAndTime(componentUid, filterFieldsFunc);
505     }
506
507     @Test
508     public void testGetComponentFromCacheDisabled() throws Exception {
509         String componentUid = "";
510         Long lastModificationTime = null;
511         Function<Component, Component> filterFieldsFunc = null;
512         Either<ImmutablePair<Component, ComponentCacheData>, ActionStatus> result;
513
514         // test 1
515         lastModificationTime = null;
516         testSubject.setEnabled(false);
517         result = Deencapsulation.invoke(testSubject, "getComponentFromCache",
518                 new Object[]{componentUid, Long.class, Function.class});
519     }
520
521     @Test
522     public void testDeleteComponentFromCacheFails() throws Exception {
523
524         String id = "";
525         ActionStatus result;
526
527         // default test
528
529         result = testSubject.deleteComponentFromCache(id);
530     }
531
532     @Test
533     public void testDeleteComponentFromCacheDisabled() throws Exception {
534
535         String id = "";
536         ActionStatus result;
537
538         // default test
539         testSubject.setEnabled(false);
540         result = testSubject.deleteComponentFromCache(id);
541     }
542
543     @Test
544     public void testDeleteComponentFromCache() throws Exception {
545
546         String id = "";
547         ActionStatus result;
548
549         // default test
550         Mockito.when(componentCassandraDao.deleteComponent(Mockito.anyString())).thenReturn(CassandraOperationStatus.OK);
551         result = testSubject.deleteComponentFromCache(id);
552     }
553 }