X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=E2Manager%2FrNibWriter%2FrNibWriter_test.go;h=3dadce8fa093ba3948992f48edfd88718128825b;hb=dc8932a553fabccf33b5cbb4e37084362493dbcf;hp=ab67b80188809320dbcdbc434e26cc1aa8f56c8e;hpb=76709204dfd7968156b5fa81190f146a8991812d;p=ric-plt%2Fe2mgr.git diff --git a/E2Manager/rNibWriter/rNibWriter_test.go b/E2Manager/rNibWriter/rNibWriter_test.go index ab67b80..3dadce8 100644 --- a/E2Manager/rNibWriter/rNibWriter_test.go +++ b/E2Manager/rNibWriter/rNibWriter_test.go @@ -20,6 +20,7 @@ package rNibWriter import ( + "e2mgr/configuration" "e2mgr/mocks" "encoding/json" "errors" @@ -33,13 +34,14 @@ import ( ) var namespace = "namespace" + const ( RanName = "test" ) func initSdlInstanceMock(namespace string) (w RNibWriter, sdlInstanceMock *mocks.MockSdlInstance) { sdlInstanceMock = new(mocks.MockSdlInstance) - w = GetRNibWriter(sdlInstanceMock) + w = GetRNibWriter(sdlInstanceMock, configuration.RnibWriterConfig{StateChangeMessageChannel: "RAN_CONNECTION_STATUS_CHANGE", RanManipulationMessageChannel: "RAN_MANIPULATION"}) return } @@ -66,7 +68,7 @@ func generateNodebInfo(inventoryName string, nodeType entities.Node_Type, plmnId func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell { - servedNrCells := []*entities.ServedNRCell{} + var servedNrCells []*entities.ServedNRCell for i, v := range cellIds { servedNrCells = append(servedNrCells, &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{ @@ -85,10 +87,42 @@ func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell { return servedNrCells } +func generateServedCells(cellIds ...string) []*entities.ServedCellInfo { + + var servedCells []*entities.ServedCellInfo + + for i, v := range cellIds { + servedCells = append(servedCells, &entities.ServedCellInfo{ + CellId: v, + ChoiceEutraMode: &entities.ChoiceEUTRAMode{ + Fdd: &entities.FddInfo{}, + }, + Pci: uint32(i + 1), + BroadcastPlmns: []string{"whatever"}, + }) + } + + return servedCells +} + +func generateServedCellInfos(cellIds ...string) []*entities.ServedCellInfo { + + servedCells := []*entities.ServedCellInfo{} + + for i, v := range cellIds { + servedCells = append(servedCells, &entities.ServedCellInfo{ + CellId: v, + Pci: uint32(i + 1), + }) + } + + return servedCells +} + func TestRemoveServedNrCellsSuccess(t *testing.T) { w, sdlInstanceMock := initSdlInstanceMock(namespace) servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2") - sdlInstanceMock.On("Remove", buildCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(nil) + sdlInstanceMock.On("Remove", buildServedNRCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(nil) err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove) assert.Nil(t, err) } @@ -96,7 +130,7 @@ func TestRemoveServedNrCellsSuccess(t *testing.T) { func TestRemoveServedNrCellsFailure(t *testing.T) { w, sdlInstanceMock := initSdlInstanceMock(namespace) servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2") - sdlInstanceMock.On("Remove", buildCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(errors.New("expected error")) + sdlInstanceMock.On("Remove", buildServedNRCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(errors.New("expected error")) err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove) assert.IsType(t, &common.InternalError{}, err) } @@ -123,6 +157,34 @@ func TestUpdateGnbCellsInvalidCellFailure(t *testing.T) { assert.IsType(t, &common.ValidationError{}, rNibErr) } +func getUpdateEnbCellsSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedCells []*entities.ServedCellInfo) []interface{} { + + nodebInfoData, err := proto.Marshal(nodebInfo) + if err != nil { + t.Fatalf("#rNibWriter_test.getUpdateEnbCellsSetExpected - 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 _, cell := range servedCells { + + cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}} + cellData, err := proto.Marshal(&cellEntity) + + if err != nil { + t.Fatalf("#rNibWriter_test.getUpdateEnbCellsSetExpected - Failed to marshal cell entity. Error: %s", err) + } + + nrCellIdKey, _ := common.ValidateAndBuildCellIdKey(cell.GetCellId()) + cellNamePciKey, _ := common.ValidateAndBuildCellNamePciKey(nodebInfo.RanName, cell.GetPci()) + setExpected = append(setExpected, nrCellIdKey, cellData, cellNamePciKey, cellData) + } + + return setExpected +} + func getUpdateGnbCellsSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) []interface{} { nodebInfoData, err := proto.Marshal(nodebInfo) @@ -252,14 +314,20 @@ func TestUpdateNodebInfoMissingGlobalNbId(t *testing.T) { } func TestSaveEnb(t *testing.T) { - name := "name" - ranName := "RAN:" + name + ranName := "RAN:" + RanName w, sdlInstanceMock := initSdlInstanceMock(namespace) - nb := entities.NodebInfo{} - nb.NodeType = entities.Node_ENB - nb.ConnectionStatus = 1 - nb.Ip = "localhost" - nb.Port = 5656 + nb := entities.NodebInfo{ + RanName: RanName, + NodeType: entities.Node_ENB, + ConnectionStatus: entities.ConnectionStatus_CONNECTED, + Ip: "localhost", + Port: 5656, + GlobalNbId: &entities.GlobalNbId{ + NbId: "4a952a0a", + PlmnId: "02f829", + }, + } + enb := entities.Enb{} cell := &entities.ServedCellInfo{CellId: "aaff", Pci: 3} cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}} @@ -279,31 +347,18 @@ func TestSaveEnb(t *testing.T) { setExpected = append(setExpected, ranName, data) setExpected = append(setExpected, "ENB:02f829:4a952a0a", data) setExpected = append(setExpected, fmt.Sprintf("CELL:%s", cell.GetCellId()), cellData) - setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetPci()), cellData) - - sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e) + setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", RanName, cell.GetPci()), cellData) - nbIdData, err := proto.Marshal(&entities.NbIdentity{InventoryName: name}) - if err != nil { - t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err) - } - sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e) - - nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}} - nbIdData, err = proto.Marshal(nbIdentity) - if err != nil { - t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB Identity entity. Error: %v", err) - } - sdlInstanceMock.On("AddMember", "ENB", []interface{}{nbIdData}).Return(e) + sdlInstanceMock.On("SetAndPublish", []string{"RAN_MANIPULATION", RanName + "_" + RanAddedEvent}, []interface{}{setExpected}).Return(e) - rNibErr := w.SaveNodeb(nbIdentity, &nb) + rNibErr := w.SaveNodeb(&nb) assert.Nil(t, rNibErr) } func TestSaveEnbCellIdValidationFailure(t *testing.T) { - name := "name" w, _ := initSdlInstanceMock(namespace) nb := entities.NodebInfo{} + nb.RanName = "name" nb.NodeType = entities.Node_ENB nb.ConnectionStatus = 1 nb.Ip = "localhost" @@ -312,9 +367,7 @@ func TestSaveEnbCellIdValidationFailure(t *testing.T) { cell := &entities.ServedCellInfo{Pci: 3} enb.ServedCells = []*entities.ServedCellInfo{cell} nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb} - - nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}} - rNibErr := w.SaveNodeb(nbIdentity, &nb) + rNibErr := w.SaveNodeb(&nb) assert.NotNil(t, rNibErr) assert.IsType(t, &common.ValidationError{}, rNibErr) assert.Equal(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", rNibErr.Error()) @@ -322,27 +375,30 @@ func TestSaveEnbCellIdValidationFailure(t *testing.T) { func TestSaveEnbInventoryNameValidationFailure(t *testing.T) { w, _ := initSdlInstanceMock(namespace) - nb := entities.NodebInfo{} - nb.NodeType = entities.Node_ENB - nb.ConnectionStatus = 1 - nb.Ip = "localhost" - nb.Port = 5656 + nb := entities.NodebInfo{ + NodeType: entities.Node_ENB, + ConnectionStatus: entities.ConnectionStatus_CONNECTED, + Ip: "localhost", + Port: 5656, + GlobalNbId: &entities.GlobalNbId{ + NbId: "4a952a0a", + PlmnId: "02f829", + }, + } enb := entities.Enb{} cell := &entities.ServedCellInfo{CellId: "aaa", Pci: 3} enb.ServedCells = []*entities.ServedCellInfo{cell} nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb} - - nbIdentity := &entities.NbIdentity{InventoryName: "", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}} - rNibErr := w.SaveNodeb(nbIdentity, &nb) + rNibErr := w.SaveNodeb(&nb) assert.NotNil(t, rNibErr) assert.IsType(t, &common.ValidationError{}, rNibErr) assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error()) } func TestSaveGnbCellIdValidationFailure(t *testing.T) { - name := "name" w, _ := initSdlInstanceMock(namespace) nb := entities.NodebInfo{} + nb.RanName = "name" nb.NodeType = entities.Node_GNB nb.ConnectionStatus = 1 nb.Ip = "localhost" @@ -353,22 +409,27 @@ func TestSaveGnbCellIdValidationFailure(t *testing.T) { gnb.ServedNrCells = []*entities.ServedNRCell{cell} nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb} - nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}} - rNibErr := w.SaveNodeb(nbIdentity, &nb) + rNibErr := w.SaveNodeb(&nb) assert.NotNil(t, rNibErr) assert.IsType(t, &common.ValidationError{}, rNibErr) assert.Equal(t, "#utils.ValidateAndBuildNrCellIdKey - an empty cell id received", rNibErr.Error()) } func TestSaveGnb(t *testing.T) { - name := "name" - ranName := "RAN:" + name + ranName := "RAN:" + RanName w, sdlInstanceMock := initSdlInstanceMock(namespace) - nb := entities.NodebInfo{} - nb.NodeType = entities.Node_GNB - nb.ConnectionStatus = 1 - nb.Ip = "localhost" - nb.Port = 5656 + nb := entities.NodebInfo{ + RanName: RanName, + NodeType: entities.Node_GNB, + ConnectionStatus: 1, + GlobalNbId: &entities.GlobalNbId{ + NbId: "4a952a0a", + PlmnId: "02f829", + }, + Ip: "localhost", + Port: 5656, + } + gnb := entities.Gnb{} cellInfo := &entities.ServedNRCellInformation{NrPci: 2, CellId: "ccdd"} cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo} @@ -389,23 +450,10 @@ func TestSaveGnb(t *testing.T) { setExpected = append(setExpected, ranName, data) setExpected = append(setExpected, "GNB:02f829:4a952a0a", data) setExpected = append(setExpected, fmt.Sprintf("NRCELL:%s", cell.GetServedNrCellInformation().GetCellId()), cellData) - setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetServedNrCellInformation().GetNrPci()), cellData) + setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", RanName, cell.GetServedNrCellInformation().GetNrPci()), cellData) sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e) - nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}} - nbIdData, err := proto.Marshal(nbIdentity) - if err != nil { - t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB Identity entity. Error: %v", err) - } - sdlInstanceMock.On("AddMember", "GNB", []interface{}{nbIdData}).Return(e) - - nbIdData, err = proto.Marshal(&entities.NbIdentity{InventoryName: name}) - if err != nil { - t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err) - } - sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e) - - rNibErr := w.SaveNodeb(nbIdentity, &nb) + rNibErr := w.SaveNodeb(&nb) assert.Nil(t, rNibErr) } @@ -557,18 +605,16 @@ func generateRanLoadInformation() *entities.RanLoadInformation { func TestSaveNilEntityFailure(t *testing.T) { w, _ := initSdlInstanceMock(namespace) expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil")) - nbIdentity := &entities.NbIdentity{} - actualErr := w.SaveNodeb(nbIdentity, nil) + actualErr := w.SaveNodeb(nil) assert.Equal(t, expectedErr, actualErr) } func TestSaveUnknownTypeEntityFailure(t *testing.T) { w, _ := initSdlInstanceMock(namespace) - 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) + actualErr := w.SaveNodeb(nb) assert.IsType(t, &common.ValidationError{}, actualErr) } @@ -584,12 +630,11 @@ func TestSaveEntityFailure(t *testing.T) { if err != nil { t.Errorf("#rNibWriter_test.TestSaveEntityFailure - Failed to marshal NodeB entity. Error: %v", err) } - nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}} setExpected := []interface{}{"RAN:" + name, data} setExpected = append(setExpected, "GNB:"+plmnId+":"+nbId, data) expectedErr := errors.New("expected error") sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr) - rNibErr := w.SaveNodeb(nbIdentity, &gnb) + rNibErr := w.SaveNodeb(&gnb) assert.NotEmpty(t, rNibErr) } @@ -746,9 +791,9 @@ func TestUpdateNodebInfoOnConnectionStatusInversionSuccess(t *testing.T) { inventoryName := "name" plmnId := "02f829" nbId := "4a952a0a" - channelName := "RAN_CONNECT_STATE_CHANGE" + channelName := "RAN_CONNECTION_STATUS_CHANGE" eventName := inventoryName + "_" + "CONNECTED" - w, sdlInstanceMock := initSdlInstanceMock(namespace) + w, sdlInstanceMock := initSdlInstanceMock(namespace) nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId) data, err := proto.Marshal(nodebInfo) if err != nil { @@ -762,11 +807,9 @@ func TestUpdateNodebInfoOnConnectionStatusInversionSuccess(t *testing.T) { 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) + sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e) - rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName) + rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, eventName) assert.Nil(t, rNibErr) } @@ -774,7 +817,7 @@ func TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure(t inventoryName := "name" plmnId := "02f829" nbId := "4a952a0a" - channelName := "RAN_CONNECT_STATE_CHANGE" + channelName := "RAN_CONNECTION_STATUS_CHANGE" eventName := inventoryName + "_" + "CONNECTED" w, sdlInstanceMock := initSdlInstanceMock(namespace) nodebInfo := &entities.NodebInfo{} @@ -790,11 +833,9 @@ func TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure(t 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) + sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e) - rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName) + rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, eventName) assert.NotNil(t, rNibErr) assert.IsType(t, &common.ValidationError{}, rNibErr) @@ -802,7 +843,7 @@ func TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure(t func TestUpdateNodebInfoOnConnectionStatusInversionMissingGlobalNbId(t *testing.T) { inventoryName := "name" - channelName := "RAN_CONNECT_STATE_CHANGE" + channelName := "RAN_CONNECTION_STATUS_CHANGE" eventName := inventoryName + "_" + "CONNECTED" w, sdlInstanceMock := initSdlInstanceMock(namespace) nodebInfo := &entities.NodebInfo{} @@ -816,11 +857,9 @@ func TestUpdateNodebInfoOnConnectionStatusInversionMissingGlobalNbId(t *testing. 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) + sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e) - rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName) + rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, eventName) assert.Nil(t, rNibErr) } @@ -833,7 +872,7 @@ func TestSaveGeneralConfiguration(t *testing.T) { configuration := &entities.GeneralConfiguration{} configuration.EnableRic = true - sdlInstanceMock.On("Set",[]interface{}{[]interface{}{key, []byte(configurationData)}}).Return(nil) + sdlInstanceMock.On("Set", []interface{}{[]interface{}{key, []byte(configurationData)}}).Return(nil) rNibErr := w.SaveGeneralConfiguration(configuration) assert.Nil(t, rNibErr) @@ -850,12 +889,162 @@ func TestSaveGeneralConfigurationDbError(t *testing.T) { expectedErr := errors.New("expected error") - sdlInstanceMock.On("Set",[]interface{}{[]interface{}{key, []byte(configurationData)}}).Return(expectedErr) + sdlInstanceMock.On("Set", []interface{}{[]interface{}{key, []byte(configurationData)}}).Return(expectedErr) rNibErr := w.SaveGeneralConfiguration(configuration) assert.NotNil(t, rNibErr) } +func TestRemoveServedCellsFailure(t *testing.T) { + w, sdlInstanceMock := initSdlInstanceMock(namespace) + servedCellsToRemove := generateServedCells("whatever1", "whatever2") + expectedErr := errors.New("expected error") + sdlInstanceMock.On("Remove", buildServedCellInfoKeysToRemove(RanName, servedCellsToRemove)).Return(expectedErr) + + rNibErr := w.RemoveServedCells(RanName, servedCellsToRemove) + + assert.NotNil(t, rNibErr) +} + +func TestRemoveServedCellsSuccess(t *testing.T) { + w, sdlInstanceMock := initSdlInstanceMock(namespace) + servedCellsToRemove := generateServedCells("whatever1", "whatever2") + sdlInstanceMock.On("Remove", buildServedCellInfoKeysToRemove(RanName, servedCellsToRemove)).Return(nil) + err := w.RemoveServedCells(RanName, servedCellsToRemove) + assert.Nil(t, err) +} + +func TestUpdateEnbInvalidNodebInfoFailure(t *testing.T) { + w, sdlInstanceMock := initSdlInstanceMock(namespace) + servedCells := generateServedCells("test1", "test2") + nodebInfo := &entities.NodebInfo{} + sdlInstanceMock.AssertNotCalled(t, "SetAndPublish") + rNibErr := w.UpdateEnb(nodebInfo, servedCells) + assert.IsType(t, &common.ValidationError{}, rNibErr) +} + +func TestUpdateEnbInvalidCellFailure(t *testing.T) { + inventoryName := "name" + plmnId := "02f829" + nbId := "4a952a0a" + w, sdlInstanceMock := initSdlInstanceMock(namespace) + servedCells := []*entities.ServedCellInfo{{CellId: ""}} + nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId) + nodebInfo.GetEnb().ServedCells = servedCells + sdlInstanceMock.AssertNotCalled(t, "SetAndPublish") + rNibErr := w.UpdateEnb(nodebInfo, servedCells) + assert.IsType(t, &common.ValidationError{}, rNibErr) +} + +func TestUpdateEnbSdlFailure(t *testing.T) { + inventoryName := "name" + plmnId := "02f829" + nbId := "4a952a0a" + w, sdlInstanceMock := initSdlInstanceMock(namespace) + servedCells := generateServedCells("test1", "test2") + nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId) + nodebInfo.GetEnb().ServedCells = servedCells + setExpected := getUpdateEnbCellsSetExpected(t, nodebInfo, servedCells) + sdlInstanceMock.On("SetAndPublish", []string{"RAN_MANIPULATION", inventoryName + "_" + RanUpdatedEvent}, []interface{}{setExpected}).Return(errors.New("expected error")) + rNibErr := w.UpdateEnb(nodebInfo, servedCells) + assert.IsType(t, &common.InternalError{}, rNibErr) +} + +func TestUpdateEnbSuccess(t *testing.T) { + inventoryName := "name" + plmnId := "02f829" + nbId := "4a952a0a" + w, sdlInstanceMock := initSdlInstanceMock(namespace) + servedCells := generateServedCells("test1", "test2") + nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId) + nodebInfo.GetEnb().ServedCells = servedCells + setExpected := getUpdateEnbCellsSetExpected(t, nodebInfo, servedCells) + + var e error + sdlInstanceMock.On("SetAndPublish", []string{"RAN_MANIPULATION", inventoryName + "_" + RanUpdatedEvent}, []interface{}{setExpected}).Return(e) + rNibErr := w.UpdateEnb(nodebInfo, servedCells) + assert.Nil(t, rNibErr) +} + +func getUpdateEnbSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedCells []*entities.ServedCellInfo) []interface{} { + + nodebInfoData, err := proto.Marshal(nodebInfo) + if err != nil { + t.Fatalf("#rNibWriter_test.getUpdateEnbSetExpected - 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 servedCells { + + cellEntity := entities.ServedCellInfo{CellId: "some cell id", EutraMode: entities.Eutra_FDD, CsgId: "some csg id"} + cellData, err := proto.Marshal(&cellEntity) + + if err != nil { + t.Fatalf("#rNibWriter_test.getUpdateEnbSetExpected - Failed to marshal cell entity. Error: %s", err) + } + + nrCellIdKey, _ := common.ValidateAndBuildNrCellIdKey(v.GetCellId()) + cellNamePciKey, _ := common.ValidateAndBuildCellNamePciKey(nodebInfo.RanName, v.GetPci()) + setExpected = append(setExpected, nrCellIdKey, cellData, cellNamePciKey, cellData) + } + return setExpected +} + +func TestRemoveEnbSuccess(t *testing.T) { + inventoryName := "name" + plmnId := "02f829" + nbId := "4a952a0a" + channelName := "RAN_MANIPULATION" + eventName := inventoryName + "_" + "DELETED" + w, sdlInstanceMock := initSdlInstanceMock(namespace) + nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId) + nodebInfo.GetEnb().ServedCells = generateServedCellInfos("cell1", "cell2") + + var e error + + expectedKeys := []string{} + cell1Key := fmt.Sprintf("CELL:%s", nodebInfo.GetEnb().ServedCells[0].CellId) + cell1PciKey := fmt.Sprintf("PCI:%s:%02x", inventoryName, nodebInfo.GetEnb().ServedCells[0].Pci) + cell2Key := fmt.Sprintf("CELL:%s", nodebInfo.GetEnb().ServedCells[1].CellId) + cell2PciKey := fmt.Sprintf("PCI:%s:%02x", inventoryName, nodebInfo.GetEnb().ServedCells[1].Pci) + nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName) + nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId) + expectedKeys = append(expectedKeys, cell1Key, cell1PciKey, cell2Key, cell2PciKey, nodebNameKey, nodebIdKey) + sdlInstanceMock.On("RemoveAndPublish", []string{channelName, eventName}, expectedKeys).Return(e) + + rNibErr := w.RemoveEnb(nodebInfo) + assert.Nil(t, rNibErr) + sdlInstanceMock.AssertExpectations(t) +} + +func TestRemoveEnbRemoveAndPublishError(t *testing.T) { + inventoryName := "name" + plmnId := "02f829" + nbId := "4a952a0a" + channelName := "RAN_MANIPULATION" + eventName := inventoryName + "_" + "DELETED" + w, sdlInstanceMock := initSdlInstanceMock(namespace) + nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId) + nodebInfo.GetEnb().ServedCells = generateServedCellInfos("cell1", "cell2") + + expectedKeys := []string{} + cell1Key := fmt.Sprintf("CELL:%s", nodebInfo.GetEnb().ServedCells[0].CellId) + cell1PciKey := fmt.Sprintf("PCI:%s:%02x", inventoryName, nodebInfo.GetEnb().ServedCells[0].Pci) + cell2Key := fmt.Sprintf("CELL:%s", nodebInfo.GetEnb().ServedCells[1].CellId) + cell2PciKey := fmt.Sprintf("PCI:%s:%02x", inventoryName, nodebInfo.GetEnb().ServedCells[1].Pci) + nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName) + nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId) + expectedKeys = append(expectedKeys, cell1Key, cell1PciKey, cell2Key, cell2PciKey, nodebNameKey, nodebIdKey) + sdlInstanceMock.On("RemoveAndPublish", []string{channelName, eventName}, expectedKeys).Return(errors.New("for test")) + + rNibErr := w.RemoveEnb(nodebInfo) + assert.NotNil(t, rNibErr) + sdlInstanceMock.AssertExpectations(t) +} + //Integration tests // //func TestSaveEnbGnbInteg(t *testing.T){