Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Test; |
| 20 | |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 21 | import static junit.framework.TestCase.fail; |
tom | 747a213 | 2014-10-02 08:18:41 -0700 | [diff] [blame] | 22 | import static org.junit.Assert.assertEquals; |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.assertTrue; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import static org.onosproject.net.DeviceId.deviceId; |
| 25 | import static org.onosproject.net.PortNumber.portNumber; |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 28 | * Test of the connection point entity. |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 29 | */ |
| 30 | public class ConnectPointTest { |
| 31 | |
tom | 747a213 | 2014-10-02 08:18:41 -0700 | [diff] [blame] | 32 | 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); |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 36 | |
| 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 | |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 44 | @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 Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 52 | |
| 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 Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 86 | assertEquals("16:3A:BD:6E:31:E4/None", connectPoint.hostId().toString()); |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 87 | 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 HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame] | 110 | } |