1 package org.opendaylight.controller.datastore.internal;
3 import org.junit.Before;
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;
14 import java.util.EnumSet;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
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;
27 public class ClusteredDataStoreImplTest {
34 public void constructor_WhenPassedANullClusteringServices_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
36 new ClusteredDataStoreImpl(null);
37 } catch(NullPointerException npe){
38 assertEquals("clusterGlobalServices cannot be null", npe.getMessage());
43 public void constructor_WhenClusteringServicesReturnsANullOperationalDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
45 new ClusteredDataStoreImpl(mock(IClusterGlobalServices.class));
46 } catch(NullPointerException npe){
47 assertEquals("operationalDataCache cannot be null", npe.getMessage());
52 public void constructor_WhenClusteringServicesReturnsANullOConfigurationDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
53 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
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>());
61 new ClusteredDataStoreImpl(mockClusterGlobalServices);
62 } catch(NullPointerException npe){
63 assertEquals("configurationDataCache cannot be null", npe.getMessage());
68 public void constructor_WhenOperationalDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
69 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
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>());
74 new ClusteredDataStoreImpl(mockClusterGlobalServices);
76 verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
80 public void constructor_WhenConfigurationDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
81 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
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>());
86 new ClusteredDataStoreImpl(mockClusterGlobalServices);
88 verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
93 public void constructor_WhenPassedAValidClusteringServices_ShouldNotThrowAnyExceptions() throws CacheExistException, CacheConfigException {
94 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
96 new ClusteredDataStoreImpl(mockClusterGlobalServices);
101 public void readOperationalData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
102 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
104 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
107 store.readOperationalData(null);
108 } catch(NullPointerException npe){
109 assertEquals("path cannot be null", npe.getMessage());
114 public void readOperationalData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
115 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
117 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
119 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
121 assertNull(store.readOperationalData(path));
125 public void readOperationalData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
126 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
128 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
130 ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
132 Object valueObject = mock(Object.class);
134 when(mockOperationalDataCache.get(path)).thenReturn(valueObject);
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>());
140 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
142 assertEquals(valueObject, store.readOperationalData(path));
148 public void readConfigurationData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
150 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
152 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
155 store.readConfigurationData(null);
156 } catch(NullPointerException npe){
157 assertEquals("path cannot be null", npe.getMessage());
163 public void readConfigurationData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
164 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
166 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
168 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
170 assertNull(store.readConfigurationData(path));
174 public void readConfigurationData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
175 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
177 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
179 ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
181 Object valueObject = mock(Object.class);
183 when(mockConfigurationDataCache.get(path)).thenReturn(valueObject);
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);
189 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
191 assertEquals(valueObject, store.readConfigurationData(path));
196 public void requestCommit_ShouldReturnADataTransaction() throws CacheExistException, CacheConfigException {
197 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
199 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
201 assertNotNull(store.requestCommit(mock(DataModification.class)));
207 public void finishingADataTransaction_ShouldUpdateTheUnderlyingCache() throws CacheExistException, CacheConfigException {
208 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
210 ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
211 ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
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);
216 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
218 DataModification mockModification = mock(DataModification.class);
220 Map configurationData = mock(Map.class);
221 Map operationalData = mock(Map.class);
223 when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
224 when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
226 DataCommitHandler.DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> transaction = store.requestCommit(mockModification);
228 transaction.finish();
230 verify(mockConfigurationDataCache).putAll(mockModification.getUpdatedConfigurationData());
231 verify(mockOperationalDataCache).putAll(mockModification.getUpdatedOperationalData());
236 public void rollingBackADataTransaction_ShouldDoNothing() throws CacheExistException, CacheConfigException {
237 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
239 ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
240 ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
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);
245 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
247 DataModification mockModification = mock(DataModification.class);
249 Map configurationData = mock(Map.class);
250 Map operationalData = mock(Map.class);
252 when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
253 when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
255 DataCommitHandler.DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> transaction = store.requestCommit(mockModification);
257 transaction.rollback();
259 verify(mockConfigurationDataCache, never()).putAll(mockModification.getUpdatedConfigurationData());
260 verify(mockOperationalDataCache, never()).putAll(mockModification.getUpdatedOperationalData());
265 private IClusterGlobalServices createClusterGlobalServices() throws CacheExistException, CacheConfigException {
266 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
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));
271 return mockClusterGlobalServices;