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.togglz.core.Feature;
33 import org.togglz.core.repository.FeatureState;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertNull;
41 import static org.junit.Assert.assertTrue;
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.ArgumentMatchers.contains;
44 import static org.mockito.Mockito.times;
45 import static org.mockito.Mockito.verify;
46 import static org.mockito.Mockito.when;
48 @RunWith(MockitoJUnitRunner.class)
49 public class CassandraCustomStateRepositoryTest {
50 private final String strategyId = "strategyId";
51 private final String paramName1 = "paramName1";
52 private final String paramName2 = "paramName2";
53 private final String paramVal1 = "paramVal1";
54 private final String paramVal2 = "paramVal2";
57 private FeatureToggleDao featureToggleDao;
60 private CassandraCustomStateRepository cassandraRepo;
64 cassandraRepo = new CassandraCustomStateRepository(featureToggleDao);
68 public void getFeatureStateSuccess() {
69 FeatureState stateToReturn = new FeatureState(ToggleableFeature.DEFAULT_FEATURE, true);
70 when(featureToggleDao.get(any())).thenReturn(new FeatureToggleEvent(stateToReturn));
71 FeatureState state = cassandraRepo.getFeatureState(ToggleableFeature.DEFAULT_FEATURE);
72 assertEquals(state.getFeature().name(), stateToReturn.getFeature().name());
73 assertTrue(state.isEnabled());
74 assertNull(state.getStrategyId());
75 assertEquals(0, state.getParameterMap().size());
79 public void getFeatureStateWithParamsSuccess() {
80 when(featureToggleDao.get(any())).thenReturn(createEvent(ToggleableFeature.DEFAULT_FEATURE));
81 FeatureState state = cassandraRepo.getFeatureState(ToggleableFeature.DEFAULT_FEATURE);
82 assertEquals(state.getFeature().name(), ToggleableFeature.DEFAULT_FEATURE.name());
83 assertEquals(strategyId, state.getStrategyId());
84 assertEquals(paramVal1, state.getParameter(paramName1));
85 assertEquals(paramVal2, state.getParameter(paramName2));
86 assertTrue(state.isEnabled());
89 @Test(expected = IllegalArgumentException.class)
90 public void getFeatureStateForFeatureNull() {
91 cassandraRepo.getFeatureState(null);
95 public void getFeatureStateWhenEntryNotFound() {
96 when(featureToggleDao.get(any())).thenReturn(null);
97 cassandraRepo.getFeatureState(ToggleableFeature.DEFAULT_FEATURE);
101 public void setFeatureStateSuccess() {
102 when(featureToggleDao.save(any())).thenReturn(CassandraOperationStatus.OK);
103 cassandraRepo.setFeatureState(new FeatureState(ToggleableFeature.DEFAULT_FEATURE));
106 @Test(expected = IllegalArgumentException.class)
107 public void setFeatureStateWhenStateIsNull() {
108 cassandraRepo.setFeatureState(null);
112 public void removeUnusedItems() {
113 List<FeatureToggleEvent> allEvents = Arrays.asList(
114 createEvent(ToggleableFeature.DEFAULT_FEATURE),
115 createEvent(() -> "should be deleted1"),
116 createEvent(() -> "should be deleted2"));
118 when(featureToggleDao.getAllFeatures()).thenReturn(allEvents);
119 cassandraRepo.removeUnusedItems();
120 verify(featureToggleDao, times(2)).delete(contains("should be deleted"));
124 public void removeUnusedItemsWhenNoStatesStored() {
125 when(featureToggleDao.getAllFeatures()).thenReturn(Collections.emptyList());
126 cassandraRepo.removeUnusedItems();
127 verify(featureToggleDao, times(0)).delete(any());
131 public void removeUnusedItemsWhenOnlyExistingStatesStored() {
132 when(featureToggleDao.getAllFeatures()).thenReturn(Collections.singletonList(createEvent(ToggleableFeature.DEFAULT_FEATURE)));
133 cassandraRepo.removeUnusedItems();
134 verify(featureToggleDao, times(0)).delete(any());
137 private FeatureToggleEvent createEvent(Feature feature) {
138 FeatureState stateToReturn = new FeatureState(feature, true);
139 stateToReturn.setStrategyId(strategyId);
140 stateToReturn.setParameter(paramName1, paramVal1);
141 stateToReturn.setParameter(paramName2, paramVal2);
142 return new FeatureToggleEvent(stateToReturn);