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