Initial opendaylight infrastructure commit!!
[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 import org.opendaylight.controller.clustering.stub.internal.ClusterGlobalManager;
31
32 public class TestClusteringStub {
33     @Test
34     public void testStub1() {
35         IClusterGlobalServices c = null;
36         ClusterGlobalManager cm = null;
37         try {
38             cm = new ClusterGlobalManager();
39             c = (IClusterGlobalServices) cm;
40         } catch (UnknownHostException un) {
41             // Don't expect this assertion, so if happens signal a
42             // failure in the testcase
43             Assert.assertTrue(false);
44         }
45
46         // Make sure the stub cluster manager is allocated
47         Assert.assertTrue(cm != null);
48         Assert.assertTrue(c != null);
49
50         // ========================================
51         // Now start testing the several aspects of it.
52         // ========================================
53
54         // Allocate few caches
55         ConcurrentMap<String, Integer> c1 = null;
56         ConcurrentMap<String, Integer> c2 = null;
57         ConcurrentMap<String, Integer> c3 = null;
58         try {
59             c1 = (ConcurrentMap<String, Integer>) c.createCache("c1", null);
60         } catch (CacheExistException cee) {
61             // Don't expect this assertion, so if happens signal a
62             // failure in the testcase
63             Assert.assertTrue(false);
64         } catch (CacheConfigException cce) {
65             // Don't expect this assertion, so if happens signal a
66             // failure in the testcase
67             Assert.assertTrue(false);
68         }
69
70         // Put some data to it
71         c1.put("FOO", 1);
72         c1.put("BAZ", 2);
73         c1.put("BAR", 3);
74
75         try {
76             c1 = (ConcurrentMap<String, Integer>) c.createCache("c1", null);
77         } catch (CacheExistException cee) {
78             // This exception should be raised because the cache
79             // already exists
80             Assert.assertTrue(true);
81         } catch (CacheConfigException cce) {
82             // Don't expect this assertion, so if happens signal a
83             // failure in the testcase
84             Assert.assertTrue(false);
85         }
86
87         // Make sure this cache is retrieved
88         c1 = (ConcurrentMap<String, Integer>) c.getCache("c1");
89         Assert.assertTrue(c1 != null);
90
91         // Now make sure the data exists
92         Integer res = null;
93         res = c1.get("FOO");
94         Assert.assertTrue(res != null);
95         res = c1.get("BAR");
96         Assert.assertTrue(res != null);
97         res = c1.get("BAZ");
98         Assert.assertTrue(res != null);
99
100         // Now create yet another two caches
101         try {
102             c2 = (ConcurrentMap<String, Integer>) c.createCache("c2", null);
103             c3 = (ConcurrentMap<String, Integer>) c.createCache("c3", null);
104         } catch (CacheExistException cee) {
105             // Don't expect this assertion, so if happens signal a
106             // failure in the testcase
107             Assert.assertTrue(false);
108         } catch (CacheConfigException cce) {
109             // Don't expect this assertion, so if happens signal a
110             // failure in the testcase
111             Assert.assertTrue(false);
112         }
113
114         // Make sure the caches exist
115         Assert.assertTrue(c2 != null);
116         Assert.assertTrue(c3 != null);
117
118         // Put some fake data
119         c2.put("FOO", 11);
120         c2.put("BAZ", 22);
121         c2.put("BAR", 33);
122
123         c3.put("FOOBAR", 110);
124
125         // Test for cache existance
126         Assert.assertTrue(c.existCache("c1"));
127         Assert.assertTrue(c.existCache("c2"));
128         Assert.assertTrue(c.existCache("c3"));
129
130         // Get the Cache List
131         Set<String> caches = c.getCacheList();
132         Assert.assertTrue(caches != null);
133
134         // Check if the cachelist is correct
135         System.out.println("cache size:" + caches.size());
136         Assert.assertTrue(caches.size() == 3);
137         Assert.assertTrue(caches.contains("c1"));
138         Assert.assertTrue(caches.contains("c2"));
139         Assert.assertTrue(caches.contains("c3"));
140
141         // Check that the utility API for the cluster are working too
142         Assert.assertTrue(c.getCoordinatorAddress() != null);
143         Assert.assertTrue(c.getClusteredControllers() != null);
144         // This a one man-show
145         Assert.assertTrue(c.getClusteredControllers().size() == 1);
146         Assert.assertTrue(c.getMyAddress() != null);
147         // Make sure i'm the coordinator
148         Assert.assertTrue(c.amICoordinator());
149
150         // Now destroy some caches make sure they are gone
151         c.destroyCache("c1");
152         Assert.assertTrue(!c.existCache("c1"));
153         caches = c.getCacheList();
154         Assert.assertTrue(caches.size() == 2);
155
156         // Now recreate the cache, make sure a different one is
157         // retrieved, which should be empty
158         try {
159             c1 = (ConcurrentMap<String, Integer>) c.createCache("c1", null);
160         } catch (CacheExistException cee) {
161             // This exception should be raised because the cache
162             // already exists
163             Assert.assertTrue(true);
164         } catch (CacheConfigException cce) {
165             // Don't expect this assertion, so if happens signal a
166             // failure in the testcase
167             Assert.assertTrue(false);
168         }
169         c1 = (ConcurrentMap<String, Integer>) c.getCache("c1");
170         Assert.assertTrue(c1 != null);
171         Assert.assertTrue(c1.keySet().size() == 0);
172         caches = c.getCacheList();
173         Assert.assertTrue(caches.size() == 3);
174
175         // Now destroy the cache manager and make sure things are
176         // clean
177         cm.destroy();
178         caches = c.getCacheList();
179         Assert.assertTrue(caches.size() == 0);
180
181         // Now to re-create two caches and make sure they exists, but
182         // are different than in previous life
183         try {
184             c2 = (ConcurrentMap<String, Integer>) c.createCache("c2", null);
185             c3 = (ConcurrentMap<String, Integer>) c.createCache("c3", null);
186         } catch (CacheExistException cee) {
187             // Don't expect this assertion, so if happens signal a
188             // failure in the testcase
189             Assert.assertTrue(false);
190         } catch (CacheConfigException cce) {
191             // Don't expect this assertion, so if happens signal a
192             // failure in the testcase
193             Assert.assertTrue(false);
194         }
195         Assert.assertTrue(c2 != null);
196         Assert.assertTrue(c3 != null);
197         caches = c.getCacheList();
198         Assert.assertTrue(caches.size() == 2);
199         Assert.assertTrue(c2.keySet().size() == 0);
200         Assert.assertTrue(c3.keySet().size() == 0);
201     }
202 }