2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.clustering.services_implementation.internal;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.fail;
13 import java.net.InetAddress;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Properties;
18 import org.infinispan.CacheImpl;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.opendaylight.controller.clustering.services.CacheConfigException;
22 import org.opendaylight.controller.clustering.services.CacheExistException;
23 import org.opendaylight.controller.clustering.services.IClusterServices;
24 import org.opendaylight.controller.clustering.services.IClusterServices.cacheMode;
26 public class ClusterManagerTest {
29 public void noManagerSetTest() throws CacheExistException,
30 CacheConfigException {
31 ClusterManager cm = new ClusterManager();
32 CacheImpl<String, Integer> c1 = null;
33 CacheImpl<String, Integer> c2 = null;
34 Assert.assertNull(cm.createCache("Container", "Cache", null));
35 Assert.assertNull(cm.getCacheProperties("Container", "Cache"));
36 Assert.assertNull(cm.getCache("Container", "Cache"));
37 Assert.assertFalse(cm.existCache("Container", "Cache"));
38 Assert.assertNull(cm.getCacheList("Container"));
39 Assert.assertTrue(cm.amIStandby());
40 Assert.assertNull(cm.getActiveAddress());
41 Assert.assertNull(cm.getMyAddress());
42 Assert.assertNull(cm.getClusteredControllers());
46 public void withManagerTest() throws CacheExistException,
47 CacheConfigException {
49 ClusterManager cm = new ClusterManager();
50 CacheImpl<String, Integer> c1 = null;
51 CacheImpl<String, Integer> c2 = null;
55 // Check no cache created yet
56 assertFalse(cm.existCache("NonExistantContainerName",
57 "NonExistantCacheName"));
59 String cacheName = "Cache1";
60 String containerName = "Container1";
61 // Create cache with no cacheMode set, expecting it to fail
62 HashSet<cacheMode> cacheModeSet = new HashSet<cacheMode>();
63 Assert.assertNull(cm.createCache(containerName,cacheName, cacheModeSet));
65 // Create first cache as transactional
66 cacheModeSet.add(cacheMode.TRANSACTIONAL);
68 c1 = (CacheImpl<String, Integer>) cm.createCache(containerName,
69 cacheName, cacheModeSet);
70 } catch (CacheExistException | CacheConfigException cce) {
71 fail("Failed to create cache " + cacheName);
74 // Try creating exact same cache again
76 c1 = (CacheImpl<String, Integer>) cm.createCache(containerName,
77 cacheName, cacheModeSet);
78 } catch (CacheExistException cee) {
80 } catch (CacheConfigException cce) {
81 fail("Creating cache failed with " + cce);
84 // Create second cache with both types of cacheMode, expecting it to
86 String cacheName2 = "Cache2";
87 cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);
89 c2 = (CacheImpl<String, Integer>) cm.createCache(containerName,
90 cacheName2, cacheModeSet);
91 } catch (CacheExistException cee) {
92 fail("Failed to create cache " + cacheName2 + cee);
93 } catch (CacheConfigException cce) {
97 // Create second cache NON_TRANSACTIONAL but with both ASYNC and SYNC,
99 cacheModeSet.remove(cacheMode.TRANSACTIONAL);
100 cacheModeSet.add(cacheMode.SYNC);
101 cacheModeSet.add(cacheMode.ASYNC);
103 c2 = (CacheImpl<String, Integer>) cm.createCache(containerName, cacheName2, cacheModeSet);
104 } catch (CacheExistException cee) {
105 fail("Attempted to create cache " + cacheName2 + " with illegal cache modes set " + cacheModeSet);
106 } catch (CacheConfigException cce) {
110 // Create second cache properly this time, as non_transactional and
112 cacheModeSet.remove(cacheMode.SYNC);
114 c2 = (CacheImpl<String, Integer>) cm.createCache(containerName,
115 cacheName2, cacheModeSet);
116 } catch (CacheExistException | CacheConfigException e) {
117 fail("Failed to create cache " + cacheName + " though it was supposed to succeed." + e);
120 // Make sure correct caches exists
121 Assert.assertTrue(cm.existCache(containerName, cacheName));
122 c1 = (CacheImpl<String, Integer>) cm.getCache(containerName, cacheName);
123 Assert.assertNotNull(c1);
125 Assert.assertTrue(cm.existCache(containerName, cacheName2));
126 c2 = (CacheImpl<String, Integer>) cm.getCache(containerName, cacheName2);
127 Assert.assertNotNull(c2);
129 Assert.assertNull(cm.getCache(containerName, "Cache3"));
132 HashSet<String> cacheList = (HashSet<String>) cm
133 .getCacheList("Container2");
134 Assert.assertEquals(0, cacheList.size());
136 cacheList = (HashSet<String>) cm.getCacheList(containerName);
137 Assert.assertEquals(2, cacheList.size());
138 Assert.assertTrue(cacheList.contains(cacheName));
139 Assert.assertTrue(cacheList.contains(cacheName2));
141 // Get CacheProperties
142 Assert.assertNull(cm.getCacheProperties(containerName, ""));
143 Properties p = cm.getCacheProperties(containerName, cacheName);
144 Assert.assertEquals(3, p.size());
145 Assert.assertNotNull(p
146 .getProperty(IClusterServices.cacheProps.TRANSACTION_PROP
148 Assert.assertNotNull(p
149 .getProperty(IClusterServices.cacheProps.CLUSTERING_PROP
151 Assert.assertNotNull(p
152 .getProperty(IClusterServices.cacheProps.LOCKING_PROP
155 // Destroy cache1 and make sure it's gone
156 cm.destroyCache(containerName, cacheName);
157 cm.destroyCache(containerName, "Cache3");
158 Assert.assertFalse(cm.existCache(containerName, cacheName));
159 Assert.assertTrue(cm.existCache(containerName, cacheName2));
161 // Check amIStandBy()
162 boolean standby = cm.amIStandby();
163 Assert.assertFalse(standby);
165 // Check addresses, which are all loopback
166 InetAddress activeAddress = cm.getActiveAddress();
167 Assert.assertEquals("/127.0.0.1", activeAddress.toString());
168 InetAddress myAddress = cm.getMyAddress();
169 Assert.assertEquals("/127.0.0.1", myAddress.toString());
171 List<InetAddress> cc = cm.getClusteredControllers();
172 Assert.assertEquals(0, cc.size());