Introduce ASYNC caches and use them in FRM
[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 NON_TRANSACTIONAL but with both ASYNC and SYNC,
93         // expect to complain
94         cacheModeSet.remove(cacheMode.TRANSACTIONAL);
95         cacheModeSet.add(cacheMode.SYNC);
96         cacheModeSet.add(cacheMode.ASYNC);
97         try {
98             c2 = (CacheImpl<String, Integer>) cm.createCache("Container1", "Cache2", cacheModeSet);
99         } catch (CacheExistException cee) {
100             Assert.assertTrue(false);
101         } catch (CacheConfigException cce) {
102             Assert.assertTrue(true);
103         }
104
105         // Create second cache properly this time, as non_transactional and
106         // ASYNC
107         cacheModeSet.remove(cacheMode.SYNC);
108         try {
109             c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
110                     "Cache2", cacheModeSet);
111         } catch (CacheExistException cee) {
112             Assert.assertTrue(false);
113         } catch (CacheConfigException cce) {
114             Assert.assertTrue(false);
115         }
116
117         // Make sure correct caches exists
118         Assert.assertTrue(cm.existCache("Container1", "Cache1"));
119         c1 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache1");
120         Assert.assertTrue(c1 != null);
121
122         Assert.assertTrue(cm.existCache("Container1", "Cache2"));
123         c2 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache2");
124         Assert.assertTrue(c2 != null);
125
126         Assert.assertNull(cm.getCache("Container1", "Cache3"));
127
128         // Get CacheList
129         HashSet<String> cacheList = (HashSet<String>) cm
130                 .getCacheList("Container2");
131         Assert.assertEquals(0, cacheList.size());
132
133         cacheList = (HashSet<String>) cm.getCacheList("Container1");
134         Assert.assertEquals(2, cacheList.size());
135         Assert.assertTrue(cacheList.contains("Cache1"));
136         Assert.assertTrue(cacheList.contains("Cache2"));
137
138         // Get CacheProperties
139         Assert.assertNull(cm.getCacheProperties("Container1", ""));
140         Properties p = cm.getCacheProperties("Container1", "Cache1");
141         Assert.assertEquals(3, p.size());
142         Assert.assertNotNull(p
143                 .getProperty(IClusterServices.cacheProps.TRANSACTION_PROP
144                         .toString()));
145         Assert.assertNotNull(p
146                 .getProperty(IClusterServices.cacheProps.CLUSTERING_PROP
147                         .toString()));
148         Assert.assertNotNull(p
149                 .getProperty(IClusterServices.cacheProps.LOCKING_PROP
150                         .toString()));
151
152         // Destroy cache1 and make sure it's gone
153         cm.destroyCache("Container1", "Cache1");
154         cm.destroyCache("Container1", "Cache3");
155         Assert.assertFalse(cm.existCache("Container1", "Cache1"));
156         Assert.assertTrue(cm.existCache("Container1", "Cache2"));
157
158         // Check amIStandBy()
159         boolean standby = cm.amIStandby();
160         Assert.assertFalse(standby);
161
162         // Check addresses, which are all loopback
163         InetAddress activeAddress = cm.getActiveAddress();
164         Assert.assertEquals("/127.0.0.1", activeAddress.toString());
165         InetAddress myAddress = cm.getMyAddress();
166         Assert.assertEquals("/127.0.0.1", myAddress.toString());
167
168         List<InetAddress> cc = cm.getClusteredControllers();
169         Assert.assertEquals(0, cc.size());
170
171         cm.stop();
172     }
173
174 }