Bug 1029: Remove dead code: sal-schema-repository-api
[controller.git] / opendaylight / md-sal / clustered-data-store / implementation / src / test / java / org / opendaylight / controller / datastore / internal / ClusteredDataStoreImplTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.datastore.internal;
9
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;
21
22 import java.util.EnumSet;
23 import java.util.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ConcurrentMap;
26
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;
34
35 public class ClusteredDataStoreImplTest {
36     @Before
37     public void setUp(){
38
39     }
40
41     @Test
42     public void constructor_WhenPassedANullClusteringServices_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
43         try {
44             new ClusteredDataStoreImpl(null);
45         } catch(NullPointerException npe){
46             assertEquals("clusterGlobalServices cannot be null", npe.getMessage());
47         }
48     }
49
50     @Test
51     public void constructor_WhenClusteringServicesReturnsANullOperationalDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
52         try {
53             new ClusteredDataStoreImpl(mock(IClusterGlobalServices.class));
54         } catch(NullPointerException npe){
55             assertEquals("operationalDataCache cannot be null", npe.getMessage());
56         }
57     }
58
59     @Test
60     public void constructor_WhenClusteringServicesReturnsANullOConfigurationDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
61         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
62
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>());
66
67
68         try {
69             new ClusteredDataStoreImpl(mockClusterGlobalServices);
70         } catch(NullPointerException npe){
71             assertEquals("configurationDataCache cannot be null", npe.getMessage());
72         }
73     }
74
75     @Test
76     public void constructor_WhenOperationalDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
77         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
78
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>());
81
82         new ClusteredDataStoreImpl(mockClusterGlobalServices);
83
84         verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
85     }
86
87     @Test
88     public void constructor_WhenConfigurationDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
89         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
90
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>());
93
94         new ClusteredDataStoreImpl(mockClusterGlobalServices);
95
96         verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
97     }
98
99
100     @Test
101     public void constructor_WhenPassedAValidClusteringServices_ShouldNotThrowAnyExceptions() throws CacheExistException, CacheConfigException {
102         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
103
104         new ClusteredDataStoreImpl(mockClusterGlobalServices);
105     }
106
107
108     @Test
109     public void readOperationalData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
110         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
111
112         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
113
114         try {
115             store.readOperationalData(null);
116         } catch(NullPointerException npe){
117             assertEquals("path cannot be null", npe.getMessage());
118         }
119     }
120
121     @Test
122     public void readOperationalData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
123         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
124
125         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
126
127         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
128
129         assertNull(store.readOperationalData(path));
130     }
131
132     @Test
133     public void readOperationalData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
134         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
135
136         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
137
138         ConcurrentMap<InstanceIdentifier, CompositeNode> mockOperationalDataCache = mock(ConcurrentMap.class);
139
140         CompositeNode valueObject = mock(CompositeNode.class);
141
142         when(mockOperationalDataCache.get(path)).thenReturn(valueObject);
143
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>());
146
147
148         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
149
150         assertEquals(valueObject, store.readOperationalData(path));
151     }
152
153
154
155     @Test
156     public void readConfigurationData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
157
158         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
159
160         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
161
162         try {
163             store.readConfigurationData(null);
164         } catch(NullPointerException npe){
165             assertEquals("path cannot be null", npe.getMessage());
166         }
167     }
168
169
170     @Test
171     public void readConfigurationData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
172         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
173
174         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
175
176         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
177
178         assertNull(store.readConfigurationData(path));
179     }
180
181     @Test
182     public void readConfigurationData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
183         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
184
185         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
186
187         ConcurrentMap<InstanceIdentifier, CompositeNode> mockConfigurationDataCache = mock(ConcurrentMap.class);
188
189         CompositeNode valueObject = mock(CompositeNode.class);
190
191         when(mockConfigurationDataCache.get(path)).thenReturn(valueObject);
192
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);
195
196
197         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
198
199         assertEquals(valueObject, store.readConfigurationData(path));
200     }
201
202
203     @Test
204     public void requestCommit_ShouldReturnADataTransaction() throws CacheExistException, CacheConfigException {
205         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
206
207         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
208
209         assertNotNull(store.requestCommit(mock(DataModification.class)));
210
211
212     }
213
214     @Test
215     public void finishingADataTransaction_ShouldUpdateTheUnderlyingCache() throws CacheExistException, CacheConfigException {
216         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
217
218         ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
219         ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
220
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);
223
224         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
225
226         DataModification mockModification = mock(DataModification.class);
227
228         Map configurationData = mock(Map.class);
229         Map operationalData = mock(Map.class);
230
231         when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
232         when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
233
234         DataCommitHandler.DataCommitTransaction<InstanceIdentifier, CompositeNode> transaction = store.requestCommit(mockModification);
235
236         transaction.finish();
237
238         verify(mockConfigurationDataCache).putAll(mockModification.getUpdatedConfigurationData());
239         verify(mockOperationalDataCache).putAll(mockModification.getUpdatedOperationalData());
240     }
241
242
243     @Test
244     public void rollingBackADataTransaction_ShouldDoNothing() throws CacheExistException, CacheConfigException {
245         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
246
247         ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
248         ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
249
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);
252
253         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
254
255         DataModification mockModification = mock(DataModification.class);
256
257         Map configurationData = mock(Map.class);
258         Map operationalData = mock(Map.class);
259
260         when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
261         when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
262
263         DataCommitHandler.DataCommitTransaction<InstanceIdentifier, CompositeNode> transaction = store.requestCommit(mockModification);
264
265         transaction.rollback();
266
267         verify(mockConfigurationDataCache, never()).putAll(mockModification.getUpdatedConfigurationData());
268         verify(mockOperationalDataCache, never()).putAll(mockModification.getUpdatedOperationalData());
269
270     }
271
272
273     private IClusterGlobalServices createClusterGlobalServices() throws CacheExistException, CacheConfigException {
274         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
275
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));
278
279         return mockClusterGlobalServices;
280     }
281 }