1 package org.openecomp.sdc.be.model.cache;
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;
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;
30 import java.util.function.Function;
32 public class ComponentCacheTest extends ModelConfDependentTest {
35 ComponentCache testSubject;
38 ComponentCassandraDao componentCassandraDao;
41 ToscaOperationFacade toscaOperationFacade;
44 public void setUpMocks() throws Exception {
45 MockitoAnnotations.initMocks(this);
49 public void testInit() throws Exception {
55 public void testIsEnabled() throws Exception {
61 result = testSubject.isEnabled();
65 public void testSetEnabled() throws Exception {
67 boolean enabled = false;
71 testSubject.setEnabled(enabled);
75 public void testGetComponentNotFound() throws Exception {
77 String componentUid = "mock";
78 Long lastModificationTime = null;
79 Function<Component, Component> filterFieldsFunc = null;
80 Either<Component, ActionStatus> result;
82 Mockito.when(componentCassandraDao.getComponent("mock"))
83 .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
85 result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
89 public void testGetComponentInvalidDate() throws Exception {
91 String componentUid = "mock";
92 Long lastModificationTime = 0L;
93 Function<Component, Component> filterFieldsFunc = null;
94 Either<Component, ActionStatus> result;
96 ComponentCacheData a = new ComponentCacheData();
97 a.setModificationTime(new Date());
98 Mockito.when(componentCassandraDao.getComponent("mock")).thenReturn(Either.left(a));
100 result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
104 public void testGetComponentDeserializeError() throws Exception {
106 String componentUid = "mock";
107 Long lastModificationTime = 0L;
108 Function<Component, Component> filterFieldsFunc = null;
109 Either<Component, ActionStatus> result;
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));
116 result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
120 public void testGetAllComponentIdTimeAndType() throws Exception {
122 Either<List<ComponentCacheData>, ActionStatus> result;
126 result = testSubject.getAllComponentIdTimeAndType();
127 testSubject.setEnabled(false);
128 result = testSubject.getAllComponentIdTimeAndType();
132 public void testUpdateCatalogInMemoryCacheWithCertified() throws Exception {
134 List<Component> foundComponents = new LinkedList<>();
138 Deencapsulation.invoke(testSubject, "updateCatalogInMemoryCacheWithCertified", foundComponents,
139 ComponentTypeEnum.RESOURCE);
143 public void testGetDataFromInMemoryCache() throws Exception {
145 Set<String> components = new HashSet<>();
146 components.add("mock");
147 ComponentTypeEnum componentTypeEnum = null;
148 List<Component> result;
152 result = Deencapsulation.invoke(testSubject, "getDataFromInMemoryCache", components,
153 ComponentTypeEnum.RESOURCE);
157 public void testGetComponents() throws Exception {
159 Set<String> components = new HashSet<>();
160 Function<List<Component>, List<Component>> filterFieldsFunc = new Function<List<Component>, List<Component>>() {
163 public List<Component> apply(List<Component> t) {
167 Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
169 List<ComponentCacheData> list = new LinkedList<>();
170 Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(list));
174 result = testSubject.getComponents(components, filterFieldsFunc);
178 public void testGetComponentsNotAllowed() throws Exception {
180 Set<String> components = new HashSet<>();
181 Function<List<Component>, List<Component>> filterFieldsFunc = null;
183 Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
186 testSubject.setEnabled(false);
187 result = testSubject.getComponents(components, filterFieldsFunc);
191 public void testGetComponentsCassndraError() throws Exception {
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;
197 Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class)))
198 .thenReturn(Either.right(ActionStatus.GENERAL_ERROR));
202 result = testSubject.getComponents(components, filterFieldsFunc);
206 public void testGetComponentsForLeftPanel() throws Exception {
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;
213 List<ComponentCacheData> list = new LinkedList<>();
214 Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(list));
217 result = testSubject.getComponentsForLeftPanel(ComponentTypeEnum.RESOURCE, internalComponentType,
222 public void testFilterForLeftPanel() throws Exception {
224 List<Component> components = new LinkedList<>();
225 List<Component> result;
229 result = Deencapsulation.invoke(testSubject, "filterForLeftPanel", components);
230 Assert.assertNotEquals(null, result);
234 public void testFilterForCatalog() throws Exception {
236 List<Component> components = new LinkedList<>();
237 List<Component> result;
240 result = Deencapsulation.invoke(testSubject, "filterForCatalog", components);
241 Assert.assertNotEquals(null, result);
245 public void testFilterFieldsForLeftPanel() throws Exception {
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);
258 public void testFilterFieldsForCatalog() throws Exception {
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);
275 public void testCopyFieldsForLeftPanel() throws Exception {
277 Component component = new Resource();
278 Component filteredComponent = new Resource();
279 ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition())
280 .setResourceType(ResourceTypeEnum.VL);
283 Deencapsulation.invoke(testSubject, "copyFieldsForLeftPanel", component, filteredComponent);
287 public void testGetComponentsFullDisabled() throws Exception {
289 Set<String> filteredResources = null;
290 Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
293 testSubject.setEnabled(false);
294 result = Deencapsulation.invoke(testSubject, "getComponentsFull", Set.class);
299 public void testGetComponentsFullDesirializeError() throws Exception {
301 Set<String> filteredResources = new HashSet<>();
302 filteredResources.add("mock");
303 Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
305 List<ComponentCacheData> a = new LinkedList<>();
306 ComponentCacheData e = new ComponentCacheData();
308 e.setType(NodeTypeEnum.Resource.getName());
310 Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(a));
314 result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
319 public void testGetComponent_1() throws Exception {
321 String componentUid = "mock";
322 Either<Component, ActionStatus> result;
324 Mockito.when(componentCassandraDao.getComponent("mock"))
325 .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
328 result = testSubject.getComponent(componentUid);
332 public void testGetComponent_2() throws Exception {
334 String componentUid = "mock";
335 Long lastModificationTime = null;
336 Either<Component, ActionStatus> result;
338 Mockito.when(componentCassandraDao.getComponent("mock"))
339 .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
342 Function<Component, Component> filterFieldsFunc = new Function<Component, Component>() {
344 public Component apply(Component component) {
345 return new Resource();
348 result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
352 public void testSaveComponent() throws Exception {
354 String componentUid = "";
355 Component component = new Resource();
359 Mockito.when(componentCassandraDao.saveComponent(Mockito.any(ComponentCacheData.class)))
360 .thenReturn(CassandraOperationStatus.OK);
362 result = Deencapsulation.invoke(testSubject, "saveComponent", componentUid, 0L, NodeTypeEnum.Resource,
367 public void testSetComponent_1Disabled() throws Exception {
369 Component component = new Resource();
370 component.setLastUpdateDate(0L);
374 testSubject.setEnabled(false);
375 result = testSubject.setComponent(component, NodeTypeEnum.Resource);
379 public void testSetComponent_1() throws Exception {
381 Component component = new Resource();
382 component.setLastUpdateDate(0L);
387 result = testSubject.setComponent(component, NodeTypeEnum.Resource);
392 public void testGetComponentsFull_1CannotDeserialize() throws Exception {
393 Map<String, Long> filteredResources = new HashMap<>();
394 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
397 LinkedList<ComponentCacheData> left = new LinkedList<>();
398 ComponentCacheData e = new ComponentCacheData();
399 e.setType(NodeTypeEnum.Resource.getName());
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));
404 result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
408 public void testGetComponentsFull_1Disabled() throws Exception {
409 Map<String, Long> filteredResources = new HashMap<>();
410 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
413 testSubject.setEnabled(false);
414 result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
418 public void testGetComponentsFull_1NotFound() throws Exception {
419 Map<String, Long> filteredResources = new HashMap<>();
420 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
423 Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
425 result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
429 public void testGetComponentsForCatalog_1Disabled() throws Exception {
431 Map<String, Long> components = null;
432 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
435 testSubject.setEnabled(false);
436 result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE);
440 public void testGetComponentsForCatalog_1() throws Exception {
441 Map<String, Long> components = new HashMap<>();
442 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
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));
448 result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE);
452 public void testGetComponentsForCatalog_1Error() throws Exception {
453 Map<String, Long> components = new HashMap<>();
454 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
457 Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.right(ActionStatus.COMPONENT_NOT_FOUND));
459 result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE);
463 public void testGetComponents_1Disabled() throws Exception {
465 Map<String, Long> components = null;
466 Function<List<Component>, List<Component>> filterFieldsFunc = null;
467 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
470 testSubject.setEnabled(false);
471 result = testSubject.getComponents(components, filterFieldsFunc);
475 public void testGetComponentAndTimeNotFound() throws Exception {
477 String componentUid = "";
478 Function<Component, Component> filterFieldsFunc = null;
479 Either<ImmutablePair<Component, Long>, ActionStatus> result;
482 Mockito.when(componentCassandraDao.getComponent(Mockito.anyString())).thenReturn(Either.right(ActionStatus.API_RESOURCE_NOT_FOUND));
484 result = testSubject.getComponentAndTime(componentUid, filterFieldsFunc);
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;
495 lastModificationTime = null;
496 testSubject.setEnabled(false);
497 result = Deencapsulation.invoke(testSubject, "getComponentFromCache",
498 new Object[]{componentUid, Long.class, Function.class});
502 public void testDeleteComponentFromCacheFails() throws Exception {
509 result = testSubject.deleteComponentFromCache(id);
513 public void testDeleteComponentFromCacheDisabled() throws Exception {
519 testSubject.setEnabled(false);
520 result = testSubject.deleteComponentFromCache(id);
524 public void testDeleteComponentFromCache() throws Exception {
530 Mockito.when(componentCassandraDao.deleteComponent(Mockito.anyString())).thenReturn(CassandraOperationStatus.OK);
531 result = testSubject.deleteComponentFromCache(id);