RIC:1060: Change in PTL
[ric-plt/xapp-frame.git] / pkg / xapp / rmrendpointlist_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 xapp
21
22 import (
23         "fmt"
24         "testing"
25 )
26
27 func TestRmrEndpoint1(t *testing.T) {
28         addr := "127.0.0.1"
29         port := uint16(8080)
30         str := fmt.Sprintf("%s:%d", addr, port)
31         Logger.Info("CASE: TestRmrEndpoint1 %s", str)
32         ep := NewRmrEndpoint(str)
33         if ep == nil || ep.Addr != addr || ep.Port != port {
34                 t.Errorf("NewRmrEndpoint: %s failed", str)
35         }
36 }
37
38 func TestRmrEndpoint2(t *testing.T) {
39         addr := "[2001:2003:fb69:ea00:c894:288b:4582:b5c/64]"
40         port := uint16(8080)
41         str := fmt.Sprintf("%s:%d", addr, port)
42         Logger.Info("CASE: TestRmrEndpoint2 %s", str)
43         ep := NewRmrEndpoint(str)
44         if ep == nil || ep.Addr != addr || ep.Port != port {
45                 t.Errorf("NewRmrEndpoint: %s failed", str)
46         }
47 }
48
49 func TestRmrEndpoint3(t *testing.T) {
50         addr := "127.0.0.1"
51         str := fmt.Sprintf("%s:port", addr)
52         Logger.Info("CASE: TestRmrEndpoint3 %s", str)
53         ep := NewRmrEndpoint(str)
54         if ep != nil {
55                 t.Errorf("NewRmrEndpoint: %s successful while should fail", str)
56         }
57 }
58
59 func TestRmrEndpoint4(t *testing.T) {
60         addr := "127.0.0.1"
61         str := fmt.Sprintf("%s:port", addr)
62         Logger.Info("CASE: TestRmrEndpoint4 %s", str)
63         ep := &RmrEndpoint{}
64         if ep.Set(str) == true {
65                 t.Errorf("NewRmrEndpoint: Set %s successful while should fail", str)
66         }
67         if ep.Addr != "" || ep.Port != 0 {
68                 t.Errorf("NewRmrEndpoint: Values %s successful while should fail", str)
69         }
70 }
71
72 func TestRmrEndpointList(t *testing.T) {
73         Logger.Info("CASE: TestRmrEndpointList")
74
75         epl := NewRmrEndpointList()
76
77         // Simple add / has / delete
78         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
79                 t.Errorf("RmrEndpointList: 8080 add failed")
80         }
81         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == true {
82                 t.Errorf("RmrEndpointList: 8080 duplicate add success")
83         }
84         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
85                 t.Errorf("RmrEndpointList: 8081 add failed")
86         }
87         if epl.HasEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
88                 t.Errorf("RmrEndpointList: 8081 has failed")
89         }
90
91         Logger.Info("%+v -- %+v -- %d", epl.String(), epl.StringList(), epl.Size())
92
93         if epl.DelEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
94                 t.Errorf("RmrEndpointList: 8081 del failed")
95         }
96         if epl.HasEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == true {
97                 t.Errorf("RmrEndpointList: 8081 has non existing success")
98         }
99         if epl.DelEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == true {
100                 t.Errorf("RmrEndpointList: 8081 del non existing success")
101         }
102         if epl.DelEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
103                 t.Errorf("RmrEndpointList: 8080 del failed")
104         }
105
106         // list delete
107         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
108                 t.Errorf("RmrEndpointList: 8080 add failed")
109         }
110         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
111                 t.Errorf("RmrEndpointList: 8081 add failed")
112         }
113         if epl.AddEndpoint(NewRmrEndpoint("127.0.0.1:8082")) == false {
114                 t.Errorf("RmrEndpointList: 8082 add failed")
115         }
116
117         epl2 := &RmrEndpointList{}
118         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:9080")) == false {
119                 t.Errorf("RmrEndpointList: othlist add 9080 failed")
120         }
121
122         if epl.DelEndpoints(epl2) == true {
123                 t.Errorf("RmrEndpointList: delete list not existing successs")
124         }
125
126         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:8080")) == false {
127                 t.Errorf("RmrEndpointList: othlist add 8080 failed")
128         }
129         if epl.DelEndpoints(epl2) == false {
130                 t.Errorf("RmrEndpointList: delete list 8080,9080 failed")
131         }
132
133         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:8081")) == false {
134                 t.Errorf("RmrEndpointList: othlist add 8081 failed")
135         }
136         if epl2.AddEndpoint(NewRmrEndpoint("127.0.0.1:8082")) == false {
137                 t.Errorf("RmrEndpointList: othlist add 8082 failed")
138         }
139
140         if epl.DelEndpoints(epl2) == false {
141                 t.Errorf("RmrEndpointList: delete list 8080,8081,8082,9080 failed")
142         }
143
144 }