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