Clean all unused and redundant imports in controller.
[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
5 import java.net.InetAddress;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Properties;
9
10 import org.infinispan.CacheImpl;
11 import org.junit.Assert;
12 import org.junit.Test;
13 import org.opendaylight.controller.clustering.services.CacheConfigException;
14 import org.opendaylight.controller.clustering.services.CacheExistException;
15 import org.opendaylight.controller.clustering.services.IClusterServices;
16 import org.opendaylight.controller.clustering.services.IClusterServices.cacheMode;
17
18 public class ClusterManagerTest {
19
20     @Test
21     public void NoManagerSetTest() throws CacheExistException,
22             CacheConfigException {
23         ClusterManager cm = new ClusterManager();
24         CacheImpl<String, Integer> c1 = null;
25         CacheImpl<String, Integer> c2 = null;
26         Assert.assertNull(cm.createCache("Container", "Cache", null));
27         Assert.assertNull(cm.getCacheProperties("Container", "Cache"));
28         Assert.assertNull(cm.getCache("Container", "Cache"));
29         Assert.assertFalse(cm.existCache("Container", "Cache"));
30         Assert.assertNull(cm.getCacheList("Container"));
31         Assert.assertTrue(cm.amIStandby());
32         Assert.assertNull(cm.getActiveAddress());
33         Assert.assertNull(cm.getMyAddress());
34         Assert.assertNull(cm.getClusteredControllers());
35     }
36
37     @Test
38     public void WithManagerTest() throws CacheExistException,
39             CacheConfigException {
40
41         ClusterManager cm = new ClusterManager();
42         CacheImpl<String, Integer> c1 = null;
43         CacheImpl<String, Integer> c2 = null;
44
45         cm.start();
46
47         // Check no cache created yet
48         assertFalse(cm.existCache("NonExistantContainerName",
49                 "NonExistantCacheName"));
50
51         // Create cache with no cacheMode set, expecting it to fail
52         HashSet<cacheMode> cacheModeSet = new HashSet<cacheMode>();
53         Assert.assertNull(cm.createCache("Container1", "Cache1", cacheModeSet));
54
55         // Create first cache as transactional
56         cacheModeSet.add(cacheMode.TRANSACTIONAL);
57         try {
58             c1 = (CacheImpl<String, Integer>) cm.createCache("Container1",
59                     "Cache1", cacheModeSet);
60         } catch (CacheExistException cee) {
61             Assert.assertTrue(false);
62         } catch (CacheConfigException cce) {
63             Assert.assertTrue(false);
64         }
65
66         // Try creating exact same cache again
67         try {
68             c1 = (CacheImpl<String, Integer>) cm.createCache("Container1",
69                     "Cache1", cacheModeSet);
70         } catch (CacheExistException cee) {
71             Assert.assertTrue(true);
72         } catch (CacheConfigException cce) {
73             Assert.assertTrue(false);
74         }
75
76         // Create second cache with both types of cacheMode, expecting it to
77         // complain
78         cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);
79         try {
80             c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
81                     "Cache2", cacheModeSet);
82         } catch (CacheExistException cee) {
83             Assert.assertTrue(false);
84         } catch (CacheConfigException cce) {
85             Assert.assertTrue(true);
86         }
87
88         // Create second cache NON_TRANSACTIONAL but with both ASYNC and SYNC,
89         // expect to complain
90         cacheModeSet.remove(cacheMode.TRANSACTIONAL);
91         cacheModeSet.add(cacheMode.SYNC);
92         cacheModeSet.add(cacheMode.ASYNC);
93         try {
94             c2 = (CacheImpl<String, Integer>) cm.createCache("Container1", "Cache2", cacheModeSet);
95         } catch (CacheExistException cee) {
96             Assert.assertTrue(false);
97         } catch (CacheConfigException cce) {
98             Assert.assertTrue(true);
99         }
100
101         // Create second cache properly this time, as non_transactional and
102         // ASYNC
103         cacheModeSet.remove(cacheMode.SYNC);
104         try {
105             c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
106                     "Cache2", cacheModeSet);
107         } catch (CacheExistException cee) {
108             Assert.assertTrue(false);
109         } catch (CacheConfigException cce) {
110             Assert.assertTrue(false);
111         }
112
113         // Make sure correct caches exists
114         Assert.assertTrue(cm.existCache("Container1", "Cache1"));
115         c1 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache1");
116         Assert.assertTrue(c1 != null);
117
118         Assert.assertTrue(cm.existCache("Container1", "Cache2"));
119         c2 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache2");
120         Assert.assertTrue(c2 != null);
121
122         Assert.assertNull(cm.getCache("Container1", "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("Container1");
130         Assert.assertEquals(2, cacheList.size());
131         Assert.assertTrue(cacheList.contains("Cache1"));
132         Assert.assertTrue(cacheList.contains("Cache2"));
133
134         // Get CacheProperties
135         Assert.assertNull(cm.getCacheProperties("Container1", ""));
136         Properties p = cm.getCacheProperties("Container1", "Cache1");
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("Container1", "Cache1");
150         cm.destroyCache("Container1", "Cache3");
151         Assert.assertFalse(cm.existCache("Container1", "Cache1"));
152         Assert.assertTrue(cm.existCache("Container1", "Cache2"));
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 }