From e00116908dee6f83fb1a62ee90ae73669d3be46c Mon Sep 17 00:00:00 2001 From: Patrick Chu Date: Thu, 25 Apr 2013 00:02:57 -0700 Subject: [PATCH] Adding tests for clustering services. Removed first slash for infinispan-config.xml path so tests can run. Change-Id: Id1ac6575e0d504a39427c45dfbe44ce1ba321c86 Signed-off-by: Patrick Chu --- .../internal/ClusterManager.java | 2 +- .../resources/config/infinispan-config.xml | 2 +- .../internal/ClusterManagerTest.java | 160 ++++++++++++++++++ 3 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 opendaylight/clustering/services_implementation/src/test/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManagerTest.java diff --git a/opendaylight/clustering/services_implementation/src/main/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManager.java b/opendaylight/clustering/services_implementation/src/main/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManager.java index 3034d3ca6d..0b2610797f 100644 --- a/opendaylight/clustering/services_implementation/src/main/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManager.java +++ b/opendaylight/clustering/services_implementation/src/main/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManager.java @@ -248,7 +248,7 @@ public class ClusterManager implements IClusterServices { logger.info("Starting the ClusterManager"); try { //FIXME keeps throwing FileNotFoundException - this.cm = new DefaultCacheManager("/config/infinispan-config.xml"); + this.cm = new DefaultCacheManager("config/infinispan-config.xml"); logger.debug("Allocated ClusterManager"); if (this.cm != null) { this.cm.start(); diff --git a/opendaylight/clustering/services_implementation/src/main/resources/config/infinispan-config.xml b/opendaylight/clustering/services_implementation/src/main/resources/config/infinispan-config.xml index 96eff96919..03a9b4d93e 100644 --- a/opendaylight/clustering/services_implementation/src/main/resources/config/infinispan-config.xml +++ b/opendaylight/clustering/services_implementation/src/main/resources/config/infinispan-config.xml @@ -2,7 +2,7 @@ - + diff --git a/opendaylight/clustering/services_implementation/src/test/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManagerTest.java b/opendaylight/clustering/services_implementation/src/test/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManagerTest.java new file mode 100644 index 0000000000..736a2b23ec --- /dev/null +++ b/opendaylight/clustering/services_implementation/src/test/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManagerTest.java @@ -0,0 +1,160 @@ +package org.opendaylight.controller.clustering.services_implementation.internal; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.net.InetAddress; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.infinispan.CacheImpl; +import org.junit.Assert; +import org.junit.Test; +import org.opendaylight.controller.clustering.services.CacheConfigException; +import org.opendaylight.controller.clustering.services.CacheExistException; +import org.opendaylight.controller.clustering.services.IClusterServices; +import org.opendaylight.controller.clustering.services.IClusterServices.cacheMode; + +public class ClusterManagerTest { + + @Test + public void NoManagerSetTest() throws CacheExistException, + CacheConfigException { + ClusterManager cm = new ClusterManager(); + CacheImpl c1 = null; + CacheImpl c2 = null; + Assert.assertNull(cm.createCache("Container", "Cache", null)); + Assert.assertNull(cm.getCacheProperties("Container", "Cache")); + Assert.assertNull(cm.getCache("Container", "Cache")); + Assert.assertFalse(cm.existCache("Container", "Cache")); + Assert.assertNull(cm.getCacheList("Container")); + Assert.assertTrue(cm.amIStandby()); + Assert.assertNull(cm.getActiveAddress()); + Assert.assertNull(cm.getMyAddress()); + Assert.assertNull(cm.getClusteredControllers()); + } + + @Test + public void WithManagerTest() throws CacheExistException, + CacheConfigException { + + ClusterManager cm = new ClusterManager(); + CacheImpl c1 = null; + CacheImpl c2 = null; + + cm.start(); + + // Check no cache created yet + assertFalse(cm.existCache("NonExistantContainerName", + "NonExistantCacheName")); + + // Create cache with no cacheMode set, expecting it to fail + HashSet cacheModeSet = new HashSet(); + Assert.assertNull(cm.createCache("Container1", "Cache1", cacheModeSet)); + + // Create first cache as transactional + cacheModeSet.add(cacheMode.TRANSACTIONAL); + try { + c1 = (CacheImpl) cm.createCache("Container1", + "Cache1", cacheModeSet); + } catch (CacheExistException cee) { + Assert.assertTrue(false); + } catch (CacheConfigException cce) { + Assert.assertTrue(false); + } + + // Try creating exact same cache again + try { + c1 = (CacheImpl) cm.createCache("Container1", + "Cache1", cacheModeSet); + } catch (CacheExistException cee) { + Assert.assertTrue(true); + } catch (CacheConfigException cce) { + Assert.assertTrue(false); + } + + // Create second cache with both types of cacheMode, expecting it to + // complain + cacheModeSet.add(cacheMode.NON_TRANSACTIONAL); + try { + c2 = (CacheImpl) cm.createCache("Container1", + "Cache2", cacheModeSet); + } catch (CacheExistException cee) { + Assert.assertTrue(false); + } catch (CacheConfigException cce) { + Assert.assertTrue(true); + } + + // Create second cache properly this time, as non_transactional + cacheModeSet.remove(cacheMode.TRANSACTIONAL); + try { + c2 = (CacheImpl) cm.createCache("Container1", + "Cache2", cacheModeSet); + } catch (CacheExistException cee) { + Assert.assertTrue(false); + } catch (CacheConfigException cce) { + Assert.assertTrue(false); + } + + // Make sure correct caches exists + Assert.assertTrue(cm.existCache("Container1", "Cache1")); + c1 = (CacheImpl) cm.getCache("Container1", "Cache1"); + Assert.assertTrue(c1 != null); + + Assert.assertTrue(cm.existCache("Container1", "Cache2")); + c2 = (CacheImpl) cm.getCache("Container1", "Cache2"); + Assert.assertTrue(c2 != null); + + Assert.assertNull(cm.getCache("Container1", "Cache3")); + + // Get CacheList + HashSet cacheList = (HashSet) cm + .getCacheList("Container2"); + Assert.assertEquals(0, cacheList.size()); + + cacheList = (HashSet) cm.getCacheList("Container1"); + Assert.assertEquals(2, cacheList.size()); + Assert.assertTrue(cacheList.contains("Cache1")); + Assert.assertTrue(cacheList.contains("Cache2")); + + // Get CacheProperties + Assert.assertNull(cm.getCacheProperties("Container1", "")); + Properties p = cm.getCacheProperties("Container1", "Cache1"); + Assert.assertEquals(3, p.size()); + Assert.assertNotNull(p + .getProperty(IClusterServices.cacheProps.TRANSACTION_PROP + .toString())); + Assert.assertNotNull(p + .getProperty(IClusterServices.cacheProps.CLUSTERING_PROP + .toString())); + Assert.assertNotNull(p + .getProperty(IClusterServices.cacheProps.LOCKING_PROP + .toString())); + + // Destroy cache1 and make sure it's gone + cm.destroyCache("Container1", "Cache1"); + cm.destroyCache("Container1", "Cache3"); + Assert.assertFalse(cm.existCache("Container1", "Cache1")); + Assert.assertTrue(cm.existCache("Container1", "Cache2")); + + // Check amIStandBy() + boolean standby = cm.amIStandby(); + Assert.assertFalse(standby); + + // Check addresses, which are all loopback + InetAddress activeAddress = cm.getActiveAddress(); + Assert.assertEquals("/127.0.0.1", activeAddress.toString()); + InetAddress myAddress = cm.getMyAddress(); + Assert.assertEquals("/127.0.0.1", myAddress.toString()); + + List cc = cm.getClusteredControllers(); + Assert.assertEquals(0, cc.size()); + + cm.stop(); + } + +} -- 2.36.6