1 package org.opendaylight.controller.clustering.services_implementation.internal;
3 import static org.junit.Assert.assertFalse;
5 import java.net.InetAddress;
6 import java.util.HashSet;
8 import java.util.Properties;
10 import org.infinispan.CacheImpl;
11 import org.junit.Assert;
12 import org.junit.Test;
13 import org.opendaylight.controller.clustering.services.CacheConfigException;
14 import org.opendaylight.controller.clustering.services.CacheExistException;
15 import org.opendaylight.controller.clustering.services.IClusterServices;
16 import org.opendaylight.controller.clustering.services.IClusterServices.cacheMode;
18 public class ClusterManagerTest {
21 public void NoManagerSetTest() throws CacheExistException,
22 CacheConfigException {
23 ClusterManager cm = new ClusterManager();
24 CacheImpl<String, Integer> c1 = null;
25 CacheImpl<String, Integer> c2 = null;
26 Assert.assertNull(cm.createCache("Container", "Cache", null));
27 Assert.assertNull(cm.getCacheProperties("Container", "Cache"));
28 Assert.assertNull(cm.getCache("Container", "Cache"));
29 Assert.assertFalse(cm.existCache("Container", "Cache"));
30 Assert.assertNull(cm.getCacheList("Container"));
31 Assert.assertTrue(cm.amIStandby());
32 Assert.assertNull(cm.getActiveAddress());
33 Assert.assertNull(cm.getMyAddress());
34 Assert.assertNull(cm.getClusteredControllers());
38 public void WithManagerTest() throws CacheExistException,
39 CacheConfigException {
41 ClusterManager cm = new ClusterManager();
42 CacheImpl<String, Integer> c1 = null;
43 CacheImpl<String, Integer> c2 = null;
47 // Check no cache created yet
48 assertFalse(cm.existCache("NonExistantContainerName",
49 "NonExistantCacheName"));
51 // Create cache with no cacheMode set, expecting it to fail
52 HashSet<cacheMode> cacheModeSet = new HashSet<cacheMode>();
53 Assert.assertNull(cm.createCache("Container1", "Cache1", cacheModeSet));
55 // Create first cache as transactional
56 cacheModeSet.add(cacheMode.TRANSACTIONAL);
58 c1 = (CacheImpl<String, Integer>) cm.createCache("Container1",
59 "Cache1", cacheModeSet);
60 } catch (CacheExistException cee) {
61 Assert.assertTrue(false);
62 } catch (CacheConfigException cce) {
63 Assert.assertTrue(false);
66 // Try creating exact same cache again
68 c1 = (CacheImpl<String, Integer>) cm.createCache("Container1",
69 "Cache1", cacheModeSet);
70 } catch (CacheExistException cee) {
71 Assert.assertTrue(true);
72 } catch (CacheConfigException cce) {
73 Assert.assertTrue(false);
76 // Create second cache with both types of cacheMode, expecting it to
78 cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);
80 c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
81 "Cache2", cacheModeSet);
82 } catch (CacheExistException cee) {
83 Assert.assertTrue(false);
84 } catch (CacheConfigException cce) {
85 Assert.assertTrue(true);
88 // Create second cache NON_TRANSACTIONAL but with both ASYNC and SYNC,
90 cacheModeSet.remove(cacheMode.TRANSACTIONAL);
91 cacheModeSet.add(cacheMode.SYNC);
92 cacheModeSet.add(cacheMode.ASYNC);
94 c2 = (CacheImpl<String, Integer>) cm.createCache("Container1", "Cache2", cacheModeSet);
95 } catch (CacheExistException cee) {
96 Assert.assertTrue(false);
97 } catch (CacheConfigException cce) {
98 Assert.assertTrue(true);
101 // Create second cache properly this time, as non_transactional and
103 cacheModeSet.remove(cacheMode.SYNC);
105 c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
106 "Cache2", cacheModeSet);
107 } catch (CacheExistException cee) {
108 Assert.assertTrue(false);
109 } catch (CacheConfigException cce) {
110 Assert.assertTrue(false);
113 // Make sure correct caches exists
114 Assert.assertTrue(cm.existCache("Container1", "Cache1"));
115 c1 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache1");
116 Assert.assertTrue(c1 != null);
118 Assert.assertTrue(cm.existCache("Container1", "Cache2"));
119 c2 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache2");
120 Assert.assertTrue(c2 != null);
122 Assert.assertNull(cm.getCache("Container1", "Cache3"));
125 HashSet<String> cacheList = (HashSet<String>) cm
126 .getCacheList("Container2");
127 Assert.assertEquals(0, cacheList.size());
129 cacheList = (HashSet<String>) cm.getCacheList("Container1");
130 Assert.assertEquals(2, cacheList.size());
131 Assert.assertTrue(cacheList.contains("Cache1"));
132 Assert.assertTrue(cacheList.contains("Cache2"));
134 // Get CacheProperties
135 Assert.assertNull(cm.getCacheProperties("Container1", ""));
136 Properties p = cm.getCacheProperties("Container1", "Cache1");
137 Assert.assertEquals(3, p.size());
138 Assert.assertNotNull(p
139 .getProperty(IClusterServices.cacheProps.TRANSACTION_PROP
141 Assert.assertNotNull(p
142 .getProperty(IClusterServices.cacheProps.CLUSTERING_PROP
144 Assert.assertNotNull(p
145 .getProperty(IClusterServices.cacheProps.LOCKING_PROP
148 // Destroy cache1 and make sure it's gone
149 cm.destroyCache("Container1", "Cache1");
150 cm.destroyCache("Container1", "Cache3");
151 Assert.assertFalse(cm.existCache("Container1", "Cache1"));
152 Assert.assertTrue(cm.existCache("Container1", "Cache2"));
154 // Check amIStandBy()
155 boolean standby = cm.amIStandby();
156 Assert.assertFalse(standby);
158 // Check addresses, which are all loopback
159 InetAddress activeAddress = cm.getActiveAddress();
160 Assert.assertEquals("/127.0.0.1", activeAddress.toString());
161 InetAddress myAddress = cm.getMyAddress();
162 Assert.assertEquals("/127.0.0.1", myAddress.toString());
164 List<InetAddress> cc = cm.getClusteredControllers();
165 Assert.assertEquals(0, cc.size());