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_WhenPassedAValidClusteringServices_ShouldNotThrowAnyExceptions() throws CacheExistException, CacheConfigException {
69 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
71 new ClusteredDataStoreImpl(mockClusterGlobalServices);
76 public void readOperationalData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
77 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
79 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
82 store.readOperationalData(null);
83 } catch(NullPointerException npe){
84 assertEquals("path cannot be null", npe.getMessage());
89 public void readOperationalData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
90 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
92 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
94 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
96 assertNull(store.readOperationalData(path));
100 public void readOperationalData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
101 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
103 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
105 ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
107 Object valueObject = mock(Object.class);
109 when(mockOperationalDataCache.get(path)).thenReturn(valueObject);
111 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
112 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(new ConcurrentHashMap<Object, Object>());
115 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
117 assertEquals(valueObject, store.readOperationalData(path));
123 public void readConfigurationData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
125 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
127 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
130 store.readConfigurationData(null);
131 } catch(NullPointerException npe){
132 assertEquals("path cannot be null", npe.getMessage());
138 public void readConfigurationData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
139 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
141 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
143 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
145 assertNull(store.readConfigurationData(path));
149 public void readConfigurationData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
150 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
152 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
154 ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
156 Object valueObject = mock(Object.class);
158 when(mockConfigurationDataCache.get(path)).thenReturn(valueObject);
160 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
161 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
164 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
166 assertEquals(valueObject, store.readConfigurationData(path));
171 public void requestCommit_ShouldReturnADataTransaction() throws CacheExistException, CacheConfigException {
172 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
174 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
176 assertNotNull(store.requestCommit(mock(DataModification.class)));
182 public void finishingADataTransaction_ShouldUpdateTheUnderlyingCache() throws CacheExistException, CacheConfigException {
183 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
185 ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
186 ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
188 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
189 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
191 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
193 DataModification mockModification = mock(DataModification.class);
195 Map configurationData = mock(Map.class);
196 Map operationalData = mock(Map.class);
198 when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
199 when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
201 DataCommitHandler.DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> transaction = store.requestCommit(mockModification);
203 transaction.finish();
205 verify(mockConfigurationDataCache).putAll(mockModification.getUpdatedConfigurationData());
206 verify(mockOperationalDataCache).putAll(mockModification.getUpdatedOperationalData());
211 public void rollingBackADataTransaction_ShouldDoNothing() throws CacheExistException, CacheConfigException {
212 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
214 ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
215 ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
217 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
218 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
220 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
222 DataModification mockModification = mock(DataModification.class);
224 Map configurationData = mock(Map.class);
225 Map operationalData = mock(Map.class);
227 when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
228 when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
230 DataCommitHandler.DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> transaction = store.requestCommit(mockModification);
232 transaction.rollback();
234 verify(mockConfigurationDataCache, never()).putAll(mockModification.getUpdatedConfigurationData());
235 verify(mockOperationalDataCache, never()).putAll(mockModification.getUpdatedOperationalData());
240 private IClusterGlobalServices createClusterGlobalServices() throws CacheExistException, CacheConfigException {
241 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
243 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
244 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
246 return mockClusterGlobalServices;