3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
11 * @file TestClusteringStub.java
13 * @brief Unit tests for the stub implementation of clustering,
14 * needed only to run the integration tests
16 * Unit tests for the stub implementation of clustering,
17 * needed only to run the integration tests
19 package org.opendaylight.controller.clustering.stub.internal;
22 import java.util.concurrent.ConcurrentMap;
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;
31 public class TestClusteringStub {
33 public void testStub1() {
34 IClusterGlobalServices c = null;
35 ClusterGlobalManager cm = null;
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);
45 // Make sure the stub cluster manager is allocated
46 Assert.assertTrue(cm != null);
47 Assert.assertTrue(c != null);
49 // ========================================
50 // Now start testing the several aspects of it.
51 // ========================================
53 // Allocate few caches
54 ConcurrentMap<String, Integer> c1 = null;
55 ConcurrentMap<String, Integer> c2 = null;
56 ConcurrentMap<String, Integer> c3 = null;
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);
69 // Put some data to it
75 c1 = (ConcurrentMap<String, Integer>) c.createCache("c1", null);
76 } catch (CacheExistException cee) {
77 // This exception should be raised because the cache
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);
86 // Make sure this cache is retrieved
87 c1 = (ConcurrentMap<String, Integer>) c.getCache("c1");
88 Assert.assertTrue(c1 != null);
90 // Now make sure the data exists
93 Assert.assertTrue(res != null);
95 Assert.assertTrue(res != null);
97 Assert.assertTrue(res != null);
99 // Now create yet another two caches
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);
113 // Make sure the caches exist
114 Assert.assertTrue(c2 != null);
115 Assert.assertTrue(c3 != null);
117 // Put some fake data
122 c3.put("FOOBAR", 110);
124 // Test for cache existance
125 Assert.assertTrue(c.existCache("c1"));
126 Assert.assertTrue(c.existCache("c2"));
127 Assert.assertTrue(c.existCache("c3"));
129 // Get the Cache List
130 Set<String> caches = c.getCacheList();
131 Assert.assertTrue(caches != null);
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"));
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());
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);
155 // Now recreate the cache, make sure a different one is
156 // retrieved, which should be empty
158 c1 = (ConcurrentMap<String, Integer>) c.createCache("c1", null);
159 } catch (CacheExistException cee) {
160 // This exception should be raised because the cache
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);
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);
174 // Now destroy the cache manager and make sure things are
177 caches = c.getCacheList();
178 Assert.assertTrue(caches.size() == 0);
180 // Now to re-create two caches and make sure they exists, but
181 // are different than in previous life
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);
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);