Catalog alignment
[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.openecomp.sdc.be.resources.data.togglz.ToggleableFeature;
33 import org.togglz.core.Feature;
34 import org.togglz.core.repository.FeatureState;
35
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
39
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;
48
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";
56
57     @Mock
58     private FeatureToggleDao featureToggleDao;
59
60     @InjectMocks
61     private CassandraCustomStateRepository cassandraRepo;
62
63     @Before
64     public void setUp() {
65         cassandraRepo = new CassandraCustomStateRepository(featureToggleDao);
66     }
67
68     @Test
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());
77     }
78
79     @Test
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());
88     }
89
90     @Test(expected = IllegalArgumentException.class)
91     public void getFeatureStateForFeatureNull() {
92         cassandraRepo.getFeatureState(null);
93     }
94
95     @Test
96     public void getFeatureStateWhenEntryNotFound() {
97         when(featureToggleDao.get(any())).thenReturn(null);
98         cassandraRepo.getFeatureState(ToggleableFeature.DEFAULT_FEATURE);
99     }
100
101     @Test
102     public void setFeatureStateSuccess() {
103         when(featureToggleDao.save(any())).thenReturn(CassandraOperationStatus.OK);
104         cassandraRepo.setFeatureState(new FeatureState(ToggleableFeature.DEFAULT_FEATURE));
105     }
106
107     @Test(expected = IllegalArgumentException.class)
108     public void setFeatureStateWhenStateIsNull() {
109         cassandraRepo.setFeatureState(null);
110     }
111
112     @Test
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"));
118
119         when(featureToggleDao.getAllFeatures()).thenReturn(allEvents);
120         cassandraRepo.removeUnusedItems();
121         verify(featureToggleDao, times(2)).delete(contains("should be deleted"));
122     }
123
124     @Test
125     public void removeUnusedItemsWhenNoStatesStored() {
126         when(featureToggleDao.getAllFeatures()).thenReturn(Collections.emptyList());
127         cassandraRepo.removeUnusedItems();
128         verify(featureToggleDao, times(0)).delete(any());
129     }
130
131     @Test
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());
136     }
137
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);
144     }
145 }