Improvement of 'Get Gnb Status’ command to fetch Enb details in O1 NETCONF client
[ric-plt/o1.git] / agent / pkg / nbi / nbi_test.go
index 066a6eb..7beea03 100755 (executable)
@@ -21,6 +21,7 @@ package nbi
 
 import (
        "encoding/json"
+       "fmt"
        "github.com/stretchr/testify/assert"
        "net"
        "net/http"
@@ -29,8 +30,11 @@ import (
        "testing"
        "time"
 
+       "errors"
+       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
        apimodel "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrmodel"
        "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/sbi"
+       "github.com/stretchr/testify/mock"
 )
 
 var XappConfig = `{
@@ -51,6 +55,10 @@ var XappConfig = `{
        }
   }`
 
+var XappConfigErr = `{
+       "ric": {
+  }`
+
 var XappDescriptor = `{
        "o-ran-sc-ric-xapp-desc-v1:ric": {
          "xapps": {
@@ -66,18 +74,14 @@ var XappDescriptor = `{
        }
   }`
 
-var kpodOutput = `
-NAME                               READY   STATUS    RESTARTS   AGE
-ricxapp-ueec-7bfdd587db-2jl9j      1/1     Running   53         29d
-ricxapp-anr-6748846478-8hmtz       1-1     Running   1          29d
-ricxapp-dualco-7f76f65c99-5p6c6    0/1     Running   1          29d
-`
-
 var n *Nbi
+var rnibM *rnibMock
 
 // Test cases
 func TestMain(M *testing.M) {
-       n = NewNbi(sbi.NewSBIClient("localhost:8080", "/ric/v1/", []string{"http"}, 5))
+       rnibM = new(rnibMock)
+       rnib = rnibM
+       n = NewNbi(sbi.NewSBIClient("localhost:8080", "localhost:9093", 5))
        go n.Start()
        time.Sleep(time.Duration(1) * time.Second)
 
@@ -85,7 +89,7 @@ func TestMain(M *testing.M) {
 }
 
 func TestModifyConfigmap(t *testing.T) {
-       ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", http.StatusOK, apimodel.ConfigValidationErrors{})
+       ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", 8080, http.StatusOK, apimodel.ConfigValidationErrors{})
        defer ts.Close()
 
        var f interface{}
@@ -96,8 +100,71 @@ func TestModifyConfigmap(t *testing.T) {
        assert.Equal(t, true, err == nil)
 }
 
+func TestXappDescModuleChangeCB(t *testing.T) {
+       ok := n.testModuleChangeCB("o-ran-sc-ric-xapp-desc-v1")
+       assert.True(t, ok)
+}
+
+func TestUeecConfigModuleChangeCB(t *testing.T) {
+       ok := n.testModuleChangeCB("o-ran-sc-ric-ueec-config-v1")
+       assert.True(t, ok)
+}
+
+func TestUeecConfigDoneModuleChangeCB(t *testing.T) {
+       ok := n.testModuleChangeCBDone("o-ran-sc-ric-ueec-config-v1")
+       assert.True(t, ok)
+}
+
+func TestXappDescGnbStateCB(t *testing.T) {
+       ok := n.testGnbStateCB("o-ran-sc-ric-xapp-desc-v1")
+       assert.True(t, ok)
+}
+
+func TestAlarmGnbStateCB(t *testing.T) {
+       ok := n.testGnbStateCB("o-ran-sc-ric-alarm-v1")
+       assert.True(t, ok)
+}
+
+func TestGnbStateCB(t *testing.T) {
+       var rnibOk xapp.RNIBIRNibError
+       var gNbIDs []*xapp.RNIBNbIdentity
+       gNbID := xapp.RNIBNbIdentity{
+               InventoryName: "test-gnb",
+       }
+       gNbIDs = append(gNbIDs, &gNbID)
+       nodeInfo := xapp.RNIBNodebInfo{}
+
+       rnibM.On("GetListGnbIds").Return(gNbIDs, rnibOk).Once()
+       rnibM.On("GetNodeb", mock.Anything).Return(&nodeInfo, rnibOk).Once()
+       ok := n.testGnbStateCB("")
+       assert.True(t, ok)
+}
+
+func TestGnbStateCBWhenRnibGetListGnbIdsFails(t *testing.T) {
+       var rnibErr xapp.RNIBIRNibError = errors.New("Some RNIB Error")
+
+       rnibM.On("GetListGnbIds").Return(nil, rnibErr).Once()
+       ok := n.testGnbStateCB("")
+       assert.True(t, ok)
+}
+
+func TestGnbStateCBWhenRnibGetNodebFails(t *testing.T) {
+       var rnibOk xapp.RNIBIRNibError
+       var rnibErr xapp.RNIBIRNibError = errors.New("Some RNIB Error")
+       var gNbIDs []*xapp.RNIBNbIdentity
+       gNbID := xapp.RNIBNbIdentity{
+               InventoryName: "test-gnb",
+       }
+       gNbIDs = append(gNbIDs, &gNbID)
+
+       rnibM.On("GetListGnbIds").Return(gNbIDs, rnibOk).Once()
+       rnibM.On("GetNodeb", mock.Anything).Return(nil, rnibErr).Once()
+       ok := n.testGnbStateCB("")
+       assert.True(t, ok)
+}
+
 func TestDeployXApp(t *testing.T) {
-       ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", http.StatusCreated, apimodel.Xapp{})
+       ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", 8080, http.StatusCreated, apimodel.Xapp{})
        defer ts.Close()
 
        var f interface{}
@@ -109,7 +176,7 @@ func TestDeployXApp(t *testing.T) {
 }
 
 func TestUnDeployXApp(t *testing.T) {
-       ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", http.StatusNoContent, apimodel.Xapp{})
+       ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", 8080, http.StatusNoContent, apimodel.Xapp{})
        defer ts.Close()
 
        var f interface{}
@@ -121,42 +188,13 @@ func TestUnDeployXApp(t *testing.T) {
 }
 
 func TestGetDeployedXapps(t *testing.T) {
-       ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", http.StatusOK, apimodel.AllDeployedXapps{})
+       ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", 8080, http.StatusOK, apimodel.AllDeployedXapps{})
        defer ts.Close()
 
        err := sbiClient.GetDeployedXapps()
        assert.Equal(t, true, err == nil)
 }
 
-func TestGetAllPodStatus(t *testing.T) {
-       sbi.CommandExec = func(args string) (out string, err error) {
-               assert.Equal(t, "/usr/local/bin/kubectl get pod -n ricxapp", args)
-               return kpodOutput, nil
-       }
-
-       expectedPodList := []sbi.PodStatus{
-               sbi.PodStatus{
-                       Name:   "ueec",
-                       Health: "healthy",
-                       Status: "Running",
-               },
-               sbi.PodStatus{
-                       Name:   "anr",
-                       Health: "unavailable",
-                       Status: "Running",
-               },
-               sbi.PodStatus{
-                       Name:   "dualco",
-                       Health: "unhealthy",
-                       Status: "Running",
-               },
-       }
-
-       podList, err := sbiClient.GetAllPodStatus("ricxapp")
-       assert.Equal(t, true, err == nil)
-       assert.Equal(t, podList, expectedPodList)
-}
-
 func TestErrorCases(t *testing.T) {
        // Invalid config
        err := n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", "", 2)
@@ -181,6 +219,18 @@ func TestErrorCases(t *testing.T) {
        // Invalid operation
        err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "{}", 0)
        assert.Equal(t, true, err != nil)
+
+        //Invalid json 
+        err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", XappConfigErr, 1)
+        assert.Equal(t, true, err != nil)
+
+       // Invalid operation
+       err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappDescriptor, 5)
+       assert.Equal(t, true, err == nil)
+
+        //Invalid json
+        err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappConfigErr, 1)
+        assert.Equal(t, true, err != nil)
 }
 
 func TestConnStatus2Str(t *testing.T) {
@@ -212,8 +262,8 @@ func TestTeardown(t *testing.T) {
        n.Stop()
 }
 
-func CreateHTTPServer(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
-       l, err := net.Listen("tcp", "localhost:8080")
+func CreateHTTPServer(t *testing.T, method, url string, port, status int, respData interface{}) *httptest.Server {
+       l, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
        if err != nil {
                t.Error("Failed to create listener: " + err.Error())
        }
@@ -248,3 +298,27 @@ func ConfigMatcher(result, expected *apimodel.XAppConfig) bool {
        }
        return false
 }
+
+type rnibMock struct {
+       mock.Mock
+}
+
+func (m *rnibMock) GetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) {
+       a := m.Called()
+       if a.Get(0) == nil {
+               return nil, a.Error(1)
+       }
+       return a.Get(0).([]*xapp.RNIBNbIdentity), a.Error(1)
+}
+
+func (m *rnibMock) GetNodeb(invName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) {
+       a := m.Called(invName)
+       if a.Get(0) == nil {
+               return nil, a.Error(1)
+       }
+       return a.Get(0).(*xapp.RNIBNodebInfo), a.Error(1)
+}
+
+func (m *rnibMock) GetListEnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) {
+       return nil, nil
+}
\ No newline at end of file