5e1cfac17dc78cd28cfcfd421dab2a9b7448fa22
[ric-plt/submgr.git] / pkg / control / types_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package control
21
22 import (
23         "testing"
24 )
25
26 func TestRmrEndpoint(t *testing.T) {
27
28         testEp := func(t *testing.T, val string, expect *RmrEndpoint) {
29                 res := NewRmrEndpoint(val)
30
31                 if expect == nil && res == nil {
32                         return
33                 }
34                 if res == nil {
35                         testError(t, "Endpoint elems for value %s expected addr %s port %d got nil", val, expect.GetAddr(), expect.GetPort())
36                         return
37                 }
38                 if expect.GetAddr() != res.GetAddr() || expect.GetPort() != res.GetPort() {
39                         testError(t, "Endpoint elems for value %s expected addr %s port %d got addr %s port %d", val, expect.GetAddr(), expect.GetPort(), res.GetAddr(), res.GetPort())
40                 }
41                 if expect.String() != res.String() {
42                         testError(t, "Endpoint string for value %s expected %s got %s", val, expect.String(), res.String())
43                 }
44
45         }
46
47         testEp(t, "localhost:8080", &RmrEndpoint{"localhost", 8080})
48         testEp(t, "127.0.0.1:8080", &RmrEndpoint{"127.0.0.1", 8080})
49         testEp(t, "localhost:70000", nil)
50         testEp(t, "localhost?8080", nil)
51         testEp(t, "abcdefghijklmnopqrstuvwxyz", nil)
52         testEp(t, "", nil)
53 }
54
55 func TestRmrEndpointList(t *testing.T) {
56         epl := &RmrEndpointList{}
57
58         // Simple add / has / delete
59         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
60                 testError(t, "RmrEndpointList: 8080 add failed")
61         }
62         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == true {
63                 testError(t, "RmrEndpointList: 8080 duplicate add success")
64         }
65         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
66                 testError(t, "RmrEndpointList: 8081 add failed")
67         }
68         if epl.HasEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
69                 testError(t, "RmrEndpointList: 8081 has failed")
70         }
71         if epl.DelEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
72                 testError(t, "RmrEndpointList: 8081 del failed")
73         }
74         if epl.HasEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == true {
75                 testError(t, "RmrEndpointList: 8081 has non existing success")
76         }
77         if epl.DelEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == true {
78                 testError(t, "RmrEndpointList: 8081 del non existing success")
79         }
80         if epl.DelEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
81                 testError(t, "RmrEndpointList: 8080 del failed")
82         }
83
84         // list delete
85         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
86                 testError(t, "RmrEndpointList: 8080 add failed")
87         }
88         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
89                 testError(t, "RmrEndpointList: 8081 add failed")
90         }
91         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8082")) == false {
92                 testError(t, "RmrEndpointList: 8082 add failed")
93         }
94
95         epl2 := &RmrEndpointList{}
96         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:9080")) == false {
97                 testError(t, "RmrEndpointList: othlist add 9080 failed")
98         }
99
100         if epl.DelEndpoints(epl2) == true {
101                 testError(t, "RmrEndpointList: delete list not existing successs")
102         }
103
104         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
105                 testError(t, "RmrEndpointList: othlist add 8080 failed")
106         }
107         if epl.DelEndpoints(epl2) == false {
108                 testError(t, "RmrEndpointList: delete list 8080,9080 failed")
109         }
110
111         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
112                 testError(t, "RmrEndpointList: othlist add 8081 failed")
113         }
114         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:8082")) == false {
115                 testError(t, "RmrEndpointList: othlist add 8082 failed")
116         }
117
118         if epl.DelEndpoints(epl2) == false {
119                 testError(t, "RmrEndpointList: delete list 8080,8081,8082,9080 failed")
120         }
121
122 }