1 package org.opendaylight.controller.clustering.services_implementation.internal;
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertTrue;
6 import java.net.InetAddress;
7 import java.util.ArrayList;
8 import java.util.HashSet;
10 import java.util.Properties;
11 import java.util.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
14 import org.infinispan.CacheImpl;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.controller.clustering.services.CacheConfigException;
18 import org.opendaylight.controller.clustering.services.CacheExistException;
19 import org.opendaylight.controller.clustering.services.IClusterServices;
20 import org.opendaylight.controller.clustering.services.IClusterServices.cacheMode;
22 public class ClusterManagerTest {
25 public void NoManagerSetTest() throws CacheExistException,
26 CacheConfigException {
27 ClusterManager cm = new ClusterManager();
28 CacheImpl<String, Integer> c1 = null;
29 CacheImpl<String, Integer> c2 = null;
30 Assert.assertNull(cm.createCache("Container", "Cache", null));
31 Assert.assertNull(cm.getCacheProperties("Container", "Cache"));
32 Assert.assertNull(cm.getCache("Container", "Cache"));
33 Assert.assertFalse(cm.existCache("Container", "Cache"));
34 Assert.assertNull(cm.getCacheList("Container"));
35 Assert.assertTrue(cm.amIStandby());
36 Assert.assertNull(cm.getActiveAddress());
37 Assert.assertNull(cm.getMyAddress());
38 Assert.assertNull(cm.getClusteredControllers());
42 public void WithManagerTest() throws CacheExistException,
43 CacheConfigException {
45 ClusterManager cm = new ClusterManager();
46 CacheImpl<String, Integer> c1 = null;
47 CacheImpl<String, Integer> c2 = null;
51 // Check no cache created yet
52 assertFalse(cm.existCache("NonExistantContainerName",
53 "NonExistantCacheName"));
55 // Create cache with no cacheMode set, expecting it to fail
56 HashSet<cacheMode> cacheModeSet = new HashSet<cacheMode>();
57 Assert.assertNull(cm.createCache("Container1", "Cache1", cacheModeSet));
59 // Create first cache as transactional
60 cacheModeSet.add(cacheMode.TRANSACTIONAL);
62 c1 = (CacheImpl<String, Integer>) cm.createCache("Container1",
63 "Cache1", cacheModeSet);
64 } catch (CacheExistException cee) {
65 Assert.assertTrue(false);
66 } catch (CacheConfigException cce) {
67 Assert.assertTrue(false);
70 // Try creating exact same cache again
72 c1 = (CacheImpl<String, Integer>) cm.createCache("Container1",
73 "Cache1", cacheModeSet);
74 } catch (CacheExistException cee) {
75 Assert.assertTrue(true);
76 } catch (CacheConfigException cce) {
77 Assert.assertTrue(false);
80 // Create second cache with both types of cacheMode, expecting it to
82 cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);
84 c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
85 "Cache2", cacheModeSet);
86 } catch (CacheExistException cee) {
87 Assert.assertTrue(false);
88 } catch (CacheConfigException cce) {
89 Assert.assertTrue(true);
92 // Create second cache properly this time, as non_transactional
93 cacheModeSet.remove(cacheMode.TRANSACTIONAL);
95 c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
96 "Cache2", cacheModeSet);
97 } catch (CacheExistException cee) {
98 Assert.assertTrue(false);
99 } catch (CacheConfigException cce) {
100 Assert.assertTrue(false);
103 // Make sure correct caches exists
104 Assert.assertTrue(cm.existCache("Container1", "Cache1"));
105 c1 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache1");
106 Assert.assertTrue(c1 != null);
108 Assert.assertTrue(cm.existCache("Container1", "Cache2"));
109 c2 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache2");
110 Assert.assertTrue(c2 != null);
112 Assert.assertNull(cm.getCache("Container1", "Cache3"));
115 HashSet<String> cacheList = (HashSet<String>) cm
116 .getCacheList("Container2");
117 Assert.assertEquals(0, cacheList.size());
119 cacheList = (HashSet<String>) cm.getCacheList("Container1");
120 Assert.assertEquals(2, cacheList.size());
121 Assert.assertTrue(cacheList.contains("Cache1"));
122 Assert.assertTrue(cacheList.contains("Cache2"));
124 // Get CacheProperties
125 Assert.assertNull(cm.getCacheProperties("Container1", ""));
126 Properties p = cm.getCacheProperties("Container1", "Cache1");
127 Assert.assertEquals(3, p.size());
128 Assert.assertNotNull(p
129 .getProperty(IClusterServices.cacheProps.TRANSACTION_PROP
131 Assert.assertNotNull(p
132 .getProperty(IClusterServices.cacheProps.CLUSTERING_PROP
134 Assert.assertNotNull(p
135 .getProperty(IClusterServices.cacheProps.LOCKING_PROP
138 // Destroy cache1 and make sure it's gone
139 cm.destroyCache("Container1", "Cache1");
140 cm.destroyCache("Container1", "Cache3");
141 Assert.assertFalse(cm.existCache("Container1", "Cache1"));
142 Assert.assertTrue(cm.existCache("Container1", "Cache2"));
144 // Check amIStandBy()
145 boolean standby = cm.amIStandby();
146 Assert.assertFalse(standby);
148 // Check addresses, which are all loopback
149 InetAddress activeAddress = cm.getActiveAddress();
150 Assert.assertEquals("/127.0.0.1", activeAddress.toString());
151 InetAddress myAddress = cm.getMyAddress();
152 Assert.assertEquals("/127.0.0.1", myAddress.toString());
154 List<InetAddress> cc = cm.getClusteredControllers();
155 Assert.assertEquals(0, cc.size());