e56cea40fd0eb338ec7de20e35c5a9bf42614f9e
[ric-plt/submgr.git] / pkg / xapptweaks / rmrendpoint_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 xapptweaks
21
22 import (
23         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
24         "testing"
25 )
26
27 func TestRmrEndpoint(t *testing.T) {
28
29         tent := teststub.NewTestWrapper("TestRmrEndpoint")
30
31         testEp := func(t *testing.T, val string, expect *RmrEndpoint) {
32                 res := NewRmrEndpoint(val)
33
34                 if expect == nil && res == nil {
35                         return
36                 }
37                 if res == nil {
38                         tent.TestError(t, "Endpoint elems for value %s expected addr %s port %d got nil", val, expect.GetAddr(), expect.GetPort())
39                         return
40                 }
41                 if expect.GetAddr() != res.GetAddr() || expect.GetPort() != res.GetPort() {
42                         tent.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())
43                 }
44                 if expect.String() != res.String() {
45                         tent.TestError(t, "Endpoint string for value %s expected %s got %s", val, expect.String(), res.String())
46                 }
47
48         }
49
50         testEp(t, "localhost:8080", &RmrEndpoint{"localhost", 8080})
51         testEp(t, "127.0.0.1:8080", &RmrEndpoint{"127.0.0.1", 8080})
52         testEp(t, "localhost:70000", nil)
53         testEp(t, "localhost?8080", nil)
54         testEp(t, "abcdefghijklmnopqrstuvwxyz", nil)
55         testEp(t, "", nil)
56 }
57
58 func TestRmrEndpointList(t *testing.T) {
59
60         tent := teststub.NewTestWrapper("TestRmrEndpointList")
61
62         epl := &RmrEndpointList{}
63
64         // Simple add / has / delete
65         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
66                 tent.TestError(t, "RmrEndpointList: 8080 add failed")
67         }
68         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == true {
69                 tent.TestError(t, "RmrEndpointList: 8080 duplicate add success")
70         }
71         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
72                 tent.TestError(t, "RmrEndpointList: 8081 add failed")
73         }
74         if epl.HasEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
75                 tent.TestError(t, "RmrEndpointList: 8081 has failed")
76         }
77         if epl.DelEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
78                 tent.TestError(t, "RmrEndpointList: 8081 del failed")
79         }
80         if epl.HasEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == true {
81                 tent.TestError(t, "RmrEndpointList: 8081 has non existing success")
82         }
83         if epl.DelEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == true {
84                 tent.TestError(t, "RmrEndpointList: 8081 del non existing success")
85         }
86         if epl.DelEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
87                 tent.TestError(t, "RmrEndpointList: 8080 del failed")
88         }
89
90         // list delete
91         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
92                 tent.TestError(t, "RmrEndpointList: 8080 add failed")
93         }
94         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
95                 tent.TestError(t, "RmrEndpointList: 8081 add failed")
96         }
97         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8082")) == false {
98                 tent.TestError(t, "RmrEndpointList: 8082 add failed")
99         }
100
101         epl2 := &RmrEndpointList{}
102         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:9080")) == false {
103                 tent.TestError(t, "RmrEndpointList: othlist add 9080 failed")
104         }
105
106         if epl.DelEndpoints(epl2) == true {
107                 tent.TestError(t, "RmrEndpointList: delete list not existing successs")
108         }
109
110         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
111                 tent.TestError(t, "RmrEndpointList: othlist add 8080 failed")
112         }
113         if epl.DelEndpoints(epl2) == false {
114                 tent.TestError(t, "RmrEndpointList: delete list 8080,9080 failed")
115         }
116
117         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
118                 tent.TestError(t, "RmrEndpointList: othlist add 8081 failed")
119         }
120         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:8082")) == false {
121                 tent.TestError(t, "RmrEndpointList: othlist add 8082 failed")
122         }
123
124         if epl.DelEndpoints(epl2) == false {
125                 tent.TestError(t, "RmrEndpointList: delete list 8080,8081,8082,9080 failed")
126         }
127
128 }