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.data.api.CompositeNode;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
15 import java.util.EnumSet;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
20 import static junit.framework.Assert.assertEquals;
21 import static junit.framework.Assert.assertNotNull;
22 import static junit.framework.Assert.assertNull;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.never;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 public class ClusteredDataStoreImplTest {
35 public void constructor_WhenPassedANullClusteringServices_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
37 new ClusteredDataStoreImpl(null);
38 } catch(NullPointerException npe){
39 assertEquals("clusterGlobalServices cannot be null", npe.getMessage());
44 public void constructor_WhenClusteringServicesReturnsANullOperationalDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
46 new ClusteredDataStoreImpl(mock(IClusterGlobalServices.class));
47 } catch(NullPointerException npe){
48 assertEquals("operationalDataCache cannot be null", npe.getMessage());
53 public void constructor_WhenClusteringServicesReturnsANullOConfigurationDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
54 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
56 // Confused about the following line?
57 // See this http://stackoverflow.com/questions/10952629/a-strange-generics-edge-case-with-mockito-when-and-generic-type-inference
58 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(new ConcurrentHashMap<Object, Object>());
62 new ClusteredDataStoreImpl(mockClusterGlobalServices);
63 } catch(NullPointerException npe){
64 assertEquals("configurationDataCache cannot be null", npe.getMessage());
69 public void constructor_WhenOperationalDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
70 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
72 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
73 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
75 new ClusteredDataStoreImpl(mockClusterGlobalServices);
77 verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
81 public void constructor_WhenConfigurationDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
82 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
84 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
85 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
87 new ClusteredDataStoreImpl(mockClusterGlobalServices);
89 verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
94 public void constructor_WhenPassedAValidClusteringServices_ShouldNotThrowAnyExceptions() throws CacheExistException, CacheConfigException {
95 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
97 new ClusteredDataStoreImpl(mockClusterGlobalServices);
102 public void readOperationalData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
103 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
105 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
108 store.readOperationalData(null);
109 } catch(NullPointerException npe){
110 assertEquals("path cannot be null", npe.getMessage());
115 public void readOperationalData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
116 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
118 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
120 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
122 assertNull(store.readOperationalData(path));
126 public void readOperationalData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
127 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
129 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
131 ConcurrentMap<InstanceIdentifier, CompositeNode> mockOperationalDataCache = mock(ConcurrentMap.class);
133 CompositeNode valueObject = mock(CompositeNode.class);
135 when(mockOperationalDataCache.get(path)).thenReturn(valueObject);
137 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
138 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(new ConcurrentHashMap<Object, Object>());
141 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
143 assertEquals(valueObject, store.readOperationalData(path));
149 public void readConfigurationData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
151 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
153 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
156 store.readConfigurationData(null);
157 } catch(NullPointerException npe){
158 assertEquals("path cannot be null", npe.getMessage());
164 public void readConfigurationData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
165 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
167 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
169 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
171 assertNull(store.readConfigurationData(path));
175 public void readConfigurationData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
176 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
178 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
180 ConcurrentMap<InstanceIdentifier, CompositeNode> mockConfigurationDataCache = mock(ConcurrentMap.class);
182 CompositeNode valueObject = mock(CompositeNode.class);
184 when(mockConfigurationDataCache.get(path)).thenReturn(valueObject);
186 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
187 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
190 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
192 assertEquals(valueObject, store.readConfigurationData(path));
197 public void requestCommit_ShouldReturnADataTransaction() throws CacheExistException, CacheConfigException {
198 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
200 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
202 assertNotNull(store.requestCommit(mock(DataModification.class)));
208 public void finishingADataTransaction_ShouldUpdateTheUnderlyingCache() throws CacheExistException, CacheConfigException {
209 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
211 ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
212 ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
214 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
215 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
217 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
219 DataModification mockModification = mock(DataModification.class);
221 Map configurationData = mock(Map.class);
222 Map operationalData = mock(Map.class);
224 when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
225 when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
227 DataCommitHandler.DataCommitTransaction<InstanceIdentifier, CompositeNode> transaction = store.requestCommit(mockModification);
229 transaction.finish();
231 verify(mockConfigurationDataCache).putAll(mockModification.getUpdatedConfigurationData());
232 verify(mockOperationalDataCache).putAll(mockModification.getUpdatedOperationalData());
237 public void rollingBackADataTransaction_ShouldDoNothing() throws CacheExistException, CacheConfigException {
238 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
240 ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
241 ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
243 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
244 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
246 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
248 DataModification mockModification = mock(DataModification.class);
250 Map configurationData = mock(Map.class);
251 Map operationalData = mock(Map.class);
253 when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
254 when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
256 DataCommitHandler.DataCommitTransaction<InstanceIdentifier, CompositeNode> transaction = store.requestCommit(mockModification);
258 transaction.rollback();
260 verify(mockConfigurationDataCache, never()).putAll(mockModification.getUpdatedConfigurationData());
261 verify(mockOperationalDataCache, never()).putAll(mockModification.getUpdatedOperationalData());
266 private IClusterGlobalServices createClusterGlobalServices() throws CacheExistException, CacheConfigException {
267 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
269 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
270 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
272 return mockClusterGlobalServices;