2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.togglz;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
30 import org.openecomp.sdc.be.dao.cassandra.FeatureToggleDao;
31 import org.openecomp.sdc.be.resources.data.togglz.FeatureToggleEvent;
32 import org.openecomp.sdc.be.resources.data.togglz.ToggleableFeature;
33 import org.togglz.core.Feature;
34 import org.togglz.core.repository.FeatureState;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNull;
42 import static org.junit.Assert.assertTrue;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.ArgumentMatchers.contains;
45 import static org.mockito.Mockito.times;
46 import static org.mockito.Mockito.verify;
47 import static org.mockito.Mockito.when;
49 @RunWith(MockitoJUnitRunner.class)
50 public class CassandraCustomStateRepositoryTest {
51 private final String strategyId = "strategyId";
52 private final String paramName1 = "paramName1";
53 private final String paramName2 = "paramName2";
54 private final String paramVal1 = "paramVal1";
55 private final String paramVal2 = "paramVal2";
58 private FeatureToggleDao featureToggleDao;
61 private CassandraCustomStateRepository cassandraRepo;
65 cassandraRepo = new CassandraCustomStateRepository(featureToggleDao);
69 public void getFeatureStateSuccess() {
70 FeatureState stateToReturn = new FeatureState(ToggleableFeature.DEFAULT_FEATURE, true);
71 when(featureToggleDao.get(any())).thenReturn(new FeatureToggleEvent(stateToReturn));
72 FeatureState state = cassandraRepo.getFeatureState(ToggleableFeature.DEFAULT_FEATURE);
73 assertEquals(state.getFeature().name(), stateToReturn.getFeature().name());
74 assertTrue(state.isEnabled());
75 assertNull(state.getStrategyId());
76 assertEquals(0, state.getParameterMap().size());
80 public void getFeatureStateWithParamsSuccess() {
81 when(featureToggleDao.get(any())).thenReturn(createEvent(ToggleableFeature.DEFAULT_FEATURE));
82 FeatureState state = cassandraRepo.getFeatureState(ToggleableFeature.DEFAULT_FEATURE);
83 assertEquals(state.getFeature().name(), ToggleableFeature.DEFAULT_FEATURE.name());
84 assertEquals(strategyId, state.getStrategyId());
85 assertEquals(paramVal1, state.getParameter(paramName1));
86 assertEquals(paramVal2, state.getParameter(paramName2));
87 assertTrue(state.isEnabled());
90 @Test(expected = IllegalArgumentException.class)
91 public void getFeatureStateForFeatureNull() {
92 cassandraRepo.getFeatureState(null);
96 public void getFeatureStateWhenEntryNotFound() {
97 when(featureToggleDao.get(any())).thenReturn(null);
98 cassandraRepo.getFeatureState(ToggleableFeature.DEFAULT_FEATURE);
102 public void setFeatureStateSuccess() {
103 when(featureToggleDao.save(any())).thenReturn(CassandraOperationStatus.OK);
104 cassandraRepo.setFeatureState(new FeatureState(ToggleableFeature.DEFAULT_FEATURE));
107 @Test(expected = IllegalArgumentException.class)
108 public void setFeatureStateWhenStateIsNull() {
109 cassandraRepo.setFeatureState(null);
113 public void removeUnusedItems() {
114 List<FeatureToggleEvent> allEvents = Arrays.asList(
115 createEvent(ToggleableFeature.DEFAULT_FEATURE),
116 createEvent(() -> "should be deleted1"),
117 createEvent(() -> "should be deleted2"));
119 when(featureToggleDao.getAllFeatures()).thenReturn(allEvents);
120 cassandraRepo.removeUnusedItems();
121 verify(featureToggleDao, times(2)).delete(contains("should be deleted"));
125 public void removeUnusedItemsWhenNoStatesStored() {
126 when(featureToggleDao.getAllFeatures()).thenReturn(Collections.emptyList());
127 cassandraRepo.removeUnusedItems();
128 verify(featureToggleDao, times(0)).delete(any());
132 public void removeUnusedItemsWhenOnlyExistingStatesStored() {
133 when(featureToggleDao.getAllFeatures()).thenReturn(Collections.singletonList(createEvent(ToggleableFeature.DEFAULT_FEATURE)));
134 cassandraRepo.removeUnusedItems();
135 verify(featureToggleDao, times(0)).delete(any());
138 private FeatureToggleEvent createEvent(Feature feature) {
139 FeatureState stateToReturn = new FeatureState(feature, true);
140 stateToReturn.setStrategyId(strategyId);
141 stateToReturn.setParameter(paramName1, paramVal1);
142 stateToReturn.setParameter(paramName2, paramVal2);
143 return new FeatureToggleEvent(stateToReturn);