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 edges = new LinkedList(Arrays.asList(e0, e1, e2, e3, e4));
67 } catch (ConstructionException e) {
68 // Exception is NOT expected if raised test will fail
69 Assert.assertTrue(false);
73 Path res = new Path(edges);
74 } catch (ConstructionException e) {
75 // Exception is NOT expected if raised test will fail
76 Assert.assertTrue(false);
79 // Now lets disconnect on edge so to create a disconnected
80 // path, the constructor should catch that and should not
85 Path res = new Path(edges);
86 // Exception is expected if not raised test will fail
87 Assert.assertTrue(false);
88 } catch (ConstructionException e) {
93 public void testPathComparison() {
94 List<Edge> edges1 = null;
96 List<Edge> edges2 = null;
98 List<Edge> edges3 = null;
102 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
103 NodeConnector c0 = new NodeConnector(
104 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
107 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
108 NodeConnector c1 = new NodeConnector(
109 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
111 NodeConnector c2 = new NodeConnector(
112 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
113 new Short((short) 0xCAFE), n1);
115 Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
116 NodeConnector c3 = new NodeConnector(
117 NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
118 new String("towardOF1"), n2);
119 NodeConnector c4 = new NodeConnector(
120 NodeConnector.NodeConnectorIDType.ONEPK, new String(
123 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
124 NodeConnector c5 = new NodeConnector(
125 NodeConnector.NodeConnectorIDType.ONEPK, new String(
128 Edge e0 = new Edge(c0, c1);
129 Edge e1 = new Edge(c1, c2);
130 Edge e2 = new Edge(c2, c3);
131 Edge e3 = new Edge(c3, c4);
132 Edge e4 = new Edge(c4, c5);
133 edges1 = new LinkedList(Arrays.asList(e0, e1, e2, e3, e4));
134 } catch (ConstructionException e) {
135 // Exception is NOT expected if raised test will fail
136 Assert.assertTrue(false);
140 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
141 NodeConnector c0 = new NodeConnector(
142 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
145 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
146 NodeConnector c1 = new NodeConnector(
147 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
149 NodeConnector c2 = new NodeConnector(
150 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
151 new Short((short) 0xCAFE), n1);
153 Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
154 NodeConnector c3 = new NodeConnector(
155 NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
156 new String("towardOF1"), n2);
157 NodeConnector c4 = new NodeConnector(
158 NodeConnector.NodeConnectorIDType.ONEPK, new String(
161 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
162 NodeConnector c5 = new NodeConnector(
163 NodeConnector.NodeConnectorIDType.ONEPK, new String(
166 Edge e0 = new Edge(c0, c1);
167 Edge e1 = new Edge(c1, c2);
168 Edge e2 = new Edge(c2, c3);
169 Edge e3 = new Edge(c3, c4);
170 Edge e4 = new Edge(c4, c5);
171 edges2 = new LinkedList(Arrays.asList(e0, e1, e2, e3, e4));
172 } catch (ConstructionException e) {
173 // Exception is NOT expected if raised test will fail
174 Assert.assertTrue(false);
178 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
179 NodeConnector c0 = new NodeConnector(
180 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
183 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
184 NodeConnector c1 = new NodeConnector(
185 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
187 NodeConnector c2 = new NodeConnector(
188 NodeConnector.NodeConnectorIDType.OPENFLOW2ONEPK,
189 new Short((short) 0xCAFE), n1);
191 Node n2 = new Node(Node.NodeIDType.ONEPK, new String("Router1"));
192 NodeConnector c3 = new NodeConnector(
193 NodeConnector.NodeConnectorIDType.ONEPK2OPENFLOW,
194 new String("towardOF1"), n2);
195 NodeConnector c4 = new NodeConnector(
196 NodeConnector.NodeConnectorIDType.ONEPK, new String(
199 Node n3 = new Node(Node.NodeIDType.ONEPK, new String("Router2"));
200 NodeConnector c5 = new NodeConnector(
201 NodeConnector.NodeConnectorIDType.ONEPK, new String(
204 Edge e0 = new Edge(c0, c1);
205 Edge e1 = new Edge(c1, c2);
206 Edge e2 = new Edge(c2, c3);
207 Edge e3 = new Edge(c3, c4);
208 Edge e4 = new Edge(c4, c5);
209 edges3 = new LinkedList(Arrays.asList(e0, e1, e2, e3, e4));
210 } catch (ConstructionException e) {
211 // Exception is NOT expected if raised test will fail
212 Assert.assertTrue(false);
216 path1 = new Path(edges1);
217 path2 = new Path(edges2);
218 path3 = new Path(edges3);
220 System.out.println("Path1: " + path1);
221 System.out.println("Path2: " + path2);
222 System.out.println("Path3: " + path3);
224 // Make sure the path are equals
225 Assert.assertTrue(path1.equals(path2));
227 // Make sure the path are marked as different
228 Assert.assertTrue(!path1.equals(path3));
229 } catch (ConstructionException e) {
230 // Exception is NOT expected if raised test will fail
231 Assert.assertTrue(false);
236 public void testPathEmpty() {
238 Path path = new Path(new LinkedList());
239 // Exception is expected if not raised test will fail
240 Assert.assertTrue(false);
241 } catch (ConstructionException e) {
246 public void testPathOneElement() {
248 Node n0 = new Node(Node.NodeIDType.OPENFLOW, new Long(40L));
249 NodeConnector c0 = new NodeConnector(
250 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
253 Node n1 = new Node(Node.NodeIDType.OPENFLOW, new Long(110L));
254 NodeConnector c1 = new NodeConnector(
255 NodeConnector.NodeConnectorIDType.OPENFLOW, new Short(
258 Edge e0 = new Edge(c0, c1);
260 Path path = new Path(new LinkedList(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 = new LinkedList(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 = new LinkedList(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);