3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
13 * @brief Unit Tests for Path element
15 * Unit Tests for Path element
17 package org.opendaylight.controller.sal.core;
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;
25 public class PathTest {
27 public void testPathValid() {
28 List<Edge> edges = null;
30 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
31 NodeConnector c0 = new NodeConnector(
32 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
35 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
36 NodeConnector c1 = new NodeConnector(
37 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
39 NodeConnector c2 = new NodeConnector(
40 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
41 new Short((short) 0xCAFE), n1);
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(
51 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
52 NodeConnector c5 = new NodeConnector(
53 NodeConnector.NodeConnectorIDType.ONEPK, new String(
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);
69 Path res = new Path(edges);
70 } catch (ConstructionException e) {
71 // Exception is NOT expected if raised test will fail
72 Assert.assertTrue(false);
75 // Now lets disconnect on edge so to create a disconnected
76 // path, the constructor should catch that and should not
81 Path res = new Path(edges);
82 // Exception is expected if not raised test will fail
83 Assert.assertTrue(false);
84 } catch (ConstructionException e) {
89 public void testPathComparison() {
90 List<Edge> edges1 = null;
92 List<Edge> edges2 = null;
94 List<Edge> edges3 = null;
98 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
99 NodeConnector c0 = new NodeConnector(
100 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
103 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
104 NodeConnector c1 = new NodeConnector(
105 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
107 NodeConnector c2 = new NodeConnector(
108 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
109 new Short((short) 0xCAFE), n1);
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(
119 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
120 NodeConnector c5 = new NodeConnector(
121 NodeConnector.NodeConnectorIDType.ONEPK, new String(
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);
136 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
137 NodeConnector c0 = new NodeConnector(
138 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
141 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
142 NodeConnector c1 = new NodeConnector(
143 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
145 NodeConnector c2 = new NodeConnector(
146 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
147 new Short((short) 0xCAFE), n1);
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(
157 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
158 NodeConnector c5 = new NodeConnector(
159 NodeConnector.NodeConnectorIDType.ONEPK, new String(
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);
174 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
175 NodeConnector c0 = new NodeConnector(
176 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
179 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
180 NodeConnector c1 = new NodeConnector(
181 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
183 NodeConnector c2 = new NodeConnector(
184 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
185 new Short((short) 0xCAFE), n1);
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(
195 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
196 NodeConnector c5 = new NodeConnector(
197 NodeConnector.NodeConnectorIDType.ONEPK, new String(
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);
212 path1 = new Path(edges1);
213 path2 = new Path(edges2);
214 path3 = new Path(edges3);
216 System.out.println("Path1: " + path1);
217 System.out.println("Path2: " + path2);
218 System.out.println("Path3: " + path3);
220 // Make sure the path are equals
221 Assert.assertTrue(path1.equals(path2));
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);
232 public void testPathEmpty() {
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) {
242 public void testPathOneElement() {
244 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
245 NodeConnector c0 = new NodeConnector(
246 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
249 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
250 NodeConnector c1 = new NodeConnector(
251 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
254 Edge e0 = new Edge(c0, c1);
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);
264 public void testPathGetNodes() {
265 // Test on >2 edges paths
267 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
268 NodeConnector c0 = new NodeConnector(
269 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
272 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
273 NodeConnector c1 = new NodeConnector(
274 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
276 NodeConnector c2 = new NodeConnector(
277 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
278 new Short((short) 0xCAFE), n1);
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(
288 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
289 NodeConnector c5 = new NodeConnector(
290 NodeConnector.NodeConnectorIDType.ONEPK, new String(
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);
302 Assert.assertTrue(path.getStartNode().equals(n0));
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);
311 // Test on 1 edge path
313 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
314 NodeConnector c0 = new NodeConnector(
315 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
318 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
319 NodeConnector c1 = new NodeConnector(
320 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
323 Edge e0 = new Edge(c0, c1);
324 List<Edge> edges = Arrays.asList(e0);
325 Path path = new Path(edges);
328 Assert.assertTrue(path.getStartNode().equals(n0));
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);