Cleaned up Integration Tests
[controller.git] / opendaylight / clustering / integrationtest / src / test / java / org / opendaylight / controller / clustering / services_implementation / internal / ClusteringServicesIT.java
diff --git a/opendaylight/clustering/integrationtest/src/test/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusteringServicesIT.java b/opendaylight/clustering/integrationtest/src/test/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusteringServicesIT.java
new file mode 100644 (file)
index 0000000..71a452b
--- /dev/null
@@ -0,0 +1,234 @@
+package org.opendaylight.controller.clustering.services_implementation.internal;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertFalse;\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.assertNull;\r
+import static org.junit.Assert.assertTrue;\r
+import static org.ops4j.pax.exam.CoreOptions.junitBundles;\r
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;\r
+import static org.ops4j.pax.exam.CoreOptions.options;\r
+import static org.ops4j.pax.exam.CoreOptions.systemPackages;\r
+import static org.ops4j.pax.exam.CoreOptions.systemProperty;\r
+\r
+import java.net.InetAddress;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.concurrent.ConcurrentMap;\r
+\r
+import javax.inject.Inject;\r
+\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.opendaylight.controller.clustering.services.CacheConfigException;\r
+import org.opendaylight.controller.clustering.services.CacheExistException;\r
+import org.opendaylight.controller.clustering.services.CacheListenerAddException;\r
+import org.opendaylight.controller.clustering.services.IClusterServices;\r
+import org.opendaylight.controller.clustering.services.IClusterServices.cacheMode;\r
+import org.opendaylight.controller.clustering.services.IGetUpdates;\r
+import org.ops4j.pax.exam.Option;\r
+import org.ops4j.pax.exam.junit.Configuration;\r
+import org.ops4j.pax.exam.junit.PaxExam;\r
+import org.ops4j.pax.exam.util.PathUtils;\r
+import org.osgi.framework.Bundle;\r
+import org.osgi.framework.BundleContext;\r
+import org.osgi.framework.ServiceReference;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+@RunWith(PaxExam.class)\r
+public class ClusteringServicesIT {\r
+    private Logger log = LoggerFactory\r
+            .getLogger(ClusteringServicesIT.class);\r
+    // get the OSGI bundle context\r
+    @Inject\r
+    private BundleContext bc;\r
+\r
+    private IClusterServices clusterServices = null;\r
+\r
+    // Configure the OSGi container\r
+    @Configuration\r
+    public Option[] config() {\r
+        return options(\r
+                //\r
+                systemProperty("logback.configurationFile").value(\r
+                        "file:" + PathUtils.getBaseDir()\r
+                                + "/src/test/resources/logback.xml"),\r
+                // To start OSGi console for inspection remotely\r
+                systemProperty("osgi.console").value("2401"),\r
+                // Set the systemPackages (used by clustering)\r
+                systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),\r
+                // List framework bundles\r
+                mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",\r
+                        "1.0.0.v20120522-1841"),\r
+                mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",\r
+                        "1.0.400.v20120522-2049"),\r
+                mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",\r
+                        "3.3.100.v20120522-1822"),\r
+                mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",\r
+                        "1.4.0.v20120522-1841"),\r
+                mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",\r
+                        "0.8.0.v201108120515"),\r
+                mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",\r
+                        "0.8.0.v201108120515"),\r
+                mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",\r
+                        "0.8.0.v201110170705"),\r
+                // List logger bundles\r
+                mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),\r
+                mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(),\r
+                mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(),\r
+                mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject(),\r
+                // List all the bundles on which the test case depends\r
+                mavenBundle("org.opendaylight.controller",\r
+                        "clustering.services").versionAsInProject(),\r
+                mavenBundle("org.opendaylight.controller",\r
+                        "clustering.services-implementation").versionAsInProject(),\r
+                mavenBundle("org.opendaylight.controller", "sal").versionAsInProject(),\r
+                mavenBundle("org.opendaylight.controller",\r
+                        "sal.implementation").versionAsInProject(),\r
+                mavenBundle("org.jboss.spec.javax.transaction",\r
+                        "jboss-transaction-api_1.1_spec").versionAsInProject(),\r
+                mavenBundle("org.apache.commons", "commons-lang3").versionAsInProject(),\r
+                mavenBundle("org.apache.felix",\r
+                        "org.apache.felix.dependencymanager").versionAsInProject(),\r
+                junitBundles());\r
+    }\r
+\r
+    private String stateToString(int state) {\r
+        switch (state) {\r
+        case Bundle.ACTIVE:\r
+            return "ACTIVE";\r
+        case Bundle.INSTALLED:\r
+            return "INSTALLED";\r
+        case Bundle.RESOLVED:\r
+            return "RESOLVED";\r
+        case Bundle.UNINSTALLED:\r
+            return "UNINSTALLED";\r
+        default:\r
+            return "Not CONVERTED";\r
+        }\r
+    }\r
+\r
+    @Before\r
+    public void areWeReady() {\r
+        assertNotNull(bc);\r
+        boolean debugit = false;\r
+        Bundle b[] = bc.getBundles();\r
+        for (int i = 0; i < b.length; i++) {\r
+            int state = b[i].getState();\r
+            if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {\r
+                log.debug("Bundle:" + b[i].getSymbolicName() + " state:"\r
+                        + stateToString(state));\r
+                debugit = true;\r
+            }\r
+        }\r
+        if (debugit) {\r
+            log.debug("Do some debugging because some bundle is "\r
+                    + "unresolved");\r
+        }\r
+\r
+        // Assert if true, if false we are good to go!\r
+        assertFalse(debugit);\r
+\r
+        ServiceReference r = bc.getServiceReference(IClusterServices.class\r
+                .getName());\r
+        if (r != null) {\r
+            this.clusterServices = (IClusterServices) bc.getService(r);\r
+        }\r
+        assertNotNull(this.clusterServices);\r
+\r
+    }\r
+\r
+    @Test\r
+    public void clusterTest() throws CacheExistException, CacheConfigException,\r
+            CacheListenerAddException {\r
+\r
+        String container1 = "Container1";\r
+        String container2 = "Container2";\r
+        String cache1 = "Cache1";\r
+        String cache2 = "Cache2";\r
+        String cache3 = "Cache3";\r
+\r
+        HashSet<cacheMode> cacheModeSet = new HashSet<cacheMode>();\r
+        cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);\r
+        ConcurrentMap cm11 = this.clusterServices.createCache(container1,\r
+                cache1, cacheModeSet);\r
+        assertNotNull(cm11);\r
+\r
+        assertNull(this.clusterServices.getCache(container2, cache2));\r
+        assertEquals(cm11, this.clusterServices.getCache(container1, cache1));\r
+\r
+        assertFalse(this.clusterServices.existCache(container2, cache2));\r
+        assertTrue(this.clusterServices.existCache(container1, cache1));\r
+\r
+        ConcurrentMap cm12 = this.clusterServices.createCache(container1,\r
+                cache2, cacheModeSet);\r
+        ConcurrentMap cm23 = this.clusterServices.createCache(container2,\r
+                cache3, cacheModeSet);\r
+\r
+        HashSet<String> cacheList = (HashSet<String>) this.clusterServices\r
+                .getCacheList(container1);\r
+        assertEquals(2, cacheList.size());\r
+        assertTrue(cacheList.contains(cache1));\r
+        assertTrue(cacheList.contains(cache2));\r
+        assertFalse(cacheList.contains(cache3));\r
+\r
+        assertNotNull(this.clusterServices.getCacheProperties(container1,\r
+                cache1));\r
+\r
+        HashSet<IGetUpdates<?, ?>> listeners = (HashSet<IGetUpdates<?, ?>>) this.clusterServices\r
+                .getListeners(container1, cache1);\r
+        assertEquals(0, listeners.size());\r
+\r
+        IGetUpdates<?, ?> getUpdate1 = new GetUpdates();\r
+        this.clusterServices.addListener(container1, cache1, getUpdate1);\r
+        listeners = (HashSet<IGetUpdates<?, ?>>) this.clusterServices\r
+                .getListeners(container1, cache1);\r
+        assertEquals(1, listeners.size());\r
+        this.clusterServices.addListener(container1, cache1, new GetUpdates());\r
+        listeners = (HashSet<IGetUpdates<?, ?>>) this.clusterServices\r
+                .getListeners(container1, cache1);\r
+        assertEquals(2, listeners.size());\r
+\r
+        listeners = (HashSet<IGetUpdates<?, ?>>) this.clusterServices\r
+                .getListeners(container2, cache3);\r
+        assertEquals(0, listeners.size());\r
+\r
+        this.clusterServices.removeListener(container1, cache1, getUpdate1);\r
+        listeners = (HashSet<IGetUpdates<?, ?>>) this.clusterServices\r
+                .getListeners(container1, cache1);\r
+        assertEquals(1, listeners.size());\r
+\r
+        InetAddress addr = this.clusterServices.getMyAddress();\r
+        assertNotNull(addr);\r
+\r
+        List<InetAddress> addrList = this.clusterServices\r
+                .getClusteredControllers();\r
+\r
+        this.clusterServices.destroyCache(container1, cache1);\r
+        assertFalse(this.clusterServices.existCache(container1, cache1));\r
+\r
+    }\r
+\r
+    private class GetUpdates implements IGetUpdates<Integer, String> {\r
+\r
+        @Override\r
+        public void entryCreated(Integer key, String containerName,\r
+                String cacheName, boolean originLocal) {\r
+            return;\r
+        }\r
+\r
+        @Override\r
+        public void entryUpdated(Integer key, String new_value,\r
+                String containerName, String cacheName, boolean originLocal) {\r
+            return;\r
+        }\r
+\r
+        @Override\r
+        public void entryDeleted(Integer key, String containerName,\r
+                String cacheName, boolean originLocal) {\r
+            return;\r
+        }\r
+    }\r
+}\r