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