Bug 1029: Remove dead code: samples/clustersession
[controller.git] / opendaylight / clustering / services_implementation / src / test / java / org / opendaylight / controller / clustering / services_implementation / internal / ClusterManagerTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.clustering.services_implementation.internal;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.fail;
12
13 import java.net.InetAddress;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Properties;
17
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;
25
26 public class ClusterManagerTest {
27
28     @Test
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());
43     }
44
45     @Test
46     public void withManagerTest() throws CacheExistException,
47             CacheConfigException {
48
49         ClusterManager cm = new ClusterManager();
50         CacheImpl<String, Integer> c1 = null;
51         CacheImpl<String, Integer> c2 = null;
52
53         cm.start();
54
55         // Check no cache created yet
56         assertFalse(cm.existCache("NonExistantContainerName",
57                 "NonExistantCacheName"));
58
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));
64
65         // Create first cache as transactional
66         cacheModeSet.add(cacheMode.TRANSACTIONAL);
67         try {
68             c1 = (CacheImpl<String, Integer>) cm.createCache(containerName,
69                     cacheName, cacheModeSet);
70         } catch (CacheExistException | CacheConfigException cce) {
71            fail("Failed to create cache " + cacheName);
72         }
73
74         // Try creating exact same cache again
75         try {
76             c1 = (CacheImpl<String, Integer>) cm.createCache(containerName,
77                     cacheName, cacheModeSet);
78         } catch (CacheExistException cee) {
79
80         } catch (CacheConfigException cce) {
81             fail("Creating cache failed with " + cce);
82         }
83
84         // Create second cache with both types of cacheMode, expecting it to
85         // complain
86         String cacheName2 = "Cache2";
87         cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);
88         try {
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) {
94
95         }
96
97         // Create second cache NON_TRANSACTIONAL but with both ASYNC and SYNC,
98         // expect to complain
99         cacheModeSet.remove(cacheMode.TRANSACTIONAL);
100         cacheModeSet.add(cacheMode.SYNC);
101         cacheModeSet.add(cacheMode.ASYNC);
102         try {
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) {
107
108         }
109
110         // Create second cache properly this time, as non_transactional and
111         // ASYNC
112         cacheModeSet.remove(cacheMode.SYNC);
113         try {
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);
118         }
119
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);
124
125         Assert.assertTrue(cm.existCache(containerName, cacheName2));
126         c2 = (CacheImpl<String, Integer>) cm.getCache(containerName, cacheName2);
127         Assert.assertNotNull(c2);
128
129         Assert.assertNull(cm.getCache(containerName, "Cache3"));
130
131         // Get CacheList
132         HashSet<String> cacheList = (HashSet<String>) cm
133                 .getCacheList("Container2");
134         Assert.assertEquals(0, cacheList.size());
135
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));
140
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
147                         .toString()));
148         Assert.assertNotNull(p
149                 .getProperty(IClusterServices.cacheProps.CLUSTERING_PROP
150                         .toString()));
151         Assert.assertNotNull(p
152                 .getProperty(IClusterServices.cacheProps.LOCKING_PROP
153                         .toString()));
154
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));
160
161         // Check amIStandBy()
162         boolean standby = cm.amIStandby();
163         Assert.assertFalse(standby);
164
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());
170
171         List<InetAddress> cc = cm.getClusteredControllers();
172         Assert.assertEquals(0, cc.size());
173
174         cm.stop();
175     }
176
177 }