2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.cache;
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;
50 import java.util.function.Function;
52 public class ComponentCacheTest extends ModelConfDependentTest {
55 ComponentCache testSubject;
58 ComponentCassandraDao componentCassandraDao;
61 ToscaOperationFacade toscaOperationFacade;
64 public void setUpMocks() throws Exception {
65 MockitoAnnotations.initMocks(this);
69 public void testInit() throws Exception {
75 public void testIsEnabled() throws Exception {
81 result = testSubject.isEnabled();
85 public void testSetEnabled() throws Exception {
87 boolean enabled = false;
91 testSubject.setEnabled(enabled);
95 public void testGetComponentNotFound() throws Exception {
97 String componentUid = "mock";
98 Long lastModificationTime = null;
99 Function<Component, Component> filterFieldsFunc = null;
100 Either<Component, ActionStatus> result;
102 Mockito.when(componentCassandraDao.getComponent("mock"))
103 .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
105 result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
109 public void testGetComponentInvalidDate() throws Exception {
111 String componentUid = "mock";
112 Long lastModificationTime = 0L;
113 Function<Component, Component> filterFieldsFunc = null;
114 Either<Component, ActionStatus> result;
116 ComponentCacheData a = new ComponentCacheData();
117 a.setModificationTime(new Date());
118 Mockito.when(componentCassandraDao.getComponent("mock")).thenReturn(Either.left(a));
120 result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
124 public void testGetComponentDeserializeError() throws Exception {
126 String componentUid = "mock";
127 Long lastModificationTime = 0L;
128 Function<Component, Component> filterFieldsFunc = null;
129 Either<Component, ActionStatus> result;
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));
136 result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
140 public void testGetAllComponentIdTimeAndType() throws Exception {
142 Either<List<ComponentCacheData>, ActionStatus> result;
146 result = testSubject.getAllComponentIdTimeAndType();
147 testSubject.setEnabled(false);
148 result = testSubject.getAllComponentIdTimeAndType();
152 public void testUpdateCatalogInMemoryCacheWithCertified() throws Exception {
154 List<Component> foundComponents = new LinkedList<>();
158 Deencapsulation.invoke(testSubject, "updateCatalogInMemoryCacheWithCertified", foundComponents,
159 ComponentTypeEnum.RESOURCE);
163 public void testGetDataFromInMemoryCache() throws Exception {
165 Set<String> components = new HashSet<>();
166 components.add("mock");
167 ComponentTypeEnum componentTypeEnum = null;
168 List<Component> result;
172 result = Deencapsulation.invoke(testSubject, "getDataFromInMemoryCache", components,
173 ComponentTypeEnum.RESOURCE);
177 public void testGetComponents() throws Exception {
179 Set<String> components = new HashSet<>();
180 Function<List<Component>, List<Component>> filterFieldsFunc = new Function<List<Component>, List<Component>>() {
183 public List<Component> apply(List<Component> t) {
187 Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
189 List<ComponentCacheData> list = new LinkedList<>();
190 Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(list));
194 result = testSubject.getComponents(components, filterFieldsFunc);
198 public void testGetComponentsNotAllowed() throws Exception {
200 Set<String> components = new HashSet<>();
201 Function<List<Component>, List<Component>> filterFieldsFunc = null;
203 Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
206 testSubject.setEnabled(false);
207 result = testSubject.getComponents(components, filterFieldsFunc);
211 public void testGetComponentsCassndraError() throws Exception {
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;
217 Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class)))
218 .thenReturn(Either.right(ActionStatus.GENERAL_ERROR));
222 result = testSubject.getComponents(components, filterFieldsFunc);
226 public void testGetComponentsForLeftPanel() throws Exception {
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;
233 List<ComponentCacheData> list = new LinkedList<>();
234 Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(list));
237 result = testSubject.getComponentsForLeftPanel(ComponentTypeEnum.RESOURCE, internalComponentType,
242 public void testFilterForLeftPanel() throws Exception {
244 List<Component> components = new LinkedList<>();
245 List<Component> result;
249 result = Deencapsulation.invoke(testSubject, "filterForLeftPanel", components);
250 Assert.assertNotEquals(null, result);
254 public void testFilterForCatalog() throws Exception {
256 List<Component> components = new LinkedList<>();
257 List<Component> result;
260 result = Deencapsulation.invoke(testSubject, "filterForCatalog", components);
261 Assert.assertNotEquals(null, result);
265 public void testFilterFieldsForLeftPanel() throws Exception {
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);
278 public void testFilterFieldsForCatalog() throws Exception {
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);
295 public void testCopyFieldsForLeftPanel() throws Exception {
297 Component component = new Resource();
298 Component filteredComponent = new Resource();
299 ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition())
300 .setResourceType(ResourceTypeEnum.VL);
303 Deencapsulation.invoke(testSubject, "copyFieldsForLeftPanel", component, filteredComponent);
307 public void testGetComponentsFullDisabled() throws Exception {
309 Set<String> filteredResources = null;
310 Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
313 testSubject.setEnabled(false);
314 result = Deencapsulation.invoke(testSubject, "getComponentsFull", Set.class);
319 public void testGetComponentsFullDesirializeError() throws Exception {
321 Set<String> filteredResources = new HashSet<>();
322 filteredResources.add("mock");
323 Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result;
325 List<ComponentCacheData> a = new LinkedList<>();
326 ComponentCacheData e = new ComponentCacheData();
328 e.setType(NodeTypeEnum.Resource.getName());
330 Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(a));
334 result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
339 public void testGetComponent_1() throws Exception {
341 String componentUid = "mock";
342 Either<Component, ActionStatus> result;
344 Mockito.when(componentCassandraDao.getComponent("mock"))
345 .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
348 result = testSubject.getComponent(componentUid);
352 public void testGetComponent_2() throws Exception {
354 String componentUid = "mock";
355 Long lastModificationTime = null;
356 Either<Component, ActionStatus> result;
358 Mockito.when(componentCassandraDao.getComponent("mock"))
359 .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
362 Function<Component, Component> filterFieldsFunc = new Function<Component, Component>() {
364 public Component apply(Component component) {
365 return new Resource();
368 result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc);
372 public void testSaveComponent() throws Exception {
374 String componentUid = "";
375 Component component = new Resource();
379 Mockito.when(componentCassandraDao.saveComponent(Mockito.any(ComponentCacheData.class)))
380 .thenReturn(CassandraOperationStatus.OK);
382 result = Deencapsulation.invoke(testSubject, "saveComponent", componentUid, 0L, NodeTypeEnum.Resource,
387 public void testSetComponent_1Disabled() throws Exception {
389 Component component = new Resource();
390 component.setLastUpdateDate(0L);
394 testSubject.setEnabled(false);
395 result = testSubject.setComponent(component, NodeTypeEnum.Resource);
399 public void testSetComponent_1() throws Exception {
401 Component component = new Resource();
402 component.setLastUpdateDate(0L);
407 result = testSubject.setComponent(component, NodeTypeEnum.Resource);
412 public void testGetComponentsFull_1CannotDeserialize() throws Exception {
413 Map<String, Long> filteredResources = new HashMap<>();
414 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
417 LinkedList<ComponentCacheData> left = new LinkedList<>();
418 ComponentCacheData e = new ComponentCacheData();
419 e.setType(NodeTypeEnum.Resource.getName());
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));
424 result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
428 public void testGetComponentsFull_1Disabled() throws Exception {
429 Map<String, Long> filteredResources = new HashMap<>();
430 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
433 testSubject.setEnabled(false);
434 result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
438 public void testGetComponentsFull_1NotFound() throws Exception {
439 Map<String, Long> filteredResources = new HashMap<>();
440 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
443 Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND));
445 result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources);
449 public void testGetComponentsForCatalog_1Disabled() throws Exception {
451 Map<String, Long> components = null;
452 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
455 testSubject.setEnabled(false);
456 result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE);
460 public void testGetComponentsForCatalog_1() throws Exception {
461 Map<String, Long> components = new HashMap<>();
462 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
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));
468 result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE);
472 public void testGetComponentsForCatalog_1Error() throws Exception {
473 Map<String, Long> components = new HashMap<>();
474 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
477 Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.right(ActionStatus.COMPONENT_NOT_FOUND));
479 result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE);
483 public void testGetComponents_1Disabled() throws Exception {
485 Map<String, Long> components = null;
486 Function<List<Component>, List<Component>> filterFieldsFunc = null;
487 Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result;
490 testSubject.setEnabled(false);
491 result = testSubject.getComponents(components, filterFieldsFunc);
495 public void testGetComponentAndTimeNotFound() throws Exception {
497 String componentUid = "";
498 Function<Component, Component> filterFieldsFunc = null;
499 Either<ImmutablePair<Component, Long>, ActionStatus> result;
502 Mockito.when(componentCassandraDao.getComponent(Mockito.anyString())).thenReturn(Either.right(ActionStatus.API_RESOURCE_NOT_FOUND));
504 result = testSubject.getComponentAndTime(componentUid, filterFieldsFunc);
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;
515 lastModificationTime = null;
516 testSubject.setEnabled(false);
517 result = Deencapsulation.invoke(testSubject, "getComponentFromCache",
518 new Object[]{componentUid, Long.class, Function.class});
522 public void testDeleteComponentFromCacheFails() throws Exception {
529 result = testSubject.deleteComponentFromCache(id);
533 public void testDeleteComponentFromCacheDisabled() throws Exception {
539 testSubject.setEnabled(false);
540 result = testSubject.deleteComponentFromCache(id);
544 public void testDeleteComponentFromCache() throws Exception {
550 Mockito.when(componentCassandraDao.deleteComponent(Mockito.anyString())).thenReturn(CassandraOperationStatus.OK);
551 result = testSubject.deleteComponentFromCache(id);