blob: 7bf3defee12722f16c1b2199a00726dec5296253 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import static org.onosproject.net.DeviceId.deviceId;
25import static org.onosproject.net.PortNumber.portNumber;
tom5a9383a2014-10-02 07:33:52 -070026
27/**
Jonathan Hartc3af35a2015-04-30 22:20:29 -070028 * Test of the connection point entity.
tom5a9383a2014-10-02 07:33:52 -070029 */
30public class ConnectPointTest {
31
tom747a2132014-10-02 08:18:41 -070032 private static final DeviceId DID1 = deviceId("1");
33 private static final DeviceId DID2 = deviceId("2");
34 private static final PortNumber P1 = portNumber(1);
35 private static final PortNumber P2 = portNumber(2);
tom5a9383a2014-10-02 07:33:52 -070036
37 @Test
38 public void basics() {
39 ConnectPoint p = new ConnectPoint(DID1, P2);
40 assertEquals("incorrect element id", DID1, p.deviceId());
41 assertEquals("incorrect element id", P2, p.port());
42 }
43
tom5a9383a2014-10-02 07:33:52 -070044 @Test
45 public void testEquality() {
46 new EqualsTester()
47 .addEqualityGroup(new ConnectPoint(DID1, P1), new ConnectPoint(DID1, P1))
48 .addEqualityGroup(new ConnectPoint(DID1, P2), new ConnectPoint(DID1, P2))
49 .addEqualityGroup(new ConnectPoint(DID2, P1), new ConnectPoint(DID2, P1))
50 .testEquals();
51 }
Jonathan Hartc3af35a2015-04-30 22:20:29 -070052
53 @Test
54 public void testParseDeviceConnectPoint() {
55 String cp = "of:0011223344556677/1";
56
57 ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(cp);
58 assertEquals("of:0011223344556677", connectPoint.deviceId().toString());
59 assertEquals("1", connectPoint.port().toString());
60
61 expectDeviceParseException("");
62 expectDeviceParseException("1/");
63 expectDeviceParseException("1/1/1");
64 expectDeviceParseException("of:0011223344556677/word");
65 }
66
67 /**
68 * Parse a device connect point and expect an exception to be thrown.
69 *
70 * @param string string to parse
71 */
72 private static void expectDeviceParseException(String string) {
73 try {
74 ConnectPoint.deviceConnectPoint(string);
75 fail("Expected exception was not thrown");
76 } catch (Exception e) {
77 assertTrue(true);
78 }
79 }
80
81 @Test
82 public void testParseHostConnectPoint() {
83 String cp = "16:3A:BD:6E:31:E4/-1/1";
84
85 ConnectPoint connectPoint = ConnectPoint.hostConnectPoint(cp);
Luca Prete283a9622016-03-29 16:12:20 -070086 assertEquals("16:3A:BD:6E:31:E4/None", connectPoint.hostId().toString());
Jonathan Hartc3af35a2015-04-30 22:20:29 -070087 assertEquals("1", connectPoint.port().toString());
88
89 expectHostParseException("");
90 expectHostParseException("1/");
91 expectHostParseException("1/1");
92 expectHostParseException("1/1/1/1");
93 expectHostParseException("16:3A:BD:6E:31:E4/word/1");
94 expectHostParseException("16:3A:BD:6E:31:E4/1/word");
95 }
96
97 /**
98 * Parse a host connect point and expect an exception to be thrown.
99 *
100 * @param string string to parse
101 */
102 private static void expectHostParseException(String string) {
103 try {
104 ConnectPoint.hostConnectPoint(string);
105 fail("Expected exception was not thrown");
106 } catch (Exception e) {
107 assertTrue(true);
108 }
109 }
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700110}