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
13 * @brief Unit Tests for Node element
15 * Unit Tests for Node element
17 package org.opendaylight.controller.sal.core;
19 import java.util.UUID;
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.core.ConstructionException;
23 import org.opendaylight.controller.sal.core.Node;
25 public class NodeTest {
27 public void testNodeOpenFlowOfWrongType() {
29 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new String(
30 "0xDEADBEEFCAFE0001L"));
32 // If we reach this point the exception was not raised
33 // which should have been the case
34 Assert.assertTrue(false);
35 } catch (ConstructionException e) {
36 // If we reach this point the exception has been raised
38 System.out.println("Got exception as expected!:" + e);
39 Assert.assertTrue(true);
44 public void testNodeONEPKOfWrongType() {
46 Node onepk1 = new Node(Node.NodeIDType.ONEPK, new Long(
47 0xDEADBEEFCAFE0001L));
49 // If we reach this point the exception was not raised
50 // which should have been the case
51 Assert.assertTrue(false);
52 } catch (ConstructionException e) {
53 // If we reach this point the exception has been raised
55 System.out.println("Got exception as expected!:" + e);
56 Assert.assertTrue(true);
61 public void testNodePCEPOfWrongType() {
63 Node pcep1 = new Node(Node.NodeIDType.PCEP, new Long(
64 0xDEADBEEFCAFE0001L));
66 // If we reach this point the exception was not raised
67 // which should have been the case
68 Assert.assertTrue(false);
69 } catch (ConstructionException e) {
70 // If we reach this point the exception has been raised
72 System.out.println("Got exception as expected!:" + e);
73 Assert.assertTrue(true);
78 public void testNodeOpenFlowOfCorrectType() {
80 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
81 0xDEADBEEFCAFE0001L));
83 // If we reach this point the exception has not been
84 // raised so we passed the test
85 System.out.println("Got node:" + of1);
86 Assert.assertTrue(true);
87 } catch (ConstructionException e) {
88 // If we reach this point the exception was raised
89 // which is not expected
90 Assert.assertTrue(false);
95 public void testNodeONEPKOfCorrectType() {
97 Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
98 "0xDEADBEEFCAFE0001L"));
100 // If we reach this point the exception has not been
101 // raised so we passed the test
102 System.out.println("Got node:" + onepk1);
103 Assert.assertTrue(true);
104 } catch (ConstructionException e) {
105 // If we reach this point the exception was raised
106 // which is not expected
107 Assert.assertTrue(false);
112 public void testNodePCEPOfCorrectType() {
114 Node pcep1 = new Node(Node.NodeIDType.PCEP, new UUID(0L, 0L));
116 // If we reach this point the exception has not been
117 // raised so we passed the test
118 System.out.println("Got node:" + pcep1);
119 Assert.assertTrue(true);
120 } catch (ConstructionException e) {
121 // If we reach this point the exception was raised
122 // which is not expected
123 Assert.assertTrue(false);
128 public void testTwoOpenFlowNodeEquals() {
130 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
131 0xDEADBEEFCAFE0001L));
132 Node of2 = new Node(Node.NodeIDType.OPENFLOW, new Long(
133 0xDEADBEEFCAFE0001L));
135 Assert.assertTrue(of1.equals(of2));
136 } catch (ConstructionException e) {
137 // If we reach this point the exception was raised
138 // which is not expected
139 Assert.assertTrue(false);
144 public void testTwoONEPKNodeEquals() {
146 Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
147 "0xDEADBEEFCAFE0001L"));
148 Node onepk2 = new Node(Node.NodeIDType.ONEPK, new String(
149 "0xDEADBEEFCAFE0001L"));
151 Assert.assertTrue(onepk1.equals(onepk2));
152 } catch (ConstructionException e) {
153 // If we reach this point the exception was raised
154 // which is not expected
155 Assert.assertTrue(false);
160 public void testTwoPCEPNodeEquals() {
162 Node pcep1 = new Node(Node.NodeIDType.PCEP, new UUID(
163 0xDEADBEEFCAFE0001L, 0L));
164 Node pcep2 = new Node(Node.NodeIDType.PCEP, new UUID(
165 0xDEADBEEFCAFE0001L, 0L));
167 Assert.assertTrue(pcep1.equals(pcep2));
168 } catch (ConstructionException e) {
169 // If we reach this point the exception was raised
170 // which is not expected
171 Assert.assertTrue(false);
176 public void testTwoOpenFlowNodeDifferents() {
178 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
179 0xDEADBEEFCAFE0001L));
180 Node of2 = new Node(Node.NodeIDType.OPENFLOW, new Long(
181 0xDEADBEEFCAFE0002L));
183 Assert.assertTrue(!of1.equals(of2));
184 } catch (ConstructionException e) {
185 // If we reach this point the exception was raised
186 // which is not expected
187 Assert.assertTrue(false);
192 public void testTwoONEPKNodeDifferents() {
194 Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
195 "0xDEADBEEFCAFE0001L"));
196 Node onepk2 = new Node(Node.NodeIDType.ONEPK, new String(
197 "0xDEADBEEFCAFE0002L"));
199 Assert.assertTrue(!onepk1.equals(onepk2));
200 } catch (ConstructionException e) {
201 // If we reach this point the exception was raised
202 // which is not expected
203 Assert.assertTrue(false);
208 public void testTwoPCEPNodeDifferents() {
210 Node pcep1 = new Node(Node.NodeIDType.PCEP, new UUID(
211 0xDEADBEEFCAFE0001L, 0L));
212 Node pcep2 = new Node(Node.NodeIDType.PCEP, new UUID(
213 0xDEADBEEFCAFE0001L, 1L));
215 Assert.assertTrue(!pcep1.equals(pcep2));
216 } catch (ConstructionException e) {
217 // If we reach this point the exception was raised
218 // which is not expected
219 Assert.assertTrue(false);
224 public void testToStringConversionAndBack() {
227 Node pcep = new Node(Node.NodeIDType.PCEP, new UUID(
228 0xDEADBEEFCAFE0001L, 0L));
230 String pcepToStr = pcep.toString();
231 System.out.println("Got String from PCEP=(" + pcepToStr + ")");
233 Node pcepFromStr = Node.fromString(pcepToStr);
235 // Make sure we got a node
236 Assert.assertTrue(pcepFromStr != null);
238 // Now the converted node need to be the same of the
240 Assert.assertTrue(pcep.equals(pcepFromStr));
241 } catch (ConstructionException e) {
242 // If we reach this point the exception was raised
243 // which is not expected
244 Assert.assertTrue(false);
249 Node onepk = new Node(Node.NodeIDType.ONEPK, new String(
250 "0xDEADBEEFCAFE0001L"));
252 String onepkToStr = onepk.toString();
253 System.out.println("Got String from ONEPK=(" + onepkToStr + ")");
255 Node onepkFromStr = Node.fromString(onepkToStr);
257 // Make sure we got a node
258 Assert.assertTrue(onepkFromStr != null);
260 // Now the converted node need to be the same of the
262 Assert.assertTrue(onepk.equals(onepkFromStr));
263 } catch (ConstructionException e) {
264 // If we reach this point the exception was raised
265 // which is not expected
266 Assert.assertTrue(false);
269 // Test OPENFLOW short string
271 Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(0x10L));
273 String ofToStr = of.toString();
274 System.out.println("Got String from OPENFLOW=(" + ofToStr + ")");
276 Node ofFromStr = Node.fromString(ofToStr);
278 // Make sure we got a node
279 Assert.assertTrue(ofFromStr != null);
281 // Now the converted node need to be the same of the
283 Assert.assertTrue(of.equals(ofFromStr));
284 } catch (ConstructionException e) {
285 // If we reach this point the exception was raised
286 // which is not expected
287 Assert.assertTrue(false);
290 // Test OPENFLOW longer string
292 Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(
293 0xDEADBEEFCAFE0001L));
295 String ofToStr = of.toString();
296 System.out.println("Got String from OPENFLOW=(" + ofToStr + ")");
298 Node ofFromStr = Node.fromString(ofToStr);
300 // Make sure we got a node
301 Assert.assertTrue(ofFromStr != null);
303 // Now the converted node need to be the same of the
305 Assert.assertTrue(of.equals(ofFromStr));
306 } catch (ConstructionException e) {
307 // If we reach this point the exception was raised
308 // which is not expected
309 Assert.assertTrue(false);
314 public void testToStringConversionForOpenFlow() {
315 // Test OPENFLOW longer string
317 Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(
318 0xDEADBEEFCAFE0001L));
319 Node ofFromStr = null;
321 // Decimal value for deadbeefcafe0001
322 ofFromStr = Node.fromString("-2401053089206501375");
324 // Make sure we got a node
325 Assert.assertTrue(ofFromStr != null);
327 // Now the converted node need to be the same of the
329 Assert.assertTrue(of.equals(ofFromStr));
331 ofFromStr = Node.fromString("deadbeefcafe0001");
333 // Make sure we got a node
334 Assert.assertTrue(ofFromStr != null);
336 // Now the converted node need to be the same of the
338 Assert.assertTrue(of.equals(ofFromStr));
340 ofFromStr = Node.fromString("DEADBEEFCAFE0001");
342 // Make sure we got a node
343 Assert.assertTrue(ofFromStr != null);
345 // Now the converted node need to be the same of the
347 Assert.assertTrue(of.equals(ofFromStr));
349 // Now the converted node need to be the same of the
351 Assert.assertTrue(of.equals(ofFromStr));
353 ofFromStr = Node.fromString("0xdeadbeefcafe0001");
355 // Make sure we got a node
356 Assert.assertTrue(ofFromStr != null);
358 // Now the converted node need to be the same of the
360 Assert.assertTrue(of.equals(ofFromStr));
362 ofFromStr = Node.fromString("0xDEADBEEFCAFE0001");
364 // Make sure we got a node
365 Assert.assertTrue(ofFromStr != null);
367 // Now the converted node need to be the same of the
369 Assert.assertTrue(of.equals(ofFromStr));
371 ofFromStr = Node.fromString("DE:AD:BE:EF:CA:FE:00:01");
373 // Make sure we got a node
374 Assert.assertTrue(ofFromStr != null);
376 // Now the converted node need to be the same of the
378 Assert.assertTrue(of.equals(ofFromStr));
380 ofFromStr = Node.fromString("de:ad:be:ef:ca:fe:00:01");
382 // Make sure we got a node
383 Assert.assertTrue(ofFromStr != null);
385 // Now the converted node need to be the same of the
387 Assert.assertTrue(of.equals(ofFromStr));
388 } catch (ConstructionException e) {
389 // If we reach this point the exception was raised
390 // which is not expected
391 Assert.assertTrue(false);
396 public void testExtensibleNode() {
398 Assert.assertTrue(Node.NodeIDType.registerIDType("FOO", Integer.class));
400 // Trying to re-register the node must fail
401 Assert.assertFalse(Node.NodeIDType.registerIDType("FOO",
404 Node n = new Node("FOO", new Integer(0xCAFE));
406 System.out.println("Got Extended node:" + n);
407 } catch (ConstructionException e) {
408 // Got an unexpected exception
409 Assert.assertTrue(false);
412 // Now unregister the type and make sure the node doesn't get
414 Node.NodeIDType.unRegisterIDType("FOO");
416 Node n = new Node("FOO", new Integer(0xCAFE));
418 // If we reach here, something didn't go fine, an
419 // exception should have been raised
420 Assert.assertTrue(false);
421 } catch (ConstructionException e) {
422 // Got an expected exception, do nothing!
425 Assert.assertTrue(true);