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 @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);
76 // Now lets disconnect on edge so to create a disconnected
77 // path, the constructor should catch that and should not
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) {
91 public void testPathComparison() {
92 List<Edge> edges1 = null;
94 List<Edge> edges2 = null;
96 List<Edge> edges3 = null;
100 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
101 NodeConnector c0 = new NodeConnector(
102 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
105 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
106 NodeConnector c1 = new NodeConnector(
107 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
109 NodeConnector c2 = new NodeConnector(
110 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
111 new Short((short) 0xCAFE), n1);
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(
121 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
122 NodeConnector c5 = new NodeConnector(
123 NodeConnector.NodeConnectorIDType.ONEPK, new String(
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);
138 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
139 NodeConnector c0 = new NodeConnector(
140 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
143 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
144 NodeConnector c1 = new NodeConnector(
145 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
147 NodeConnector c2 = new NodeConnector(
148 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
149 new Short((short) 0xCAFE), n1);
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(
159 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
160 NodeConnector c5 = new NodeConnector(
161 NodeConnector.NodeConnectorIDType.ONEPK, new String(
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);
176 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
177 NodeConnector c0 = new NodeConnector(
178 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
181 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
182 NodeConnector c1 = new NodeConnector(
183 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
185 NodeConnector c2 = new NodeConnector(
186 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
187 new Short((short) 0xCAFE), n1);
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(
197 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
198 NodeConnector c5 = new NodeConnector(
199 NodeConnector.NodeConnectorIDType.ONEPK, new String(
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);
214 path1 = new Path(edges1);
215 path2 = new Path(edges2);
216 path3 = new Path(edges3);
218 System.out.println("Path1: " + path1);
219 System.out.println("Path2: " + path2);
220 System.out.println("Path3: " + path3);
222 // Make sure the path are equals
223 Assert.assertTrue(path1.equals(path2));
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);
234 public void testPathEmpty() {
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) {
245 public void testPathOneElement() {
247 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
248 NodeConnector c0 = new NodeConnector(
249 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
252 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
253 NodeConnector c1 = new NodeConnector(
254 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
257 Edge e0 = new Edge(c0, c1);
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);
268 public void testPathGetNodes() {
269 // Test on >2 edges paths
271 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
272 NodeConnector c0 = new NodeConnector(
273 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
276 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
277 NodeConnector c1 = new NodeConnector(
278 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
280 NodeConnector c2 = new NodeConnector(
281 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
282 new Short((short) 0xCAFE), n1);
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(
292 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
293 NodeConnector c5 = new NodeConnector(
294 NodeConnector.NodeConnectorIDType.ONEPK, new String(
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);
306 Assert.assertTrue(path.getStartNode().equals(n0));
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);
315 // Test on 1 edge path
317 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
318 NodeConnector c0 = new NodeConnector(
319 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
322 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
323 NodeConnector c1 = new NodeConnector(
324 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
327 Edge e0 = new Edge(c0, c1);
328 List<Edge> edges = Arrays.asList(e0);
329 Path path = new Path(edges);
332 Assert.assertTrue(path.getStartNode().equals(n0));
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);