+func TestAddEnb(t *testing.T) {
+ ranName := "RAN:" + RanName
+ w, sdlMock := initSdlMock()
+ 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}}
+ enb.ServedCells = []*entities.ServedCellInfo{cell}
+ nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
+ data, err := proto.Marshal(&nb)
+ if err != nil {
+ t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
+ }
+ var e error
+
+ cellData, err := proto.Marshal(&cellEntity)
+ if err != nil {
+ t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal Cell entity. Error: %v", err)
+ }
+ var setExpected []interface{}
+ 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", RanName, cell.GetPci()), cellData)
+
+ sdlMock.On("SetAndPublish", namespace, []string{"RAN_MANIPULATION", RanName + "_" + RanAddedEvent}, []interface{}{setExpected}).Return(e)
+
+ rNibErr := w.AddEnb(&nb)
+ assert.Nil(t, rNibErr)
+}
+
+func TestAddEnbMarshalNilFailure(t *testing.T) {
+ w, _ := initSdlMock()
+
+ rNibErr := w.AddEnb(nil)
+ expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
+ assert.Equal(t, expectedErr, rNibErr)
+}
+
+func TestAddEnbCellIdValidationFailure(t *testing.T) {
+ w, _ := initSdlMock()
+ nb := entities.NodebInfo{}
+ nb.RanName = "name"
+ nb.NodeType = entities.Node_ENB
+ nb.ConnectionStatus = 1
+ nb.Ip = "localhost"
+ nb.Port = 5656
+ enb := entities.Enb{}
+ cell := &entities.ServedCellInfo{Pci: 3}
+ enb.ServedCells = []*entities.ServedCellInfo{cell}
+ nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
+ rNibErr := w.AddEnb(&nb)
+ assert.NotNil(t, rNibErr)
+ assert.IsType(t, &common.ValidationError{}, rNibErr)
+ assert.Equal(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", rNibErr.Error())
+}
+
+func TestAddEnbInventoryNameValidationFailure(t *testing.T) {
+ w, _ := initSdlMock()
+ 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}
+ rNibErr := w.AddEnb(&nb)
+ assert.NotNil(t, rNibErr)
+ assert.IsType(t, &common.ValidationError{}, rNibErr)
+ assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error())
+}
+
+func TestAddEnbGlobalNbIdPlmnValidationFailure(t *testing.T) {
+ w, _ := initSdlMock()
+ nb := entities.NodebInfo{
+ RanName: "name",
+ NodeType: entities.Node_ENB,
+ ConnectionStatus: entities.ConnectionStatus_CONNECTED,
+ Ip: "localhost",
+ Port: 5656,
+ GlobalNbId: &entities.GlobalNbId{
+ NbId: "4a952a0a",
+ //Empty PLMNID fails RNIB validation
+ PlmnId: "",
+ },
+ }
+ enb := entities.Enb{}
+ cell := &entities.ServedCellInfo{CellId: "aaa", Pci: 3}
+ enb.ServedCells = []*entities.ServedCellInfo{cell}
+ nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
+ rNibErr := w.AddEnb(&nb)
+ assert.NotNil(t, rNibErr)
+ assert.IsType(t, &common.ValidationError{}, rNibErr)
+ assert.Equal(t, "#utils.ValidateAndBuildNodeBIdKey - an empty plmnId received", rNibErr.Error())
+}
+
+func TestUpdateNbIdentityOneMemberSuccess(t *testing.T) {
+ w, sdlMock := initSdlMock()
+
+ proto, nbIdentity := createNbIdentityProto(t, "ran1", "plmnId1", "nbId1", entities.ConnectionStatus_DISCONNECTED)
+ val := []interface{}{proto}
+
+ sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), val).Return(nil)
+
+ protoAdd, nbIdentityAdd := createNbIdentityProto(t, "ran1_add", "plmnId1_add", "nbId1_add", entities.ConnectionStatus_CONNECTED)
+ sdlMock.On("AddMember", namespace, entities.Node_ENB.String(), []interface{}{protoAdd}).Return(nil)
+
+ newNbIdIdentities := []*entities.NbIdentity{nbIdentityAdd}
+ oldNbIdIdentities := []*entities.NbIdentity{nbIdentity}
+
+ rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
+ assert.Nil(t, rNibErr)
+ sdlMock.AssertExpectations(t)
+}
+
+func TestUpdateNbIdentitySuccess(t *testing.T) {
+ w, sdlMock := initSdlMock()
+
+ var nbIdIdentitiesProtoToRemove []interface{}
+ protoRan1, _ := createNbIdentityProto(t, "ran1", "plmnId1", "nbId1", entities.ConnectionStatus_DISCONNECTED)
+ protoRan2, _ := createNbIdentityProto(t, "ran2", "plmnId2", "nbId2", entities.ConnectionStatus_DISCONNECTED)
+ nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan1)
+ nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan2)
+ sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToRemove).Return(nil)
+
+ var nbIdIdentitiesProtoToAdd []interface{}
+ protoRan1Add, _ := createNbIdentityProto(t, "ran1_add", "plmnId1_add", "nbId1_add", entities.ConnectionStatus_CONNECTED)
+ protoRan2Add, _ := createNbIdentityProto(t, "ran2_add", "plmnId2_add", "nbId2_add", entities.ConnectionStatus_CONNECTED)
+ nbIdIdentitiesProtoToAdd = append(nbIdIdentitiesProtoToAdd, protoRan1Add)
+ nbIdIdentitiesProtoToAdd = append(nbIdIdentitiesProtoToAdd, protoRan2Add)
+ sdlMock.On("AddMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToAdd).Return(nil)
+
+ var newNbIdIdentities []*entities.NbIdentity
+ firstNewNbIdIdentity := &entities.NbIdentity{InventoryName: "ran1_add", ConnectionStatus: entities.ConnectionStatus_CONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1_add", NbId: "nbId1_add"}}
+ secondNewNbIdIdentity := &entities.NbIdentity{InventoryName: "ran2_add", ConnectionStatus: entities.ConnectionStatus_CONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId2_add", NbId: "nbId2_add"}}
+ newNbIdIdentities = append(newNbIdIdentities, firstNewNbIdIdentity)
+ newNbIdIdentities = append(newNbIdIdentities, secondNewNbIdIdentity)
+
+ var oldNbIdIdentities []*entities.NbIdentity
+ firstOldNbIdIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
+ secondOldNbIdIdentity := &entities.NbIdentity{InventoryName: "ran2", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId2", NbId: "nbId2"}}
+ oldNbIdIdentities = append(oldNbIdIdentities, firstOldNbIdIdentity)
+ oldNbIdIdentities = append(oldNbIdIdentities, secondOldNbIdIdentity)
+
+ rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
+ assert.Nil(t, rNibErr)
+ sdlMock.AssertExpectations(t)
+}
+
+func TestUpdateNbIdentityOldIdentityMarshalNilFailure(t *testing.T) {
+ w, _ := initSdlMock()
+
+ oldNbIdIdentities := []*entities.NbIdentity{nil}
+ newNbIdIdentities := []*entities.NbIdentity{
+ &entities.NbIdentity{
+ InventoryName: "ran1_add",
+ ConnectionStatus: entities.ConnectionStatus_CONNECTED,
+ GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1_add", NbId: "nbId1_add"},
+ },
+ }
+
+ expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
+ rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
+ assert.Equal(t, expectedErr, rNibErr)
+}
+
+func TestUpdateNbIdentityNewIdentityMarshalNilFailure(t *testing.T) {
+ w, sdlMock := initSdlMock()
+
+ var nbIdIdentitiesProtoToRemove []interface{}
+ protoRan1, _ := createNbIdentityProto(t, "ran1", "plmnId1", "nbId1", entities.ConnectionStatus_DISCONNECTED)
+ nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan1)
+ sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToRemove).Return(nil)
+
+ oldNbIdIdentities := []*entities.NbIdentity{
+ &entities.NbIdentity{
+ InventoryName: "ran1",
+ ConnectionStatus: entities.ConnectionStatus_DISCONNECTED,
+ GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"},
+ },
+ }
+ newNbIdIdentities := []*entities.NbIdentity{nil}
+
+ expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
+ rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
+ assert.Equal(t, expectedErr, rNibErr)
+}
+
+func TestUpdateNbIdentityRemoveFailure(t *testing.T) {
+ w, sdlMock := initSdlMock()
+
+ var nbIdIdentitiesProtoToRemove []interface{}
+ protoRan1, _ := createNbIdentityProto(t, "ran1", "plmnId1", "nbId1", entities.ConnectionStatus_DISCONNECTED)
+ nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan1)
+ protoRan2, _ := createNbIdentityProto(t, "ran2", "plmnId2", "nbId2", entities.ConnectionStatus_DISCONNECTED)
+ nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan2)
+
+ sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToRemove).Return(fmt.Errorf("for test"))
+
+ var oldNbIdIdentities []*entities.NbIdentity
+ firstOldNbIdIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
+ secondOldNbIdIdentity := &entities.NbIdentity{InventoryName: "ran2", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId2", NbId: "nbId2"}}
+ oldNbIdIdentities = append(oldNbIdIdentities, firstOldNbIdIdentity)
+ oldNbIdIdentities = append(oldNbIdIdentities, secondOldNbIdIdentity)
+
+ var newNbIdIdentities []*entities.NbIdentity
+
+ rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
+ assert.NotNil(t, rNibErr)
+ sdlMock.AssertExpectations(t)
+}
+
+func TestUpdateNbIdentitySdlAddMemberFailure(t *testing.T) {
+ w, sdlMock := initSdlMock()
+
+ var nbIdIdentitiesProtoToRemove []interface{}
+ protoRan1, _ := createNbIdentityProto(t, "ran1", "plmnId1", "nbId1", entities.ConnectionStatus_DISCONNECTED)
+ nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan1)
+ sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToRemove).Return(nil)
+
+ var nbIdIdentitiesProtoToAdd []interface{}
+ protoRan1Add, _ := createNbIdentityProto(t, "ran1_add", "plmnId1_add", "nbId1_add", entities.ConnectionStatus_CONNECTED)
+ nbIdIdentitiesProtoToAdd = append(nbIdIdentitiesProtoToAdd, protoRan1Add)
+ sdlMock.On("AddMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToAdd).Return(fmt.Errorf("for test"))
+
+ var oldNbIdIdentities []*entities.NbIdentity
+ firstOldNbIdIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
+ oldNbIdIdentities = append(oldNbIdIdentities, firstOldNbIdIdentity)
+
+ var newNbIdIdentities []*entities.NbIdentity
+ firstNewNbIdIdentity := &entities.NbIdentity{InventoryName: "ran1_add", ConnectionStatus: entities.ConnectionStatus_CONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1_add", NbId: "nbId1_add"}}
+ newNbIdIdentities = append(newNbIdIdentities, firstNewNbIdIdentity)
+
+ rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
+ assert.NotNil(t, rNibErr)
+ sdlMock.AssertExpectations(t)
+}
+
+func TestUpdateNbIdentityAddFailure(t *testing.T) {
+ w, sdlMock := initSdlMock()
+ nbIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
+ nbIdData, err := proto.Marshal(nbIdentity)
+ if err != nil {
+ t.Errorf("#TestRemoveNbIdentitySuccess - failed to Marshal NbIdentity")
+ }
+ sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), []interface{}{nbIdData}).Return(fmt.Errorf("for test"))
+
+ rNibErr := w.RemoveNbIdentity(entities.Node_ENB, nbIdentity)
+ assert.NotNil(t, rNibErr)
+ sdlMock.AssertExpectations(t)
+}
+
+func TestUpdateNbIdentityNoNbIdentityToRemove(t *testing.T) {
+ w, sdlMock := initSdlMock()
+ nbIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
+ nbIdData, err := proto.Marshal(nbIdentity)
+ if err != nil {
+ t.Errorf("#TestRemoveNbIdentitySuccess - failed to Marshal NbIdentity")
+ }
+ sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), []interface{}{nbIdData}).Return(fmt.Errorf("for test"))
+
+ rNibErr := w.RemoveNbIdentity(entities.Node_ENB, nbIdentity)
+ assert.NotNil(t, rNibErr)
+ sdlMock.AssertExpectations(t)
+}
+
+func TestUpdateNbIdentityNoNbIdentityToAdd(t *testing.T) {
+ w, sdlMock := initSdlMock()
+ nbIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
+ nbIdData, err := proto.Marshal(nbIdentity)
+ if err != nil {
+ t.Errorf("#TestRemoveNbIdentitySuccess - failed to Marshal NbIdentity")
+ }
+ sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), []interface{}{nbIdData}).Return(fmt.Errorf("for test"))
+
+ rNibErr := w.RemoveNbIdentity(entities.Node_ENB, nbIdentity)
+ assert.NotNil(t, rNibErr)
+ sdlMock.AssertExpectations(t)
+}
+
+func createNbIdentityProto(t *testing.T, ranName string, plmnId string, nbId string, connectionStatus entities.ConnectionStatus) ([]byte, *entities.NbIdentity) {
+ nbIdentity := &entities.NbIdentity{InventoryName: ranName, ConnectionStatus: connectionStatus, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}}
+ nbIdData, err := proto.Marshal(nbIdentity)
+ if err != nil {
+ t.Errorf("#createNbIdentityProto - failed to Marshal NbIdentity")
+ }
+ return nbIdData, nbIdentity
+}