andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | |
| 17 | package org.onosproject.net.behaviour; |
| 18 | |
| 19 | |
| 20 | import org.junit.Rule; |
| 21 | import org.junit.Test; |
| 22 | import org.junit.rules.ExpectedException; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Arrays; |
| 27 | import java.util.List; |
| 28 | |
| 29 | import static org.junit.Assert.*; |
| 30 | |
| 31 | /** |
| 32 | * Test for ControllerInfo class. |
| 33 | */ |
| 34 | public class ControllerInfoTest { |
| 35 | @Rule |
| 36 | public ExpectedException thrown = ExpectedException.none(); |
| 37 | |
| 38 | @Test |
| 39 | public void tcpSslFormat() { |
| 40 | String target = "tcp:192.168.1.1:6653"; |
| 41 | ControllerInfo controllerInfo = new ControllerInfo(target); |
| 42 | assertEquals("wrong type", controllerInfo.type(), "tcp"); |
| 43 | assertEquals("wrong ip", controllerInfo.ip(), IpAddress.valueOf("192.168.1.1")); |
| 44 | assertEquals("wrong port", controllerInfo.port(), 6653); |
| 45 | |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | public void ptcpPsslFormat() { |
| 50 | String target = "ptcp:6653:192.168.1.1"; |
| 51 | ControllerInfo controllerInfo = new ControllerInfo(target); |
| 52 | assertEquals("wrong type", controllerInfo.type(), "ptcp"); |
| 53 | assertEquals("wrong ip", controllerInfo.ip(), IpAddress.valueOf("192.168.1.1")); |
| 54 | assertEquals("wrong port", controllerInfo.port(), 6653); |
| 55 | |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void unixFormat() { |
| 60 | String target = "unix:file"; |
| 61 | thrown.expect(IllegalArgumentException.class); |
| 62 | ControllerInfo controllerInfo = new ControllerInfo(target); |
| 63 | assertTrue("wrong type", controllerInfo.type().contains("unix")); |
| 64 | assertNull("wrong ip", controllerInfo.ip()); |
| 65 | assertEquals("wrong port", controllerInfo.port(), -1); |
| 66 | |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void defaultValues() { |
| 71 | String target = "tcp:192.168.1.1"; |
| 72 | ControllerInfo controllerInfo = new ControllerInfo(target); |
| 73 | assertEquals("wrong type", controllerInfo.type(), "tcp"); |
| 74 | assertEquals("wrong ip", controllerInfo.ip(), IpAddress.valueOf("192.168.1.1")); |
| 75 | assertEquals("wrong port", controllerInfo.port(), 6653); |
| 76 | String target1 = "ptcp:5000:"; |
| 77 | ControllerInfo controllerInfo2 = new ControllerInfo(target1); |
| 78 | assertEquals("wrong type", controllerInfo2.type(), "ptcp"); |
| 79 | assertEquals("wrong ip", controllerInfo2.ip(), IpAddress.valueOf("0.0.0.0")); |
| 80 | assertEquals("wrong port", controllerInfo2.port(), 5000); |
| 81 | String target2 = "ptcp:"; |
| 82 | ControllerInfo controllerInfo3 = new ControllerInfo(target2); |
| 83 | assertEquals("wrong type", controllerInfo3.type(), "ptcp"); |
| 84 | assertEquals("wrong ip", controllerInfo3.ip(), IpAddress.valueOf("0.0.0.0")); |
| 85 | assertEquals("wrong port", controllerInfo3.port(), 6653); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | @Test |
| 90 | public void testEquals() { |
| 91 | String target1 = "ptcp:6653:192.168.1.1"; |
| 92 | ControllerInfo controllerInfo1 = new ControllerInfo(target1); |
| 93 | String target2 = "ptcp:6653:192.168.1.1"; |
| 94 | ControllerInfo controllerInfo2 = new ControllerInfo(target2); |
| 95 | assertTrue("wrong equals method", controllerInfo1.equals(controllerInfo2)); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testListEquals() { |
| 100 | String target1 = "ptcp:6653:192.168.1.1"; |
| 101 | ControllerInfo controllerInfo1 = new ControllerInfo(target1); |
| 102 | String target2 = "ptcp:6653:192.168.1.1"; |
| 103 | ControllerInfo controllerInfo2 = new ControllerInfo(target2); |
| 104 | String target3 = "tcp:192.168.1.1:6653"; |
| 105 | ControllerInfo controllerInfo3 = new ControllerInfo(target3); |
| 106 | String target4 = "tcp:192.168.1.1:6653"; |
| 107 | ControllerInfo controllerInfo4 = new ControllerInfo(target4); |
| 108 | List<ControllerInfo> list1 = new ArrayList<>(Arrays.asList(controllerInfo1, controllerInfo3)); |
| 109 | List<ControllerInfo> list2 = new ArrayList<>(Arrays.asList(controllerInfo2, controllerInfo4)); |
| 110 | assertTrue("wrong equals list method", list1.equals(list2)); |
| 111 | } |
| 112 | } |