[RIC-584] - Improve logging when enb type in add enb request is not valid
[ric-plt/e2mgr.git] / E2Manager / controllers / nodeb_controller_test.go
index a8a4e9f..f57522c 100644 (file)
@@ -101,7 +101,7 @@ type updateEnbCellsParams struct {
        err error
 }
 
-type saveNodebParams struct {
+type addEnbParams struct {
        err error
 }
 
@@ -139,7 +139,7 @@ type controllerUpdateGnbTestContext struct {
 
 type controllerAddEnbTestContext struct {
        getNodebInfoResult   *getNodebInfoResult
-       saveNodebParams      *saveNodebParams
+       addEnbParams         *addEnbParams
        addNbIdentityParams  *addNbIdentityParams
        requestBody          map[string]interface{}
        expectedStatusCode   int
@@ -432,7 +432,7 @@ func controllerGetNodebIdListTestExecuter(t *testing.T, context *controllerGetNo
        controller.GetNodebIdList(writer, req)
        assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
        bodyBytes, _ := ioutil.ReadAll(writer.Body)
-       assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
+       assert.Contains(t, context.expectedJsonResponse, string(bodyBytes))
 }
 
 func activateControllerUpdateEnbMocks(context *controllerUpdateEnbTestContext, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock, updateEnbRequest *models.UpdateEnbRequest) {
@@ -446,6 +446,11 @@ func activateControllerUpdateEnbMocks(context *controllerUpdateEnbTestContext, r
 
        if context.updateEnbCellsParams != nil {
                updatedNodebInfo := *context.getNodebInfoResult.nodebInfo
+
+               if context.getNodebInfoResult.nodebInfo.SetupFromNetwork {
+                       updateEnbRequest.Enb.EnbType = context.getNodebInfoResult.nodebInfo.GetEnb().EnbType
+               }
+
                updatedNodebInfo.Configuration = &entities.NodebInfo_Enb{Enb: updateEnbRequest.Enb}
 
                writerMock.On("UpdateEnb", &updatedNodebInfo, updateEnbRequest.Enb.ServedCells).Return(context.updateEnbCellsParams.err)
@@ -562,7 +567,7 @@ func activateControllerAddEnbMocks(context *controllerAddEnbTestContext, readerM
                readerMock.On("GetNodeb", RanName).Return(context.getNodebInfoResult.nodebInfo, context.getNodebInfoResult.rnibError)
        }
 
-       if context.saveNodebParams != nil {
+       if context.addEnbParams != nil {
                nodebInfo := entities.NodebInfo{
                        RanName:          addEnbRequest.RanName,
                        Ip:               addEnbRequest.Ip,
@@ -573,7 +578,7 @@ func activateControllerAddEnbMocks(context *controllerAddEnbTestContext, readerM
                        ConnectionStatus: entities.ConnectionStatus_DISCONNECTED,
                }
 
-               writerMock.On("SaveNodeb", &nodebInfo).Return(context.saveNodebParams.err)
+               writerMock.On("AddEnb", &nodebInfo).Return(context.addEnbParams.err)
        }
 
        if context.addNbIdentityParams != nil {
@@ -601,7 +606,8 @@ func controllerAddEnbTestExecuter(t *testing.T, context *controllerAddEnbTestCon
 func controllerDeleteEnbTestExecuter(t *testing.T, context *controllerDeleteEnbTestContext, preAddNbIdentity bool) {
        controller, readerMock, writerMock, nbIdentity := setupDeleteEnbControllerTest(t, preAddNbIdentity)
        readerMock.On("GetNodeb", RanName).Return(context.getNodebInfoResult.nodebInfo, context.getNodebInfoResult.rnibError)
-       if context.getNodebInfoResult.rnibError == nil && context.getNodebInfoResult.nodebInfo.GetNodeType() == entities.Node_ENB {
+       if context.getNodebInfoResult.rnibError == nil && context.getNodebInfoResult.nodebInfo.GetNodeType() == entities.Node_ENB &&
+               !context.getNodebInfoResult.nodebInfo.SetupFromNetwork {
                writerMock.On("RemoveEnb", context.getNodebInfoResult.nodebInfo).Return(nil)
                if preAddNbIdentity {
                        writerMock.On("RemoveNbIdentity", entities.Node_ENB, nbIdentity).Return(nil)
@@ -1157,8 +1163,38 @@ func TestControllerUpdateEnbExistingEmptyCellsSuccess(t *testing.T) {
        controllerUpdateEnbTestExecuter(t, &context)
 }
 
+func TestControllerUpdateEnbNgEnbFailure(t *testing.T) {
+
+       requestBody := map[string]interface{}{
+               "enb": map[string]interface{}{
+                       "enbType": 3,
+                       "servedCells": []interface{}{
+                               buildServedCell(""),
+                       }},
+       }
 
-func TestControllerUpdateEnbSuccess(t *testing.T) {
+       oldServedCells := generateServedCells("whatever1", "whatever2")
+
+       context := controllerUpdateEnbTestContext{
+               getNodebInfoResult:  &getNodebInfoResult{
+               nodebInfo: &entities.NodebInfo{
+               RanName:                      RanName,
+               ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
+               AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
+               NodeType:                     entities.Node_ENB,
+               Configuration:                &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells, EnbType: entities.EnbType_MACRO_NG_ENB}},
+       },
+               rnibError: nil,
+       },
+               requestBody:          requestBody,
+               expectedStatusCode:   http.StatusBadRequest,
+               expectedJsonResponse: ValidationFailureJson,
+       }
+
+       controllerUpdateEnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateEnbSuccessSetupFromNwFalse(t *testing.T) {
        oldServedCells := generateServedCells("whatever1", "whatever2")
        context := controllerUpdateEnbTestContext{
                removeServedCellsParams: &removeServedCellsParams{
@@ -1174,7 +1210,7 @@ func TestControllerUpdateEnbSuccess(t *testing.T) {
                                ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
                                AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
                                NodeType:                     entities.Node_ENB,
-                               Configuration:                &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells, EnbType: entities.EnbType_MACRO_ENB}},
+                               Configuration:                &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells, EnbType: entities.EnbType_LONG_MACRO_ENB}},
                        },
                        rnibError: nil,
                },
@@ -1186,6 +1222,35 @@ func TestControllerUpdateEnbSuccess(t *testing.T) {
        controllerUpdateEnbTestExecuter(t, &context)
 }
 
+func TestControllerUpdateEnbSuccessSetupFromNwTrue(t *testing.T) {
+       oldServedCells := generateServedCells("whatever1", "whatever2")
+       context := controllerUpdateEnbTestContext{
+               removeServedCellsParams: &removeServedCellsParams{
+                       err:            nil,
+                       servedCellInfo: oldServedCells,
+               },
+               updateEnbCellsParams: &updateEnbCellsParams{
+                       err: nil,
+               },
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: &entities.NodebInfo{
+                               RanName:                      RanName,
+                               ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
+                               AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
+                               NodeType:                     entities.Node_ENB,
+                               Configuration:                &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells, EnbType: entities.EnbType_LONG_MACRO_ENB}},
+                               SetupFromNetwork:             true,
+                       },
+                       rnibError: nil,
+               },
+               requestBody:          getUpdateEnbRequest(""),
+               expectedStatusCode:   http.StatusOK,
+               expectedJsonResponse: "{\"ranName\":\"test\",\"connectionStatus\":\"CONNECTED\",\"nodeType\":\"ENB\",\"enb\":{\"enbType\":\"LONG_MACRO_ENB\",\"servedCells\":[{\"pci\":1,\"cellId\":\"whatever\",\"tac\":\"whatever3\",\"broadcastPlmns\":[\"whatever\"],\"choiceEutraMode\":{\"fdd\":{}},\"eutraMode\":\"FDD\"}]},\"associatedE2tInstanceAddress\":\"10.0.2.15:38000\",\"setupFromNetwork\":true}",
+       }
+
+       controllerUpdateEnbTestExecuter(t, &context)
+}
+
 /*
 AddEnb UTs
 */
@@ -1220,8 +1285,8 @@ func TestControllerAddEnbNodebExistsFailure(t *testing.T) {
 
 func TestControllerAddEnbSaveNodebFailure(t *testing.T) {
        context := controllerAddEnbTestContext{
-               saveNodebParams: &saveNodebParams{
-                       err: common.NewInternalError(errors.New("#reader.SaveeNodeb - Internal Error")),
+               addEnbParams: &addEnbParams{
+                       err: common.NewInternalError(errors.New("#reader.AddEnb - Internal Error")),
                },
                getNodebInfoResult: &getNodebInfoResult{
                        nodebInfo: nil,
@@ -1237,7 +1302,7 @@ func TestControllerAddEnbSaveNodebFailure(t *testing.T) {
 
 func TestControllerAddEnbAddNbIdentityFailure(t *testing.T) {
        context := controllerAddEnbTestContext{
-               saveNodebParams: &saveNodebParams{
+               addEnbParams: &addEnbParams{
                        err: nil,
                },
                addNbIdentityParams: &addNbIdentityParams{
@@ -1340,9 +1405,31 @@ func TestControllerAddEnbMissingRequiredServedCellProps(t *testing.T) {
        }
 }
 
+func TestControllerAddEnbNgEnbFailure(t *testing.T) {
+
+       requestBody := map[string]interface{}{
+               "ranName":    RanName,
+               "globalNbId": buildGlobalNbId(""),
+               "enb": map[string]interface{}{
+                       "enbType": 5,
+                       "servedCells": []interface{}{
+                               buildServedCell(""),
+                       },
+               },
+       }
+
+       context := controllerAddEnbTestContext{
+               requestBody:          requestBody,
+               expectedStatusCode:   http.StatusBadRequest,
+               expectedJsonResponse: ValidationFailureJson,
+       }
+
+       controllerAddEnbTestExecuter(t, &context)
+}
+
 func TestControllerAddEnbSuccess(t *testing.T) {
        context := controllerAddEnbTestContext{
-               saveNodebParams: &saveNodebParams{
+               addEnbParams: &addEnbParams{
                        err: nil,
                },
                addNbIdentityParams: &addNbIdentityParams{
@@ -1403,6 +1490,18 @@ func TestControllerDeleteEnbNodebNotEnb(t *testing.T) {
        controllerDeleteEnbTestExecuter(t, &context, false)
 }
 
+func TestControllerDeleteEnbSetupFromNetworkTrueFailure(t *testing.T) {
+       context := controllerDeleteEnbTestContext{
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: &entities.NodebInfo{RanName: RanName, NodeType: entities.Node_ENB, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, SetupFromNetwork: true},
+                       rnibError: nil,
+               },
+               expectedStatusCode:   http.StatusBadRequest,
+               expectedJsonResponse: ValidationFailureJson,
+       }
+       controllerDeleteEnbTestExecuter(t, &context, true)
+}
+
 func TestControllerDeleteEnbSuccess(t *testing.T) {
        context := controllerDeleteEnbTestContext{
                getNodebInfoResult: &getNodebInfoResult{
@@ -1479,7 +1578,7 @@ func TestControllerGetNodebIdListSuccess(t *testing.T) {
                nodebIdList:          nodebIdList,
                rnibError:            rnibError,
                expectedStatusCode:   http.StatusOK,
-               expectedJsonResponse: "[{\"inventoryName\":\"test1\",\"globalNbId\":{\"plmnId\":\"plmnId1\",\"nbId\":\"nbId1\"}},{\"inventoryName\":\"test2\",\"globalNbId\":{\"plmnId\":\"plmnId2\",\"nbId\":\"nbId2\"}}]",
+               expectedJsonResponse: "[{\"inventoryName\":\"test1\",\"globalNbId\":{\"plmnId\":\"plmnId1\",\"nbId\":\"nbId1\"}},{\"inventoryName\":\"test2\",\"globalNbId\":{\"plmnId\":\"plmnId2\",\"nbId\":\"nbId2\"}}][{\"inventoryName\":\"test2\",\"globalNbId\":{\"plmnId\":\"plmnId2\",\"nbId\":\"nbId2\"}},{\"inventoryName\":\"test1\",\"globalNbId\":{\"plmnId\":\"plmnId1\",\"nbId\":\"nbId1\"}}]",
        }
 
        controllerGetNodebIdListTestExecuter(t, &context)