aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/api/src/test/java/org/onosproject/net/behaviour/ControllerInfoTest.java
blob: ece7f19997f0c8ec4779e341d5006e92ee7217cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onosproject.net.behaviour;


import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onlab.packet.IpAddress;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

/**
 * Test for ControllerInfo class.
 */
public class ControllerInfoTest {
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void tcpSslFormat() {
        String target = "tcp:192.168.1.1:6653";
        ControllerInfo controllerInfo = new ControllerInfo(target);
        assertEquals("wrong type", controllerInfo.type(), "tcp");
        assertEquals("wrong ip", controllerInfo.ip(), IpAddress.valueOf("192.168.1.1"));
        assertEquals("wrong port", controllerInfo.port(), 6653);

    }

    @Test
    public void ptcpPsslFormat() {
        String target = "ptcp:6653:192.168.1.1";
        ControllerInfo controllerInfo = new ControllerInfo(target);
        assertEquals("wrong type", controllerInfo.type(), "ptcp");
        assertEquals("wrong ip", controllerInfo.ip(), IpAddress.valueOf("192.168.1.1"));
        assertEquals("wrong port", controllerInfo.port(), 6653);

    }

    @Test
    public void unixFormat() {
        String target = "unix:file";
        thrown.expect(IllegalArgumentException.class);
        ControllerInfo controllerInfo = new ControllerInfo(target);
        assertTrue("wrong type", controllerInfo.type().contains("unix"));
        assertNull("wrong ip", controllerInfo.ip());
        assertEquals("wrong port", controllerInfo.port(), -1);

    }

    @Test
    public void defaultValues() {
        String target = "tcp:192.168.1.1";
        ControllerInfo controllerInfo = new ControllerInfo(target);
        assertEquals("wrong type", controllerInfo.type(), "tcp");
        assertEquals("wrong ip", controllerInfo.ip(), IpAddress.valueOf("192.168.1.1"));
        assertEquals("wrong port", controllerInfo.port(), 6653);
        String target1 = "ptcp:5000:";
        ControllerInfo controllerInfo2 = new ControllerInfo(target1);
        assertEquals("wrong type", controllerInfo2.type(), "ptcp");
        assertEquals("wrong ip", controllerInfo2.ip(), IpAddress.valueOf("0.0.0.0"));
        assertEquals("wrong port", controllerInfo2.port(), 5000);
        String target2 = "ptcp:";
        ControllerInfo controllerInfo3 = new ControllerInfo(target2);
        assertEquals("wrong type", controllerInfo3.type(), "ptcp");
        assertEquals("wrong ip", controllerInfo3.ip(), IpAddress.valueOf("0.0.0.0"));
        assertEquals("wrong port", controllerInfo3.port(), 6653);
    }


    @Test
    public void testEquals() {
        String target1 = "ptcp:6653:192.168.1.1";
        ControllerInfo controllerInfo1 = new ControllerInfo(target1);
        String target2 = "ptcp:6653:192.168.1.1";
        ControllerInfo controllerInfo2 = new ControllerInfo(target2);
        assertTrue("wrong equals method", controllerInfo1.equals(controllerInfo2));
    }

    @Test
    public void testListEquals() {
        String target1 = "ptcp:6653:192.168.1.1";
        ControllerInfo controllerInfo1 = new ControllerInfo(target1);
        String target2 = "ptcp:6653:192.168.1.1";
        ControllerInfo controllerInfo2 = new ControllerInfo(target2);
        String target3 = "tcp:192.168.1.1:6653";
        ControllerInfo controllerInfo3 = new ControllerInfo(target3);
        String target4 = "tcp:192.168.1.1:6653";
        ControllerInfo controllerInfo4 = new ControllerInfo(target4);
        List<ControllerInfo> list1 = new ArrayList<>(Arrays.asList(controllerInfo1, controllerInfo3));
        List<ControllerInfo> list2 = new ArrayList<>(Arrays.asList(controllerInfo2, controllerInfo4));
        assertTrue("wrong equals list method", list1.equals(list2));
    }
}