Merge "Bring some reliability in the eclipse and maven mixed builds"
[controller.git] / opendaylight / md-sal / clustered-data-store / integrationtest / src / test / java / org / opendaylight / controller / datastore / ClusteredDataStoreIT.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.datastore;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.ops4j.pax.exam.Configuration;
19 import org.ops4j.pax.exam.Option;
20 import org.ops4j.pax.exam.junit.PaxExam;
21 import org.ops4j.pax.exam.util.PathUtils;
22 import org.osgi.framework.Bundle;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.ServiceReference;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import javax.inject.Inject;
29
30 import static junit.framework.Assert.assertNotNull;
31 import static junit.framework.Assert.assertNull;
32 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
33 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
34 import static org.ops4j.pax.exam.CoreOptions.options;
35 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
36 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
37
38 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
41 import static org.mockito.Mockito.mock;
42 import static org.junit.Assert.assertEquals;
43 import static org.mockito.Mockito.when;
44
45 @RunWith(PaxExam.class)
46 public class ClusteredDataStoreIT {
47     private Logger log = LoggerFactory
48             .getLogger(ClusteredDataStoreIT.class);
49     // get the OSGI bundle context
50     @Inject
51     private BundleContext bc;
52     @Inject
53     private ClusteredDataStore clusteredDS;
54     // Configure the OSGi container
55     @Configuration
56     public Option[] config() {
57         return options(
58                 //
59                 systemProperty("logback.configurationFile").value(
60                         "file:" + PathUtils.getBaseDir()
61                                 + "/src/test/resources/logback.xml"),
62                 // To start OSGi console for inspection remotely
63                 systemProperty("osgi.console").value("2401"),
64                 // Set the systemPackages (used by clustering)
65                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
66                 systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("DEBUG"),
67                 // List framework bundles
68                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console").versionAsInProject(),
69                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util").versionAsInProject(),
70                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services").versionAsInProject(),
71                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds").versionAsInProject(),
72                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command").versionAsInProject(),
73                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime").versionAsInProject(),
74                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell").versionAsInProject(),
75                 // List logger bundles
76                 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
77                 mavenBundle("org.slf4j", "log4j-over-slf4j")
78                         .versionAsInProject(),
79                 mavenBundle("ch.qos.logback", "logback-core")
80                         .versionAsInProject(),
81                 mavenBundle("ch.qos.logback", "logback-classic")
82                         .versionAsInProject(),
83                 // needed by statisticsmanager
84                 mavenBundle("org.opendaylight.controller", "containermanager")
85                     .versionAsInProject(),
86                 mavenBundle("org.opendaylight.controller", "containermanager.it.implementation")
87                     .versionAsInProject(),
88                 mavenBundle("org.opendaylight.controller", "clustering.services")
89                     .versionAsInProject(),
90                 mavenBundle("org.opendaylight.controller", "clustering.stub")
91                     .versionAsInProject(),
92
93                 // List all the bundles on which the test case depends
94                 mavenBundle("org.opendaylight.controller", "sal")
95                     .versionAsInProject(),
96                 mavenBundle("org.opendaylight.controller", "sal.implementation")
97                     .versionAsInProject(),
98                 mavenBundle("org.opendaylight.controller", "protocol_plugins.stub")
99                     .versionAsInProject(),
100
101                 //clustered-data-store-implementation dependencies
102                 mavenBundle("com.google.guava", "guava")
103                     .versionAsInProject(),
104                 mavenBundle("org.opendaylight.controller", "sal-common-api")
105                     .versionAsInProject(),
106                 mavenBundle("org.opendaylight.controller", "sal-common-util")
107                     .versionAsInProject(),
108                 mavenBundle("org.opendaylight.controller", "sal-common-impl")
109                     .versionAsInProject(),
110                 mavenBundle("org.opendaylight.yangtools", "yang-binding")
111                     .versionAsInProject(),
112
113
114                 //sal-common-api dependencies
115                 mavenBundle("org.opendaylight.controller", "sal-common")
116                     .versionAsInProject(),
117                 mavenBundle("org.opendaylight.yangtools", "yang-common")
118                     .versionAsInProject(),
119                 mavenBundle("org.opendaylight.yangtools", "concepts")
120                     .versionAsInProject(),
121                 mavenBundle("org.osgi", "org.osgi.core")
122                     .versionAsInProject(),
123                 //adding new maven bundles
124                 mavenBundle("org.mockito", "mockito-all")
125                     .versionAsInProject(),
126
127                 // needed by hosttracker
128                 mavenBundle("org.opendaylight.controller", "clustered-datastore-implementation")
129                         .versionAsInProject(),
130                 mavenBundle("org.jboss.spec.javax.transaction",
131                         "jboss-transaction-api_1.1_spec").versionAsInProject(),
132                 mavenBundle("org.apache.commons", "commons-lang3")
133                         .versionAsInProject(),
134                 mavenBundle("org.apache.felix",
135                         "org.apache.felix.dependencymanager")
136                         .versionAsInProject(), junitBundles());
137     }
138
139     private String stateToString(int state) {
140         switch (state) {
141         case Bundle.ACTIVE:
142             return "ACTIVE";
143         case Bundle.INSTALLED:
144             return "INSTALLED";
145         case Bundle.RESOLVED:
146             return "RESOLVED";
147         case Bundle.UNINSTALLED:
148             return "UNINSTALLED";
149         default:
150             return "Not CONVERTED";
151         }
152     }
153
154     @Before
155     public void areWeReady() {
156         assertNotNull(bc);
157         boolean debugit = false;
158         Bundle b[] = bc.getBundles();
159         for (int i = 0; i < b.length; i++) {
160             int state = b[i].getState();
161             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
162                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
163                           + stateToString(state));
164                 debugit = true;
165             }
166         }
167         if (debugit) {
168             log.debug("Do some debugging because some bundle is "
169                       + "unresolved");
170         }
171     }
172
173     @Test
174     public void testBundleContextClusteredDS_NotNull() throws Exception{
175         ServiceReference serviceReference = bc.getServiceReference(ClusteredDataStore.class);
176         ClusteredDataStore store = ClusteredDataStore.class.cast(bc.getService(serviceReference));
177         assertNotNull(store);
178     }
179
180     @Test
181     public void testInjected_ClusteredDS_NotNull(){
182         assertNotNull(clusteredDS);
183     }
184
185     @Test
186     public void requestCommit_readConfigurationData_ShouldVerifyDataAndNoException(){
187         DataModification dataModification = mock(DataModification.class);
188         HashMap map = new HashMap();
189         List list = new ArrayList();
190         list.add("key");
191         InstanceIdentifier key = new InstanceIdentifier(list,String.class);
192         map.put(key, "value");
193         when(dataModification.getUpdatedConfigurationData()).thenReturn(map);
194         DataCommitTransaction dataCommitTrans = clusteredDS.requestCommit(dataModification);
195         dataCommitTrans.finish();
196         String value = (String)clusteredDS.readConfigurationData(key);
197         assertEquals("value",value);
198     }
199
200     @Test(expected = NullPointerException.class)
201     public void requestCommit_ShouldThrowException(){
202         DataModification dataModification = null;
203         DataCommitTransaction dataCommitTrans = clusteredDS.requestCommit(dataModification);
204         dataCommitTrans.finish();
205     }
206
207     @Test
208     public void requestCommit_readOperationalData_ShouldVerifyDataAndNoException(){
209         DataModification dataModification = mock(DataModification.class);
210         HashMap map = new HashMap();
211         List list = new ArrayList();
212         list.add("key");
213         InstanceIdentifier key = new InstanceIdentifier(list,String.class);
214         map.put(key, "value");
215         when(dataModification.getUpdatedOperationalData()).thenReturn(map);
216         DataCommitTransaction dataCommitTrans = clusteredDS.requestCommit(dataModification);
217         dataCommitTrans.finish();
218         String value = (String)clusteredDS.readOperationalData(key);
219         assertEquals("value",value);
220     }
221
222     @Test
223     public void requestCommit_readConfigurationData_NonExistingKey_ShouldVerifyNoMappedValueAndNoException(){
224         DataModification dataModification = mock(DataModification.class);
225         HashMap map = new HashMap();
226         List list = new ArrayList();
227         list.add("key");
228         InstanceIdentifier key = new InstanceIdentifier(list,String.class);
229         map.put(key, "value");
230         when(dataModification.getUpdatedConfigurationData()).thenReturn(map);
231         DataCommitTransaction dataCommitTrans = clusteredDS.requestCommit(dataModification);
232         dataCommitTrans.finish();
233         list = new ArrayList();
234         list.add("key1");
235         InstanceIdentifier key1 = new InstanceIdentifier(list,String.class);
236
237         String value = (String)clusteredDS.readConfigurationData(key1);
238         assertNull(value);
239     }
240
241     @Test
242     public void requestCommit_readOperationalData_NonExistingKey_ShouldVerifyNoMappedValueAndNoException(){
243         DataModification dataModification = mock(DataModification.class);
244         HashMap map = new HashMap();
245         List list = new ArrayList();
246         list.add("key");
247         InstanceIdentifier key = new InstanceIdentifier(list,String.class);
248         map.put(key, "value");
249         when(dataModification.getUpdatedOperationalData()).thenReturn(map);
250         DataCommitTransaction dataCommitTrans = clusteredDS.requestCommit(dataModification);
251         dataCommitTrans.finish();
252         list = new ArrayList();
253         list.add("key1");
254         InstanceIdentifier key1 = new InstanceIdentifier(list,String.class);
255
256         String value = (String)clusteredDS.readOperationalData(key1);
257         assertNull(value);
258     }
259
260     @Test(expected = NullPointerException.class)
261     public void requestCommit_readConfigurationData_WithNullPathShouldThrowException(){
262         DataModification dataModification = mock(DataModification.class);
263         HashMap map = new HashMap();
264         List list = new ArrayList();
265         list.add("key");
266         InstanceIdentifier key = new InstanceIdentifier(list,String.class);
267         map.put(key, "value");
268         when(dataModification.getUpdatedConfigurationData()).thenReturn(map);
269         DataCommitTransaction dataCommitTrans = clusteredDS.requestCommit(dataModification);
270         dataCommitTrans.finish();
271         String value = (String)clusteredDS.readConfigurationData(null);
272     }
273
274     @Test(expected = NullPointerException.class)
275     public void requestCommit_readOperationalData_WithNullPathShouldThrowException(){
276         DataModification dataModification = mock(DataModification.class);
277         HashMap map = new HashMap();
278         List list = new ArrayList();
279         list.add("key");
280         InstanceIdentifier key = new InstanceIdentifier(list,String.class);
281         map.put(key, "value");
282         when(dataModification.getOriginalOperationalData()).thenReturn(map);
283         DataCommitTransaction dataCommitTrans = clusteredDS.requestCommit(dataModification);
284         dataCommitTrans.finish();
285         String value = (String)clusteredDS.readOperationalData(null);
286     }
287 }