Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 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 ( |
wahidw | 413abf5 | 2020-12-15 12:17:09 +0000 | [diff] [blame] | 23 | "bytes" |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 24 | "github.com/gorilla/mux" |
Juha Hyttinen | 3cfee96 | 2020-02-13 13:51:00 +0200 | [diff] [blame] | 25 | "github.com/spf13/viper" |
wahidw | 413abf5 | 2020-12-15 12:17:09 +0000 | [diff] [blame] | 26 | "github.com/stretchr/testify/assert" |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 27 | "net/http" |
| 28 | "net/http/httptest" |
| 29 | "os" |
| 30 | "strings" |
| 31 | "testing" |
| 32 | "time" |
| 33 | ) |
| 34 | |
Juha Hyttinen | 3cfee96 | 2020-02-13 13:51:00 +0200 | [diff] [blame] | 35 | //var _ = func() bool { |
| 36 | // testing.Init() |
| 37 | // return true |
| 38 | //}() |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 39 | |
Juha Hyttinen | 3cfee96 | 2020-02-13 13:51:00 +0200 | [diff] [blame] | 40 | type Consumer struct{} |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 41 | |
Mohamed Abukar | f11ab7a | 2019-08-14 16:55:01 +0300 | [diff] [blame] | 42 | func (m Consumer) Consume(params *RMRParams) (err error) { |
Mohamed Abukar | f11ab7a | 2019-08-14 16:55:01 +0300 | [diff] [blame] | 43 | Sdl.Store("myKey", params.Payload) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 44 | return nil |
| 45 | } |
| 46 | |
| 47 | // Test cases |
| 48 | func TestMain(m *testing.M) { |
Mohamed Abukar | 827a641 | 2020-11-12 10:02:41 +0200 | [diff] [blame] | 49 | go RunWithParams(Consumer{}, viper.GetBool("controls.waitForSdl")) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 50 | time.Sleep(time.Duration(5) * time.Second) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 51 | code := m.Run() |
| 52 | os.Exit(code) |
| 53 | } |
| 54 | |
| 55 | func TestGetHealthCheckRetursServiceUnavailableError(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 56 | Logger.Info("CASE: TestGetHealthCheckRetursServiceUnavailableError") |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 57 | req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil) |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 58 | /*response :=*/ executeRequest(req, nil) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 59 | |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 60 | //checkResponseCode(t, http.StatusServiceUnavailable, response.Code) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | func TestGetHealthCheckReturnsSuccess(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 64 | Logger.Info("CASE: TestGetHealthCheckReturnsSuccess") |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 65 | for Rmr.IsReady() == false { |
| 66 | time.Sleep(time.Duration(2) * time.Second) |
| 67 | } |
| 68 | |
| 69 | req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil) |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 70 | response := executeRequest(req, nil) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 71 | |
| 72 | checkResponseCode(t, http.StatusOK, response.Code) |
| 73 | } |
| 74 | |
| 75 | func TestInjectQuerySinglePath(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 76 | Logger.Info("CASE: TestInjectQuerySinglePath") |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 77 | var handler = func(w http.ResponseWriter, r *http.Request) { |
| 78 | } |
| 79 | |
| 80 | Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar") |
| 81 | |
| 82 | req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar", nil) |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 83 | response := executeRequest(req, nil) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 84 | checkResponseCode(t, http.StatusOK, response.Code) |
| 85 | } |
| 86 | |
| 87 | func TestInjectQueryMultiplePaths(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 88 | Logger.Info("CASE: TestInjectQueryMultiplePaths") |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 89 | var handler = func(w http.ResponseWriter, r *http.Request) { |
| 90 | } |
| 91 | |
| 92 | Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey") |
| 93 | |
| 94 | req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil) |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 95 | response := executeRequest(req, nil) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 96 | checkResponseCode(t, http.StatusOK, response.Code) |
| 97 | } |
| 98 | |
| 99 | func TestInjectQueryFailures(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 100 | Logger.Info("CASE: TestInjectQueryFailures") |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 101 | var handler = func(w http.ResponseWriter, r *http.Request) { |
| 102 | } |
| 103 | |
| 104 | Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey") |
| 105 | |
| 106 | req, _ := http.NewRequest("GET", "/ric/v1/user?invalid=bar&no=mykey", nil) |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 107 | response := executeRequest(req, nil) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 108 | checkResponseCode(t, http.StatusNotFound, response.Code) |
| 109 | } |
| 110 | |
| 111 | func TestMessagesReceivedSuccessfully(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 112 | Logger.Info("CASE: TestMessagesReceivedSuccessfully") |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 113 | time.Sleep(time.Duration(5) * time.Second) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 114 | for i := 0; i < 100; i++ { |
Mohamed Abukar | f11ab7a | 2019-08-14 16:55:01 +0300 | [diff] [blame] | 115 | params := &RMRParams{} |
| 116 | params.Mtype = 10004 |
| 117 | params.SubId = -1 |
| 118 | params.Payload = []byte{1, 2, 3, 4, 5, 6} |
Mohamed Abukar | 19461e1 | 2019-08-23 08:46:11 +0300 | [diff] [blame] | 119 | params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"} |
Mohamed Abukar | f11ab7a | 2019-08-14 16:55:01 +0300 | [diff] [blame] | 120 | params.Xid = "TestXID" |
wahidw | 413abf5 | 2020-12-15 12:17:09 +0000 | [diff] [blame] | 121 | |
| 122 | if i%2 == 0 { |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 123 | Rmr.SendMsg(params) |
| 124 | } else { |
| 125 | Rmr.SendWithRetry(params, false, 1) |
| 126 | } |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 127 | } |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 128 | Rmr.RegisterMetrics() |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 129 | |
| 130 | // Allow time to process the messages |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 131 | time.Sleep(time.Duration(5) * time.Second) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 132 | |
Mohamed Abukar | 827a641 | 2020-11-12 10:02:41 +0200 | [diff] [blame] | 133 | waitForSdl := viper.GetBool("controls.waitForSdl") |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 134 | stats := getMetrics(t) |
| 135 | if !strings.Contains(stats, "ricxapp_RMR_Transmitted 100") { |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 136 | t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | if !strings.Contains(stats, "ricxapp_RMR_Received 100") { |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 140 | t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") { |
| 144 | t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect") |
| 145 | } |
| 146 | |
| 147 | if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") { |
| 148 | t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect") |
| 149 | } |
| 150 | |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 151 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 100") { |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 152 | t.Errorf("Error: ricxapp_SDL_Stored value incorrect") |
| 153 | } |
| 154 | |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 155 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") { |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 156 | t.Errorf("Error: ricxapp_SDL_StoreError value incorrect") |
| 157 | } |
| 158 | } |
| 159 | |
wahidw | ad0a271 | 2020-03-04 09:54:15 +0000 | [diff] [blame] | 160 | func TestMessagesReceivedSuccessfullyUsingWh(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 161 | Logger.Info("CASE: TestMessagesReceivedSuccessfullyUsingWh") |
wahidw | ad0a271 | 2020-03-04 09:54:15 +0000 | [diff] [blame] | 162 | time.Sleep(time.Duration(5) * time.Second) |
| 163 | whid := Rmr.Openwh("localhost:4560") |
| 164 | time.Sleep(time.Duration(1) * time.Second) |
| 165 | for i := 0; i < 100; i++ { |
| 166 | params := &RMRParams{} |
| 167 | params.Mtype = 10004 |
| 168 | params.SubId = -1 |
| 169 | params.Payload = []byte{1, 2, 3, 4, 5, 6} |
| 170 | params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"} |
| 171 | params.Xid = "TestXID" |
| 172 | params.Whid = int(whid) |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 173 | |
| 174 | if i == 0 { |
| 175 | Logger.Info("%+v", params.String()) |
| 176 | } |
| 177 | |
wahidw | ad0a271 | 2020-03-04 09:54:15 +0000 | [diff] [blame] | 178 | Rmr.SendMsg(params) |
| 179 | } |
| 180 | |
| 181 | // Allow time to process the messages |
Juha Hyttinen | f619d03 | 2020-05-07 12:42:26 +0300 | [diff] [blame] | 182 | time.Sleep(time.Duration(5) * time.Second) |
wahidw | ad0a271 | 2020-03-04 09:54:15 +0000 | [diff] [blame] | 183 | |
Mohamed Abukar | 827a641 | 2020-11-12 10:02:41 +0200 | [diff] [blame] | 184 | waitForSdl := viper.GetBool("controls.waitForSdl") |
wahidw | ad0a271 | 2020-03-04 09:54:15 +0000 | [diff] [blame] | 185 | stats := getMetrics(t) |
| 186 | if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") { |
| 187 | t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats) |
| 188 | } |
| 189 | |
| 190 | if !strings.Contains(stats, "ricxapp_RMR_Received 200") { |
| 191 | t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats) |
| 192 | } |
| 193 | |
| 194 | if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") { |
| 195 | t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect") |
| 196 | } |
| 197 | |
| 198 | if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") { |
| 199 | t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect") |
| 200 | } |
| 201 | |
| 202 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 200") { |
| 203 | t.Errorf("Error: ricxapp_SDL_Stored value incorrect") |
| 204 | } |
| 205 | |
| 206 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") { |
| 207 | t.Errorf("Error: ricxapp_SDL_StoreError value incorrect") |
| 208 | } |
| 209 | Rmr.Closewh(int(whid)) |
| 210 | } |
| 211 | |
wahidw | e0948fe | 2020-03-19 14:49:41 +0000 | [diff] [blame] | 212 | func TestMessagesReceivedSuccessfullyUsingWhCall(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 213 | Logger.Info("CASE: TestMessagesReceivedSuccessfullyUsingWhCall") |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 214 | time.Sleep(time.Duration(5) * time.Second) |
| 215 | whid := Rmr.Openwh("localhost:4560") |
| 216 | params := &RMRParams{} |
| 217 | params.Payload = []byte("newrt|start\nnewrt|end\n") |
| 218 | params.Whid = int(whid) |
| 219 | params.Callid = 4 |
| 220 | params.Timeout = 1000 |
| 221 | Rmr.SendCallMsg(params) |
wahidw | e0948fe | 2020-03-19 14:49:41 +0000 | [diff] [blame] | 222 | |
| 223 | // Allow time to process the messages |
| 224 | time.Sleep(time.Duration(2) * time.Second) |
| 225 | |
Mohamed Abukar | 827a641 | 2020-11-12 10:02:41 +0200 | [diff] [blame] | 226 | waitForSdl := viper.GetBool("controls.waitForSdl") |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 227 | stats := getMetrics(t) |
| 228 | if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") { |
| 229 | t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats) |
| 230 | } |
wahidw | e0948fe | 2020-03-19 14:49:41 +0000 | [diff] [blame] | 231 | |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 232 | if !strings.Contains(stats, "ricxapp_RMR_Received 201") { |
| 233 | t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats) |
| 234 | } |
wahidw | e0948fe | 2020-03-19 14:49:41 +0000 | [diff] [blame] | 235 | |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 236 | if !strings.Contains(stats, "ricxapp_RMR_TransmitError 1") { |
| 237 | t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect") |
| 238 | } |
wahidw | e0948fe | 2020-03-19 14:49:41 +0000 | [diff] [blame] | 239 | |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 240 | if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") { |
| 241 | t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect") |
| 242 | } |
wahidw | e0948fe | 2020-03-19 14:49:41 +0000 | [diff] [blame] | 243 | |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 244 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 201") { |
| 245 | t.Errorf("Error: ricxapp_SDL_Stored value incorrect") |
| 246 | } |
wahidw | e0948fe | 2020-03-19 14:49:41 +0000 | [diff] [blame] | 247 | |
Mohamed Abukar | 5953f7e | 2020-04-02 10:08:14 +0300 | [diff] [blame] | 248 | if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") { |
| 249 | t.Errorf("Error: ricxapp_SDL_StoreError value incorrect") |
| 250 | } |
| 251 | Rmr.Closewh(int(whid)) |
wahidw | e0948fe | 2020-03-19 14:49:41 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 254 | func TestSubscribeChannels(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 255 | Logger.Info("CASE: TestSubscribeChannels") |
Mohamed Abukar | 827a641 | 2020-11-12 10:02:41 +0200 | [diff] [blame] | 256 | if !viper.GetBool("controls.waitForSdl") { |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 257 | return |
| 258 | } |
| 259 | |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 260 | var NotificationCb = func(ch string, events ...string) { |
| 261 | if ch != "channel1" { |
| 262 | t.Errorf("Error: Callback function called with incorrect params") |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if err := Sdl.Subscribe(NotificationCb, "channel1"); err != nil { |
| 267 | t.Errorf("Error: Subscribe failed: %v", err) |
| 268 | } |
| 269 | time.Sleep(time.Duration(2) * time.Second) |
| 270 | |
| 271 | if err := Sdl.StoreAndPublish("channel1", "event", "key1", "data1"); err != nil { |
| 272 | t.Errorf("Error: Publish failed: %v", err) |
| 273 | } |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 274 | |
| 275 | // Misc. |
| 276 | Sdl.MStoreAndPublish([]string{"channel1"}, "event", "key1", "data1") |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 277 | } |
| 278 | |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 279 | func TestGetRicMessageSuccess(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 280 | Logger.Info("CASE: TestGetRicMessageSuccess") |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 281 | id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ") |
| 282 | if !ok || id != 12010 { |
| 283 | t.Errorf("Error: GetRicMessageId failed: id=%d", id) |
| 284 | } |
| 285 | |
| 286 | name := Rmr.GetRicMessageName(12010) |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 287 | if name != "RIC_SUB_REQ" { |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 288 | t.Errorf("Error: GetRicMessageName failed: name=%s", name) |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | func TestGetRicMessageFails(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 293 | Logger.Info("CASE: TestGetRicMessageFails") |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 294 | ok := Rmr.IsRetryError(&RMRParams{status: 0}) |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 295 | if ok { |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 296 | t.Errorf("Error: IsRetryError returned wrong value") |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 297 | } |
| 298 | |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 299 | ok = Rmr.IsRetryError(&RMRParams{status: 10}) |
| 300 | if !ok { |
| 301 | t.Errorf("Error: IsRetryError returned wrong value") |
| 302 | } |
| 303 | |
| 304 | ok = Rmr.IsNoEndPointError(&RMRParams{status: 5}) |
| 305 | if ok { |
| 306 | t.Errorf("Error: IsNoEndPointError returned wrong value") |
| 307 | } |
| 308 | |
| 309 | ok = Rmr.IsNoEndPointError(&RMRParams{status: 2}) |
| 310 | if !ok { |
| 311 | t.Errorf("Error: IsNoEndPointError returned wrong value") |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | func TestIsErrorFunctions(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 316 | Logger.Info("CASE: TestIsErrorFunctions") |
Mohamed Abukar | 791a77f | 2020-02-12 19:08:42 +0200 | [diff] [blame] | 317 | id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ") |
| 318 | if !ok || id != 12010 { |
| 319 | t.Errorf("Error: GetRicMessageId failed: id=%d", id) |
| 320 | } |
| 321 | |
| 322 | name := Rmr.GetRicMessageName(12010) |
| 323 | if name != "RIC_SUB_REQ" { |
| 324 | t.Errorf("Error: GetRicMessageName failed: name=%s", name) |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 328 | func TestAddConfigChangeListener(t *testing.T) { |
| 329 | Logger.Info("CASE: AddConfigChangeListener") |
| 330 | AddConfigChangeListener(func(f string) {}) |
| 331 | } |
| 332 | |
| 333 | func TestConfigAccess(t *testing.T) { |
| 334 | Logger.Info("CASE: AddConfigChangeListener") |
wahidw | 413abf5 | 2020-12-15 12:17:09 +0000 | [diff] [blame] | 335 | |
| 336 | assert.Equal(t, Config.GetString("name"), "xapp") |
| 337 | assert.Equal(t, Config.GetInt("controls.logger.level"), 3) |
| 338 | assert.Equal(t, Config.GetUint32("controls.logger.level"), uint32(3)) |
| 339 | assert.Equal(t, Config.GetBool("controls.waitForSdl"), false) |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 340 | Config.Get("controls") |
| 341 | Config.GetStringSlice("messaging.ports") |
| 342 | Config.GetStringMap("messaging.ports") |
Mohamed Abukar | c1f00b7 | 2020-11-25 13:51:00 +0200 | [diff] [blame] | 343 | Config.IsSet("messaging") |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | func TestPublishConfigChange(t *testing.T) { |
| 347 | Logger.Info("CASE: AddConfigChangeListener") |
| 348 | PublishConfigChange("testApp", "values") |
| 349 | } |
| 350 | |
| 351 | func TestNewSubscriber(t *testing.T) { |
| 352 | Logger.Info("CASE: TestNewSubscriber") |
| 353 | assert.NotNil(t, NewSubscriber("", 0), "NewSubscriber failed") |
| 354 | } |
| 355 | |
| 356 | func TestNewRMRClient(t *testing.T) { |
wahidw | 413abf5 | 2020-12-15 12:17:09 +0000 | [diff] [blame] | 357 | c := map[string]interface{}{"protPort": "tcp:4560"} |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 358 | viper.Set("rmr", c) |
| 359 | assert.NotNil(t, NewRMRClient(), "NewRMRClient failed") |
| 360 | |
| 361 | params := &RMRParams{} |
| 362 | params.Mtype = 1234 |
| 363 | params.SubId = -1 |
| 364 | params.Payload = []byte{1, 2, 3, 4, 5, 6} |
| 365 | Rmr.SendWithRetry(params, false, 1) |
| 366 | } |
| 367 | |
| 368 | func TestInjectRoutePrefix(t *testing.T) { |
| 369 | Logger.Info("CASE: TestInjectRoutePrefix") |
| 370 | assert.NotNil(t, Resource.InjectRoutePrefix("test", nil), "InjectRoutePrefix failed") |
| 371 | } |
| 372 | |
| 373 | func TestInjectStatusCb(t *testing.T) { |
| 374 | Logger.Info("CASE: TestInjectStatusCb") |
| 375 | |
| 376 | var f = func() bool { |
| 377 | return true |
| 378 | } |
| 379 | Resource.InjectStatusCb(f) |
| 380 | } |
| 381 | |
| 382 | func TestSdlInterfaces(t *testing.T) { |
| 383 | Sdl.Read("myKey") |
| 384 | Sdl.MRead([]string{"myKey"}) |
| 385 | Sdl.ReadAllKeys("myKey") |
| 386 | Sdl.Store("myKey", "Values") |
| 387 | Sdl.MStore("myKey", "Values") |
| 388 | Sdl.RegisterMetrics() |
| 389 | |
| 390 | // Misc. |
| 391 | var NotificationCb = func(ch string, events ...string) {} |
| 392 | Sdl.Subscribe(NotificationCb, "channel1") |
| 393 | Sdl.MSubscribe(NotificationCb, "channel1", "channel2") |
| 394 | Sdl.MStoreAndPublish([]string{"channel1"}, "event", "key1", "data1") |
| 395 | } |
| 396 | |
| 397 | func TestRnibInterfaces(t *testing.T) { |
| 398 | Rnib.GetNodeb("test-gnb") |
| 399 | Rnib.GetCellList("test-gnb") |
| 400 | Rnib.GetListGnbIds() |
| 401 | Rnib.GetListEnbIds() |
| 402 | Rnib.GetCountGnbList() |
| 403 | Rnib.GetCell("test-gnb", 0) |
| 404 | Rnib.GetCell("test-gnb", 0) |
| 405 | Rnib.GetCellById(0, "cell-1") |
| 406 | |
| 407 | // Misc. |
| 408 | var NotificationCb = func(ch string, events ...string) {} |
| 409 | Rnib.Subscribe(NotificationCb, "channel1") |
| 410 | Rnib.StoreAndPublish("channel1", "event", "key1", "data1") |
| 411 | } |
| 412 | |
| 413 | func TestLogger(t *testing.T) { |
| 414 | Logger.Error("CASE: TestNewSubscriber") |
| 415 | Logger.Warn("CASE: TestNewSubscriber") |
Mohamed Abukar | c1f00b7 | 2020-11-25 13:51:00 +0200 | [diff] [blame] | 416 | Logger.GetLevel() |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | func TestConfigHandler(t *testing.T) { |
| 420 | Logger.Error("CASE: TestConfigHandler") |
| 421 | req, _ := http.NewRequest("POST", "/ric/v1/cm/appname", bytes.NewBuffer([]byte{})) |
| 422 | handleFunc := http.HandlerFunc(configHandler) |
| 423 | executeRequest(req, handleFunc) |
| 424 | } |
| 425 | |
wahidw | 413abf5 | 2020-12-15 12:17:09 +0000 | [diff] [blame] | 426 | func TestappconfigHandler(t *testing.T) { |
| 427 | Logger.Error("CASE: TestappconfigHandler") |
| 428 | req, _ := http.NewRequest("POST", "/ric/v1/config", bytes.NewBuffer([]byte{})) |
| 429 | handleFunc := http.HandlerFunc(appconfigHandler) |
| 430 | executeRequest(req, handleFunc) |
| 431 | } |
| 432 | |
Mohamed Abukar | 256c304 | 2020-12-30 17:48:12 +0200 | [diff] [blame^] | 433 | func TestRegisterXapp(t *testing.T) { |
| 434 | Logger.Error("CASE: TestRegisterXapp") |
| 435 | doRegister() |
wahidw | 413abf5 | 2020-12-15 12:17:09 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Mohamed Abukar | 256c304 | 2020-12-30 17:48:12 +0200 | [diff] [blame^] | 438 | func TestDeregisterXapp(t *testing.T) { |
| 439 | Logger.Error("CASE: TestDeregisterXapp") |
| 440 | doDeregister() |
wahidw | 413abf5 | 2020-12-15 12:17:09 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 443 | func TestMisc(t *testing.T) { |
| 444 | Logger.Info("CASE: TestMisc") |
| 445 | |
| 446 | IsReady() |
| 447 | SetReadyCB(func(interface{}) {}, "") |
| 448 | XappReadyCb("") |
| 449 | SetShutdownCB(func() {}) |
| 450 | } |
| 451 | |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 452 | func TestTeardown(t *testing.T) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 453 | Logger.Info("CASE: TestTeardown") |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 454 | Sdl.Delete([]string{"myKey"}) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 455 | Sdl.Clear() |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 456 | Sdl.IsReady() |
| 457 | Sdl.GetStat() |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // Helper functions |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 461 | func executeRequest(req *http.Request, handleR http.HandlerFunc) *httptest.ResponseRecorder { |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 462 | rr := httptest.NewRecorder() |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 463 | if handleR != nil { |
| 464 | vars := map[string]string{"name": "myxapp"} |
| 465 | req = mux.SetURLVars(req, vars) |
| 466 | handleR.ServeHTTP(rr, req) |
| 467 | } else { |
| 468 | vars := map[string]string{"id": "1"} |
| 469 | req = mux.SetURLVars(req, vars) |
| 470 | Resource.router.ServeHTTP(rr, req) |
| 471 | } |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 472 | return rr |
| 473 | } |
| 474 | |
| 475 | func checkResponseCode(t *testing.T, expected, actual int) { |
| 476 | if expected != actual { |
| 477 | t.Errorf("Expected response code %d. Got %d\n", expected, actual) |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | func getMetrics(t *testing.T) string { |
| 482 | req, _ := http.NewRequest("GET", "/ric/v1/metrics", nil) |
Mohamed Abukar | ed11919 | 2020-11-19 14:38:22 +0200 | [diff] [blame] | 483 | response := executeRequest(req, nil) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 484 | |
| 485 | return response.Body.String() |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 486 | } |