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