Merge "Bring some reliability in the eclipse and maven mixed builds"
[controller.git] / opendaylight / md-sal / clustered-data-store / implementation / src / test / java / org / opendaylight / controller / datastore / internal / ClusteredDataStoreImplTest.java
1 package org.opendaylight.controller.datastore.internal;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.mockito.Mockito;
6 import org.opendaylight.controller.clustering.services.CacheConfigException;
7 import org.opendaylight.controller.clustering.services.CacheExistException;
8 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
9 import org.opendaylight.controller.clustering.services.IClusterServices;
10 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
11 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
12 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
13
14 import java.util.EnumSet;
15 import java.util.Map;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18
19 import static junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertNotNull;
21 import static junit.framework.Assert.assertNull;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26
27 public class ClusteredDataStoreImplTest {
28     @Before
29     public void setUp(){
30
31     }
32
33     @Test
34     public void constructor_WhenPassedANullClusteringServices_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
35         try {
36             new ClusteredDataStoreImpl(null);
37         } catch(NullPointerException npe){
38             assertEquals("clusterGlobalServices cannot be null", npe.getMessage());
39         }
40     }
41
42     @Test
43     public void constructor_WhenClusteringServicesReturnsANullOperationalDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
44         try {
45             new ClusteredDataStoreImpl(mock(IClusterGlobalServices.class));
46         } catch(NullPointerException npe){
47             assertEquals("operationalDataCache cannot be null", npe.getMessage());
48         }
49     }
50
51     @Test
52     public void constructor_WhenClusteringServicesReturnsANullOConfigurationDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
53         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
54
55         // Confused about the following line?
56         // See this http://stackoverflow.com/questions/10952629/a-strange-generics-edge-case-with-mockito-when-and-generic-type-inference
57         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(new ConcurrentHashMap<Object, Object>());
58
59
60         try {
61             new ClusteredDataStoreImpl(mockClusterGlobalServices);
62         } catch(NullPointerException npe){
63             assertEquals("configurationDataCache cannot be null", npe.getMessage());
64         }
65     }
66
67     @Test
68     public void constructor_WhenOperationalDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
69         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
70
71         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
72         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
73
74         new ClusteredDataStoreImpl(mockClusterGlobalServices);
75
76         verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
77     }
78
79     @Test
80     public void constructor_WhenConfigurationDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
81         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
82
83         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
84         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
85
86         new ClusteredDataStoreImpl(mockClusterGlobalServices);
87
88         verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
89     }
90
91
92     @Test
93     public void constructor_WhenPassedAValidClusteringServices_ShouldNotThrowAnyExceptions() throws CacheExistException, CacheConfigException {
94         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
95
96         new ClusteredDataStoreImpl(mockClusterGlobalServices);
97     }
98
99
100     @Test
101     public void readOperationalData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
102         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
103
104         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
105
106         try {
107             store.readOperationalData(null);
108         } catch(NullPointerException npe){
109             assertEquals("path cannot be null", npe.getMessage());
110         }
111     }
112
113     @Test
114     public void readOperationalData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
115         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
116
117         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
118
119         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
120
121         assertNull(store.readOperationalData(path));
122     }
123
124     @Test
125     public void readOperationalData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
126         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
127
128         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
129
130         ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
131
132         Object valueObject = mock(Object.class);
133
134         when(mockOperationalDataCache.get(path)).thenReturn(valueObject);
135
136         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
137         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(new ConcurrentHashMap<Object, Object>());
138
139
140         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
141
142         assertEquals(valueObject, store.readOperationalData(path));
143     }
144
145
146
147     @Test
148     public void readConfigurationData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
149
150         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
151
152         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
153
154         try {
155             store.readConfigurationData(null);
156         } catch(NullPointerException npe){
157             assertEquals("path cannot be null", npe.getMessage());
158         }
159     }
160
161
162     @Test
163     public void readConfigurationData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
164         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
165
166         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
167
168         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
169
170         assertNull(store.readConfigurationData(path));
171     }
172
173     @Test
174     public void readConfigurationData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
175         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
176
177         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
178
179         ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
180
181         Object valueObject = mock(Object.class);
182
183         when(mockConfigurationDataCache.get(path)).thenReturn(valueObject);
184
185         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
186         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
187
188
189         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
190
191         assertEquals(valueObject, store.readConfigurationData(path));
192     }
193
194
195     @Test
196     public void requestCommit_ShouldReturnADataTransaction() throws CacheExistException, CacheConfigException {
197         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
198
199         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
200
201         assertNotNull(store.requestCommit(mock(DataModification.class)));
202
203
204     }
205
206     @Test
207     public void finishingADataTransaction_ShouldUpdateTheUnderlyingCache() throws CacheExistException, CacheConfigException {
208         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
209
210         ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
211         ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
212
213         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
214         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
215
216         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
217
218         DataModification mockModification = mock(DataModification.class);
219
220         Map configurationData = mock(Map.class);
221         Map operationalData = mock(Map.class);
222
223         when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
224         when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
225
226         DataCommitHandler.DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> transaction = store.requestCommit(mockModification);
227
228         transaction.finish();
229
230         verify(mockConfigurationDataCache).putAll(mockModification.getUpdatedConfigurationData());
231         verify(mockOperationalDataCache).putAll(mockModification.getUpdatedOperationalData());
232     }
233
234
235     @Test
236     public void rollingBackADataTransaction_ShouldDoNothing() throws CacheExistException, CacheConfigException {
237         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
238
239         ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
240         ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
241
242         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
243         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
244
245         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
246
247         DataModification mockModification = mock(DataModification.class);
248
249         Map configurationData = mock(Map.class);
250         Map operationalData = mock(Map.class);
251
252         when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
253         when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
254
255         DataCommitHandler.DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> transaction = store.requestCommit(mockModification);
256
257         transaction.rollback();
258
259         verify(mockConfigurationDataCache, never()).putAll(mockModification.getUpdatedConfigurationData());
260         verify(mockOperationalDataCache, never()).putAll(mockModification.getUpdatedOperationalData());
261
262     }
263
264
265     private IClusterGlobalServices createClusterGlobalServices() throws CacheExistException, CacheConfigException {
266         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
267
268         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
269         Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
270
271         return mockClusterGlobalServices;
272     }
273 }