Backward compatible with old xApp descriptor
[ric-plt/xapp-frame.git] / pkg / xapp / xapp_test.go
index 06345af..baafa16 100755 (executable)
@@ -21,6 +21,7 @@ package xapp
 
 import (
        "github.com/gorilla/mux"
+       "github.com/spf13/viper"
        "net/http"
        "net/http/httptest"
        "os"
@@ -29,32 +30,36 @@ import (
        "time"
 )
 
-type Consumer struct {
-}
+//var _ = func() bool {
+//     testing.Init()
+//     return true
+//}()
+
+type Consumer struct{}
 
-func (m Consumer) Consume(mtype, sid, len int, payload []byte) (err error) {
-       Sdl.Store("myKey", payload)
+func (m Consumer) Consume(params *RMRParams) (err error) {
+       Sdl.Store("myKey", params.Payload)
        return nil
 }
 
 // Test cases
 func TestMain(m *testing.M) {
-       // Just run on the background (for coverage)
-       go Run(Consumer{})
-
+       go RunWithParams(Consumer{}, viper.GetBool("controls.waitForSdl"))
+       time.Sleep(time.Duration(5) * time.Second)
        code := m.Run()
        os.Exit(code)
 }
 
 func TestGetHealthCheckRetursServiceUnavailableError(t *testing.T) {
+       Logger.Info("CASE: TestGetHealthCheckRetursServiceUnavailableError")
        req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
-       response := executeRequest(req)
+       /*response :=*/ executeRequest(req)
 
-       checkResponseCode(t, http.StatusServiceUnavailable, response.Code)
+       //checkResponseCode(t, http.StatusServiceUnavailable, response.Code)
 }
 
 func TestGetHealthCheckReturnsSuccess(t *testing.T) {
-       // Wait until RMR is up-and-running
+       Logger.Info("CASE: TestGetHealthCheckReturnsSuccess")
        for Rmr.IsReady() == false {
                time.Sleep(time.Duration(2) * time.Second)
        }
@@ -66,6 +71,7 @@ func TestGetHealthCheckReturnsSuccess(t *testing.T) {
 }
 
 func TestInjectQuerySinglePath(t *testing.T) {
+       Logger.Info("CASE: TestInjectQuerySinglePath")
        var handler = func(w http.ResponseWriter, r *http.Request) {
        }
 
@@ -77,6 +83,7 @@ func TestInjectQuerySinglePath(t *testing.T) {
 }
 
 func TestInjectQueryMultiplePaths(t *testing.T) {
+       Logger.Info("CASE: TestInjectQueryMultiplePaths")
        var handler = func(w http.ResponseWriter, r *http.Request) {
        }
 
@@ -88,6 +95,7 @@ func TestInjectQueryMultiplePaths(t *testing.T) {
 }
 
 func TestInjectQueryFailures(t *testing.T) {
+       Logger.Info("CASE: TestInjectQueryFailures")
        var handler = func(w http.ResponseWriter, r *http.Request) {
        }
 
@@ -99,20 +107,29 @@ func TestInjectQueryFailures(t *testing.T) {
 }
 
 func TestMessagesReceivedSuccessfully(t *testing.T) {
+       Logger.Info("CASE: TestMessagesReceivedSuccessfully")
+       time.Sleep(time.Duration(5) * time.Second)
        for i := 0; i < 100; i++ {
-               Rmr.Send(10004, 1111, 100, []byte{1, 2, 3, 4, 5, 6})
+               params := &RMRParams{}
+               params.Mtype = 10004
+               params.SubId = -1
+               params.Payload = []byte{1, 2, 3, 4, 5, 6}
+               params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
+               params.Xid = "TestXID"
+               Rmr.SendMsg(params)
        }
 
        // Allow time to process the messages
-       time.Sleep(time.Duration(2) * time.Second)
+       time.Sleep(time.Duration(5) * time.Second)
 
+       waitForSdl := viper.GetBool("controls.waitForSdl")
        stats := getMetrics(t)
        if !strings.Contains(stats, "ricxapp_RMR_Transmitted 100") {
-               t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect")
+               t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
        }
 
        if !strings.Contains(stats, "ricxapp_RMR_Received 100") {
-               t.Errorf("Error: ricxapp_RMR_Received value incorrect")
+               t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
        }
 
        if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
@@ -123,26 +140,110 @@ func TestMessagesReceivedSuccessfully(t *testing.T) {
                t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
        }
 
-       if !strings.Contains(stats, "ricxapp_SDL_Stored 100") {
+       if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 100") {
                t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
        }
 
-       if !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
+       if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
                t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
        }
 }
 
-func TestGetgNBList(t *testing.T) {
-       Rnib.Store("Kiikale", "Hello")
-       Rnib.Store("mykey", "myval")
+func TestMessagesReceivedSuccessfullyUsingWh(t *testing.T) {
+       Logger.Info("CASE: TestMessagesReceivedSuccessfullyUsingWh")
+       time.Sleep(time.Duration(5) * time.Second)
+       whid := Rmr.Openwh("localhost:4560")
+       time.Sleep(time.Duration(1) * time.Second)
+       for i := 0; i < 100; i++ {
+               params := &RMRParams{}
+               params.Mtype = 10004
+               params.SubId = -1
+               params.Payload = []byte{1, 2, 3, 4, 5, 6}
+               params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
+               params.Xid = "TestXID"
+               params.Whid = int(whid)
+               Rmr.SendMsg(params)
+       }
+
+       // Allow time to process the messages
+       time.Sleep(time.Duration(5) * time.Second)
+
+       waitForSdl := viper.GetBool("controls.waitForSdl")
+       stats := getMetrics(t)
+       if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") {
+               t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
+       }
+
+       if !strings.Contains(stats, "ricxapp_RMR_Received 200") {
+               t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
+       }
+
+       if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
+               t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
+       }
+
+       if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
+               t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
+       }
 
-       v, _ := Rnib.GetgNBList()
-       if v["Kiikale"] != "Hello" || v["mykey"] != "myval" {
-               t.Errorf("Error: GetgNBList failed!")
+       if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 200") {
+               t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
+       }
+
+       if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
+               t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
        }
+       Rmr.Closewh(int(whid))
+}
+
+func TestMessagesReceivedSuccessfullyUsingWhCall(t *testing.T) {
+       Logger.Info("CASE: TestMessagesReceivedSuccessfullyUsingWhCall")
+       time.Sleep(time.Duration(5) * time.Second)
+       whid := Rmr.Openwh("localhost:4560")
+       params := &RMRParams{}
+       params.Payload = []byte("newrt|start\nnewrt|end\n")
+       params.Whid = int(whid)
+       params.Callid = 4
+       params.Timeout = 1000
+       Rmr.SendCallMsg(params)
+
+       // Allow time to process the messages
+       time.Sleep(time.Duration(2) * time.Second)
+
+       waitForSdl := viper.GetBool("controls.waitForSdl")
+       stats := getMetrics(t)
+       if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") {
+               t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
+       }
+
+       if !strings.Contains(stats, "ricxapp_RMR_Received 201") {
+               t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
+       }
+
+       if !strings.Contains(stats, "ricxapp_RMR_TransmitError 1") {
+               t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
+       }
+
+       if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
+               t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
+       }
+
+       if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 201") {
+               t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
+       }
+
+       if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
+               t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
+       }
+       Rmr.Closewh(int(whid))
 }
 
 func TestSubscribeChannels(t *testing.T) {
+       Logger.Info("CASE: TestSubscribeChannels")
+       if !viper.GetBool("controls.waitForSdl") {
+               return
+       }
+
        var NotificationCb = func(ch string, events ...string) {
                if ch != "channel1" {
                        t.Errorf("Error: Callback function called with incorrect params")
@@ -160,32 +261,57 @@ func TestSubscribeChannels(t *testing.T) {
 }
 
 func TestGetRicMessageSuccess(t *testing.T) {
+       Logger.Info("CASE: TestGetRicMessageSuccess")
        id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
        if !ok || id != 12010 {
                t.Errorf("Error: GetRicMessageId failed: id=%d", id)
        }
 
        name := Rmr.GetRicMessageName(12010)
-       if name !=  "RIC_SUB_REQ" {
+       if name != "RIC_SUB_REQ" {
                t.Errorf("Error: GetRicMessageName failed: name=%s", name)
        }
 }
 
 func TestGetRicMessageFails(t *testing.T) {
-       id, ok := Rmr.GetRicMessageId("INVALID")
+       Logger.Info("CASE: TestGetRicMessageFails")
+       ok := Rmr.IsRetryError(&RMRParams{status: 0})
        if ok {
-               t.Errorf("Error: GetRicMessageId returned invalid value id=%d", id)
+               t.Errorf("Error: IsRetryError returned wrong value")
        }
 
-       name := Rmr.GetRicMessageName(123456)
-       if name !=  "" {
-               t.Errorf("Error: GetRicMessageName returned invalid value: name=%s", name)
+       ok = Rmr.IsRetryError(&RMRParams{status: 10})
+       if !ok {
+               t.Errorf("Error: IsRetryError returned wrong value")
+       }
+
+       ok = Rmr.IsNoEndPointError(&RMRParams{status: 5})
+       if ok {
+               t.Errorf("Error: IsNoEndPointError returned wrong value")
+       }
+
+       ok = Rmr.IsNoEndPointError(&RMRParams{status: 2})
+       if !ok {
+               t.Errorf("Error: IsNoEndPointError returned wrong value")
+       }
+}
+
+func TestIsErrorFunctions(t *testing.T) {
+       Logger.Info("CASE: TestIsErrorFunctions")
+       id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
+       if !ok || id != 12010 {
+               t.Errorf("Error: GetRicMessageId failed: id=%d", id)
+       }
+
+       name := Rmr.GetRicMessageName(12010)
+       if name != "RIC_SUB_REQ" {
+               t.Errorf("Error: GetRicMessageName failed: name=%s", name)
        }
 }
 
 func TestTeardown(t *testing.T) {
+       Logger.Info("CASE: TestTeardown")
        Sdl.Clear()
-       Rnib.Clear()
 }
 
 // Helper functions
@@ -209,4 +335,4 @@ func getMetrics(t *testing.T) string {
        response := executeRequest(req)
 
        return response.Body.String()
-}
\ No newline at end of file
+}