Merge "Added support for annotations in generated APIs."
[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.assertTrue;
5
6 import java.net.InetAddress;
7 import java.util.ArrayList;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.Properties;
11 import java.util.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
13
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;
21
22 public class ClusterManagerTest {
23
24     @Test
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());
39     }
40
41     @Test
42     public void WithManagerTest() throws CacheExistException,
43             CacheConfigException {
44
45         ClusterManager cm = new ClusterManager();
46         CacheImpl<String, Integer> c1 = null;
47         CacheImpl<String, Integer> c2 = null;
48
49         cm.start();
50
51         // Check no cache created yet
52         assertFalse(cm.existCache("NonExistantContainerName",
53                 "NonExistantCacheName"));
54
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));
58
59         // Create first cache as transactional
60         cacheModeSet.add(cacheMode.TRANSACTIONAL);
61         try {
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);
68         }
69
70         // Try creating exact same cache again
71         try {
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);
78         }
79
80         // Create second cache with both types of cacheMode, expecting it to
81         // complain
82         cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);
83         try {
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);
90         }
91
92         // Create second cache properly this time, as non_transactional
93         cacheModeSet.remove(cacheMode.TRANSACTIONAL);
94         try {
95             c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
96                     "Cache2", cacheModeSet);
97         } catch (CacheExistException cee) {
98             Assert.assertTrue(false);
99         } catch (CacheConfigException cce) {
100             Assert.assertTrue(false);
101         }
102
103         // Make sure correct caches exists
104         Assert.assertTrue(cm.existCache("Container1", "Cache1"));
105         c1 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache1");
106         Assert.assertTrue(c1 != null);
107
108         Assert.assertTrue(cm.existCache("Container1", "Cache2"));
109         c2 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache2");
110         Assert.assertTrue(c2 != null);
111
112         Assert.assertNull(cm.getCache("Container1", "Cache3"));
113
114         // Get CacheList
115         HashSet<String> cacheList = (HashSet<String>) cm
116                 .getCacheList("Container2");
117         Assert.assertEquals(0, cacheList.size());
118
119         cacheList = (HashSet<String>) cm.getCacheList("Container1");
120         Assert.assertEquals(2, cacheList.size());
121         Assert.assertTrue(cacheList.contains("Cache1"));
122         Assert.assertTrue(cacheList.contains("Cache2"));
123
124         // Get CacheProperties
125         Assert.assertNull(cm.getCacheProperties("Container1", ""));
126         Properties p = cm.getCacheProperties("Container1", "Cache1");
127         Assert.assertEquals(3, p.size());
128         Assert.assertNotNull(p
129                 .getProperty(IClusterServices.cacheProps.TRANSACTION_PROP
130                         .toString()));
131         Assert.assertNotNull(p
132                 .getProperty(IClusterServices.cacheProps.CLUSTERING_PROP
133                         .toString()));
134         Assert.assertNotNull(p
135                 .getProperty(IClusterServices.cacheProps.LOCKING_PROP
136                         .toString()));
137
138         // Destroy cache1 and make sure it's gone
139         cm.destroyCache("Container1", "Cache1");
140         cm.destroyCache("Container1", "Cache3");
141         Assert.assertFalse(cm.existCache("Container1", "Cache1"));
142         Assert.assertTrue(cm.existCache("Container1", "Cache2"));
143
144         // Check amIStandBy()
145         boolean standby = cm.amIStandby();
146         Assert.assertFalse(standby);
147
148         // Check addresses, which are all loopback
149         InetAddress activeAddress = cm.getActiveAddress();
150         Assert.assertEquals("/127.0.0.1", activeAddress.toString());
151         InetAddress myAddress = cm.getMyAddress();
152         Assert.assertEquals("/127.0.0.1", myAddress.toString());
153
154         List<InetAddress> cc = cm.getClusteredControllers();
155         Assert.assertEquals(0, cc.size());
156
157         cm.stop();
158     }
159
160 }