Toggle
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / togglz / CassandraCustomStateRepositoryTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.togglz;
22
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;
34
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38
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;
47
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";
55
56     @Mock
57     private FeatureToggleDao featureToggleDao;
58
59     @InjectMocks
60     private CassandraCustomStateRepository cassandraRepo;
61
62     @Before
63     public void setUp() {
64         cassandraRepo = new CassandraCustomStateRepository(featureToggleDao);
65     }
66
67     @Test
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());
76     }
77
78     @Test
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());
87     }
88
89     @Test(expected = IllegalArgumentException.class)
90     public void getFeatureStateForFeatureNull() {
91         cassandraRepo.getFeatureState(null);
92     }
93
94     @Test
95     public void getFeatureStateWhenEntryNotFound() {
96         when(featureToggleDao.get(any())).thenReturn(null);
97         cassandraRepo.getFeatureState(ToggleableFeature.DEFAULT_FEATURE);
98     }
99
100     @Test
101     public void setFeatureStateSuccess() {
102         when(featureToggleDao.save(any())).thenReturn(CassandraOperationStatus.OK);
103         cassandraRepo.setFeatureState(new FeatureState(ToggleableFeature.DEFAULT_FEATURE));
104     }
105
106     @Test(expected = IllegalArgumentException.class)
107     public void setFeatureStateWhenStateIsNull() {
108         cassandraRepo.setFeatureState(null);
109     }
110
111     @Test
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"));
117
118         when(featureToggleDao.getAllFeatures()).thenReturn(allEvents);
119         cassandraRepo.removeUnusedItems();
120         verify(featureToggleDao, times(2)).delete(contains("should be deleted"));
121     }
122
123     @Test
124     public void removeUnusedItemsWhenNoStatesStored() {
125         when(featureToggleDao.getAllFeatures()).thenReturn(Collections.emptyList());
126         cassandraRepo.removeUnusedItems();
127         verify(featureToggleDao, times(0)).delete(any());
128     }
129
130     @Test
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());
135     }
136
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);
143     }
144 }