merge of Natalia - change rnib errors
[ric-plt/e2mgr.git] / E2Manager / rNibWriter / rNibWriter_test.go
index c2b6d23..c887da6 100644 (file)
@@ -71,6 +71,89 @@ func initSdlInstanceMock(namespace string, poolSize int) *mocks.MockSdlInstance
        return sdlInstanceMock
 }
 
+func TestUpdateNodebInfoSuccess(t *testing.T) {
+       inventoryName := "name"
+       plmnId := "02f829"
+       nbId := "4a952a0a"
+       writerPool = nil
+       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
+       w := GetRNibWriter()
+       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}
+       data, err := proto.Marshal(nodebInfo)
+       if err != nil {
+               t.Errorf("#rNibWriter_test.TestSaveEnb - 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)
+
+       rNibErr := w.UpdateNodebInfo(nodebInfo)
+       assert.Nil(t, rNibErr)
+}
+
+func TestUpdateNodebInfoMissingInventoryNameFailure(t *testing.T) {
+       inventoryName := "name"
+       plmnId := "02f829"
+       nbId := "4a952a0a"
+       writerPool = nil
+       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
+       w := GetRNibWriter()
+       nodebInfo := &entities.NodebInfo{}
+       data, err := proto.Marshal(nodebInfo)
+       if err != nil {
+               t.Errorf("#rNibWriter_test.TestSaveEnb - 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)
+
+       rNibErr := w.UpdateNodebInfo(nodebInfo)
+
+       assert.NotNil(t, rNibErr)
+       assert.IsType(t, &common.ValidationError{}, rNibErr)
+}
+
+func TestUpdateNodebInfoMissingGlobalNbId(t *testing.T) {
+       inventoryName := "name"
+       writerPool = nil
+       sdlInstanceMock := initSdlInstanceMock(namespace, 1)
+       w := GetRNibWriter()
+       nodebInfo := &entities.NodebInfo{}
+       nodebInfo.RanName = inventoryName
+       data, err := proto.Marshal(nodebInfo)
+       if err != nil {
+               t.Errorf("#rNibWriter_test.TestSaveEnb - 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)
+
+       rNibErr := w.UpdateNodebInfo(nodebInfo)
+
+       assert.Nil(t, rNibErr)
+}
+
 func TestSaveEnb(t *testing.T) {
        name := "name"
        ranName := "RAN:" + name
@@ -104,12 +187,20 @@ func TestSaveEnb(t *testing.T) {
        setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetPci()), cellData)
 
        sdlInstanceMock.On("Set", []interface{}{setExpected}).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)
+
        nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
-       nbIdData, err := proto.Marshal(nbIdentity)
+       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)
+
        rNibErr := w.SaveNodeb(nbIdentity, &nb)
        assert.Nil(t, rNibErr)
 }
@@ -132,8 +223,8 @@ func TestSaveEnbCellIdValidationFailure(t *testing.T) {
        nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
        rNibErr := w.SaveNodeb(nbIdentity, &nb)
        assert.NotNil(t, rNibErr)
-       assert.Equal(t, common.VALIDATION_ERROR, rNibErr.GetCode())
-       assert.Equal(t, "3 VALIDATION_ERROR - #utils.ValidateAndBuildCellIdKey - an empty cell id received", rNibErr.Error())
+       assert.IsType(t, &common.ValidationError{}, rNibErr)
+       assert.Equal(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", rNibErr.Error())
 }
 
 func TestSaveEnbInventoryNameValidationFailure(t *testing.T) {
@@ -153,8 +244,8 @@ func TestSaveEnbInventoryNameValidationFailure(t *testing.T) {
        nbIdentity := &entities.NbIdentity{InventoryName: "", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
        rNibErr := w.SaveNodeb(nbIdentity, &nb)
        assert.NotNil(t, rNibErr)
-       assert.Equal(t, common.VALIDATION_ERROR, rNibErr.GetCode())
-       assert.Equal(t, "3 VALIDATION_ERROR - #utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error())
+       assert.IsType(t, &common.ValidationError{}, rNibErr)
+       assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error())
 }
 
 func TestSaveEnbOnClosedPool(t *testing.T) {
@@ -200,8 +291,8 @@ func TestSaveGnbCellIdValidationFailure(t *testing.T) {
        nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
        rNibErr := w.SaveNodeb(nbIdentity, &nb)
        assert.NotNil(t, rNibErr)
-       assert.Equal(t, common.VALIDATION_ERROR, rNibErr.GetCode())
-       assert.Equal(t, "3 VALIDATION_ERROR - #utils.ValidateAndBuildNrCellIdKey - an empty cell id received", rNibErr.Error())
+       assert.IsType(t, &common.ValidationError{}, rNibErr)
+       assert.Equal(t, "#utils.ValidateAndBuildNrCellIdKey - an empty cell id received", rNibErr.Error())
 }
 
 func TestSaveGnb(t *testing.T) {
@@ -245,6 +336,12 @@ func TestSaveGnb(t *testing.T) {
        }
        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)
        assert.Nil(t, rNibErr)
 }
@@ -296,7 +393,7 @@ func TestSaveRanLoadInformationEmptyInventoryNameFailure(t *testing.T) {
 
        err := w.SaveRanLoadInformation(inventoryName, nil)
        assert.NotNil(t, err)
-       assert.Equal(t, common.VALIDATION_ERROR, err.GetCode())
+       assert.IsType(t, &common.ValidationError{}, err)
 }
 
 func TestSaveRanLoadInformationSdlFailure(t *testing.T) {
@@ -326,8 +423,7 @@ func TestSaveRanLoadInformationSdlFailure(t *testing.T) {
 
        rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
        assert.NotNil(t, rNibErr)
-       assert.Equal(t, common.INTERNAL_ERROR, rNibErr.GetCode())
-       assert.Equal(t, expectedErr, rNibErr.GetError())
+       assert.IsType(t, &common.InternalError{}, rNibErr)
 }
 
 func generateCellLoadInformation() *entities.CellLoadInformation {
@@ -417,7 +513,7 @@ func TestSaveUnknownTypeEntityFailure(t *testing.T) {
        writerPool = nil
        initSdlInstanceMock(namespace, 1)
        w := GetRNibWriter()
-       expectedErr := common.NewValidationError(errors.New("#rNibWriter.saveNodeB - Unknown responding node type, entity: ip:\"localhost\" port:5656 "))
+       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
@@ -451,17 +547,17 @@ func TestSaveEntityFailure(t *testing.T) {
 
 func TestGetRNibWriterPoolNotInitializedFailure(t *testing.T) {
        writerPool = nil
-       assert.Panics(t, func() { GetRNibWriter() })
+       assert.Panics(t, func() { GetRNibWriter().SaveNodeb(nil,nil) })
 }
 
 func TestGetRNibWriter(t *testing.T) {
        writerPool = nil
        initSdlInstanceMock(namespace, 1)
        received := GetRNibWriter()
-       assert.NotEmpty(t, received)
+       assert.Empty(t, received)
        available, created := writerPool.Stats()
        assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
-       assert.Equal(t, 1, created, "number of created objects in the writerPool should be 1")
+       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
        writerPool.Close()
 }
 
@@ -474,13 +570,12 @@ func TestClose(t *testing.T) {
        writerPool.Put(w2)
        available, created := writerPool.Stats()
        assert.Equal(t, 2, available, "number of available objects in the writerPool should be 2")
-       assert.Equal(t, 2, created, "number of created objects in the writerPool should be 2")
+       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
        var e error
        instanceMock.On("Close").Return(e)
        Close()
        available, created = writerPool.Stats()
        assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
-       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
 }
 
 func TestCloseOnClosedPoolFailure(t *testing.T) {
@@ -490,7 +585,7 @@ func TestCloseOnClosedPoolFailure(t *testing.T) {
        writerPool.Put(w1)
        available, created := writerPool.Stats()
        assert.Equal(t, 1, available, "number of available objects in the writerPool should be 1")
-       assert.Equal(t, 1, created, "number of created objects in the writerPool should be 1")
+       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
        var e error
        instanceMock.On("Close").Return(e)
        Close()
@@ -504,13 +599,12 @@ func TestCloseFailure(t *testing.T) {
        writerPool.Put(w1)
        available, created := writerPool.Stats()
        assert.Equal(t, 1, available, "number of available objects in the writerPool should be 1")
-       assert.Equal(t, 1, created, "number of created objects in the writerPool should be 1")
+       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
        e := errors.New("expected error")
        instanceMock.On("Close").Return(e)
        Close()
        available, created = writerPool.Stats()
        assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
-       assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
 }
 
 func TestInit(t *testing.T) {
@@ -526,7 +620,7 @@ func TestInit(t *testing.T) {
 
 //Integration tests
 //
-//func TestSaveEnbInteg(t *testing.T){
+//func TestSaveEnbGnbInteg(t *testing.T){
 //     for i := 0; i<10; i++{
 //             Init("e2Manager", 1)
 //             w := GetRNibWriter()
@@ -551,7 +645,7 @@ func TestInit(t *testing.T) {
 //
 //             nb1 := entities.NodebInfo{}
 //             nb1.NodeType = entities.Node_GNB
-//             nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED
+//             nb1.ConnectionStatus = entities.ConnectionStatus_CONNECTED
 //             nb1.Ip = "localhost"
 //             nb1.Port =  uint32(6565 + i)
 //             gnb := entities.Gnb{}
@@ -567,7 +661,23 @@ func TestInit(t *testing.T) {
 //             }
 //     }
 //}
-
+//
+//func TestSaveNbRanNamesInteg(t *testing.T){
+//     for i := 0; i<10; i++{
+//             Init("e2Manager", 1)
+//             w := GetRNibWriter()
+//             nb := entities.NodebInfo{}
+//             nb.ConnectionStatus = entities.ConnectionStatus_CONNECTING
+//             nb.Ip = "localhost"
+//             nb.Port = uint32(5656 + i)
+//             nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameOnly%d" ,i)}
+//             err := w.SaveNodeb(nbIdentity, &nb)
+//             if err != nil{
+//                     t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
+//             }
+//     }
+//}
+//
 //func TestSaveRanLoadInformationInteg(t *testing.T){
 //             Init("e2Manager", 1)
 //             w := GetRNibWriter()