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;
23 public class NodeTest {
25 public void testNodeOpenFlowOfWrongType() {
27 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new String(
28 "0xDEADBEEFCAFE0001L"));
30 // If we reach this point the exception was not raised
31 // which should have been the case
32 Assert.assertTrue(false);
33 } catch (ConstructionException e) {
34 // If we reach this point the exception has been raised
36 System.out.println("Got exception as expected!:" + e);
37 Assert.assertTrue(true);
42 public void testNodeONEPKOfWrongType() {
44 Node onepk1 = new Node(Node.NodeIDType.ONEPK, new Long(
45 0xDEADBEEFCAFE0001L));
47 // If we reach this point the exception was not raised
48 // which should have been the case
49 Assert.assertTrue(false);
50 } catch (ConstructionException e) {
51 // If we reach this point the exception has been raised
53 System.out.println("Got exception as expected!:" + e);
54 Assert.assertTrue(true);
59 public void testNodePCEPOfWrongType() {
61 Node pcep1 = new Node(Node.NodeIDType.PCEP, new Long(
62 0xDEADBEEFCAFE0001L));
64 // If we reach this point the exception was not raised
65 // which should have been the case
66 Assert.assertTrue(false);
67 } catch (ConstructionException e) {
68 // If we reach this point the exception has been raised
70 System.out.println("Got exception as expected!:" + e);
71 Assert.assertTrue(true);
76 public void testNodeOpenFlowOfCorrectType() {
78 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
79 0xDEADBEEFCAFE0001L));
81 // If we reach this point the exception has not been
82 // raised so we passed the test
83 System.out.println("Got node:" + of1);
84 Assert.assertTrue(true);
85 } catch (ConstructionException e) {
86 // If we reach this point the exception was raised
87 // which is not expected
88 Assert.assertTrue(false);
93 public void testNodeONEPKOfCorrectType() {
95 Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
96 "0xDEADBEEFCAFE0001L"));
98 // If we reach this point the exception has not been
99 // raised so we passed the test
100 System.out.println("Got node:" + onepk1);
101 Assert.assertTrue(true);
102 } catch (ConstructionException e) {
103 // If we reach this point the exception was raised
104 // which is not expected
105 Assert.assertTrue(false);
110 public void testNodePCEPOfCorrectType() {
112 Node pcep1 = new Node(Node.NodeIDType.PCEP, new UUID(0L, 0L));
114 // If we reach this point the exception has not been
115 // raised so we passed the test
116 System.out.println("Got node:" + pcep1);
117 Assert.assertTrue(true);
118 } catch (ConstructionException e) {
119 // If we reach this point the exception was raised
120 // which is not expected
121 Assert.assertTrue(false);
126 public void testTwoOpenFlowNodeEquals() {
128 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
129 0xDEADBEEFCAFE0001L));
130 Node of2 = new Node(Node.NodeIDType.OPENFLOW, new Long(
131 0xDEADBEEFCAFE0001L));
133 Assert.assertTrue(of1.equals(of2));
134 } catch (ConstructionException e) {
135 // If we reach this point the exception was raised
136 // which is not expected
137 Assert.assertTrue(false);
142 public void testTwoONEPKNodeEquals() {
144 Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
145 "0xDEADBEEFCAFE0001L"));
146 Node onepk2 = new Node(Node.NodeIDType.ONEPK, new String(
147 "0xDEADBEEFCAFE0001L"));
149 Assert.assertTrue(onepk1.equals(onepk2));
150 } catch (ConstructionException e) {
151 // If we reach this point the exception was raised
152 // which is not expected
153 Assert.assertTrue(false);
158 public void testTwoPCEPNodeEquals() {
160 Node pcep1 = new Node(Node.NodeIDType.PCEP, new UUID(
161 0xDEADBEEFCAFE0001L, 0L));
162 Node pcep2 = new Node(Node.NodeIDType.PCEP, new UUID(
163 0xDEADBEEFCAFE0001L, 0L));
165 Assert.assertTrue(pcep1.equals(pcep2));
166 } catch (ConstructionException e) {
167 // If we reach this point the exception was raised
168 // which is not expected
169 Assert.assertTrue(false);
174 public void testTwoOpenFlowNodeDifferents() {
176 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
177 0xDEADBEEFCAFE0001L));
178 Node of2 = new Node(Node.NodeIDType.OPENFLOW, new Long(
179 0xDEADBEEFCAFE0002L));
181 Assert.assertTrue(!of1.equals(of2));
182 } catch (ConstructionException e) {
183 // If we reach this point the exception was raised
184 // which is not expected
185 Assert.assertTrue(false);
190 public void testTwoONEPKNodeDifferents() {
192 Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
193 "0xDEADBEEFCAFE0001L"));
194 Node onepk2 = new Node(Node.NodeIDType.ONEPK, new String(
195 "0xDEADBEEFCAFE0002L"));
197 Assert.assertTrue(!onepk1.equals(onepk2));
198 } catch (ConstructionException e) {
199 // If we reach this point the exception was raised
200 // which is not expected
201 Assert.assertTrue(false);
206 public void testTwoPCEPNodeDifferents() {
208 Node pcep1 = new Node(Node.NodeIDType.PCEP, new UUID(
209 0xDEADBEEFCAFE0001L, 0L));
210 Node pcep2 = new Node(Node.NodeIDType.PCEP, new UUID(
211 0xDEADBEEFCAFE0001L, 1L));
213 Assert.assertTrue(!pcep1.equals(pcep2));
214 } catch (ConstructionException e) {
215 // If we reach this point the exception was raised
216 // which is not expected
217 Assert.assertTrue(false);
222 public void testToStringConversionAndBack() {
225 Node pcep = new Node(Node.NodeIDType.PCEP, new UUID(
226 0xDEADBEEFCAFE0001L, 0L));
228 String pcepToStr = pcep.toString();
229 System.out.println("Got String from PCEP=(" + pcepToStr + ")");
231 Node pcepFromStr = Node.fromString(pcepToStr);
233 // Make sure we got a node
234 Assert.assertTrue(pcepFromStr != null);
236 // Now the converted node need to be the same of the
238 Assert.assertTrue(pcep.equals(pcepFromStr));
239 } catch (ConstructionException e) {
240 // If we reach this point the exception was raised
241 // which is not expected
242 Assert.assertTrue(false);
247 Node onepk = new Node(Node.NodeIDType.ONEPK, new String(
248 "0xDEADBEEFCAFE0001L"));
250 String onepkToStr = onepk.toString();
251 System.out.println("Got String from ONEPK=(" + onepkToStr + ")");
253 Node onepkFromStr = Node.fromString(onepkToStr);
255 // Make sure we got a node
256 Assert.assertTrue(onepkFromStr != null);
258 // Now the converted node need to be the same of the
260 Assert.assertTrue(onepk.equals(onepkFromStr));
261 } catch (ConstructionException e) {
262 // If we reach this point the exception was raised
263 // which is not expected
264 Assert.assertTrue(false);
267 // Test OPENFLOW short string
269 Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(0x10L));
271 String ofToStr = of.toString();
272 System.out.println("Got String from OPENFLOW=(" + ofToStr + ")");
274 Node ofFromStr = Node.fromString(ofToStr);
276 // Make sure we got a node
277 Assert.assertTrue(ofFromStr != null);
279 // Now the converted node need to be the same of the
281 Assert.assertTrue(of.equals(ofFromStr));
282 } catch (ConstructionException e) {
283 // If we reach this point the exception was raised
284 // which is not expected
285 Assert.assertTrue(false);
288 // Test OPENFLOW longer string
290 Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(
291 0xDEADBEEFCAFE0001L));
293 String ofToStr = of.toString();
294 System.out.println("Got String from OPENFLOW=(" + ofToStr + ")");
296 Node ofFromStr = Node.fromString(ofToStr);
298 // Make sure we got a node
299 Assert.assertTrue(ofFromStr != null);
301 // Now the converted node need to be the same of the
303 Assert.assertTrue(of.equals(ofFromStr));
304 } catch (ConstructionException e) {
305 // If we reach this point the exception was raised
306 // which is not expected
307 Assert.assertTrue(false);
312 public void testToStringConversionForOpenFlow() {
313 // Test OPENFLOW longer string
315 Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(
316 0xDEADBEEFCAFE0001L));
317 Node ofFromStr = null;
319 // Decimal value for deadbeefcafe0001
320 ofFromStr = Node.fromString("-2401053089206501375");
322 // Make sure we got a node
323 Assert.assertTrue(ofFromStr != null);
325 // Now the converted node need to be the same of the
327 Assert.assertTrue(of.equals(ofFromStr));
329 ofFromStr = Node.fromString("deadbeefcafe0001");
331 // Make sure we got a node
332 Assert.assertTrue(ofFromStr != null);
334 // Now the converted node need to be the same of the
336 Assert.assertTrue(of.equals(ofFromStr));
338 ofFromStr = Node.fromString("DEADBEEFCAFE0001");
340 // Make sure we got a node
341 Assert.assertTrue(ofFromStr != null);
343 // Now the converted node need to be the same of the
345 Assert.assertTrue(of.equals(ofFromStr));
347 // Now the converted node need to be the same of the
349 Assert.assertTrue(of.equals(ofFromStr));
351 ofFromStr = Node.fromString("0xdeadbeefcafe0001");
353 // Make sure we got a node
354 Assert.assertTrue(ofFromStr != null);
356 // Now the converted node need to be the same of the
358 Assert.assertTrue(of.equals(ofFromStr));
360 ofFromStr = Node.fromString("0xDEADBEEFCAFE0001");
362 // Make sure we got a node
363 Assert.assertTrue(ofFromStr != null);
365 // Now the converted node need to be the same of the
367 Assert.assertTrue(of.equals(ofFromStr));
369 ofFromStr = Node.fromString("DE:AD:BE:EF:CA:FE:00:01");
371 // Make sure we got a node
372 Assert.assertTrue(ofFromStr != null);
374 // Now the converted node need to be the same of the
376 Assert.assertTrue(of.equals(ofFromStr));
378 ofFromStr = Node.fromString("de:ad:be:ef:ca:fe:00:01");
380 // Make sure we got a node
381 Assert.assertTrue(ofFromStr != null);
383 // Now the converted node need to be the same of the
385 Assert.assertTrue(of.equals(ofFromStr));
386 } catch (ConstructionException e) {
387 // If we reach this point the exception was raised
388 // which is not expected
389 Assert.assertTrue(false);
394 public void testExtensibleNode() {
396 Assert.assertTrue(Node.NodeIDType.registerIDType("FOO", Integer.class));
398 // Trying to re-register the node must fail
399 Assert.assertFalse(Node.NodeIDType.registerIDType("FOO",
402 Node n = new Node("FOO", new Integer(0xCAFE));
404 System.out.println("Got Extended node:" + n);
405 } catch (ConstructionException e) {
406 // Got an unexpected exception
407 Assert.assertTrue(false);
410 // Now unregister the type and make sure the node doesn't get
412 Node.NodeIDType.unRegisterIDType("FOO");
414 Node n = new Node("FOO", new Integer(0xCAFE));
416 // If we reach here, something didn't go fine, an
417 // exception should have been raised
418 Assert.assertTrue(false);
419 } catch (ConstructionException e) {
420 // Got an expected exception, do nothing!
423 Assert.assertTrue(true);