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: TestConfigAccess")
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: TestPublishConfigChange")
352 PublishConfigChange("testApp", "values")
353 ReadConfig("testApp")
356 func TestNewSubscriber(t *testing.T) {
357 Logger.Info("CASE: TestNewSubscriber")
358 assert.NotNil(t, NewSubscriber("", 0), "NewSubscriber failed")
361 func TestNewRMRClient(t *testing.T) {
362 c := map[string]interface{}{"protPort": "tcp:4560"}
364 assert.NotNil(t, NewRMRClient(), "NewRMRClient failed")
366 params := &RMRParams{}
369 params.Payload = []byte{1, 2, 3, 4, 5, 6}
370 Rmr.SendWithRetry(params, false, 1)
373 func TestInjectRoutePrefix(t *testing.T) {
374 Logger.Info("CASE: TestInjectRoutePrefix")
375 assert.NotNil(t, Resource.InjectRoutePrefix("test", nil), "InjectRoutePrefix failed")
378 func TestInjectStatusCb(t *testing.T) {
379 Logger.Info("CASE: TestInjectStatusCb")
381 var f = func() bool {
384 Resource.InjectStatusCb(f)
385 Resource.CheckStatus()
388 func TestSdlInterfaces(t *testing.T) {
390 Sdl.MRead([]string{"myKey"})
391 Sdl.ReadAllKeys("myKey")
392 Sdl.Store("myKey", "Values")
393 Sdl.MStore("myKey", "Values")
394 Sdl.RegisterMetrics()
395 Sdl.UpdateStatCounter("Stored")
398 var NotificationCb = func(ch string, events ...string) {}
399 Sdl.Subscribe(NotificationCb, "channel1")
400 Sdl.MSubscribe(NotificationCb, "channel1", "channel2")
401 Sdl.StoreAndPublish("channel1", "event", "key1", "data1")
402 Sdl.MStoreAndPublish([]string{"channel1"}, "event", "key1", "data1")
405 func TestRnibInterfaces(t *testing.T) {
406 Rnib.GetNodeb("test-gnb")
407 Rnib.GetCellList("test-gnb")
410 Rnib.GetCountGnbList()
411 Rnib.GetCell("test-gnb", 0)
412 Rnib.GetCell("test-gnb", 0)
413 Rnib.GetCellById(0, "cell-1")
416 var NotificationCb = func(ch string, events ...string) {}
417 Rnib.Subscribe(NotificationCb, "channel1")
418 Rnib.StoreAndPublish("channel1", "event", "key1", "data1")
421 func TestLogger(t *testing.T) {
422 Logger.Error("CASE: TestNewSubscriber")
423 Logger.Warn("CASE: TestNewSubscriber")
427 func TestConfigHandler(t *testing.T) {
428 Logger.Error("CASE: TestConfigHandler")
429 req, _ := http.NewRequest("POST", "/ric/v1/cm/appname", bytes.NewBuffer([]byte{}))
430 handleFunc := http.HandlerFunc(configHandler)
431 executeRequest(req, handleFunc)
434 func TestappconfigHandler(t *testing.T) {
435 Logger.Error("CASE: TestappconfigHandler")
436 req, _ := http.NewRequest("POST", "/ric/v1/config", bytes.NewBuffer([]byte{}))
437 handleFunc := http.HandlerFunc(appconfigHandler)
438 executeRequest(req, handleFunc)
441 func TestConfigChange(t *testing.T) {
442 Logger.Error("CASE: TestConfigChange: %s", os.Getenv("CFG_FILE"))
444 input, err := ioutil.ReadFile(os.Getenv("CFG_FILE"))
445 assert.Equal(t, err, nil)
447 err = ioutil.WriteFile(os.Getenv("CFG_FILE"), input, 0644)
448 assert.Equal(t, err, nil)
451 func TestRegisterXapp(t *testing.T) {
452 Logger.Error("CASE: TestRegisterXapp")
456 func TestDeregisterXapp(t *testing.T) {
457 Logger.Error("CASE: TestDeregisterXapp")
461 func TestMisc(t *testing.T) {
462 Logger.Info("CASE: TestMisc")
465 SetReadyCB(func(interface{}) {}, "")
469 getService("ueec", SERVICE_HTTP)
477 mb := Rmr.Allocate(100)
478 Rmr.ReAllocate(mb, 200)
480 NewMetrics("", "", Resource.router)
483 func TestTeardown(t *testing.T) {
484 Logger.Info("CASE: TestTeardown")
485 Sdl.Delete([]string{"myKey"})
489 Rnib.GetNodebByGlobalNbId(1, &RNIBGlobalNbId{})
490 Rnib.SaveNodeb(&RNIBNbIdentity{}, &RNIBNodebInfo{})
491 go Sdl.TestConnection()
492 time.Sleep(time.Duration(2) * time.Second)
496 func executeRequest(req *http.Request, handleR http.HandlerFunc) *httptest.ResponseRecorder {
497 rr := httptest.NewRecorder()
499 vars := map[string]string{"name": "myxapp"}
500 req = mux.SetURLVars(req, vars)
501 handleR.ServeHTTP(rr, req)
503 vars := map[string]string{"id": "1"}
504 req = mux.SetURLVars(req, vars)
505 Resource.router.ServeHTTP(rr, req)
510 func checkResponseCode(t *testing.T, expected, actual int) {
511 if expected != actual {
512 t.Errorf("Expected response code %d. Got %d\n", expected, actual)
516 func getMetrics(t *testing.T) string {
517 req, _ := http.NewRequest("GET", "/ric/v1/metrics", nil)
518 response := executeRequest(req, nil)
520 return response.Body.String()