[RIC-475] [RIC-507] Inject RanStatusChangeManager | Enhance E2 Setup flow | Remove...
[ric-plt/e2mgr.git] / E2Manager / rNibWriter / rNibWriter_test.go
index 6c8743e..ab67b80 100644 (file)
@@ -17,7 +17,6 @@
 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
 //  platform project (RICP).
 
-
 package rNibWriter
 
 import (
@@ -33,26 +32,160 @@ import (
        "time"
 )
 
+var namespace = "namespace"
+const (
+       RanName = "test"
+)
+
 func initSdlInstanceMock(namespace string) (w RNibWriter, sdlInstanceMock *mocks.MockSdlInstance) {
        sdlInstanceMock = new(mocks.MockSdlInstance)
        w = GetRNibWriter(sdlInstanceMock)
        return
 }
 
-var namespace = "namespace"
+func generateNodebInfo(inventoryName string, nodeType entities.Node_Type, plmnId string, nbId string) *entities.NodebInfo {
+       nodebInfo := &entities.NodebInfo{
+               RanName:          inventoryName,
+               GlobalNbId:       &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId},
+               NodeType:         nodeType,
+               ConnectionStatus: entities.ConnectionStatus_CONNECTED,
+       }
+
+       if nodeType == entities.Node_ENB {
+               nodebInfo.Configuration = &entities.NodebInfo_Enb{
+                       Enb: &entities.Enb{},
+               }
+       } else if nodeType == entities.Node_GNB {
+               nodebInfo.Configuration = &entities.NodebInfo_Gnb{
+                       Gnb: &entities.Gnb{},
+               }
+       }
+
+       return nodebInfo
+}
+
+func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell {
+
+       servedNrCells := []*entities.ServedNRCell{}
+
+       for i, v := range cellIds {
+               servedNrCells = append(servedNrCells, &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{
+                       CellId: v,
+                       ChoiceNrMode: &entities.ServedNRCellInformation_ChoiceNRMode{
+                               Fdd: &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{
+
+                               },
+                       },
+                       NrMode:      entities.Nr_FDD,
+                       NrPci:       uint32(i + 1),
+                       ServedPlmns: []string{"whatever"},
+               }})
+       }
+
+       return servedNrCells
+}
+
+func TestRemoveServedNrCellsSuccess(t *testing.T) {
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
+       servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2")
+       sdlInstanceMock.On("Remove", buildCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(nil)
+       err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove)
+       assert.Nil(t, err)
+}
+
+func TestRemoveServedNrCellsFailure(t *testing.T) {
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
+       servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2")
+       sdlInstanceMock.On("Remove", buildCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(errors.New("expected error"))
+       err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove)
+       assert.IsType(t, &common.InternalError{}, err)
+}
+
+func TestUpdateGnbCellsInvalidNodebInfoFailure(t *testing.T) {
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
+       servedNrCells := generateServedNrCells("test1", "test2")
+       nodebInfo := &entities.NodebInfo{}
+       sdlInstanceMock.AssertNotCalled(t, "Set")
+       rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
+       assert.IsType(t, &common.ValidationError{}, rNibErr)
+}
+
+func TestUpdateGnbCellsInvalidCellFailure(t *testing.T) {
+       inventoryName := "name"
+       plmnId := "02f829"
+       nbId := "4a952a0a"
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
+       servedNrCells := []*entities.ServedNRCell{{ServedNrCellInformation: &entities.ServedNRCellInformation{}}}
+       nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
+       nodebInfo.GetGnb().ServedNrCells = servedNrCells
+       sdlInstanceMock.AssertNotCalled(t, "Set")
+       rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
+       assert.IsType(t, &common.ValidationError{}, rNibErr)
+}
+
+func getUpdateGnbCellsSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) []interface{} {
+
+       nodebInfoData, err := proto.Marshal(nodebInfo)
+       if err != nil {
+               t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal NodeB entity. Error: %s", err)
+       }
+
+       nodebNameKey, _ := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
+       nodebIdKey, _ := common.ValidateAndBuildNodeBIdKey(nodebInfo.NodeType.String(), nodebInfo.GlobalNbId.PlmnId, nodebInfo.GlobalNbId.NbId)
+       setExpected := []interface{}{nodebNameKey, nodebInfoData, nodebIdKey, nodebInfoData}
+
+       for _, v := range servedNrCells {
+
+               cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: v}}
+               cellData, err := proto.Marshal(&cellEntity)
+
+               if err != nil {
+                       t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal cell entity. Error: %s", err)
+               }
+
+               nrCellIdKey, _ := common.ValidateAndBuildNrCellIdKey(v.GetServedNrCellInformation().GetCellId())
+               cellNamePciKey, _ := common.ValidateAndBuildCellNamePciKey(nodebInfo.RanName, v.GetServedNrCellInformation().GetNrPci())
+               setExpected = append(setExpected, nrCellIdKey, cellData, cellNamePciKey, cellData)
+       }
+
+       return setExpected
+}
+
+func TestUpdateGnbCellsSdlFailure(t *testing.T) {
+       inventoryName := "name"
+       plmnId := "02f829"
+       nbId := "4a952a0a"
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
+       servedNrCells := generateServedNrCells("test1", "test2")
+       nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
+       nodebInfo.GetGnb().ServedNrCells = servedNrCells
+       setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
+       sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(errors.New("expected error"))
+       rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
+       assert.IsType(t, &common.InternalError{}, rNibErr)
+}
+
+func TestUpdateGnbCellsSuccess(t *testing.T) {
+       inventoryName := "name"
+       plmnId := "02f829"
+       nbId := "4a952a0a"
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
+       servedNrCells := generateServedNrCells("test1", "test2")
+       nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
+       nodebInfo.GetGnb().ServedNrCells = servedNrCells
+       setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
+       var e error
+       sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
+       rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
+       assert.Nil(t, rNibErr)
+}
 
 func TestUpdateNodebInfoSuccess(t *testing.T) {
        inventoryName := "name"
        plmnId := "02f829"
        nbId := "4a952a0a"
        w, sdlInstanceMock := initSdlInstanceMock(namespace)
-       nodebInfo := &entities.NodebInfo{}
-       nodebInfo.RanName = inventoryName
-       nodebInfo.GlobalNbId = &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}
-       nodebInfo.NodeType = entities.Node_ENB
-       nodebInfo.ConnectionStatus = 1
-       enb := entities.Enb{}
-       nodebInfo.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
+       nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
        data, err := proto.Marshal(nodebInfo)
        if err != nil {
                t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
@@ -391,7 +524,7 @@ func generateCellLoadInformation() *entities.CellLoadInformation {
        }
 
        compInformationItem := &entities.CompInformationItem{
-               CompHypothesisSets: []*entities.CompHypothesisSet{&entities.CompHypothesisSet{CellId: "789", CompHypothesis: "xxx"}},
+               CompHypothesisSets: []*entities.CompHypothesisSet{{CellId: "789", CompHypothesis: "xxx"}},
                BenefitMetric:      50,
        }
 
@@ -431,13 +564,12 @@ func TestSaveNilEntityFailure(t *testing.T) {
 
 func TestSaveUnknownTypeEntityFailure(t *testing.T) {
        w, _ := initSdlInstanceMock(namespace)
-       expectedErr := common.NewValidationError("#rNibWriter.saveNodeB - Unknown responding node type, entity: ip:\"localhost\" port:5656 ")
        nbIdentity := &entities.NbIdentity{InventoryName: "name", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
        nb := &entities.NodebInfo{}
        nb.Port = 5656
        nb.Ip = "localhost"
        actualErr := w.SaveNodeb(nbIdentity, nb)
-       assert.Equal(t, expectedErr, actualErr)
+       assert.IsType(t, &common.ValidationError{}, actualErr)
 }
 
 func TestSaveEntityFailure(t *testing.T) {
@@ -495,7 +627,7 @@ func TestSaveE2TInstanceSuccess(t *testing.T) {
 func TestSaveE2TInstanceNullE2tInstanceFailure(t *testing.T) {
        w, _ := initSdlInstanceMock(namespace)
        var address string
-       e2tInstance := entities.NewE2TInstance(address)
+       e2tInstance := entities.NewE2TInstance(address, "test")
        err := w.SaveE2TInstance(e2tInstance)
        assert.NotNil(t, err)
        assert.IsType(t, &common.ValidationError{}, err)
@@ -529,7 +661,7 @@ func TestSaveE2TInstanceSdlFailure(t *testing.T) {
 }
 
 func generateE2tInstance(address string) *entities.E2TInstance {
-       e2tInstance := entities.NewE2TInstance(address)
+       e2tInstance := entities.NewE2TInstance(address, "pod test")
 
        e2tInstance.AssociatedRanList = []string{"test1", "test2"}
 
@@ -610,6 +742,120 @@ func TestRemoveE2TInstanceEmptyAddressFailure(t *testing.T) {
        sdlInstanceMock.AssertExpectations(t)
 }
 
+func TestUpdateNodebInfoOnConnectionStatusInversionSuccess(t *testing.T) {
+       inventoryName := "name"
+       plmnId := "02f829"
+       nbId := "4a952a0a"
+       channelName := "RAN_CONNECT_STATE_CHANGE"
+       eventName := inventoryName + "_" + "CONNECTED"
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
+       nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
+       data, err := proto.Marshal(nodebInfo)
+       if err != nil {
+               t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionSuccess - Failed to marshal NodeB entity. Error: %v", err)
+       }
+       var e error
+       var setExpected []interface{}
+
+       nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
+       nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
+       setExpected = append(setExpected, nodebNameKey, data)
+       setExpected = append(setExpected, nodebIdKey, data)
+
+       sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
+       // TODO: after SetAndPublish problem is solved, bring back this line
+       // sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
+
+       rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
+       assert.Nil(t, rNibErr)
+}
+
+func TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure(t *testing.T) {
+       inventoryName := "name"
+       plmnId := "02f829"
+       nbId := "4a952a0a"
+       channelName := "RAN_CONNECT_STATE_CHANGE"
+       eventName := inventoryName + "_" + "CONNECTED"
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
+       nodebInfo := &entities.NodebInfo{}
+       data, err := proto.Marshal(nodebInfo)
+       if err != nil {
+               t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
+       }
+       var e error
+       var setExpected []interface{}
+
+       nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
+       nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
+       setExpected = append(setExpected, nodebNameKey, data)
+       setExpected = append(setExpected, nodebIdKey, data)
+
+       sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
+       // TODO: after SetAndPublish problem is solved, bring back this line
+       //sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
+
+       rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
+
+       assert.NotNil(t, rNibErr)
+       assert.IsType(t, &common.ValidationError{}, rNibErr)
+}
+
+func TestUpdateNodebInfoOnConnectionStatusInversionMissingGlobalNbId(t *testing.T) {
+       inventoryName := "name"
+       channelName := "RAN_CONNECT_STATE_CHANGE"
+       eventName := inventoryName + "_" + "CONNECTED"
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
+       nodebInfo := &entities.NodebInfo{}
+       nodebInfo.RanName = inventoryName
+       data, err := proto.Marshal(nodebInfo)
+       if err != nil {
+               t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
+       }
+       var e error
+       var setExpected []interface{}
+
+       nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
+       setExpected = append(setExpected, nodebNameKey, data)
+       sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
+       // TODO: after SetAndPublish problem is solved, bring back this line
+       //sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
+
+       rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
+
+       assert.Nil(t, rNibErr)
+}
+
+func TestSaveGeneralConfiguration(t *testing.T) {
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
+
+       key := common.BuildGeneralConfigurationKey()
+       configurationData := "{\"enableRic\":true}"
+       configuration := &entities.GeneralConfiguration{}
+       configuration.EnableRic = true
+
+       sdlInstanceMock.On("Set",[]interface{}{[]interface{}{key, []byte(configurationData)}}).Return(nil)
+       rNibErr := w.SaveGeneralConfiguration(configuration)
+
+       assert.Nil(t, rNibErr)
+       sdlInstanceMock.AssertExpectations(t)
+}
+
+func TestSaveGeneralConfigurationDbError(t *testing.T) {
+       w, sdlInstanceMock := initSdlInstanceMock(namespace)
+
+       key := common.BuildGeneralConfigurationKey()
+       configurationData := "{\"enableRic\":true}"
+       configuration := &entities.GeneralConfiguration{}
+       configuration.EnableRic = true
+
+       expectedErr := errors.New("expected error")
+
+       sdlInstanceMock.On("Set",[]interface{}{[]interface{}{key, []byte(configurationData)}}).Return(expectedErr)
+       rNibErr := w.SaveGeneralConfiguration(configuration)
+
+       assert.NotNil(t, rNibErr)
+}
+
 //Integration tests
 //
 //func TestSaveEnbGnbInteg(t *testing.T){
@@ -629,7 +875,7 @@ func TestRemoveE2TInstanceEmptyAddressFailure(t *testing.T) {
 //             nb.Configuration = &entities.NodebInfo_Enb{Enb:&enb}
 //             plmnId := 0x02f828
 //             nbId := 0x4a952a0a
-//             nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameEnb%d" ,i), GlobalNbId:&entities.GlobalNbId{PlmnId:fmt.Sprintf("%02x", plmnId + i), NbId:fmt.Sprintf("%02x", nbId + i)}}
+//             nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameEnb%d" ,i), GlobalNbId:&entities.GlobalNbId{RicId:fmt.Sprintf("%02x", plmnId + i), NbId:fmt.Sprintf("%02x", nbId + i)}}
 //             err := w.SaveNodeb(nbIdentity, &nb)
 //             if err != nil{
 //                     t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
@@ -646,7 +892,7 @@ func TestRemoveE2TInstanceEmptyAddressFailure(t *testing.T) {
 //             gCell3 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",3333 + i), NrPci:uint32(3 + i)}}
 //             gnb.ServedNrCells = []*entities.ServedNRCell{gCell1, gCell2, gCell3,}
 //             nb1.Configuration = &entities.NodebInfo_Gnb{Gnb:&gnb}
-//             nbIdentity = &entities.NbIdentity{InventoryName: fmt.Sprintf("nameGnb%d" ,i), GlobalNbId:&entities.GlobalNbId{PlmnId:fmt.Sprintf("%02x", plmnId - i), NbId:fmt.Sprintf("%02x", nbId - i)}}
+//             nbIdentity = &entities.NbIdentity{InventoryName: fmt.Sprintf("nameGnb%d" ,i), GlobalNbId:&entities.GlobalNbId{RicId:fmt.Sprintf("%02x", plmnId - i), NbId:fmt.Sprintf("%02x", nbId - i)}}
 //             err = w.SaveNodeb(nbIdentity, &nb1)
 //             if err != nil{
 //                     t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)