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;
24 import org.opendaylight.controller.sal.core.ConstructionException;
25 import org.opendaylight.controller.sal.core.Edge;
26 import org.opendaylight.controller.sal.core.Node;
27 import org.opendaylight.controller.sal.core.NodeConnector;
28 import org.opendaylight.controller.sal.core.Path;
30 public class PathTest {
32 public void testPathValid() {
33 List<Edge> edges = null;
35 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
36 NodeConnector c0 = new NodeConnector(
37 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
40 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
41 NodeConnector c1 = new NodeConnector(
42 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
44 NodeConnector c2 = new NodeConnector(
45 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
46 new Short((short) 0xCAFE), n1);
48 Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
49 NodeConnector c3 = new NodeConnector(
50 NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
51 new String("towardOF1"), n2);
52 NodeConnector c4 = new NodeConnector(
53 NodeConnector.NodeConnectorIDType.ONEPK, new String(
56 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
57 NodeConnector c5 = new NodeConnector(
58 NodeConnector.NodeConnectorIDType.ONEPK, new String(
61 Edge e0 = new Edge(c0, c1);
62 Edge e1 = new Edge(c1, c2);
63 Edge e2 = new Edge(c2, c3);
64 Edge e3 = new Edge(c3, c4);
65 Edge e4 = new Edge(c4, c5);
66 // actually need a LinkedList because we're going to .remove()
67 edges = new LinkedList<Edge>(Arrays.asList(e0, e1, e2, e3, e4));
68 } catch (ConstructionException e) {
69 // Exception is NOT expected if raised test will fail
70 Assert.assertTrue(false);
74 Path res = new Path(edges);
75 } catch (ConstructionException e) {
76 // Exception is NOT expected if raised test will fail
77 Assert.assertTrue(false);
80 // Now lets disconnect on edge so to create a disconnected
81 // path, the constructor should catch that and should not
86 Path res = new Path(edges);
87 // Exception is expected if not raised test will fail
88 Assert.assertTrue(false);
89 } catch (ConstructionException e) {
94 public void testPathComparison() {
95 List<Edge> edges1 = null;
97 List<Edge> edges2 = null;
99 List<Edge> edges3 = null;
103 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
104 NodeConnector c0 = new NodeConnector(
105 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
108 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
109 NodeConnector c1 = new NodeConnector(
110 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
112 NodeConnector c2 = new NodeConnector(
113 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
114 new Short((short) 0xCAFE), n1);
116 Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
117 NodeConnector c3 = new NodeConnector(
118 NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
119 new String("towardOF1"), n2);
120 NodeConnector c4 = new NodeConnector(
121 NodeConnector.NodeConnectorIDType.ONEPK, new String(
124 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
125 NodeConnector c5 = new NodeConnector(
126 NodeConnector.NodeConnectorIDType.ONEPK, new String(
129 Edge e0 = new Edge(c0, c1);
130 Edge e1 = new Edge(c1, c2);
131 Edge e2 = new Edge(c2, c3);
132 Edge e3 = new Edge(c3, c4);
133 Edge e4 = new Edge(c4, c5);
134 edges1 = Arrays.asList(e0, e1, e2, e3, e4);
135 } catch (ConstructionException e) {
136 // Exception is NOT expected if raised test will fail
137 Assert.assertTrue(false);
141 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
142 NodeConnector c0 = new NodeConnector(
143 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
146 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
147 NodeConnector c1 = new NodeConnector(
148 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
150 NodeConnector c2 = new NodeConnector(
151 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
152 new Short((short) 0xCAFE), n1);
154 Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
155 NodeConnector c3 = new NodeConnector(
156 NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
157 new String("towardOF1"), n2);
158 NodeConnector c4 = new NodeConnector(
159 NodeConnector.NodeConnectorIDType.ONEPK, new String(
162 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
163 NodeConnector c5 = new NodeConnector(
164 NodeConnector.NodeConnectorIDType.ONEPK, new String(
167 Edge e0 = new Edge(c0, c1);
168 Edge e1 = new Edge(c1, c2);
169 Edge e2 = new Edge(c2, c3);
170 Edge e3 = new Edge(c3, c4);
171 Edge e4 = new Edge(c4, c5);
172 edges2 = Arrays.asList(e0, e1, e2, e3, e4);
173 } catch (ConstructionException e) {
174 // Exception is NOT expected if raised test will fail
175 Assert.assertTrue(false);
179 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
180 NodeConnector c0 = new NodeConnector(
181 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
184 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
185 NodeConnector c1 = new NodeConnector(
186 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
188 NodeConnector c2 = new NodeConnector(
189 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
190 new Short((short) 0xCAFE), n1);
192 Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
193 NodeConnector c3 = new NodeConnector(
194 NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
195 new String("towardOF1"), n2);
196 NodeConnector c4 = new NodeConnector(
197 NodeConnector.NodeConnectorIDType.ONEPK, new String(
200 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
201 NodeConnector c5 = new NodeConnector(
202 NodeConnector.NodeConnectorIDType.ONEPK, new String(
205 Edge e0 = new Edge(c0, c1);
206 Edge e1 = new Edge(c1, c2);
207 Edge e2 = new Edge(c2, c3);
208 Edge e3 = new Edge(c3, c4);
209 Edge e4 = new Edge(c4, c5);
210 edges3 = Arrays.asList(e0, e1, e2, e3, e4);
211 } catch (ConstructionException e) {
212 // Exception is NOT expected if raised test will fail
213 Assert.assertTrue(false);
217 path1 = new Path(edges1);
218 path2 = new Path(edges2);
219 path3 = new Path(edges3);
221 System.out.println("Path1: " + path1);
222 System.out.println("Path2: " + path2);
223 System.out.println("Path3: " + path3);
225 // Make sure the path are equals
226 Assert.assertTrue(path1.equals(path2));
228 // Make sure the path are marked as different
229 Assert.assertTrue(!path1.equals(path3));
230 } catch (ConstructionException e) {
231 // Exception is NOT expected if raised test will fail
232 Assert.assertTrue(false);
237 public void testPathEmpty() {
239 Path path = new Path(new LinkedList());
240 // Exception is expected if not raised test will fail
241 Assert.assertTrue(false);
242 } catch (ConstructionException e) {
247 public void testPathOneElement() {
249 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
250 NodeConnector c0 = new NodeConnector(
251 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
254 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
255 NodeConnector c1 = new NodeConnector(
256 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
259 Edge e0 = new Edge(c0, c1);
261 Path path = new Path(Arrays.asList(e0));
262 } catch (ConstructionException e) {
263 // Exception is NOT expected if raised test will fail
264 Assert.assertTrue(false);
269 public void testPathGetNodes() {
270 // Test on >2 edges paths
272 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
273 NodeConnector c0 = new NodeConnector(
274 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
277 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
278 NodeConnector c1 = new NodeConnector(
279 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
281 NodeConnector c2 = new NodeConnector(
282 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
283 new Short((short) 0xCAFE), n1);
285 Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
286 NodeConnector c3 = new NodeConnector(
287 NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
288 new String("towardOF1"), n2);
289 NodeConnector c4 = new NodeConnector(
290 NodeConnector.NodeConnectorIDType.ONEPK, new String(
293 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
294 NodeConnector c5 = new NodeConnector(
295 NodeConnector.NodeConnectorIDType.ONEPK, new String(
298 Edge e0 = new Edge(c0, c1);
299 Edge e1 = new Edge(c1, c2);
300 Edge e2 = new Edge(c2, c3);
301 Edge e3 = new Edge(c3, c4);
302 Edge e4 = new Edge(c4, c5);
303 List<Edge> edges = Arrays.asList(e0, e1, e2, e3, e4);
304 Path path = new Path(edges);
307 Assert.assertTrue(path.getStartNode().equals(n0));
310 Assert.assertTrue(path.getEndNode().equals(n3));
311 } catch (ConstructionException e) {
312 // Exception is NOT expected if raised test will fail
313 Assert.assertTrue(false);
316 // Test on 1 edge path
318 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
319 NodeConnector c0 = new NodeConnector(
320 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
323 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
324 NodeConnector c1 = new NodeConnector(
325 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
328 Edge e0 = new Edge(c0, c1);
329 List<Edge> edges = Arrays.asList(e0);
330 Path path = new Path(edges);
333 Assert.assertTrue(path.getStartNode().equals(n0));
336 Assert.assertTrue(path.getEndNode().equals(n1));
337 } catch (ConstructionException e) {
338 // Exception is NOT expected if raised test will fail
339 Assert.assertTrue(false);