[RIC-346] Refactor E2 Setup flow and update EnGnb xml
[ric-plt/e2mgr.git] / E2Manager / controllers / nodeb_controller_test.go
index 4716e75..a9bccdc 100644 (file)
@@ -38,6 +38,7 @@ import (
        "fmt"
        "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
        "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
+       "github.com/golang/protobuf/jsonpb"
        "github.com/gorilla/mux"
        "github.com/pkg/errors"
        "github.com/stretchr/testify/assert"
@@ -57,6 +58,7 @@ const (
        ValidationFailureJson        = "{\"errorCode\":402,\"errorMessage\":\"Validation error\"}"
        ResourceNotFoundJson         = "{\"errorCode\":404,\"errorMessage\":\"Resource not found\"}"
        RnibErrorJson                = "{\"errorCode\":500,\"errorMessage\":\"RNIB error\"}"
+       InternalErrorJson            = "{\"errorCode\":501,\"errorMessage\":\"Internal Server Error. Please try again later\"}"
 )
 
 var (
@@ -84,11 +86,43 @@ type getNodebInfoResult struct {
        rnibError error
 }
 
+type updateGnbCellsParams struct {
+       err error
+}
+
+type removeServedNrCellsParams struct {
+       servedNrCells []*entities.ServedNRCell
+       err           error
+}
+
 type controllerUpdateGnbTestContext struct {
-       getNodebInfoResult   *getNodebInfoResult
-       requestBody          map[string]interface{}
-       expectedStatusCode   int
-       expectedJsonResponse string
+       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{} {
@@ -175,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
@@ -207,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
@@ -268,23 +300,62 @@ func controllerGetNodebIdListTestExecuter(t *testing.T, context *controllerGetNo
        assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
 }
 
-func controllerUpdateGnbTestExecuter(t *testing.T, context *controllerUpdateGnbTestContext) {
-       controller, readerMock, _, _, _ := setupControllerTest(t)
-       writer := httptest.NewRecorder()
-
+func activateControllerUpdateGnbMocks(context *controllerUpdateGnbTestContext, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock) {
        if context.getNodebInfoResult != nil {
                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)
+               updatedGnb := *updatedNodebInfo.GetGnb()
+               updatedGnb.ServedNrCells = gnb.ServedNrCells
+               writerMock.On("UpdateGnbCells", &updatedNodebInfo, gnb.ServedNrCells).Return(context.updateGnbCellsParams.err)
+       }
+}
+
+func assertControllerUpdateGnb(t *testing.T, context *controllerUpdateGnbTestContext, writer *httptest.ResponseRecorder, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock) {
+       assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
+       bodyBytes, _ := ioutil.ReadAll(writer.Body)
+       assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
+       readerMock.AssertExpectations(t)
+       writerMock.AssertExpectations(t)
+
+       if context.getNodebInfoResult != nil {
+               readerMock.AssertNotCalled(t, "GetNodeb")
+       }
+
+       if context.updateGnbCellsParams != nil {
+               writerMock.AssertNotCalled(t, "UpdateGnb")
+       }
+
+       if context.removeServedNrCellsParams != nil {
+               writerMock.AssertNotCalled(t, "RemoveServedNrCells")
+       }
+}
+
+func buildUpdateGnbRequest(context *controllerUpdateGnbTestContext) *http.Request {
        updateGnbUrl := fmt.Sprintf("/nodeb/%s/update", RanName)
        requestBody := getJsonRequestAsBuffer(context.requestBody)
        req, _ := http.NewRequest(http.MethodGet, updateGnbUrl, requestBody)
        req.Header.Set("Content-Type", "application/json")
        req = mux.SetURLVars(req, map[string]string{"ranName": RanName})
+       return req
+}
+
+func controllerUpdateGnbTestExecuter(t *testing.T, context *controllerUpdateGnbTestContext) {
+       controller, readerMock, writerMock, _, _ := setupControllerTest(t)
+       writer := httptest.NewRecorder()
+
+       activateControllerUpdateGnbMocks(context, readerMock, writerMock)
+       req := buildUpdateGnbRequest(context)
        controller.UpdateGnb(writer, req)
-       assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
-       bodyBytes, _ := ioutil.ReadAll(writer.Body)
-       assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
+       assertControllerUpdateGnb(t, context, writer, readerMock, writerMock)
 }
 
 func TestControllerUpdateGnbEmptyServedNrCells(t *testing.T) {
@@ -370,7 +441,7 @@ func TestControllerUpdateGnbMissingNeighbourInfoFddOrTdd(t *testing.T) {
                        "servedNrCells": []interface{}{
                                map[string]interface{}{
                                        "servedNrCellInformation": buildServedNrCellInformation(""),
-                                       "nrNeighbourInfos":        []interface{}{
+                                       "nrNeighbourInfos": []interface{}{
                                                nrNeighbourInfo,
                                        },
                                },
@@ -392,7 +463,7 @@ func TestControllerUpdateGnbMissingNrNeighbourInformationRequiredProp(t *testing
                                "servedNrCells": []interface{}{
                                        map[string]interface{}{
                                                "servedNrCellInformation": buildServedNrCellInformation(""),
-                                               "nrNeighbourInfos":        []interface{}{
+                                               "nrNeighbourInfos": []interface{}{
                                                        buildNrNeighbourInformation(v),
                                                },
                                        },
@@ -406,59 +477,132 @@ func TestControllerUpdateGnbMissingNrNeighbourInformationRequiredProp(t *testing
        }
 }
 
-func TestControllerUpdateGnbValidServedNrCellInformationAndNrNeighbourInfoGetNodebSuccess(t *testing.T) {
+func TestControllerUpdateGnbValidServedNrCellInformationGetNodebNotFound(t *testing.T) {
+       context := controllerUpdateGnbTestContext{
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: nil,
+                       rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
+               },
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": buildServedNrCellInformation(""),
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusNotFound,
+               expectedJsonResponse: ResourceNotFoundJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbValidServedNrCellInformationGetNodebInternalError(t *testing.T) {
        context := controllerUpdateGnbTestContext{
                getNodebInfoResult: &getNodebInfoResult{
-                       nodebInfo: &entities.NodebInfo{RanName: RanName, ConnectionStatus: entities.ConnectionStatus_CONNECTED, AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress},
+                       nodebInfo: nil,
+                       rnibError: common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
+               },
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": buildServedNrCellInformation(""),
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusInternalServerError,
+               expectedJsonResponse: RnibErrorJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbGetNodebSuccessInvalidGnbConfiguration(t *testing.T) {
+       context := controllerUpdateGnbTestContext{
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: &entities.NodebInfo{
+                               RanName:                      RanName,
+                               ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
+                               AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
+                       },
                        rnibError: nil,
                },
                requestBody: map[string]interface{}{
                        "servedNrCells": []interface{}{
                                map[string]interface{}{
                                        "servedNrCellInformation": buildServedNrCellInformation(""),
-                                       "nrNeighbourInfos":        []interface{}{
+                                       "nrNeighbourInfos": []interface{}{
                                                buildNrNeighbourInformation(""),
                                        },
                                },
                        },
                },
-               expectedStatusCode:   http.StatusOK,
-               expectedJsonResponse: "{\"ranName\":\"test\",\"connectionStatus\":\"CONNECTED\",\"associatedE2tInstanceAddress\":\"10.0.2.15:38000\"}",
+               expectedStatusCode:   http.StatusInternalServerError,
+               expectedJsonResponse: InternalErrorJson,
        }
 
        controllerUpdateGnbTestExecuter(t, &context)
 }
 
-func TestControllerUpdateGnbValidServedNrCellInformationGetNodebNotFound(t *testing.T) {
+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: nil,
-                       rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
+                       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.StatusNotFound,
-               expectedJsonResponse: ResourceNotFoundJson,
+               expectedStatusCode:   http.StatusInternalServerError,
+               expectedJsonResponse: RnibErrorJson,
        }
 
        controllerUpdateGnbTestExecuter(t, &context)
 }
 
-func TestControllerUpdateGnbValidServedNrCellInformationGetNodebInternalError(t *testing.T) {
+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")),
+               },
                getNodebInfoResult: &getNodebInfoResult{
-                       nodebInfo: nil,
-                       rnibError: common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
+                       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(""),
+                                       },
                                },
                        },
                },
@@ -469,10 +613,41 @@ func TestControllerUpdateGnbValidServedNrCellInformationGetNodebInternalError(t
        controllerUpdateGnbTestExecuter(t, &context)
 }
 
+func TestControllerUpdateGnbSuccess(t *testing.T) {
+       context := controllerUpdateGnbTestContext{
+               updateGnbCellsParams: &updateGnbCellsParams{
+                       err: nil,
+               },
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: &entities.NodebInfo{
+                               RanName:                      RanName,
+                               ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
+                               AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
+                               Configuration:                &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{}},
+                       },
+                       rnibError: nil,
+               },
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": buildServedNrCellInformation(""),
+                                       "nrNeighbourInfos": []interface{}{
+                                               buildNrNeighbourInformation(""),
+                                       },
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusOK,
+               expectedJsonResponse: "{\"ranName\":\"test\",\"connectionStatus\":\"CONNECTED\",\"gnb\":{\"servedNrCells\":[{\"servedNrCellInformation\":{\"nrPci\":1,\"cellId\":\"whatever\",\"servedPlmns\":[\"whatever\"],\"nrMode\":\"FDD\",\"choiceNrMode\":{\"fdd\":{}}},\"nrNeighbourInfos\":[{\"nrPci\":1,\"nrCgi\":\"whatever\",\"nrMode\":\"FDD\",\"choiceNrMode\":{\"tdd\":{}}}]}]},\"associatedE2tInstanceAddress\":\"10.0.2.15:38000\"}",
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
 func getJsonRequestAsBuffer(requestJson map[string]interface{}) *bytes.Buffer {
        b := new(bytes.Buffer)
        _ = json.NewEncoder(b).Encode(requestJson)
-       return b;
+       return b
 }
 
 func TestControllerGetNodebSuccess(t *testing.T) {
@@ -660,7 +835,7 @@ func parseJsonRequest(t *testing.T, r io.Reader) models.ErrorResponse {
        if err != nil {
                t.Errorf("Error cannot deserialize json request")
        }
-       _ =json.Unmarshal(body, &errorResponse)
+       _ = json.Unmarshal(body, &errorResponse)
 
        return errorResponse
 }