package nbi
import (
- "os"
- "time"
"encoding/json"
- "testing"
- "net"
- "net/http"
- "net/http/httptest"
+ "fmt"
"github.com/stretchr/testify/assert"
+ "net"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "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 = `{
}
}`
+var XappConfigErr = `{
+ "ric": {
+ }`
+
var XappDescriptor = `{
"o-ran-sc-ric-xapp-desc-v1:ric": {
"xapps": {
}`
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)
}
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{}
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{}
}
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{}
}
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()
err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "", 1)
assert.Equal(t, true, err == nil)
- // Invalid module
- err = n.ManageConfigmaps("", "{}", 1)
- assert.Equal(t, true, err == nil)
-
- // Unexpected module
- err = n.ManageConfigmaps("o-ran-sc-ric-xapp-desc-v1", "{}", 0)
- assert.Equal(t, true, err == nil)
-
// 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) {
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())
+ t.Error("Failed to create listener: " + err.Error())
}
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, method)
func ConfigMatcher(result, expected *apimodel.XAppConfig) bool {
if *result.Metadata.XappName == *expected.Metadata.XappName &&
- *result.Metadata.Namespace == *expected.Metadata.Namespace {
+ *result.Metadata.Namespace == *expected.Metadata.Namespace {
return true
}
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