df9036467f66966273f9a83bb57c5db774fefd40
[controller.git] / opendaylight / clustering / services_implementation / src / test / java / org / opendaylight / controller / clustering / services_implementation / internal / ClusterManagerTest.java
1 package org.opendaylight.controller.clustering.services_implementation.internal;
2
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.fail;
5
6 import java.net.InetAddress;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Properties;
10
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;
18
19 public class ClusterManagerTest {
20
21     @Test
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());
36     }
37
38     @Test
39     public void withManagerTest() throws CacheExistException,
40             CacheConfigException {
41
42         ClusterManager cm = new ClusterManager();
43         CacheImpl<String, Integer> c1 = null;
44         CacheImpl<String, Integer> c2 = null;
45
46         cm.start();
47
48         // Check no cache created yet
49         assertFalse(cm.existCache("NonExistantContainerName",
50                 "NonExistantCacheName"));
51
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));
57
58         // Create first cache as transactional
59         cacheModeSet.add(cacheMode.TRANSACTIONAL);
60         try {
61             c1 = (CacheImpl<String, Integer>) cm.createCache(containerName,
62                     cacheName, cacheModeSet);
63         } catch (CacheExistException | CacheConfigException cce) {
64            fail("Failed to create cache " + cacheName);
65         }
66
67         // Try creating exact same cache again
68         try {
69             c1 = (CacheImpl<String, Integer>) cm.createCache(containerName,
70                     cacheName, cacheModeSet);
71         } catch (CacheExistException cee) {
72
73         } catch (CacheConfigException cce) {
74             fail("Creating cache failed with " + cce);
75         }
76
77         // Create second cache with both types of cacheMode, expecting it to
78         // complain
79         String cacheName2 = "Cache2";
80         cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);
81         try {
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) {
87
88         }
89
90         // Create second cache NON_TRANSACTIONAL but with both ASYNC and SYNC,
91         // expect to complain
92         cacheModeSet.remove(cacheMode.TRANSACTIONAL);
93         cacheModeSet.add(cacheMode.SYNC);
94         cacheModeSet.add(cacheMode.ASYNC);
95         try {
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) {
100
101         }
102
103         // Create second cache properly this time, as non_transactional and
104         // ASYNC
105         cacheModeSet.remove(cacheMode.SYNC);
106         try {
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);
111         }
112
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);
117
118         Assert.assertTrue(cm.existCache(containerName, cacheName2));
119         c2 = (CacheImpl<String, Integer>) cm.getCache(containerName, cacheName2);
120         Assert.assertNotNull(c2);
121
122         Assert.assertNull(cm.getCache(containerName, "Cache3"));
123
124         // Get CacheList
125         HashSet<String> cacheList = (HashSet<String>) cm
126                 .getCacheList("Container2");
127         Assert.assertEquals(0, cacheList.size());
128
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));
133
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
140                         .toString()));
141         Assert.assertNotNull(p
142                 .getProperty(IClusterServices.cacheProps.CLUSTERING_PROP
143                         .toString()));
144         Assert.assertNotNull(p
145                 .getProperty(IClusterServices.cacheProps.LOCKING_PROP
146                         .toString()));
147
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));
153
154         // Check amIStandBy()
155         boolean standby = cm.amIStandby();
156         Assert.assertFalse(standby);
157
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());
163
164         List<InetAddress> cc = cm.getClusteredControllers();
165         Assert.assertEquals(0, cc.size());
166
167         cm.stop();
168     }
169
170 }