2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.datastore.internal;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.mockito.Mockito;
13 import org.opendaylight.controller.clustering.services.CacheConfigException;
14 import org.opendaylight.controller.clustering.services.CacheExistException;
15 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
16 import org.opendaylight.controller.clustering.services.IClusterServices;
17 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
18 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
22 import java.util.EnumSet;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ConcurrentMap;
27 import static junit.framework.Assert.assertEquals;
28 import static junit.framework.Assert.assertNotNull;
29 import static junit.framework.Assert.assertNull;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 public class ClusteredDataStoreImplTest {
42 public void constructor_WhenPassedANullClusteringServices_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
44 new ClusteredDataStoreImpl(null);
45 } catch(NullPointerException npe){
46 assertEquals("clusterGlobalServices cannot be null", npe.getMessage());
51 public void constructor_WhenClusteringServicesReturnsANullOperationalDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
53 new ClusteredDataStoreImpl(mock(IClusterGlobalServices.class));
54 } catch(NullPointerException npe){
55 assertEquals("operationalDataCache cannot be null", npe.getMessage());
60 public void constructor_WhenClusteringServicesReturnsANullOConfigurationDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
61 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
63 // Confused about the following line?
64 // See this http://stackoverflow.com/questions/10952629/a-strange-generics-edge-case-with-mockito-when-and-generic-type-inference
65 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(new ConcurrentHashMap<Object, Object>());
69 new ClusteredDataStoreImpl(mockClusterGlobalServices);
70 } catch(NullPointerException npe){
71 assertEquals("configurationDataCache cannot be null", npe.getMessage());
76 public void constructor_WhenOperationalDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
77 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
79 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
80 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
82 new ClusteredDataStoreImpl(mockClusterGlobalServices);
84 verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
88 public void constructor_WhenConfigurationDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
89 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
91 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
92 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
94 new ClusteredDataStoreImpl(mockClusterGlobalServices);
96 verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
101 public void constructor_WhenPassedAValidClusteringServices_ShouldNotThrowAnyExceptions() throws CacheExistException, CacheConfigException {
102 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
104 new ClusteredDataStoreImpl(mockClusterGlobalServices);
109 public void readOperationalData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
110 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
112 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
115 store.readOperationalData(null);
116 } catch(NullPointerException npe){
117 assertEquals("path cannot be null", npe.getMessage());
122 public void readOperationalData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
123 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
125 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
127 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
129 assertNull(store.readOperationalData(path));
133 public void readOperationalData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
134 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
136 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
138 ConcurrentMap<InstanceIdentifier, CompositeNode> mockOperationalDataCache = mock(ConcurrentMap.class);
140 CompositeNode valueObject = mock(CompositeNode.class);
142 when(mockOperationalDataCache.get(path)).thenReturn(valueObject);
144 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
145 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(new ConcurrentHashMap<Object, Object>());
148 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
150 assertEquals(valueObject, store.readOperationalData(path));
156 public void readConfigurationData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
158 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
160 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
163 store.readConfigurationData(null);
164 } catch(NullPointerException npe){
165 assertEquals("path cannot be null", npe.getMessage());
171 public void readConfigurationData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
172 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
174 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
176 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
178 assertNull(store.readConfigurationData(path));
182 public void readConfigurationData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
183 InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
185 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
187 ConcurrentMap<InstanceIdentifier, CompositeNode> mockConfigurationDataCache = mock(ConcurrentMap.class);
189 CompositeNode valueObject = mock(CompositeNode.class);
191 when(mockConfigurationDataCache.get(path)).thenReturn(valueObject);
193 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
194 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
197 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
199 assertEquals(valueObject, store.readConfigurationData(path));
204 public void requestCommit_ShouldReturnADataTransaction() throws CacheExistException, CacheConfigException {
205 IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
207 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
209 assertNotNull(store.requestCommit(mock(DataModification.class)));
215 public void finishingADataTransaction_ShouldUpdateTheUnderlyingCache() throws CacheExistException, CacheConfigException {
216 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
218 ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
219 ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
221 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
222 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
224 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
226 DataModification mockModification = mock(DataModification.class);
228 Map configurationData = mock(Map.class);
229 Map operationalData = mock(Map.class);
231 when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
232 when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
234 DataCommitHandler.DataCommitTransaction<InstanceIdentifier, CompositeNode> transaction = store.requestCommit(mockModification);
236 transaction.finish();
238 verify(mockConfigurationDataCache).putAll(mockModification.getUpdatedConfigurationData());
239 verify(mockOperationalDataCache).putAll(mockModification.getUpdatedOperationalData());
244 public void rollingBackADataTransaction_ShouldDoNothing() throws CacheExistException, CacheConfigException {
245 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
247 ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
248 ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
250 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockOperationalDataCache);
251 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mockConfigurationDataCache);
253 ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
255 DataModification mockModification = mock(DataModification.class);
257 Map configurationData = mock(Map.class);
258 Map operationalData = mock(Map.class);
260 when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
261 when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
263 DataCommitHandler.DataCommitTransaction<InstanceIdentifier, CompositeNode> transaction = store.requestCommit(mockModification);
265 transaction.rollback();
267 verify(mockConfigurationDataCache, never()).putAll(mockModification.getUpdatedConfigurationData());
268 verify(mockOperationalDataCache, never()).putAll(mockModification.getUpdatedOperationalData());
273 private IClusterGlobalServices createClusterGlobalServices() throws CacheExistException, CacheConfigException {
274 IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
276 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
277 Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL))).thenReturn(mock(ConcurrentMap.class));
279 return mockClusterGlobalServices;