blob: abf9345e6d04895c86185aa68e0426cfabb51de7 [file] [log] [blame]
Mohamed Abukar2e78e422019-06-02 11:45:52 +03001/*
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
20package xapp
21
22import (
23 "github.com/gorilla/mux"
Juha Hyttinen3cfee962020-02-13 13:51:00 +020024 "github.com/spf13/viper"
Mohamed Abukar2e78e422019-06-02 11:45:52 +030025 "net/http"
26 "net/http/httptest"
27 "os"
28 "strings"
29 "testing"
30 "time"
31)
32
Juha Hyttinen3cfee962020-02-13 13:51:00 +020033//var _ = func() bool {
34// testing.Init()
35// return true
36//}()
Mohamed Abukar5120ec12020-02-04 11:01:24 +020037
Juha Hyttinen3cfee962020-02-13 13:51:00 +020038type Consumer struct{}
Mohamed Abukar2e78e422019-06-02 11:45:52 +030039
Mohamed Abukarf11ab7a2019-08-14 16:55:01 +030040func (m Consumer) Consume(params *RMRParams) (err error) {
Mohamed Abukarf11ab7a2019-08-14 16:55:01 +030041 Sdl.Store("myKey", params.Payload)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030042 return nil
43}
44
45// Test cases
46func TestMain(m *testing.M) {
Mohamed Abukar791a77f2020-02-12 19:08:42 +020047 go RunWithParams(Consumer{}, viper.GetBool("db.waitForSdl"))
Mohamed Abukar5120ec12020-02-04 11:01:24 +020048 time.Sleep(time.Duration(5) * time.Second)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030049 code := m.Run()
50 os.Exit(code)
51}
52
53func TestGetHealthCheckRetursServiceUnavailableError(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030054 Logger.Info("CASE: TestGetHealthCheckRetursServiceUnavailableError")
Mohamed Abukar2e78e422019-06-02 11:45:52 +030055 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
Mohamed Abukar5120ec12020-02-04 11:01:24 +020056 /*response :=*/ executeRequest(req)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030057
Mohamed Abukar5120ec12020-02-04 11:01:24 +020058 //checkResponseCode(t, http.StatusServiceUnavailable, response.Code)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030059}
60
61func TestGetHealthCheckReturnsSuccess(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030062 Logger.Info("CASE: TestGetHealthCheckReturnsSuccess")
Mohamed Abukar2e78e422019-06-02 11:45:52 +030063 for Rmr.IsReady() == false {
64 time.Sleep(time.Duration(2) * time.Second)
65 }
66
67 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
68 response := executeRequest(req)
69
70 checkResponseCode(t, http.StatusOK, response.Code)
71}
72
73func TestInjectQuerySinglePath(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030074 Logger.Info("CASE: TestInjectQuerySinglePath")
Mohamed Abukar2e78e422019-06-02 11:45:52 +030075 var handler = func(w http.ResponseWriter, r *http.Request) {
76 }
77
78 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar")
79
80 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar", nil)
81 response := executeRequest(req)
82 checkResponseCode(t, http.StatusOK, response.Code)
83}
84
85func TestInjectQueryMultiplePaths(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030086 Logger.Info("CASE: TestInjectQueryMultiplePaths")
Mohamed Abukar2e78e422019-06-02 11:45:52 +030087 var handler = func(w http.ResponseWriter, r *http.Request) {
88 }
89
90 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
91
92 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil)
93 response := executeRequest(req)
94 checkResponseCode(t, http.StatusOK, response.Code)
95}
96
97func TestInjectQueryFailures(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030098 Logger.Info("CASE: TestInjectQueryFailures")
Mohamed Abukar2e78e422019-06-02 11:45:52 +030099 var handler = func(w http.ResponseWriter, r *http.Request) {
100 }
101
102 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
103
104 req, _ := http.NewRequest("GET", "/ric/v1/user?invalid=bar&no=mykey", nil)
105 response := executeRequest(req)
106 checkResponseCode(t, http.StatusNotFound, response.Code)
107}
108
109func TestMessagesReceivedSuccessfully(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +0300110 Logger.Info("CASE: TestMessagesReceivedSuccessfully")
Mohamed Abukar9568a2d2020-02-18 16:50:32 +0200111 time.Sleep(time.Duration(5) * time.Second)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300112 for i := 0; i < 100; i++ {
Mohamed Abukarf11ab7a2019-08-14 16:55:01 +0300113 params := &RMRParams{}
114 params.Mtype = 10004
115 params.SubId = -1
116 params.Payload = []byte{1, 2, 3, 4, 5, 6}
Mohamed Abukar19461e12019-08-23 08:46:11 +0300117 params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
Mohamed Abukarf11ab7a2019-08-14 16:55:01 +0300118 params.Xid = "TestXID"
119 Rmr.SendMsg(params)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300120 }
121
122 // Allow time to process the messages
Juha Hyttinenf619d032020-05-07 12:42:26 +0300123 time.Sleep(time.Duration(5) * time.Second)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300124
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200125 waitForSdl := viper.GetBool("db.waitForSdl")
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300126 stats := getMetrics(t)
127 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 100") {
Mohamed Abukar9568a2d2020-02-18 16:50:32 +0200128 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300129 }
130
131 if !strings.Contains(stats, "ricxapp_RMR_Received 100") {
Mohamed Abukar9568a2d2020-02-18 16:50:32 +0200132 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300133 }
134
135 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
136 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
137 }
138
139 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
140 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
141 }
142
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200143 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 100") {
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300144 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
145 }
146
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200147 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300148 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
149 }
150}
151
wahidwad0a2712020-03-04 09:54:15 +0000152func TestMessagesReceivedSuccessfullyUsingWh(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +0300153 Logger.Info("CASE: TestMessagesReceivedSuccessfullyUsingWh")
wahidwad0a2712020-03-04 09:54:15 +0000154 time.Sleep(time.Duration(5) * time.Second)
155 whid := Rmr.Openwh("localhost:4560")
156 time.Sleep(time.Duration(1) * time.Second)
157 for i := 0; i < 100; i++ {
158 params := &RMRParams{}
159 params.Mtype = 10004
160 params.SubId = -1
161 params.Payload = []byte{1, 2, 3, 4, 5, 6}
162 params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
163 params.Xid = "TestXID"
164 params.Whid = int(whid)
165 Rmr.SendMsg(params)
166 }
167
168 // Allow time to process the messages
Juha Hyttinenf619d032020-05-07 12:42:26 +0300169 time.Sleep(time.Duration(5) * time.Second)
wahidwad0a2712020-03-04 09:54:15 +0000170
171 waitForSdl := viper.GetBool("db.waitForSdl")
172 stats := getMetrics(t)
173 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") {
174 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
175 }
176
177 if !strings.Contains(stats, "ricxapp_RMR_Received 200") {
178 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
179 }
180
181 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
182 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
183 }
184
185 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
186 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
187 }
188
189 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 200") {
190 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
191 }
192
193 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
194 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
195 }
196 Rmr.Closewh(int(whid))
197}
198
wahidwe0948fe2020-03-19 14:49:41 +0000199func TestMessagesReceivedSuccessfullyUsingWhCall(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +0300200 Logger.Info("CASE: TestMessagesReceivedSuccessfullyUsingWhCall")
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300201 time.Sleep(time.Duration(5) * time.Second)
202 whid := Rmr.Openwh("localhost:4560")
203 params := &RMRParams{}
204 params.Payload = []byte("newrt|start\nnewrt|end\n")
205 params.Whid = int(whid)
206 params.Callid = 4
207 params.Timeout = 1000
208 Rmr.SendCallMsg(params)
wahidwe0948fe2020-03-19 14:49:41 +0000209
210 // Allow time to process the messages
211 time.Sleep(time.Duration(2) * time.Second)
212
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300213 waitForSdl := viper.GetBool("db.waitForSdl")
214 stats := getMetrics(t)
215 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") {
216 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
217 }
wahidwe0948fe2020-03-19 14:49:41 +0000218
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300219 if !strings.Contains(stats, "ricxapp_RMR_Received 201") {
220 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
221 }
wahidwe0948fe2020-03-19 14:49:41 +0000222
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300223 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 1") {
224 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
225 }
wahidwe0948fe2020-03-19 14:49:41 +0000226
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300227 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
228 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
229 }
wahidwe0948fe2020-03-19 14:49:41 +0000230
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300231 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 201") {
232 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
233 }
wahidwe0948fe2020-03-19 14:49:41 +0000234
Mohamed Abukar5953f7e2020-04-02 10:08:14 +0300235 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
236 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
237 }
238 Rmr.Closewh(int(whid))
wahidwe0948fe2020-03-19 14:49:41 +0000239}
240
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300241func TestSubscribeChannels(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +0300242 Logger.Info("CASE: TestSubscribeChannels")
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200243 if !viper.GetBool("db.waitForSdl") {
244 return
245 }
246
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300247 var NotificationCb = func(ch string, events ...string) {
248 if ch != "channel1" {
249 t.Errorf("Error: Callback function called with incorrect params")
250 }
251 }
252
253 if err := Sdl.Subscribe(NotificationCb, "channel1"); err != nil {
254 t.Errorf("Error: Subscribe failed: %v", err)
255 }
256 time.Sleep(time.Duration(2) * time.Second)
257
258 if err := Sdl.StoreAndPublish("channel1", "event", "key1", "data1"); err != nil {
259 t.Errorf("Error: Publish failed: %v", err)
260 }
261}
262
Mohamed Abukar775722c2019-06-10 16:41:57 +0300263func TestGetRicMessageSuccess(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +0300264 Logger.Info("CASE: TestGetRicMessageSuccess")
Mohamed Abukar775722c2019-06-10 16:41:57 +0300265 id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
266 if !ok || id != 12010 {
267 t.Errorf("Error: GetRicMessageId failed: id=%d", id)
268 }
269
270 name := Rmr.GetRicMessageName(12010)
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300271 if name != "RIC_SUB_REQ" {
Mohamed Abukar775722c2019-06-10 16:41:57 +0300272 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
273 }
274}
275
276func TestGetRicMessageFails(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +0300277 Logger.Info("CASE: TestGetRicMessageFails")
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200278 ok := Rmr.IsRetryError(&RMRParams{status: 0})
Mohamed Abukar775722c2019-06-10 16:41:57 +0300279 if ok {
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200280 t.Errorf("Error: IsRetryError returned wrong value")
Mohamed Abukar775722c2019-06-10 16:41:57 +0300281 }
282
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200283 ok = Rmr.IsRetryError(&RMRParams{status: 10})
284 if !ok {
285 t.Errorf("Error: IsRetryError returned wrong value")
286 }
287
288 ok = Rmr.IsNoEndPointError(&RMRParams{status: 5})
289 if ok {
290 t.Errorf("Error: IsNoEndPointError returned wrong value")
291 }
292
293 ok = Rmr.IsNoEndPointError(&RMRParams{status: 2})
294 if !ok {
295 t.Errorf("Error: IsNoEndPointError returned wrong value")
296 }
297}
298
299func TestIsErrorFunctions(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +0300300 Logger.Info("CASE: TestIsErrorFunctions")
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200301 id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
302 if !ok || id != 12010 {
303 t.Errorf("Error: GetRicMessageId failed: id=%d", id)
304 }
305
306 name := Rmr.GetRicMessageName(12010)
307 if name != "RIC_SUB_REQ" {
308 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
Mohamed Abukar775722c2019-06-10 16:41:57 +0300309 }
310}
311
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300312func TestTeardown(t *testing.T) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +0300313 Logger.Info("CASE: TestTeardown")
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300314 Sdl.Clear()
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300315}
316
317// Helper functions
318func executeRequest(req *http.Request) *httptest.ResponseRecorder {
319 rr := httptest.NewRecorder()
320 vars := map[string]string{"id": "1"}
321 req = mux.SetURLVars(req, vars)
322 Resource.router.ServeHTTP(rr, req)
323
324 return rr
325}
326
327func checkResponseCode(t *testing.T, expected, actual int) {
328 if expected != actual {
329 t.Errorf("Expected response code %d. Got %d\n", expected, actual)
330 }
331}
332
333func getMetrics(t *testing.T) string {
334 req, _ := http.NewRequest("GET", "/ric/v1/metrics", nil)
335 response := executeRequest(req)
336
337 return response.Body.String()
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300338}