Clean all unused and redundant imports in controller.
[controller.git] / opendaylight / clustering / stub / src / test / java / org / opendaylight / controller / clustering / stub / internal / TestClusteringStub.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 /**
11  * @file   TestClusteringStub.java
12  *
13  * @brief  Unit tests for the stub implementation of clustering,
14  * needed only to run the integration tests
15  *
16  * Unit tests for the stub implementation of clustering,
17  * needed only to run the integration tests
18  */
19 package org.opendaylight.controller.clustering.stub.internal;
20
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentMap;
23
24 import java.net.UnknownHostException;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.opendaylight.controller.clustering.services.CacheConfigException;
28 import org.opendaylight.controller.clustering.services.CacheExistException;
29 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
30
31 public class TestClusteringStub {
32     @Test
33     public void testStub1() {
34         IClusterGlobalServices c = null;
35         ClusterGlobalManager cm = null;
36         try {
37             cm = new ClusterGlobalManager();
38             c = (IClusterGlobalServices) cm;
39         } catch (UnknownHostException un) {
40             // Don't expect this assertion, so if happens signal a
41             // failure in the testcase
42             Assert.assertTrue(false);
43         }
44
45         // Make sure the stub cluster manager is allocated
46         Assert.assertTrue(cm != null);
47         Assert.assertTrue(c != null);
48
49         // ========================================
50         // Now start testing the several aspects of it.
51         // ========================================
52
53         // Allocate few caches
54         ConcurrentMap<String, Integer> c1 = null;
55         ConcurrentMap<String, Integer> c2 = null;
56         ConcurrentMap<String, Integer> c3 = null;
57         try {
58             c1 = (ConcurrentMap<String, Integer>) c.createCache("c1", null);
59         } catch (CacheExistException cee) {
60             // Don't expect this assertion, so if happens signal a
61             // failure in the testcase
62             Assert.assertTrue(false);
63         } catch (CacheConfigException cce) {
64             // Don't expect this assertion, so if happens signal a
65             // failure in the testcase
66             Assert.assertTrue(false);
67         }
68
69         // Put some data to it
70         c1.put("FOO", 1);
71         c1.put("BAZ", 2);
72         c1.put("BAR", 3);
73
74         try {
75             c1 = (ConcurrentMap<String, Integer>) c.createCache("c1", null);
76         } catch (CacheExistException cee) {
77             // This exception should be raised because the cache
78             // already exists
79             Assert.assertTrue(true);
80         } catch (CacheConfigException cce) {
81             // Don't expect this assertion, so if happens signal a
82             // failure in the testcase
83             Assert.assertTrue(false);
84         }
85
86         // Make sure this cache is retrieved
87         c1 = (ConcurrentMap<String, Integer>) c.getCache("c1");
88         Assert.assertTrue(c1 != null);
89
90         // Now make sure the data exists
91         Integer res = null;
92         res = c1.get("FOO");
93         Assert.assertTrue(res != null);
94         res = c1.get("BAR");
95         Assert.assertTrue(res != null);
96         res = c1.get("BAZ");
97         Assert.assertTrue(res != null);
98
99         // Now create yet another two caches
100         try {
101             c2 = (ConcurrentMap<String, Integer>) c.createCache("c2", null);
102             c3 = (ConcurrentMap<String, Integer>) c.createCache("c3", null);
103         } catch (CacheExistException cee) {
104             // Don't expect this assertion, so if happens signal a
105             // failure in the testcase
106             Assert.assertTrue(false);
107         } catch (CacheConfigException cce) {
108             // Don't expect this assertion, so if happens signal a
109             // failure in the testcase
110             Assert.assertTrue(false);
111         }
112
113         // Make sure the caches exist
114         Assert.assertTrue(c2 != null);
115         Assert.assertTrue(c3 != null);
116
117         // Put some fake data
118         c2.put("FOO", 11);
119         c2.put("BAZ", 22);
120         c2.put("BAR", 33);
121
122         c3.put("FOOBAR", 110);
123
124         // Test for cache existance
125         Assert.assertTrue(c.existCache("c1"));
126         Assert.assertTrue(c.existCache("c2"));
127         Assert.assertTrue(c.existCache("c3"));
128
129         // Get the Cache List
130         Set<String> caches = c.getCacheList();
131         Assert.assertTrue(caches != null);
132
133         // Check if the cachelist is correct
134         System.out.println("cache size:" + caches.size());
135         Assert.assertTrue(caches.size() == 3);
136         Assert.assertTrue(caches.contains("c1"));
137         Assert.assertTrue(caches.contains("c2"));
138         Assert.assertTrue(caches.contains("c3"));
139
140         // Check that the utility API for the cluster are working too
141         Assert.assertTrue(c.getCoordinatorAddress() != null);
142         Assert.assertTrue(c.getClusteredControllers() != null);
143         // This a one man-show
144         Assert.assertTrue(c.getClusteredControllers().size() == 1);
145         Assert.assertTrue(c.getMyAddress() != null);
146         // Make sure i'm the coordinator
147         Assert.assertTrue(c.amICoordinator());
148
149         // Now destroy some caches make sure they are gone
150         c.destroyCache("c1");
151         Assert.assertTrue(!c.existCache("c1"));
152         caches = c.getCacheList();
153         Assert.assertTrue(caches.size() == 2);
154
155         // Now recreate the cache, make sure a different one is
156         // retrieved, which should be empty
157         try {
158             c1 = (ConcurrentMap<String, Integer>) c.createCache("c1", null);
159         } catch (CacheExistException cee) {
160             // This exception should be raised because the cache
161             // already exists
162             Assert.assertTrue(true);
163         } catch (CacheConfigException cce) {
164             // Don't expect this assertion, so if happens signal a
165             // failure in the testcase
166             Assert.assertTrue(false);
167         }
168         c1 = (ConcurrentMap<String, Integer>) c.getCache("c1");
169         Assert.assertTrue(c1 != null);
170         Assert.assertTrue(c1.keySet().size() == 0);
171         caches = c.getCacheList();
172         Assert.assertTrue(caches.size() == 3);
173
174         // Now destroy the cache manager and make sure things are
175         // clean
176         cm.destroy();
177         caches = c.getCacheList();
178         Assert.assertTrue(caches.size() == 0);
179
180         // Now to re-create two caches and make sure they exists, but
181         // are different than in previous life
182         try {
183             c2 = (ConcurrentMap<String, Integer>) c.createCache("c2", null);
184             c3 = (ConcurrentMap<String, Integer>) c.createCache("c3", null);
185         } catch (CacheExistException cee) {
186             // Don't expect this assertion, so if happens signal a
187             // failure in the testcase
188             Assert.assertTrue(false);
189         } catch (CacheConfigException cce) {
190             // Don't expect this assertion, so if happens signal a
191             // failure in the testcase
192             Assert.assertTrue(false);
193         }
194         Assert.assertTrue(c2 != null);
195         Assert.assertTrue(c3 != null);
196         caches = c.getCacheList();
197         Assert.assertTrue(caches.size() == 2);
198         Assert.assertTrue(c2.keySet().size() == 0);
199         Assert.assertTrue(c3.keySet().size() == 0);
200     }
201 }