Move adsal into its own subdirectory.
[controller.git] / opendaylight / adsal / sal / api / src / test / java / org / opendaylight / controller / sal / core / PathTest.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   PathTest.java
12  *
13  * @brief  Unit Tests for Path element
14  *
15  * Unit Tests for Path element
16  */
17 package org.opendaylight.controller.sal.core;
18
19 import java.util.LinkedList;
20 import java.util.List;
21 import java.util.Arrays;
22 import org.junit.Assert;
23 import org.junit.Test;
24
25 public class PathTest {
26     @Test
27     public void testPathValid() {
28         List<Edge> edges = null;
29         try {
30             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
31             NodeConnector c0 = new NodeConnector(
32                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
33                             (short) 0x4), n0);
34
35             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
36             NodeConnector c1 = new NodeConnector(
37                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
38                             (short) 0x1), n1);
39             NodeConnector c2 = new NodeConnector(
40                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
41                     new Short((short) 0xCAFE), n1);
42
43             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
44             NodeConnector c3 = new NodeConnector(
45                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
46                     new String("towardOF1"), n2);
47             NodeConnector c4 = new NodeConnector(
48                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
49                             "Gi1/0/1"), n2);
50
51             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
52             NodeConnector c5 = new NodeConnector(
53                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
54                             "Gi1/0/1"), n3);
55
56             Edge e0 = new Edge(c0, c1);
57             Edge e1 = new Edge(c1, c2);
58             Edge e2 = new Edge(c2, c3);
59             Edge e3 = new Edge(c3, c4);
60             Edge e4 = new Edge(c4, c5);
61             // actually need a LinkedList because we're going to .remove()
62             edges = new LinkedList<Edge>(Arrays.asList(e0, e1, e2, e3, e4));
63         } catch (ConstructionException e) {
64             // Exception is NOT expected if raised test will fail
65             Assert.assertTrue(false);
66         }
67
68         try {
69             @SuppressWarnings("unused")
70             Path res = new Path(edges);
71         } catch (ConstructionException e) {
72             // Exception is NOT expected if raised test will fail
73             Assert.assertTrue(false);
74         }
75
76         // Now lets disconnect on edge so to create a disconnected
77         // path, the constructor should catch that and should not
78         // create the path
79         edges.remove(2);
80
81         try {
82             @SuppressWarnings("unused")
83             Path res = new Path(edges);
84             // Exception is expected if not raised test will fail
85             Assert.assertTrue(false);
86         } catch (ConstructionException e) {
87         }
88     }
89
90     @Test
91     public void testPathComparison() {
92         List<Edge> edges1 = null;
93         Path path1 = null;
94         List<Edge> edges2 = null;
95         Path path2 = null;
96         List<Edge> edges3 = null;
97         Path path3 = null;
98
99         try {
100             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
101             NodeConnector c0 = new NodeConnector(
102                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
103                             (short) 0x4), n0);
104
105             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
106             NodeConnector c1 = new NodeConnector(
107                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
108                             (short) 0x1), n1);
109             NodeConnector c2 = new NodeConnector(
110                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
111                     new Short((short) 0xCAFE), n1);
112
113             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
114             NodeConnector c3 = new NodeConnector(
115                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
116                     new String("towardOF1"), n2);
117             NodeConnector c4 = new NodeConnector(
118                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
119                             "Gi1/0/1"), n2);
120
121             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
122             NodeConnector c5 = new NodeConnector(
123                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
124                             "Gi1/0/1"), n3);
125
126             Edge e0 = new Edge(c0, c1);
127             Edge e1 = new Edge(c1, c2);
128             Edge e2 = new Edge(c2, c3);
129             Edge e3 = new Edge(c3, c4);
130             Edge e4 = new Edge(c4, c5);
131             edges1 = Arrays.asList(e0, e1, e2, e3, e4);
132         } catch (ConstructionException e) {
133             // Exception is NOT expected if raised test will fail
134             Assert.assertTrue(false);
135         }
136
137         try {
138             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
139             NodeConnector c0 = new NodeConnector(
140                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
141                             (short) 0x4), n0);
142
143             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
144             NodeConnector c1 = new NodeConnector(
145                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
146                             (short) 0x1), n1);
147             NodeConnector c2 = new NodeConnector(
148                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
149                     new Short((short) 0xCAFE), n1);
150
151             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
152             NodeConnector c3 = new NodeConnector(
153                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
154                     new String("towardOF1"), n2);
155             NodeConnector c4 = new NodeConnector(
156                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
157                             "Gi1/0/1"), n2);
158
159             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
160             NodeConnector c5 = new NodeConnector(
161                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
162                             "Gi1/0/1"), n3);
163
164             Edge e0 = new Edge(c0, c1);
165             Edge e1 = new Edge(c1, c2);
166             Edge e2 = new Edge(c2, c3);
167             Edge e3 = new Edge(c3, c4);
168             Edge e4 = new Edge(c4, c5);
169             edges2 = Arrays.asList(e0, e1, e2, e3, e4);
170         } catch (ConstructionException e) {
171             // Exception is NOT expected if raised test will fail
172             Assert.assertTrue(false);
173         }
174
175         try {
176             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
177             NodeConnector c0 = new NodeConnector(
178                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
179                             (short) 0x5), n0);
180
181             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
182             NodeConnector c1 = new NodeConnector(
183                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
184                             (short) 0x1), n1);
185             NodeConnector c2 = new NodeConnector(
186                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
187                     new Short((short) 0xCAFE), n1);
188
189             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
190             NodeConnector c3 = new NodeConnector(
191                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
192                     new String("towardOF1"), n2);
193             NodeConnector c4 = new NodeConnector(
194                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
195                             "Gi1/0/1"), n2);
196
197             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
198             NodeConnector c5 = new NodeConnector(
199                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
200                             "Gi1/0/1"), n3);
201
202             Edge e0 = new Edge(c0, c1);
203             Edge e1 = new Edge(c1, c2);
204             Edge e2 = new Edge(c2, c3);
205             Edge e3 = new Edge(c3, c4);
206             Edge e4 = new Edge(c4, c5);
207             edges3 = Arrays.asList(e0, e1, e2, e3, e4);
208         } catch (ConstructionException e) {
209             // Exception is NOT expected if raised test will fail
210             Assert.assertTrue(false);
211         }
212
213         try {
214             path1 = new Path(edges1);
215             path2 = new Path(edges2);
216             path3 = new Path(edges3);
217
218             System.out.println("Path1: " + path1);
219             System.out.println("Path2: " + path2);
220             System.out.println("Path3: " + path3);
221
222             // Make sure the path are equals
223             Assert.assertTrue(path1.equals(path2));
224
225             // Make sure the path are marked as different
226             Assert.assertTrue(!path1.equals(path3));
227         } catch (ConstructionException e) {
228             // Exception is NOT expected if raised test will fail
229             Assert.assertTrue(false);
230         }
231     }
232
233     @Test
234     public void testPathEmpty() {
235         try {
236             @SuppressWarnings("unused")
237             Path path = new Path(new LinkedList<Edge>());
238             // Exception is expected if not raised test will fail
239             Assert.assertTrue(false);
240         } catch (ConstructionException e) {
241         }
242     }
243
244     @Test
245     public void testPathOneElement() {
246         try {
247             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
248             NodeConnector c0 = new NodeConnector(
249                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
250                             (short) 0x5), n0);
251
252             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
253             NodeConnector c1 = new NodeConnector(
254                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
255                             (short) 0x1), n1);
256
257             Edge e0 = new Edge(c0, c1);
258
259             @SuppressWarnings("unused")
260             Path path = new Path(Arrays.asList(e0));
261         } catch (ConstructionException e) {
262             // Exception is NOT expected if raised test will fail
263             Assert.assertTrue(false);
264         }
265     }
266
267     @Test
268     public void testPathGetNodes() {
269         // Test on >2 edges paths
270         try {
271             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
272             NodeConnector c0 = new NodeConnector(
273                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
274                             (short) 0x4), n0);
275
276             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
277             NodeConnector c1 = new NodeConnector(
278                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
279                             (short) 0x1), n1);
280             NodeConnector c2 = new NodeConnector(
281                     NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
282                     new Short((short) 0xCAFE), n1);
283
284             Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
285             NodeConnector c3 = new NodeConnector(
286                     NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
287                     new String("towardOF1"), n2);
288             NodeConnector c4 = new NodeConnector(
289                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
290                             "Gi1/0/1"), n2);
291
292             Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
293             NodeConnector c5 = new NodeConnector(
294                     NodeConnector.NodeConnectorIDType.ONEPK, new String(
295                             "Gi1/0/1"), n3);
296
297             Edge e0 = new Edge(c0, c1);
298             Edge e1 = new Edge(c1, c2);
299             Edge e2 = new Edge(c2, c3);
300             Edge e3 = new Edge(c3, c4);
301             Edge e4 = new Edge(c4, c5);
302             List<Edge> edges = Arrays.asList(e0, e1, e2, e3, e4);
303             Path path = new Path(edges);
304
305             // Test start node
306             Assert.assertTrue(path.getStartNode().equals(n0));
307
308             // Test end node
309             Assert.assertTrue(path.getEndNode().equals(n3));
310         } catch (ConstructionException e) {
311             // Exception is NOT expected if raised test will fail
312             Assert.assertTrue(false);
313         }
314
315         // Test on 1 edge path
316         try {
317             Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
318             NodeConnector c0 = new NodeConnector(
319                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
320                             (short) 0x4), n0);
321
322             Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
323             NodeConnector c1 = new NodeConnector(
324                     NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
325                             (short) 0x1), n1);
326
327             Edge e0 = new Edge(c0, c1);
328             List<Edge> edges = Arrays.asList(e0);
329             Path path = new Path(edges);
330
331             // Test start node
332             Assert.assertTrue(path.getStartNode().equals(n0));
333
334             // Test end node
335             Assert.assertTrue(path.getEndNode().equals(n1));
336         } catch (ConstructionException e) {
337             // Exception is NOT expected if raised test will fail
338             Assert.assertTrue(false);
339         }
340     }
341 }