Bring some reliability in the eclipse and maven mixed builds
[controller.git] / opendaylight / md-sal / clustered-data-store / implementation / src / test / java / org / opendaylight / controller / datastore / internal / ClusteredDataStoreImplTest.java
1 package org.opendaylight.controller.datastore.internal;
2
3 import org.junit.Before;
4 import org.junit.Test;
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;
13
14 import java.util.EnumSet;
15 import java.util.Map;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18
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;
26
27 public class ClusteredDataStoreImplTest {
28     @Before
29     public void setUp(){
30
31     }
32
33     @Test
34     public void constructor_WhenPassedANullClusteringServices_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
35         try {
36             new ClusteredDataStoreImpl(null);
37         } catch(NullPointerException npe){
38             assertEquals("clusterGlobalServices cannot be null", npe.getMessage());
39         }
40     }
41
42     @Test
43     public void constructor_WhenClusteringServicesReturnsANullOperationalDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
44         try {
45             new ClusteredDataStoreImpl(mock(IClusterGlobalServices.class));
46         } catch(NullPointerException npe){
47             assertEquals("operationalDataCache cannot be null", npe.getMessage());
48         }
49     }
50
51     @Test
52     public void constructor_WhenClusteringServicesReturnsANullOConfigurationDataCache_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
53         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
54
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>());
58
59
60         try {
61             new ClusteredDataStoreImpl(mockClusterGlobalServices);
62         } catch(NullPointerException npe){
63             assertEquals("configurationDataCache cannot be null", npe.getMessage());
64         }
65     }
66
67     @Test
68     public void constructor_WhenPassedAValidClusteringServices_ShouldNotThrowAnyExceptions() throws CacheExistException, CacheConfigException {
69         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
70
71         new ClusteredDataStoreImpl(mockClusterGlobalServices);
72     }
73
74
75     @Test
76     public void readOperationalData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
77         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
78
79         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
80
81         try {
82             store.readOperationalData(null);
83         } catch(NullPointerException npe){
84             assertEquals("path cannot be null", npe.getMessage());
85         }
86     }
87
88     @Test
89     public void readOperationalData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
90         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
91
92         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
93
94         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
95
96         assertNull(store.readOperationalData(path));
97     }
98
99     @Test
100     public void readOperationalData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
101         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
102
103         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
104
105         ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
106
107         Object valueObject = mock(Object.class);
108
109         when(mockOperationalDataCache.get(path)).thenReturn(valueObject);
110
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>());
113
114
115         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
116
117         assertEquals(valueObject, store.readOperationalData(path));
118     }
119
120
121
122     @Test
123     public void readConfigurationData_WhenPassedANullPath_ShouldThrowANullPointerException() throws CacheExistException, CacheConfigException {
124
125         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
126
127         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
128
129         try {
130             store.readConfigurationData(null);
131         } catch(NullPointerException npe){
132             assertEquals("path cannot be null", npe.getMessage());
133         }
134     }
135
136
137     @Test
138     public void readConfigurationData_WhenPassedAKeyThatDoesNotExistInTheCache_ShouldReturnNull() throws CacheExistException, CacheConfigException {
139         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
140
141         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
142
143         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
144
145         assertNull(store.readConfigurationData(path));
146     }
147
148     @Test
149     public void readConfigurationData_WhenPassedAKeyThatDoesExistInTheCache_ShouldReturnTheValueObject() throws CacheExistException, CacheConfigException {
150         InstanceIdentifier path = InstanceIdentifier.builder().toInstance();
151
152         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
153
154         ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
155
156         Object valueObject = mock(Object.class);
157
158         when(mockConfigurationDataCache.get(path)).thenReturn(valueObject);
159
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);
162
163
164         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
165
166         assertEquals(valueObject, store.readConfigurationData(path));
167     }
168
169
170     @Test
171     public void requestCommit_ShouldReturnADataTransaction() throws CacheExistException, CacheConfigException {
172         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();
173
174         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
175
176         assertNotNull(store.requestCommit(mock(DataModification.class)));
177
178
179     }
180
181     @Test
182     public void finishingADataTransaction_ShouldUpdateTheUnderlyingCache() throws CacheExistException, CacheConfigException {
183         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
184
185         ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
186         ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
187
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);
190
191         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
192
193         DataModification mockModification = mock(DataModification.class);
194
195         Map configurationData = mock(Map.class);
196         Map operationalData = mock(Map.class);
197
198         when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
199         when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
200
201         DataCommitHandler.DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> transaction = store.requestCommit(mockModification);
202
203         transaction.finish();
204
205         verify(mockConfigurationDataCache).putAll(mockModification.getUpdatedConfigurationData());
206         verify(mockOperationalDataCache).putAll(mockModification.getUpdatedOperationalData());
207     }
208
209
210     @Test
211     public void rollingBackADataTransaction_ShouldDoNothing() throws CacheExistException, CacheConfigException {
212         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
213
214         ConcurrentMap mockConfigurationDataCache = mock(ConcurrentMap.class);
215         ConcurrentMap mockOperationalDataCache = mock(ConcurrentMap.class);
216
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);
219
220         ClusteredDataStoreImpl store = new ClusteredDataStoreImpl(mockClusterGlobalServices);
221
222         DataModification mockModification = mock(DataModification.class);
223
224         Map configurationData = mock(Map.class);
225         Map operationalData = mock(Map.class);
226
227         when(mockModification.getUpdatedConfigurationData()).thenReturn(configurationData);
228         when(mockModification.getUpdatedOperationalData()).thenReturn(operationalData);
229
230         DataCommitHandler.DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> transaction = store.requestCommit(mockModification);
231
232         transaction.rollback();
233
234         verify(mockConfigurationDataCache, never()).putAll(mockModification.getUpdatedConfigurationData());
235         verify(mockOperationalDataCache, never()).putAll(mockModification.getUpdatedOperationalData());
236
237     }
238
239
240     private IClusterGlobalServices createClusterGlobalServices() throws CacheExistException, CacheConfigException {
241         IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
242
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));
245
246         return mockClusterGlobalServices;
247     }
248 }