[RIC-298] Added handling for orphan cells and updated Swagger
[ric-plt/e2mgr.git] / E2Manager / controllers / nodeb_controller_test.go
index 4d5e829..c61249e 100644 (file)
@@ -90,14 +90,41 @@ type updateGnbCellsParams struct {
        err error
 }
 
+type removeServedNrCellsParams struct {
+       servedNrCells []*entities.ServedNRCell
+       err error
+}
+
 type controllerUpdateGnbTestContext struct {
        getNodebInfoResult   *getNodebInfoResult
+       removeServedNrCellsParams *removeServedNrCellsParams
        updateGnbCellsParams *updateGnbCellsParams
        requestBody          map[string]interface{}
        expectedStatusCode   int
        expectedJsonResponse string
 }
 
+func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell {
+
+       servedNrCells := []*entities.ServedNRCell{}
+
+       for _, 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:       5,
+                       ServedPlmns: []string{"whatever"},
+               }})
+       }
+
+       return servedNrCells
+}
+
 func buildNrNeighbourInformation(propToOmit string) map[string]interface{} {
        ret := map[string]interface{}{
                "nrCgi": "whatever",
@@ -182,10 +209,9 @@ func TestX2SetupSuccess(t *testing.T) {
        nb := &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, AssociatedE2TInstanceAddress: "10.0.2.15:8989"}
        readerMock.On("GetNodeb", ranName).Return(nb, nil)
        var nbUpdated = *nb
-       nbUpdated.ConnectionAttempts = 0
        writerMock.On("UpdateNodebInfo", &nbUpdated).Return(nil)
 
-       var nbUpdated2 = &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, ConnectionAttempts: 1, AssociatedE2TInstanceAddress: "10.0.2.15:8989"}
+       var nbUpdated2 = &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, AssociatedE2TInstanceAddress: "10.0.2.15:8989"}
        writerMock.On("UpdateNodebInfo", nbUpdated2).Return(nil)
 
        payload := e2pdus.PackedX2setupRequest
@@ -214,10 +240,9 @@ func TestEndcSetupSuccess(t *testing.T) {
        nb := &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST, AssociatedE2TInstanceAddress: "10.0.2.15:8989"}
        readerMock.On("GetNodeb", ranName).Return(nb, nil)
        var nbUpdated = *nb
-       nbUpdated.ConnectionAttempts = 0
        writerMock.On("UpdateNodebInfo", &nbUpdated).Return(nil)
 
-       var nbUpdated2 = &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST, ConnectionAttempts: 1, AssociatedE2TInstanceAddress: "10.0.2.15:8989"}
+       var nbUpdated2 = &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST, AssociatedE2TInstanceAddress: "10.0.2.15:8989"}
        writerMock.On("UpdateNodebInfo", nbUpdated2).Return(nil)
 
        payload := e2pdus.PackedEndcX2setupRequest
@@ -280,11 +305,16 @@ func activateControllerUpdateGnbMocks(context *controllerUpdateGnbTestContext, r
                readerMock.On("GetNodeb", RanName).Return(context.getNodebInfoResult.nodebInfo, context.getNodebInfoResult.rnibError)
        }
 
+       if context.removeServedNrCellsParams != nil {
+               writerMock.On("RemoveServedNrCells", RanName, context.removeServedNrCellsParams.servedNrCells).Return(context.removeServedNrCellsParams.err)
+       }
+
        if context.updateGnbCellsParams != nil {
                updatedNodebInfo := *context.getNodebInfoResult.nodebInfo
                gnb := entities.Gnb{}
                _ = jsonpb.Unmarshal(getJsonRequestAsBuffer(context.requestBody), &gnb)
-               updatedNodebInfo.GetGnb().ServedNrCells = gnb.ServedNrCells
+               updatedGnb := *updatedNodebInfo.GetGnb()
+               updatedGnb.ServedNrCells = gnb.ServedNrCells
                writerMock.On("UpdateGnbCells", &updatedNodebInfo, gnb.ServedNrCells).Return(context.updateGnbCellsParams.err)
        }
 }
@@ -303,6 +333,10 @@ func assertControllerUpdateGnb(t *testing.T, context *controllerUpdateGnbTestCon
        if context.updateGnbCellsParams != nil {
                writerMock.AssertNotCalled(t, "UpdateGnb")
        }
+
+       if context.removeServedNrCellsParams != nil {
+               writerMock.AssertNotCalled(t, "RemoveServedNrCells")
+       }
 }
 
 func buildUpdateGnbRequest(context *controllerUpdateGnbTestContext) *http.Request {
@@ -510,8 +544,46 @@ func TestControllerUpdateGnbGetNodebSuccessInvalidGnbConfiguration(t *testing.T)
        controllerUpdateGnbTestExecuter(t, &context)
 }
 
+func TestControllerUpdateGnbGetNodebSuccessRemoveServedNrCellsFailure(t *testing.T) {
+       oldServedNrCells := generateServedNrCells("whatever1","whatever2")
+       context := controllerUpdateGnbTestContext{
+               removeServedNrCellsParams: &removeServedNrCellsParams{
+                       err: common.NewInternalError(errors.New("#writer.UpdateGnbCells - Internal Error")),
+                       servedNrCells: oldServedNrCells,
+               },
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: &entities.NodebInfo{
+                               RanName:                      RanName,
+                               ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
+                               AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
+                               Configuration:                &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{ServedNrCells: oldServedNrCells}},
+                       },
+                       rnibError: nil,
+               },
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": buildServedNrCellInformation(""),
+                                       "nrNeighbourInfos": []interface{}{
+                                               buildNrNeighbourInformation(""),
+                                       },
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusInternalServerError,
+               expectedJsonResponse: RnibErrorJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
 func TestControllerUpdateGnbGetNodebSuccessUpdateGnbCellsFailure(t *testing.T) {
+       oldServedNrCells := generateServedNrCells("whatever1","whatever2")
        context := controllerUpdateGnbTestContext{
+               removeServedNrCellsParams: &removeServedNrCellsParams{
+                       err: nil,
+                       servedNrCells: oldServedNrCells,
+               },
                updateGnbCellsParams: &updateGnbCellsParams{
                        err: common.NewInternalError(errors.New("#writer.UpdateGnbCells - Internal Error")),
                },
@@ -520,7 +592,7 @@ func TestControllerUpdateGnbGetNodebSuccessUpdateGnbCellsFailure(t *testing.T) {
                                RanName:                      RanName,
                                ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
                                AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
-                               Configuration:                &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{}},
+                               Configuration:                &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{ServedNrCells: oldServedNrCells}},
                        },
                        rnibError: nil,
                },