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 @SuppressWarnings("unused")
28 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new String(
29 "0xDEADBEEFCAFE0001L"));
31 // If we reach this point the exception was not raised
32 // which should have been the case
33 Assert.assertTrue(false);
34 } catch (ConstructionException e) {
35 // If we reach this point the exception has been raised
37 System.out.println("Got exception as expected!:" + e);
38 Assert.assertTrue(true);
43 public void testNodeONEPKOfWrongType() {
45 @SuppressWarnings("unused")
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 @SuppressWarnings("unused")
64 Node pcep1 = new Node(Node.NodeIDType.PCEP, new Long(
65 0xDEADBEEFCAFE0001L));
67 // If we reach this point the exception was not raised
68 // which should have been the case
69 Assert.assertTrue(false);
70 } catch (ConstructionException e) {
71 // If we reach this point the exception has been raised
73 System.out.println("Got exception as expected!:" + e);
74 Assert.assertTrue(true);
79 public void testNodeOpenFlowOfCorrectType() {
81 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
82 0xDEADBEEFCAFE0001L));
84 // If we reach this point the exception has not been
85 // raised so we passed the test
86 System.out.println("Got node:" + of1);
87 Assert.assertTrue(true);
88 } catch (ConstructionException e) {
89 // If we reach this point the exception was raised
90 // which is not expected
91 Assert.assertTrue(false);
96 public void testNodeONEPKOfCorrectType() {
98 Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
99 "0xDEADBEEFCAFE0001L"));
101 // If we reach this point the exception has not been
102 // raised so we passed the test
103 System.out.println("Got node:" + onepk1);
104 Assert.assertTrue(true);
105 } catch (ConstructionException e) {
106 // If we reach this point the exception was raised
107 // which is not expected
108 Assert.assertTrue(false);
113 public void testNodePCEPOfCorrectType() {
115 Node pcep1 = new Node(Node.NodeIDType.PCEP, new UUID(0L, 0L));
117 // If we reach this point the exception has not been
118 // raised so we passed the test
119 System.out.println("Got node:" + pcep1);
120 Assert.assertTrue(true);
121 } catch (ConstructionException e) {
122 // If we reach this point the exception was raised
123 // which is not expected
124 Assert.assertTrue(false);
129 public void testTwoOpenFlowNodeEquals() {
131 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
132 0xDEADBEEFCAFE0001L));
133 Node of2 = new Node(Node.NodeIDType.OPENFLOW, new Long(
134 0xDEADBEEFCAFE0001L));
136 Assert.assertTrue(of1.equals(of2));
137 } catch (ConstructionException e) {
138 // If we reach this point the exception was raised
139 // which is not expected
140 Assert.assertTrue(false);
145 public void testTwoONEPKNodeEquals() {
147 Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
148 "0xDEADBEEFCAFE0001L"));
149 Node onepk2 = new Node(Node.NodeIDType.ONEPK, new String(
150 "0xDEADBEEFCAFE0001L"));
152 Assert.assertTrue(onepk1.equals(onepk2));
153 } catch (ConstructionException e) {
154 // If we reach this point the exception was raised
155 // which is not expected
156 Assert.assertTrue(false);
161 public void testTwoPCEPNodeEquals() {
163 Node pcep1 = new Node(Node.NodeIDType.PCEP, new UUID(
164 0xDEADBEEFCAFE0001L, 0L));
165 Node pcep2 = new Node(Node.NodeIDType.PCEP, new UUID(
166 0xDEADBEEFCAFE0001L, 0L));
168 Assert.assertTrue(pcep1.equals(pcep2));
169 } catch (ConstructionException e) {
170 // If we reach this point the exception was raised
171 // which is not expected
172 Assert.assertTrue(false);
177 public void testTwoOpenFlowNodeDifferents() {
179 Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
180 0xDEADBEEFCAFE0001L));
181 Node of2 = new Node(Node.NodeIDType.OPENFLOW, new Long(
182 0xDEADBEEFCAFE0002L));
184 Assert.assertTrue(!of1.equals(of2));
185 } catch (ConstructionException e) {
186 // If we reach this point the exception was raised
187 // which is not expected
188 Assert.assertTrue(false);
193 public void testTwoONEPKNodeDifferents() {
195 Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
196 "0xDEADBEEFCAFE0001L"));
197 Node onepk2 = new Node(Node.NodeIDType.ONEPK, new String(
198 "0xDEADBEEFCAFE0002L"));
200 Assert.assertTrue(!onepk1.equals(onepk2));
201 } catch (ConstructionException e) {
202 // If we reach this point the exception was raised
203 // which is not expected
204 Assert.assertTrue(false);
209 public void testTwoPCEPNodeDifferents() {
211 Node pcep1 = new Node(Node.NodeIDType.PCEP, new UUID(
212 0xDEADBEEFCAFE0001L, 0L));
213 Node pcep2 = new Node(Node.NodeIDType.PCEP, new UUID(
214 0xDEADBEEFCAFE0001L, 1L));
216 Assert.assertTrue(!pcep1.equals(pcep2));
217 } catch (ConstructionException e) {
218 // If we reach this point the exception was raised
219 // which is not expected
220 Assert.assertTrue(false);
225 public void testToStringConversionAndBack() {
228 Node pcep = new Node(Node.NodeIDType.PCEP, new UUID(
229 0xDEADBEEFCAFE0001L, 0L));
231 String pcepToStr = pcep.toString();
232 System.out.println("Got String from PCEP=(" + pcepToStr + ")");
234 Node pcepFromStr = Node.fromString(pcepToStr);
236 // Make sure we got a node
237 Assert.assertTrue(pcepFromStr != null);
239 // Now the converted node need to be the same of the
241 Assert.assertTrue(pcep.equals(pcepFromStr));
242 } catch (ConstructionException e) {
243 // If we reach this point the exception was raised
244 // which is not expected
245 Assert.assertTrue(false);
250 Node onepk = new Node(Node.NodeIDType.ONEPK, new String(
251 "0xDEADBEEFCAFE0001L"));
253 String onepkToStr = onepk.toString();
254 System.out.println("Got String from ONEPK=(" + onepkToStr + ")");
256 Node onepkFromStr = Node.fromString(onepkToStr);
258 // Make sure we got a node
259 Assert.assertTrue(onepkFromStr != null);
261 // Now the converted node need to be the same of the
263 Assert.assertTrue(onepk.equals(onepkFromStr));
264 } catch (ConstructionException e) {
265 // If we reach this point the exception was raised
266 // which is not expected
267 Assert.assertTrue(false);
270 // Test OPENFLOW short string
272 Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(0x10L));
274 String ofToStr = of.toString();
275 System.out.println("Got String from OPENFLOW=(" + ofToStr + ")");
277 Node ofFromStr = Node.fromString(ofToStr);
279 // Make sure we got a node
280 Assert.assertTrue(ofFromStr != null);
282 // Now the converted node need to be the same of the
284 Assert.assertTrue(of.equals(ofFromStr));
285 } catch (ConstructionException e) {
286 // If we reach this point the exception was raised
287 // which is not expected
288 Assert.assertTrue(false);
291 // Test OPENFLOW longer string
293 Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(
294 0xDEADBEEFCAFE0001L));
296 String ofToStr = of.toString();
297 System.out.println("Got String from OPENFLOW=(" + ofToStr + ")");
299 Node ofFromStr = Node.fromString(ofToStr);
301 // Make sure we got a node
302 Assert.assertTrue(ofFromStr != null);
304 // Now the converted node need to be the same of the
306 Assert.assertTrue(of.equals(ofFromStr));
307 } catch (ConstructionException e) {
308 // If we reach this point the exception was raised
309 // which is not expected
310 Assert.assertTrue(false);
315 public void testToStringConversionForOpenFlow() {
316 // Test OPENFLOW longer string
318 Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(
319 0xDEADBEEFCAFE0001L));
320 Node ofFromStr = null;
322 // Decimal value for deadbeefcafe0001
323 ofFromStr = Node.fromString("-2401053089206501375");
325 // Make sure we got a node
326 Assert.assertTrue(ofFromStr != null);
328 // Now the converted node need to be the same of the
330 Assert.assertTrue(of.equals(ofFromStr));
332 ofFromStr = Node.fromString("deadbeefcafe0001");
334 // Make sure we got a node
335 Assert.assertTrue(ofFromStr != null);
337 // Now the converted node need to be the same of the
339 Assert.assertTrue(of.equals(ofFromStr));
341 ofFromStr = Node.fromString("DEADBEEFCAFE0001");
343 // Make sure we got a node
344 Assert.assertTrue(ofFromStr != null);
346 // Now the converted node need to be the same of the
348 Assert.assertTrue(of.equals(ofFromStr));
350 // Now the converted node need to be the same of the
352 Assert.assertTrue(of.equals(ofFromStr));
354 ofFromStr = Node.fromString("0xdeadbeefcafe0001");
356 // Make sure we got a node
357 Assert.assertTrue(ofFromStr != null);
359 // Now the converted node need to be the same of the
361 Assert.assertTrue(of.equals(ofFromStr));
363 ofFromStr = Node.fromString("0xDEADBEEFCAFE0001");
365 // Make sure we got a node
366 Assert.assertTrue(ofFromStr != null);
368 // Now the converted node need to be the same of the
370 Assert.assertTrue(of.equals(ofFromStr));
372 ofFromStr = Node.fromString("DE:AD:BE:EF:CA:FE:00:01");
374 // Make sure we got a node
375 Assert.assertTrue(ofFromStr != null);
377 // Now the converted node need to be the same of the
379 Assert.assertTrue(of.equals(ofFromStr));
381 ofFromStr = Node.fromString("de:ad:be:ef:ca:fe:00:01");
383 // Make sure we got a node
384 Assert.assertTrue(ofFromStr != null);
386 // Now the converted node need to be the same of the
388 Assert.assertTrue(of.equals(ofFromStr));
389 } catch (ConstructionException e) {
390 // If we reach this point the exception was raised
391 // which is not expected
392 Assert.assertTrue(false);
397 public void testExtensibleNode() {
399 Assert.assertTrue(Node.NodeIDType.registerIDType("FOO", Integer.class));
401 // Trying to re-register the node must fail
402 Assert.assertFalse(Node.NodeIDType.registerIDType("FOO",
405 Node n = new Node("FOO", new Integer(0xCAFE));
407 System.out.println("Got Extended node:" + n);
408 } catch (ConstructionException e) {
409 // Got an unexpected exception
410 Assert.assertTrue(false);
413 // Now unregister the type and make sure the node doesn't get
415 Node.NodeIDType.unRegisterIDType("FOO");
417 @SuppressWarnings("unused")
418 Node n = new Node("FOO", new Integer(0xCAFE));
420 // If we reach here, something didn't go fine, an
421 // exception should have been raised
422 Assert.assertTrue(false);
423 } catch (ConstructionException e) {
424 // Got an expected exception, do nothing!
427 Assert.assertTrue(true);