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;
30 import org.opendaylight.controller.clustering.stub.internal.ClusterGlobalManager;
32 public class TestClusteringStub {
34 public void testStub1() {
35 IClusterGlobalServices c = null;
36 ClusterGlobalManager cm = null;
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);
46 // Make sure the stub cluster manager is allocated
47 Assert.assertTrue(cm != null);
48 Assert.assertTrue(c != null);
50 // ========================================
51 // Now start testing the several aspects of it.
52 // ========================================
54 // Allocate few caches
55 ConcurrentMap<String, Integer> c1 = null;
56 ConcurrentMap<String, Integer> c2 = null;
57 ConcurrentMap<String, Integer> c3 = null;
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);
70 // Put some data to it
76 c1 = (ConcurrentMap<String, Integer>) c.createCache("c1", null);
77 } catch (CacheExistException cee) {
78 // This exception should be raised because the cache
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);
87 // Make sure this cache is retrieved
88 c1 = (ConcurrentMap<String, Integer>) c.getCache("c1");
89 Assert.assertTrue(c1 != null);
91 // Now make sure the data exists
94 Assert.assertTrue(res != null);
96 Assert.assertTrue(res != null);
98 Assert.assertTrue(res != null);
100 // Now create yet another two caches
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);
114 // Make sure the caches exist
115 Assert.assertTrue(c2 != null);
116 Assert.assertTrue(c3 != null);
118 // Put some fake data
123 c3.put("FOOBAR", 110);
125 // Test for cache existance
126 Assert.assertTrue(c.existCache("c1"));
127 Assert.assertTrue(c.existCache("c2"));
128 Assert.assertTrue(c.existCache("c3"));
130 // Get the Cache List
131 Set<String> caches = c.getCacheList();
132 Assert.assertTrue(caches != null);
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"));
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());
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);
156 // Now recreate the cache, make sure a different one is
157 // retrieved, which should be empty
159 c1 = (ConcurrentMap<String, Integer>) c.createCache("c1", null);
160 } catch (CacheExistException cee) {
161 // This exception should be raised because the cache
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);
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);
175 // Now destroy the cache manager and make sure things are
178 caches = c.getCacheList();
179 Assert.assertTrue(caches.size() == 0);
181 // Now to re-create two caches and make sure they exists, but
182 // are different than in previous life
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);
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);