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 NON_TRANSACTIONAL but with both ASYNC and SYNC,
94 cacheModeSet.remove(cacheMode.TRANSACTIONAL);
95 cacheModeSet.add(cacheMode.SYNC);
96 cacheModeSet.add(cacheMode.ASYNC);
98 c2 = (CacheImpl<String, Integer>) cm.createCache("Container1", "Cache2", cacheModeSet);
99 } catch (CacheExistException cee) {
100 Assert.assertTrue(false);
101 } catch (CacheConfigException cce) {
102 Assert.assertTrue(true);
105 // Create second cache properly this time, as non_transactional and
107 cacheModeSet.remove(cacheMode.SYNC);
109 c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
110 "Cache2", cacheModeSet);
111 } catch (CacheExistException cee) {
112 Assert.assertTrue(false);
113 } catch (CacheConfigException cce) {
114 Assert.assertTrue(false);
117 // Make sure correct caches exists
118 Assert.assertTrue(cm.existCache("Container1", "Cache1"));
119 c1 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache1");
120 Assert.assertTrue(c1 != null);
122 Assert.assertTrue(cm.existCache("Container1", "Cache2"));
123 c2 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache2");
124 Assert.assertTrue(c2 != null);
126 Assert.assertNull(cm.getCache("Container1", "Cache3"));
129 HashSet<String> cacheList = (HashSet<String>) cm
130 .getCacheList("Container2");
131 Assert.assertEquals(0, cacheList.size());
133 cacheList = (HashSet<String>) cm.getCacheList("Container1");
134 Assert.assertEquals(2, cacheList.size());
135 Assert.assertTrue(cacheList.contains("Cache1"));
136 Assert.assertTrue(cacheList.contains("Cache2"));
138 // Get CacheProperties
139 Assert.assertNull(cm.getCacheProperties("Container1", ""));
140 Properties p = cm.getCacheProperties("Container1", "Cache1");
141 Assert.assertEquals(3, p.size());
142 Assert.assertNotNull(p
143 .getProperty(IClusterServices.cacheProps.TRANSACTION_PROP
145 Assert.assertNotNull(p
146 .getProperty(IClusterServices.cacheProps.CLUSTERING_PROP
148 Assert.assertNotNull(p
149 .getProperty(IClusterServices.cacheProps.LOCKING_PROP
152 // Destroy cache1 and make sure it's gone
153 cm.destroyCache("Container1", "Cache1");
154 cm.destroyCache("Container1", "Cache3");
155 Assert.assertFalse(cm.existCache("Container1", "Cache1"));
156 Assert.assertTrue(cm.existCache("Container1", "Cache2"));
158 // Check amIStandBy()
159 boolean standby = cm.amIStandby();
160 Assert.assertFalse(standby);
162 // Check addresses, which are all loopback
163 InetAddress activeAddress = cm.getActiveAddress();
164 Assert.assertEquals("/127.0.0.1", activeAddress.toString());
165 InetAddress myAddress = cm.getMyAddress();
166 Assert.assertEquals("/127.0.0.1", myAddress.toString());
168 List<InetAddress> cc = cm.getClusteredControllers();
169 Assert.assertEquals(0, cc.size());