1 package org.opendaylight.controller.clustering.services_implementation.internal;
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.fail;
6 import java.net.InetAddress;
7 import java.util.HashSet;
9 import java.util.Properties;
11 import org.infinispan.CacheImpl;
12 import org.junit.Assert;
13 import org.junit.Test;
14 import org.opendaylight.controller.clustering.services.CacheConfigException;
15 import org.opendaylight.controller.clustering.services.CacheExistException;
16 import org.opendaylight.controller.clustering.services.IClusterServices;
17 import org.opendaylight.controller.clustering.services.IClusterServices.cacheMode;
19 public class ClusterManagerTest {
22 public void noManagerSetTest() throws CacheExistException,
23 CacheConfigException {
24 ClusterManager cm = new ClusterManager();
25 CacheImpl<String, Integer> c1 = null;
26 CacheImpl<String, Integer> c2 = null;
27 Assert.assertNull(cm.createCache("Container", "Cache", null));
28 Assert.assertNull(cm.getCacheProperties("Container", "Cache"));
29 Assert.assertNull(cm.getCache("Container", "Cache"));
30 Assert.assertFalse(cm.existCache("Container", "Cache"));
31 Assert.assertNull(cm.getCacheList("Container"));
32 Assert.assertTrue(cm.amIStandby());
33 Assert.assertNull(cm.getActiveAddress());
34 Assert.assertNull(cm.getMyAddress());
35 Assert.assertNull(cm.getClusteredControllers());
39 public void withManagerTest() throws CacheExistException,
40 CacheConfigException {
42 ClusterManager cm = new ClusterManager();
43 CacheImpl<String, Integer> c1 = null;
44 CacheImpl<String, Integer> c2 = null;
48 // Check no cache created yet
49 assertFalse(cm.existCache("NonExistantContainerName",
50 "NonExistantCacheName"));
52 String cacheName = "Cache1";
53 String containerName = "Container1";
54 // Create cache with no cacheMode set, expecting it to fail
55 HashSet<cacheMode> cacheModeSet = new HashSet<cacheMode>();
56 Assert.assertNull(cm.createCache(containerName,cacheName, cacheModeSet));
58 // Create first cache as transactional
59 cacheModeSet.add(cacheMode.TRANSACTIONAL);
61 c1 = (CacheImpl<String, Integer>) cm.createCache(containerName,
62 cacheName, cacheModeSet);
63 } catch (CacheExistException | CacheConfigException cce) {
64 fail("Failed to create cache " + cacheName);
67 // Try creating exact same cache again
69 c1 = (CacheImpl<String, Integer>) cm.createCache(containerName,
70 cacheName, cacheModeSet);
71 } catch (CacheExistException cee) {
73 } catch (CacheConfigException cce) {
74 fail("Creating cache failed with " + cce);
77 // Create second cache with both types of cacheMode, expecting it to
79 String cacheName2 = "Cache2";
80 cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);
82 c2 = (CacheImpl<String, Integer>) cm.createCache(containerName,
83 cacheName2, cacheModeSet);
84 } catch (CacheExistException cee) {
85 fail("Failed to create cache " + cacheName2 + cee);
86 } catch (CacheConfigException cce) {
90 // Create second cache NON_TRANSACTIONAL but with both ASYNC and SYNC,
92 cacheModeSet.remove(cacheMode.TRANSACTIONAL);
93 cacheModeSet.add(cacheMode.SYNC);
94 cacheModeSet.add(cacheMode.ASYNC);
96 c2 = (CacheImpl<String, Integer>) cm.createCache(containerName, cacheName2, cacheModeSet);
97 } catch (CacheExistException cee) {
98 fail("Attempted to create cache " + cacheName2 + " with illegal cache modes set " + cacheModeSet);
99 } catch (CacheConfigException cce) {
103 // Create second cache properly this time, as non_transactional and
105 cacheModeSet.remove(cacheMode.SYNC);
107 c2 = (CacheImpl<String, Integer>) cm.createCache(containerName,
108 cacheName2, cacheModeSet);
109 } catch (CacheExistException | CacheConfigException e) {
110 fail("Failed to create cache " + cacheName + " though it was supposed to succeed." + e);
113 // Make sure correct caches exists
114 Assert.assertTrue(cm.existCache(containerName, cacheName));
115 c1 = (CacheImpl<String, Integer>) cm.getCache(containerName, cacheName);
116 Assert.assertNotNull(c1);
118 Assert.assertTrue(cm.existCache(containerName, cacheName2));
119 c2 = (CacheImpl<String, Integer>) cm.getCache(containerName, cacheName2);
120 Assert.assertNotNull(c2);
122 Assert.assertNull(cm.getCache(containerName, "Cache3"));
125 HashSet<String> cacheList = (HashSet<String>) cm
126 .getCacheList("Container2");
127 Assert.assertEquals(0, cacheList.size());
129 cacheList = (HashSet<String>) cm.getCacheList(containerName);
130 Assert.assertEquals(2, cacheList.size());
131 Assert.assertTrue(cacheList.contains(cacheName));
132 Assert.assertTrue(cacheList.contains(cacheName2));
134 // Get CacheProperties
135 Assert.assertNull(cm.getCacheProperties(containerName, ""));
136 Properties p = cm.getCacheProperties(containerName, cacheName);
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(containerName, cacheName);
150 cm.destroyCache(containerName, "Cache3");
151 Assert.assertFalse(cm.existCache(containerName, cacheName));
152 Assert.assertTrue(cm.existCache(containerName, cacheName2));
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());