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