2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
32 "github.com/gorilla/mux"
33 "github.com/spf13/viper"
34 "github.com/stretchr/testify/assert"
42 type Consumer struct{}
44 func (m Consumer) Consume(params *RMRParams) (err error) {
45 Sdl.Store("myKey", params.Payload)
50 func TestMain(m *testing.M) {
51 os.Setenv("SERVICE_RICXAPP_UEEC_HTTP_PORT", "tcp://localhost:8080")
52 os.Setenv("SERVICE_RICXAPP_UEEC_RMR_PORT", "tcp://localhost:4561")
53 go RunWithParams(Consumer{}, viper.GetBool("controls.waitForSdl"))
54 time.Sleep(time.Duration(5) * time.Second)
59 func TestGetHealthCheckRetursServiceUnavailableError(t *testing.T) {
60 Logger.Info("CASE: TestGetHealthCheckRetursServiceUnavailableError")
61 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
62 /*response :=*/ executeRequest(req, nil)
64 //checkResponseCode(t, http.StatusServiceUnavailable, response.Code)
67 func TestGetHealthCheckReturnsSuccess(t *testing.T) {
68 Logger.Info("CASE: TestGetHealthCheckReturnsSuccess")
69 for Rmr.IsReady() == false {
70 time.Sleep(time.Duration(2) * time.Second)
73 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
74 response := executeRequest(req, nil)
76 checkResponseCode(t, http.StatusOK, response.Code)
79 func TestInjectQuerySinglePath(t *testing.T) {
80 Logger.Info("CASE: TestInjectQuerySinglePath")
81 var handler = func(w http.ResponseWriter, r *http.Request) {
84 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar")
86 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar", nil)
87 response := executeRequest(req, nil)
88 checkResponseCode(t, http.StatusOK, response.Code)
91 func TestInjectQueryMultiplePaths(t *testing.T) {
92 Logger.Info("CASE: TestInjectQueryMultiplePaths")
93 var handler = func(w http.ResponseWriter, r *http.Request) {
96 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
98 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil)
99 response := executeRequest(req, nil)
100 checkResponseCode(t, http.StatusOK, response.Code)
103 func TestInjectQueryFailures(t *testing.T) {
104 Logger.Info("CASE: TestInjectQueryFailures")
105 var handler = func(w http.ResponseWriter, r *http.Request) {
108 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
110 req, _ := http.NewRequest("GET", "/ric/v1/user?invalid=bar&no=mykey", nil)
111 response := executeRequest(req, nil)
112 checkResponseCode(t, http.StatusNotFound, response.Code)
115 func TestMessagesReceivedSuccessfully(t *testing.T) {
116 Logger.Info("CASE: TestMessagesReceivedSuccessfully")
117 time.Sleep(time.Duration(5) * time.Second)
118 for i := 0; i < 100; i++ {
119 params := &RMRParams{}
122 params.Payload = []byte{1, 2, 3, 4, 5, 6}
123 params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
124 params.Xid = "TestXID"
129 Rmr.SendWithRetry(params, false, 1)
132 Rmr.RegisterMetrics()
134 // Allow time to process the messages
135 time.Sleep(time.Duration(5) * time.Second)
137 waitForSdl := viper.GetBool("controls.waitForSdl")
138 stats := getMetrics(t)
139 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 100") {
140 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
143 if !strings.Contains(stats, "ricxapp_RMR_Received 100") {
144 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
147 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
148 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
151 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
152 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
155 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 100") {
156 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
159 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
160 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
164 func TestMessagesReceivedSuccessfullyUsingWh(t *testing.T) {
165 Logger.Info("CASE: TestMessagesReceivedSuccessfullyUsingWh")
166 time.Sleep(time.Duration(5) * time.Second)
167 whid := Rmr.Openwh("localhost:4560")
168 time.Sleep(time.Duration(1) * time.Second)
169 for i := 0; i < 100; i++ {
170 params := &RMRParams{}
173 params.Payload = []byte{1, 2, 3, 4, 5, 6}
174 params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
175 params.Xid = "TestXID"
176 params.Whid = int(whid)
179 Logger.Info("%+v", params.String())
185 // Allow time to process the messages
186 time.Sleep(time.Duration(5) * time.Second)
188 waitForSdl := viper.GetBool("controls.waitForSdl")
189 stats := getMetrics(t)
190 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") {
191 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
194 if !strings.Contains(stats, "ricxapp_RMR_Received 200") {
195 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
198 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
199 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
202 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
203 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
206 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 200") {
207 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
210 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
211 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
213 Rmr.Closewh(int(whid))
216 func TestMessagesReceivedSuccessfullyUsingWhCall(t *testing.T) {
217 Logger.Info("CASE: TestMessagesReceivedSuccessfullyUsingWhCall")
218 time.Sleep(time.Duration(5) * time.Second)
219 whid := Rmr.Openwh("localhost:4560")
220 params := &RMRParams{}
221 params.Payload = []byte("newrt|start\nnewrt|end\n")
222 params.Whid = int(whid)
224 params.Timeout = 1000
225 Rmr.SendCallMsg(params)
227 // Allow time to process the messages
228 time.Sleep(time.Duration(2) * time.Second)
230 waitForSdl := viper.GetBool("controls.waitForSdl")
231 stats := getMetrics(t)
232 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") {
233 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
236 if !strings.Contains(stats, "ricxapp_RMR_Received 201") {
237 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
240 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 1") {
241 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
244 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
245 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
248 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 201") {
249 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
252 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
253 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
255 Rmr.Closewh(int(whid))
258 func TestSubscribeChannels(t *testing.T) {
259 Logger.Info("CASE: TestSubscribeChannels")
260 if !viper.GetBool("controls.waitForSdl") {
264 var NotificationCb = func(ch string, events ...string) {
265 if ch != "channel1" {
266 t.Errorf("Error: Callback function called with incorrect params")
270 if err := Sdl.Subscribe(NotificationCb, "channel1"); err != nil {
271 t.Errorf("Error: Subscribe failed: %v", err)
273 time.Sleep(time.Duration(2) * time.Second)
275 if err := Sdl.StoreAndPublish("channel1", "event", "key1", "data1"); err != nil {
276 t.Errorf("Error: Publish failed: %v", err)
280 Sdl.MStoreAndPublish([]string{"channel1"}, "event", "key1", "data1")
283 func TestGetRicMessageSuccess(t *testing.T) {
284 Logger.Info("CASE: TestGetRicMessageSuccess")
285 id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
286 if !ok || id != 12010 {
287 t.Errorf("Error: GetRicMessageId failed: id=%d", id)
290 name := Rmr.GetRicMessageName(12010)
291 if name != "RIC_SUB_REQ" {
292 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
296 func TestGetRicMessageFails(t *testing.T) {
297 Logger.Info("CASE: TestGetRicMessageFails")
298 ok := Rmr.IsRetryError(&RMRParams{status: 0})
300 t.Errorf("Error: IsRetryError returned wrong value")
303 ok = Rmr.IsRetryError(&RMRParams{status: 10})
305 t.Errorf("Error: IsRetryError returned wrong value")
308 ok = Rmr.IsNoEndPointError(&RMRParams{status: 5})
310 t.Errorf("Error: IsNoEndPointError returned wrong value")
313 ok = Rmr.IsNoEndPointError(&RMRParams{status: 2})
315 t.Errorf("Error: IsNoEndPointError returned wrong value")
319 func TestIsErrorFunctions(t *testing.T) {
320 Logger.Info("CASE: TestIsErrorFunctions")
321 id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
322 if !ok || id != 12010 {
323 t.Errorf("Error: GetRicMessageId failed: id=%d", id)
326 name := Rmr.GetRicMessageName(12010)
327 if name != "RIC_SUB_REQ" {
328 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
332 func TestAddConfigChangeListener(t *testing.T) {
333 Logger.Info("CASE: AddConfigChangeListener")
334 AddConfigChangeListener(func(f string) {})
337 func TestConfigAccess(t *testing.T) {
338 Logger.Info("CASE: AddConfigChangeListener")
340 assert.Equal(t, Config.GetString("name"), "xapp")
341 assert.Equal(t, Config.GetInt("controls.logger.level"), 3)
342 assert.Equal(t, Config.GetUint32("controls.logger.level"), uint32(3))
343 assert.Equal(t, Config.GetBool("controls.waitForSdl"), false)
344 Config.Get("controls")
345 Config.GetStringSlice("messaging.ports")
346 Config.GetStringMap("messaging.ports")
347 Config.IsSet("messaging")
350 func TestPublishConfigChange(t *testing.T) {
351 Logger.Info("CASE: AddConfigChangeListener")
352 PublishConfigChange("testApp", "values")
355 func TestNewSubscriber(t *testing.T) {
356 Logger.Info("CASE: TestNewSubscriber")
357 assert.NotNil(t, NewSubscriber("", 0), "NewSubscriber failed")
360 func TestNewRMRClient(t *testing.T) {
361 c := map[string]interface{}{"protPort": "tcp:4560"}
363 assert.NotNil(t, NewRMRClient(), "NewRMRClient failed")
365 params := &RMRParams{}
368 params.Payload = []byte{1, 2, 3, 4, 5, 6}
369 Rmr.SendWithRetry(params, false, 1)
372 func TestInjectRoutePrefix(t *testing.T) {
373 Logger.Info("CASE: TestInjectRoutePrefix")
374 assert.NotNil(t, Resource.InjectRoutePrefix("test", nil), "InjectRoutePrefix failed")
377 func TestInjectStatusCb(t *testing.T) {
378 Logger.Info("CASE: TestInjectStatusCb")
380 var f = func() bool {
383 Resource.InjectStatusCb(f)
384 Resource.CheckStatus()
387 func TestSdlInterfaces(t *testing.T) {
389 Sdl.MRead([]string{"myKey"})
390 Sdl.ReadAllKeys("myKey")
391 Sdl.Store("myKey", "Values")
392 Sdl.MStore("myKey", "Values")
393 Sdl.RegisterMetrics()
396 var NotificationCb = func(ch string, events ...string) {}
397 Sdl.Subscribe(NotificationCb, "channel1")
398 Sdl.MSubscribe(NotificationCb, "channel1", "channel2")
399 Sdl.MStoreAndPublish([]string{"channel1"}, "event", "key1", "data1")
402 func TestRnibInterfaces(t *testing.T) {
403 Rnib.GetNodeb("test-gnb")
404 Rnib.GetCellList("test-gnb")
407 Rnib.GetCountGnbList()
408 Rnib.GetCell("test-gnb", 0)
409 Rnib.GetCell("test-gnb", 0)
410 Rnib.GetCellById(0, "cell-1")
413 var NotificationCb = func(ch string, events ...string) {}
414 Rnib.Subscribe(NotificationCb, "channel1")
415 Rnib.StoreAndPublish("channel1", "event", "key1", "data1")
418 func TestLogger(t *testing.T) {
419 Logger.Error("CASE: TestNewSubscriber")
420 Logger.Warn("CASE: TestNewSubscriber")
424 func TestConfigHandler(t *testing.T) {
425 Logger.Error("CASE: TestConfigHandler")
426 req, _ := http.NewRequest("POST", "/ric/v1/cm/appname", bytes.NewBuffer([]byte{}))
427 handleFunc := http.HandlerFunc(configHandler)
428 executeRequest(req, handleFunc)
431 func TestappconfigHandler(t *testing.T) {
432 Logger.Error("CASE: TestappconfigHandler")
433 req, _ := http.NewRequest("POST", "/ric/v1/config", bytes.NewBuffer([]byte{}))
434 handleFunc := http.HandlerFunc(appconfigHandler)
435 executeRequest(req, handleFunc)
438 func TestConfigChange(t *testing.T) {
439 Logger.Error("CASE: TestConfigChange: %s", os.Getenv("CFG_FILE"))
441 input, err := ioutil.ReadFile(os.Getenv("CFG_FILE"))
442 assert.Equal(t, err, nil)
444 err = ioutil.WriteFile(os.Getenv("CFG_FILE"), input, 0644)
445 assert.Equal(t, err, nil)
448 func TestRegisterXapp(t *testing.T) {
449 Logger.Error("CASE: TestRegisterXapp")
453 func TestDeregisterXapp(t *testing.T) {
454 Logger.Error("CASE: TestDeregisterXapp")
458 func TestMisc(t *testing.T) {
459 Logger.Info("CASE: TestMisc")
462 SetReadyCB(func(interface{}) {}, "")
466 getService("ueec", SERVICE_HTTP)
474 mb := Rmr.Allocate(100)
475 Rmr.ReAllocate(mb, 200)
477 NewMetrics("", "", Resource.router)
480 func TestTeardown(t *testing.T) {
481 Logger.Info("CASE: TestTeardown")
482 Sdl.Delete([]string{"myKey"})
486 Rnib.GetNodebByGlobalNbId(1, &RNIBGlobalNbId{})
487 Rnib.SaveNodeb(&RNIBNbIdentity{}, &RNIBNodebInfo{})
488 go Sdl.TestConnection()
489 time.Sleep(time.Duration(2) * time.Second)
493 func executeRequest(req *http.Request, handleR http.HandlerFunc) *httptest.ResponseRecorder {
494 rr := httptest.NewRecorder()
496 vars := map[string]string{"name": "myxapp"}
497 req = mux.SetURLVars(req, vars)
498 handleR.ServeHTTP(rr, req)
500 vars := map[string]string{"id": "1"}
501 req = mux.SetURLVars(req, vars)
502 Resource.router.ServeHTTP(rr, req)
507 func checkResponseCode(t *testing.T, expected, actual int) {
508 if expected != actual {
509 t.Errorf("Expected response code %d. Got %d\n", expected, actual)
513 func getMetrics(t *testing.T) string {
514 req, _ := http.NewRequest("GET", "/ric/v1/metrics", nil)
515 response := executeRequest(req, nil)
517 return response.Body.String()