blob: 74b162e4a947c44b6c5eaee96297a337e5fb97c1 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
tom5a9383a2014-10-02 07:33:52 -070017
18import com.google.common.testing.EqualsTester;
19import org.junit.Test;
20
Jonathan Hartc3af35a2015-04-30 22:20:29 -070021import static junit.framework.TestCase.fail;
tom747a2132014-10-02 08:18:41 -070022import static org.junit.Assert.assertEquals;
Jonathan Hartc3af35a2015-04-30 22:20:29 -070023import static org.junit.Assert.assertTrue;
Simon Hunt86943082017-06-15 13:18:42 -070024import static org.onosproject.net.ConnectPointTest.Relate.AFTER;
25import static org.onosproject.net.ConnectPointTest.Relate.BEFORE;
26import static org.onosproject.net.ConnectPointTest.Relate.SAME_AS;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import static org.onosproject.net.DeviceId.deviceId;
28import static org.onosproject.net.PortNumber.portNumber;
tom5a9383a2014-10-02 07:33:52 -070029
30/**
Jonathan Hartc3af35a2015-04-30 22:20:29 -070031 * Test of the connection point entity.
tom5a9383a2014-10-02 07:33:52 -070032 */
33public class ConnectPointTest {
34
tom747a2132014-10-02 08:18:41 -070035 private static final DeviceId DID1 = deviceId("1");
36 private static final DeviceId DID2 = deviceId("2");
37 private static final PortNumber P1 = portNumber(1);
38 private static final PortNumber P2 = portNumber(2);
tom5a9383a2014-10-02 07:33:52 -070039
Simon Hunt86943082017-06-15 13:18:42 -070040 private ConnectPoint cp(DeviceId d, PortNumber p) {
41 return new ConnectPoint(d, p);
42 }
43
tom5a9383a2014-10-02 07:33:52 -070044 @Test
45 public void basics() {
Simon Hunt86943082017-06-15 13:18:42 -070046 ConnectPoint p = cp(DID1, P2);
tom5a9383a2014-10-02 07:33:52 -070047 assertEquals("incorrect element id", DID1, p.deviceId());
48 assertEquals("incorrect element id", P2, p.port());
49 }
50
tom5a9383a2014-10-02 07:33:52 -070051 @Test
52 public void testEquality() {
53 new EqualsTester()
Simon Hunt86943082017-06-15 13:18:42 -070054 .addEqualityGroup(cp(DID1, P1), cp(DID1, P1))
55 .addEqualityGroup(cp(DID1, P2), cp(DID1, P2))
56 .addEqualityGroup(cp(DID2, P1), cp(DID2, P1))
tom5a9383a2014-10-02 07:33:52 -070057 .testEquals();
58 }
Jonathan Hartc3af35a2015-04-30 22:20:29 -070059
60 @Test
61 public void testParseDeviceConnectPoint() {
62 String cp = "of:0011223344556677/1";
63
64 ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(cp);
65 assertEquals("of:0011223344556677", connectPoint.deviceId().toString());
66 assertEquals("1", connectPoint.port().toString());
67
68 expectDeviceParseException("");
69 expectDeviceParseException("1/");
70 expectDeviceParseException("1/1/1");
71 expectDeviceParseException("of:0011223344556677/word");
72 }
73
74 /**
75 * Parse a device connect point and expect an exception to be thrown.
76 *
77 * @param string string to parse
78 */
79 private static void expectDeviceParseException(String string) {
80 try {
81 ConnectPoint.deviceConnectPoint(string);
82 fail("Expected exception was not thrown");
83 } catch (Exception e) {
84 assertTrue(true);
85 }
86 }
87
88 @Test
89 public void testParseHostConnectPoint() {
90 String cp = "16:3A:BD:6E:31:E4/-1/1";
91
92 ConnectPoint connectPoint = ConnectPoint.hostConnectPoint(cp);
Luca Prete283a9622016-03-29 16:12:20 -070093 assertEquals("16:3A:BD:6E:31:E4/None", connectPoint.hostId().toString());
Jonathan Hartc3af35a2015-04-30 22:20:29 -070094 assertEquals("1", connectPoint.port().toString());
95
96 expectHostParseException("");
97 expectHostParseException("1/");
98 expectHostParseException("1/1");
99 expectHostParseException("1/1/1/1");
100 expectHostParseException("16:3A:BD:6E:31:E4/word/1");
101 expectHostParseException("16:3A:BD:6E:31:E4/1/word");
102 }
103
104 /**
105 * Parse a host connect point and expect an exception to be thrown.
106 *
107 * @param string string to parse
108 */
109 private static void expectHostParseException(String string) {
110 try {
111 ConnectPoint.hostConnectPoint(string);
112 fail("Expected exception was not thrown");
113 } catch (Exception e) {
114 assertTrue(true);
115 }
116 }
Simon Hunt86943082017-06-15 13:18:42 -0700117
118 enum Relate { BEFORE, SAME_AS, AFTER }
119
120 private void checkComparison(ConnectPoint cpA, Relate r, ConnectPoint cpB) {
121 switch (r) {
122 case BEFORE:
123 assertTrue("Bad before", cpA.compareTo(cpB) < 0);
124 break;
125 case SAME_AS:
126 assertTrue("Bad same_as", cpA.compareTo(cpB) == 0);
127 break;
128 case AFTER:
129 assertTrue("Bad after", cpA.compareTo(cpB) > 0);
130 break;
131 default:
132 fail("Bad relation");
133 }
134 }
135
136 @Test
137 public void comparator() {
138 checkComparison(cp(DID1, P1), SAME_AS, cp(DID1, P1));
139 checkComparison(cp(DID1, P1), BEFORE, cp(DID1, P2));
140 checkComparison(cp(DID1, P2), AFTER, cp(DID1, P1));
141
142 checkComparison(cp(DID1, P1), BEFORE, cp(DID2, P1));
143 checkComparison(cp(DID1, P2), BEFORE, cp(DID2, P1));
144 checkComparison(cp(DID1, P1), BEFORE, cp(DID2, P2));
145 checkComparison(cp(DID1, P2), BEFORE, cp(DID2, P2));
146 }
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700147}