Checkstyle enforcer
[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 import org.opendaylight.controller.sal.core.ConstructionException;
23 import org.opendaylight.controller.sal.core.Node;
24
25 public class NodeTest {
26     @Test
27     public void testNodeOpenFlowOfWrongType() {
28         try {
29             Node of1 = new Node(Node.NodeIDType.OPENFLOW, new String(
30                     "0xDEADBEEFCAFE0001L"));
31
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
37             // and so test passed
38             System.out.println("Got exception as expected!:" + e);
39             Assert.assertTrue(true);
40         }
41     }
42
43     @Test
44     public void testNodeONEPKOfWrongType() {
45         try {
46             Node onepk1 = new Node(Node.NodeIDType.ONEPK, new Long(
47                     0xDEADBEEFCAFE0001L));
48
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
54             // and so test passed
55             System.out.println("Got exception as expected!:" + e);
56             Assert.assertTrue(true);
57         }
58     }
59
60     @Test
61     public void testNodePCEPOfWrongType() {
62         try {
63             Node pcep1 = new Node(Node.NodeIDType.PCEP, new Long(
64                     0xDEADBEEFCAFE0001L));
65
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
71             // and so test passed
72             System.out.println("Got exception as expected!:" + e);
73             Assert.assertTrue(true);
74         }
75     }
76
77     @Test
78     public void testNodeOpenFlowOfCorrectType() {
79         try {
80             Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
81                     0xDEADBEEFCAFE0001L));
82
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);
91         }
92     }
93
94     @Test
95     public void testNodeONEPKOfCorrectType() {
96         try {
97             Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
98                     "0xDEADBEEFCAFE0001L"));
99
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);
108         }
109     }
110
111     @Test
112     public void testNodePCEPOfCorrectType() {
113         try {
114             Node pcep1 = new Node(Node.NodeIDType.PCEP, new UUID(0L, 0L));
115
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);
124         }
125     }
126
127     @Test
128     public void testTwoOpenFlowNodeEquals() {
129         try {
130             Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
131                     0xDEADBEEFCAFE0001L));
132             Node of2 = new Node(Node.NodeIDType.OPENFLOW, new Long(
133                     0xDEADBEEFCAFE0001L));
134
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);
140         }
141     }
142
143     @Test
144     public void testTwoONEPKNodeEquals() {
145         try {
146             Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
147                     "0xDEADBEEFCAFE0001L"));
148             Node onepk2 = new Node(Node.NodeIDType.ONEPK, new String(
149                     "0xDEADBEEFCAFE0001L"));
150
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);
156         }
157     }
158
159     @Test
160     public void testTwoPCEPNodeEquals() {
161         try {
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));
166
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);
172         }
173     }
174
175     @Test
176     public void testTwoOpenFlowNodeDifferents() {
177         try {
178             Node of1 = new Node(Node.NodeIDType.OPENFLOW, new Long(
179                     0xDEADBEEFCAFE0001L));
180             Node of2 = new Node(Node.NodeIDType.OPENFLOW, new Long(
181                     0xDEADBEEFCAFE0002L));
182
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);
188         }
189     }
190
191     @Test
192     public void testTwoONEPKNodeDifferents() {
193         try {
194             Node onepk1 = new Node(Node.NodeIDType.ONEPK, new String(
195                     "0xDEADBEEFCAFE0001L"));
196             Node onepk2 = new Node(Node.NodeIDType.ONEPK, new String(
197                     "0xDEADBEEFCAFE0002L"));
198
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);
204         }
205     }
206
207     @Test
208     public void testTwoPCEPNodeDifferents() {
209         try {
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));
214
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);
220         }
221     }
222
223     @Test
224     public void testToStringConversionAndBack() {
225         // Test PCEP
226         try {
227             Node pcep = new Node(Node.NodeIDType.PCEP, new UUID(
228                     0xDEADBEEFCAFE0001L, 0L));
229
230             String pcepToStr = pcep.toString();
231             System.out.println("Got String from PCEP=(" + pcepToStr + ")");
232
233             Node pcepFromStr = Node.fromString(pcepToStr);
234
235             // Make sure we got a node
236             Assert.assertTrue(pcepFromStr != null);
237
238             // Now the converted node need to be the same of the
239             // original one
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);
245         }
246
247         // Test ONEPK
248         try {
249             Node onepk = new Node(Node.NodeIDType.ONEPK, new String(
250                     "0xDEADBEEFCAFE0001L"));
251
252             String onepkToStr = onepk.toString();
253             System.out.println("Got String from ONEPK=(" + onepkToStr + ")");
254
255             Node onepkFromStr = Node.fromString(onepkToStr);
256
257             // Make sure we got a node
258             Assert.assertTrue(onepkFromStr != null);
259
260             // Now the converted node need to be the same of the
261             // original one
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);
267         }
268
269         // Test OPENFLOW short string
270         try {
271             Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(0x10L));
272
273             String ofToStr = of.toString();
274             System.out.println("Got String from OPENFLOW=(" + ofToStr + ")");
275
276             Node ofFromStr = Node.fromString(ofToStr);
277
278             // Make sure we got a node
279             Assert.assertTrue(ofFromStr != null);
280
281             // Now the converted node need to be the same of the
282             // original one
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);
288         }
289
290         // Test OPENFLOW longer string
291         try {
292             Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(
293                     0xDEADBEEFCAFE0001L));
294
295             String ofToStr = of.toString();
296             System.out.println("Got String from OPENFLOW=(" + ofToStr + ")");
297
298             Node ofFromStr = Node.fromString(ofToStr);
299
300             // Make sure we got a node
301             Assert.assertTrue(ofFromStr != null);
302
303             // Now the converted node need to be the same of the
304             // original one
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);
310         }
311     }
312
313     @Test
314     public void testToStringConversionForOpenFlow() {
315         // Test OPENFLOW longer string
316         try {
317             Node of = new Node(Node.NodeIDType.OPENFLOW, new Long(
318                     0xDEADBEEFCAFE0001L));
319             Node ofFromStr = null;
320
321             // Decimal value for deadbeefcafe0001
322             ofFromStr = Node.fromString("-2401053089206501375");
323
324             // Make sure we got a node
325             Assert.assertTrue(ofFromStr != null);
326
327             // Now the converted node need to be the same of the
328             // original one
329             Assert.assertTrue(of.equals(ofFromStr));
330
331             ofFromStr = Node.fromString("deadbeefcafe0001");
332
333             // Make sure we got a node
334             Assert.assertTrue(ofFromStr != null);
335
336             // Now the converted node need to be the same of the
337             // original one
338             Assert.assertTrue(of.equals(ofFromStr));
339
340             ofFromStr = Node.fromString("DEADBEEFCAFE0001");
341
342             // Make sure we got a node
343             Assert.assertTrue(ofFromStr != null);
344
345             // Now the converted node need to be the same of the
346             // original one
347             Assert.assertTrue(of.equals(ofFromStr));
348
349             // Now the converted node need to be the same of the
350             // original one
351             Assert.assertTrue(of.equals(ofFromStr));
352
353             ofFromStr = Node.fromString("0xdeadbeefcafe0001");
354
355             // Make sure we got a node
356             Assert.assertTrue(ofFromStr != null);
357
358             // Now the converted node need to be the same of the
359             // original one
360             Assert.assertTrue(of.equals(ofFromStr));
361
362             ofFromStr = Node.fromString("0xDEADBEEFCAFE0001");
363
364             // Make sure we got a node
365             Assert.assertTrue(ofFromStr != null);
366
367             // Now the converted node need to be the same of the
368             // original one
369             Assert.assertTrue(of.equals(ofFromStr));
370
371             ofFromStr = Node.fromString("DE:AD:BE:EF:CA:FE:00:01");
372
373             // Make sure we got a node
374             Assert.assertTrue(ofFromStr != null);
375
376             // Now the converted node need to be the same of the
377             // original one
378             Assert.assertTrue(of.equals(ofFromStr));
379
380             ofFromStr = Node.fromString("de:ad:be:ef:ca:fe:00:01");
381
382             // Make sure we got a node
383             Assert.assertTrue(ofFromStr != null);
384
385             // Now the converted node need to be the same of the
386             // original one
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);
392         }
393     }
394
395     @Test
396     public void testExtensibleNode() {
397         // Add a new ID type
398         Assert.assertTrue(Node.NodeIDType.registerIDType("FOO", Integer.class));
399
400         // Trying to re-register the node must fail
401         Assert.assertFalse(Node.NodeIDType.registerIDType("FOO",
402                                                           Integer.class));
403         try {
404             Node n = new Node("FOO", new Integer(0xCAFE));
405
406             System.out.println("Got Extended node:" + n);
407         } catch (ConstructionException e) {
408             // Got an unexpected exception
409             Assert.assertTrue(false);
410         }
411
412         // Now unregister the type and make sure the node doesn't get
413         // created
414         Node.NodeIDType.unRegisterIDType("FOO");
415         try {
416             Node n = new Node("FOO", new Integer(0xCAFE));
417
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!
423         }
424
425         Assert.assertTrue(true);
426     }
427 }