551769a55790cce97759f5a9258e217b89e71095
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / core / NodeTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 /**
11  * @file   NodeTest.java
12  *
13  * @brief  Unit Tests for Node element
14  *
15  * Unit Tests for Node element
16  */
17 package org.opendaylight.controller.sal.core;
18
19 import java.util.UUID;
20 import org.junit.Assert;
21 import org.junit.Test;
22
23 public class NodeTest {
24     @Test
25     public void testNodeOpenFlowOfWrongType() {
26         try {
27             Node of1 = new Node(Node.NodeIDType.OPENFLOW, new String(
28                     "0xDEADBEEFCAFE0001L"));
29
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
35             // and so test passed
36             System.out.println("Got exception as expected!:" + e);
37             Assert.assertTrue(true);
38         }
39     }
40
41     @Test
42     public void testNodeONEPKOfWrongType() {
43         try {
44             Node onepk1 = new Node(Node.NodeIDType.ONEPK, new Long(
45                     0xDEADBEEFCAFE0001L));
46
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
52             // and so test passed
53             System.out.println("Got exception as expected!:" + e);
54             Assert.assertTrue(true);
55         }
56     }
57
58     @Test
59     public void testNodePCEPOfWrongType() {
60         try {
61             Node pcep1 = new Node(Node.NodeIDType.PCEP, new Long(
62                     0xDEADBEEFCAFE0001L));
63
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
69             // and so test passed
70             System.out.println("Got exception as expected!:" + e);
71             Assert.assertTrue(true);
72         }
73     }
74
75     @Test
76     public void testNodeOpenFlowOfCorrectType() {
77         try {
78             Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
79                     0xDEADBEEFCAFE0001L));
80
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);
89         }
90     }
91
92     @Test
93     public void testNodeONEPKOfCorrectType() {
94         try {
95             Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
96                     "0xDEADBEEFCAFE0001L"));
97
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);
106         }
107     }
108
109     @Test
110     public void testNodePCEPOfCorrectType() {
111         try {
112             Node pcep1 = new Node(Node.NodeIDType.PCEP, new UUID(0L, 0L));
113
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);
122         }
123     }
124
125     @Test
126     public void testTwoOpenFlowNodeEquals() {
127         try {
128             Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
129                     0xDEADBEEFCAFE0001L));
130             Node of2 = new Node(Node.NodeIDType.OPENFLOW, new Long(
131                     0xDEADBEEFCAFE0001L));
132
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);
138         }
139     }
140
141     @Test
142     public void testTwoONEPKNodeEquals() {
143         try {
144             Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
145                     "0xDEADBEEFCAFE0001L"));
146             Node onepk2 = new Node(Node.NodeIDType.ONEPK, new String(
147                     "0xDEADBEEFCAFE0001L"));
148
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);
154         }
155     }
156
157     @Test
158     public void testTwoPCEPNodeEquals() {
159         try {
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));
164
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);
170         }
171     }
172
173     @Test
174     public void testTwoOpenFlowNodeDifferents() {
175         try {
176             Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
177                     0xDEADBEEFCAFE0001L));
178             Node of2 = new Node(Node.NodeIDType.OPENFLOW, new Long(
179                     0xDEADBEEFCAFE0002L));
180
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);
186         }
187     }
188
189     @Test
190     public void testTwoONEPKNodeDifferents() {
191         try {
192             Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
193                     "0xDEADBEEFCAFE0001L"));
194             Node onepk2 = new Node(Node.NodeIDType.ONEPK, new String(
195                     "0xDEADBEEFCAFE0002L"));
196
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);
202         }
203     }
204
205     @Test
206     public void testTwoPCEPNodeDifferents() {
207         try {
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));
212
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);
218         }
219     }
220
221     @Test
222     public void testToStringConversionAndBack() {
223         // Test PCEP
224         try {
225             Node pcep = new Node(Node.NodeIDType.PCEP, new UUID(
226                     0xDEADBEEFCAFE0001L, 0L));
227
228             String pcepToStr = pcep.toString();
229             System.out.println("Got String from PCEP=(" + pcepToStr + ")");
230
231             Node pcepFromStr = Node.fromString(pcepToStr);
232
233             // Make sure we got a node
234             Assert.assertTrue(pcepFromStr != null);
235
236             // Now the converted node need to be the same of the
237             // original one
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);
243         }
244
245         // Test ONEPK
246         try {
247             Node onepk = new Node(Node.NodeIDType.ONEPK, new String(
248                     "0xDEADBEEFCAFE0001L"));
249
250             String onepkToStr = onepk.toString();
251             System.out.println("Got String from ONEPK=(" + onepkToStr + ")");
252
253             Node onepkFromStr = Node.fromString(onepkToStr);
254
255             // Make sure we got a node
256             Assert.assertTrue(onepkFromStr != null);
257
258             // Now the converted node need to be the same of the
259             // original one
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);
265         }
266
267         // Test OPENFLOW short string
268         try {
269             Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(0x10L));
270
271             String ofToStr = of.toString();
272             System.out.println("Got String from OPENFLOW=(" + ofToStr + ")");
273
274             Node ofFromStr = Node.fromString(ofToStr);
275
276             // Make sure we got a node
277             Assert.assertTrue(ofFromStr != null);
278
279             // Now the converted node need to be the same of the
280             // original one
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);
286         }
287
288         // Test OPENFLOW longer string
289         try {
290             Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(
291                     0xDEADBEEFCAFE0001L));
292
293             String ofToStr = of.toString();
294             System.out.println("Got String from OPENFLOW=(" + ofToStr + ")");
295
296             Node ofFromStr = Node.fromString(ofToStr);
297
298             // Make sure we got a node
299             Assert.assertTrue(ofFromStr != null);
300
301             // Now the converted node need to be the same of the
302             // original one
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);
308         }
309     }
310
311     @Test
312     public void testToStringConversionForOpenFlow() {
313         // Test OPENFLOW longer string
314         try {
315             Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(
316                     0xDEADBEEFCAFE0001L));
317             Node ofFromStr = null;
318
319             // Decimal value for deadbeefcafe0001
320             ofFromStr = Node.fromString("-2401053089206501375");
321
322             // Make sure we got a node
323             Assert.assertTrue(ofFromStr != null);
324
325             // Now the converted node need to be the same of the
326             // original one
327             Assert.assertTrue(of.equals(ofFromStr));
328
329             ofFromStr = Node.fromString("deadbeefcafe0001");
330
331             // Make sure we got a node
332             Assert.assertTrue(ofFromStr != null);
333
334             // Now the converted node need to be the same of the
335             // original one
336             Assert.assertTrue(of.equals(ofFromStr));
337
338             ofFromStr = Node.fromString("DEADBEEFCAFE0001");
339
340             // Make sure we got a node
341             Assert.assertTrue(ofFromStr != null);
342
343             // Now the converted node need to be the same of the
344             // original one
345             Assert.assertTrue(of.equals(ofFromStr));
346
347             // Now the converted node need to be the same of the
348             // original one
349             Assert.assertTrue(of.equals(ofFromStr));
350
351             ofFromStr = Node.fromString("0xdeadbeefcafe0001");
352
353             // Make sure we got a node
354             Assert.assertTrue(ofFromStr != null);
355
356             // Now the converted node need to be the same of the
357             // original one
358             Assert.assertTrue(of.equals(ofFromStr));
359
360             ofFromStr = Node.fromString("0xDEADBEEFCAFE0001");
361
362             // Make sure we got a node
363             Assert.assertTrue(ofFromStr != null);
364
365             // Now the converted node need to be the same of the
366             // original one
367             Assert.assertTrue(of.equals(ofFromStr));
368
369             ofFromStr = Node.fromString("DE:AD:BE:EF:CA:FE:00:01");
370
371             // Make sure we got a node
372             Assert.assertTrue(ofFromStr != null);
373
374             // Now the converted node need to be the same of the
375             // original one
376             Assert.assertTrue(of.equals(ofFromStr));
377
378             ofFromStr = Node.fromString("de:ad:be:ef:ca:fe:00:01");
379
380             // Make sure we got a node
381             Assert.assertTrue(ofFromStr != null);
382
383             // Now the converted node need to be the same of the
384             // original one
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);
390         }
391     }
392
393     @Test
394     public void testExtensibleNode() {
395         // Add a new ID type
396         Assert.assertTrue(Node.NodeIDType.registerIDType("FOO", Integer.class));
397
398         // Trying to re-register the node must fail
399         Assert.assertFalse(Node.NodeIDType.registerIDType("FOO",
400                                                           Integer.class));
401         try {
402             Node n = new Node("FOO", new Integer(0xCAFE));
403
404             System.out.println("Got Extended node:" + n);
405         } catch (ConstructionException e) {
406             // Got an unexpected exception
407             Assert.assertTrue(false);
408         }
409
410         // Now unregister the type and make sure the node doesn't get
411         // created
412         Node.NodeIDType.unRegisterIDType("FOO");
413         try {
414             Node n = new Node("FOO", new Integer(0xCAFE));
415
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!
421         }
422
423         Assert.assertTrue(true);
424     }
425 }