+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)
+ assertControllerUpdateGnb(t, context, writer, readerMock, writerMock)
+}
+
+func activateControllerAddEnbMocks(context *controllerAddEnbTestContext, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock, addEnbRequest *models.AddEnbRequest) {
+ if context.getNodebInfoResult != nil {
+ readerMock.On("GetNodeb", RanName).Return(context.getNodebInfoResult.nodebInfo, context.getNodebInfoResult.rnibError)
+ }
+
+ if context.addEnbParams != nil {
+ nodebInfo := entities.NodebInfo{
+ RanName: addEnbRequest.RanName,
+ Ip: addEnbRequest.Ip,
+ Port: addEnbRequest.Port,
+ NodeType: entities.Node_ENB,
+ GlobalNbId: addEnbRequest.GlobalNbId,
+ Configuration: &entities.NodebInfo_Enb{Enb: addEnbRequest.Enb},
+ ConnectionStatus: entities.ConnectionStatus_DISCONNECTED,
+ }
+
+ writerMock.On("AddEnb", &nodebInfo).Return(context.addEnbParams.err)
+ }
+
+ if context.addNbIdentityParams != nil {
+ nbIdentity := entities.NbIdentity{InventoryName: addEnbRequest.RanName, GlobalNbId: addEnbRequest.GlobalNbId, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED}
+ writerMock.On("AddNbIdentity", entities.Node_ENB, &nbIdentity).Return(context.addNbIdentityParams.err)
+ }
+}
+
+func controllerAddEnbTestExecuter(t *testing.T, context *controllerAddEnbTestContext) {
+ controller, readerMock, writerMock, _, _, _ := setupControllerTest(t)
+ writer := httptest.NewRecorder()
+ r := buildAddEnbRequest(context)
+ body, _ := ioutil.ReadAll(io.LimitReader(r.Body, LimitRequest))
+
+ addEnbRequest := models.AddEnbRequest{}
+
+ _ = json.Unmarshal(body, &addEnbRequest)
+ activateControllerAddEnbMocks(context, readerMock, writerMock, &addEnbRequest)
+ r = buildAddEnbRequest(context)
+ defer r.Body.Close()
+ controller.AddEnb(writer, r)
+ assertControllerAddEnb(t, context, writer, readerMock, writerMock)
+}
+
+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 &&
+ !context.getNodebInfoResult.nodebInfo.SetupFromNetwork {
+ writerMock.On("RemoveEnb", context.getNodebInfoResult.nodebInfo).Return(nil)
+ if preAddNbIdentity {
+ writerMock.On("RemoveNbIdentity", entities.Node_ENB, nbIdentity).Return(nil)
+ }
+ }
+ writer := httptest.NewRecorder()
+ r, _ := http.NewRequest(http.MethodDelete, AddEnbUrl+"/"+RanName, nil)
+ r.Header.Set("Content-Type", "application/json")
+ r = mux.SetURLVars(r, map[string]string{"ranName": RanName})
+ controller.DeleteEnb(writer, r)
+ assertControllerDeleteEnb(t, context, writer, readerMock, writerMock)
+}
+
+/*
+UpdateGnb UTs
+*/
+
+// BEGIN - UpdateGnb Validation Failure UTs
+
+func TestControllerUpdateGnbEmptyServedNrCells(t *testing.T) {
+ context := controllerUpdateGnbTestContext{
+ getNodebInfoResult: nil,
+ requestBody: map[string]interface{}{
+ "servedNrCells": []interface{}{
+ },
+ },
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbMissingServedNrCellInformation(t *testing.T) {
+ context := controllerUpdateGnbTestContext{
+ getNodebInfoResult: nil,
+ requestBody: map[string]interface{}{
+ "servedNrCells": []interface{}{
+ map[string]interface{}{
+ "servedNrCellInformation": nil,
+ },
+ },
+ },
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbMissingServedNrCellRequiredProp(t *testing.T) {
+
+ for _, v := range ServedNrCellInformationRequiredFields {
+ context := controllerUpdateGnbTestContext{
+ getNodebInfoResult: nil,
+ requestBody: map[string]interface{}{
+ "servedNrCells": []interface{}{
+ map[string]interface{}{
+ "servedNrCellInformation": buildServedNrCellInformation(v),
+ },
+ },
+ },
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateGnbTestExecuter(t, &context)
+ }
+}
+
+func TestControllerUpdateGnbMissingServedNrCellFddOrTdd(t *testing.T) {
+
+ servedNrCellInformation := buildServedNrCellInformation("")
+ servedNrCellInformation["choiceNrMode"] = map[string]interface{}{}
+
+ context := controllerUpdateGnbTestContext{
+ getNodebInfoResult: nil,
+ requestBody: map[string]interface{}{
+ "servedNrCells": []interface{}{
+ map[string]interface{}{
+ "servedNrCellInformation": servedNrCellInformation,
+ },
+ },
+ },
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbMissingNeighbourInfoFddOrTdd(t *testing.T) {
+
+ nrNeighbourInfo := buildNrNeighbourInformation("")
+ nrNeighbourInfo["choiceNrMode"] = map[string]interface{}{}
+
+ context := controllerUpdateGnbTestContext{
+ getNodebInfoResult: nil,
+ requestBody: map[string]interface{}{
+ "servedNrCells": []interface{}{
+ map[string]interface{}{
+ "servedNrCellInformation": buildServedNrCellInformation(""),
+ "nrNeighbourInfos": []interface{}{
+ nrNeighbourInfo,
+ },
+ },
+ },
+ },
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbMissingNrNeighbourInformationRequiredProp(t *testing.T) {
+
+ for _, v := range NrNeighbourInformationRequiredFields {
+ context := controllerUpdateGnbTestContext{
+ getNodebInfoResult: nil,
+ requestBody: map[string]interface{}{
+ "servedNrCells": []interface{}{
+ map[string]interface{}{
+ "servedNrCellInformation": buildServedNrCellInformation(""),
+ "nrNeighbourInfos": []interface{}{
+ buildNrNeighbourInformation(v),
+ },
+ },
+ },
+ },
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateGnbTestExecuter(t, &context)
+ }
+}
+
+// END - UpdateGnb Validation Failure UTs
+
+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: 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,
+ NodeType: entities.Node_ENB,
+ },
+ rnibError: nil,
+ },
+ requestBody: map[string]interface{}{
+ "servedNrCells": []interface{}{
+ map[string]interface{}{
+ "servedNrCellInformation": buildServedNrCellInformation(""),
+ "nrNeighbourInfos": []interface{}{
+ buildNrNeighbourInformation(""),
+ },
+ },
+ },
+ },
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ 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}},
+ NodeType: entities.Node_GNB,
+ },
+ 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")),
+ },
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: &entities.NodebInfo{
+ RanName: RanName,
+ ConnectionStatus: entities.ConnectionStatus_CONNECTED,
+ AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
+ Configuration: &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{ServedNrCells: oldServedNrCells}},
+ NodeType: entities.Node_GNB,
+ },
+ 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 TestControllerUpdateGnbExistingEmptyCellsSuccess(t *testing.T) {
+ oldServedNrCells := []*entities.ServedNRCell{}
+
+ 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{ServedNrCells: oldServedNrCells}},
+ NodeType: entities.Node_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\",\"nodeType\":\"GNB\",\"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 TestControllerUpdateGnbSuccess(t *testing.T) {
+ oldServedNrCells := generateServedNrCells("whatever1", "whatever2")
+
+ context := controllerUpdateGnbTestContext{
+ removeServedNrCellsParams: &removeServedNrCellsParams{
+ err: nil,
+ servedNrCells: oldServedNrCells,
+ },
+ updateGnbCellsParams: &updateGnbCellsParams{
+ err: nil,
+ },
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: &entities.NodebInfo{
+ RanName: RanName,
+ ConnectionStatus: entities.ConnectionStatus_CONNECTED,
+ AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
+ Configuration: &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{ServedNrCells: oldServedNrCells}},
+ NodeType: entities.Node_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\",\"nodeType\":\"GNB\",\"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)
+}
+
+/*
+UpdateEnb UTs
+*/
+
+func TestControllerUpdateEnbInvalidRequest(t *testing.T) {
+ controller, _, _, _, _, _ := setupControllerTest(t)
+
+ writer := httptest.NewRecorder()
+ invalidJson := strings.NewReader("{enb:\"whatever\"")
+
+ updateEnbUrl := fmt.Sprintf("/nodeb/enb/%s", RanName)
+ req, _ := http.NewRequest(http.MethodPut, updateEnbUrl, invalidJson)
+ req.Header.Set("Content-Type", "application/json")
+ req = mux.SetURLVars(req, map[string]string{"ranName": RanName})
+
+ controller.UpdateEnb(writer, req)
+
+ assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
+ bodyBytes, _ := ioutil.ReadAll(writer.Body)
+ assert.Equal(t, CorruptedJson, string(bodyBytes))
+}
+
+func TestControllerUpdateEnbEmptyEnbType(t *testing.T) {
+ context := controllerUpdateEnbTestContext{
+ getNodebInfoResult: nil,
+ requestBody: getUpdateEnbRequest(EnbRequiredFields[0]),
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateEnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateEnbEmptyServedCells(t *testing.T) {
+ context := controllerUpdateEnbTestContext{
+ getNodebInfoResult: nil,
+ requestBody: getUpdateEnbRequest(EnbRequiredFields[1]),
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateEnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateEnbMissingEnb(t *testing.T) {
+ context := controllerUpdateEnbTestContext{
+ getNodebInfoResult: nil,
+ requestBody: getUpdateEnbRequest(UpdateEnbRequestRequiredFields[0]),
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateEnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateEnbMissingRequiredServedCellProps(t *testing.T) {
+
+ r := getUpdateEnbRequest("")
+
+ for _, v := range ServedCellRequiredFields {
+ enb := r["enb"]
+
+ enbMap, _ := enb.(map[string]interface{})
+
+ enbMap["servedCells"] = []interface{}{
+ buildServedCell(v),
+ }
+
+ context := controllerUpdateEnbTestContext{
+ requestBody: r,
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateEnbTestExecuter(t, &context)
+ }
+}
+
+func TestControllerUpdateEnbValidServedCellsGetNodebNotFound(t *testing.T) {
+ context := controllerUpdateEnbTestContext{
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: nil,
+ rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
+ },
+ requestBody: getUpdateEnbRequest(""),
+ expectedStatusCode: http.StatusNotFound,
+ expectedJsonResponse: ResourceNotFoundJson,
+ }
+
+ controllerUpdateEnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateEnbValidServedCellsGetNodebInternalError(t *testing.T) {
+ context := controllerUpdateEnbTestContext{
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: nil,
+ rnibError: common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
+ },
+ requestBody: getUpdateEnbRequest(""),
+ expectedStatusCode: http.StatusInternalServerError,
+ expectedJsonResponse: RnibErrorJson,
+ }
+
+ controllerUpdateEnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateEnbGetNodebSuccessGnbTypeFailure(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_GNB,
+ Configuration: &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells}},
+ },
+ rnibError: nil,
+ },
+ requestBody: getUpdateEnbRequest(""),
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateEnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateEnbGetNodebSuccessRemoveServedCellsFailure(t *testing.T) {
+ oldServedCells := generateServedCells("whatever1", "whatever2")
+ context := controllerUpdateEnbTestContext{
+ removeServedCellsParams: &removeServedCellsParams{
+ err: common.NewInternalError(errors.New("#writer.RemoveServedCells - Internal Error")),
+ servedCellInfo: oldServedCells,
+ },
+ 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}},
+ },
+ rnibError: nil,
+ },
+ requestBody: getUpdateEnbRequest(""),
+ expectedStatusCode: http.StatusInternalServerError,
+ expectedJsonResponse: RnibErrorJson,
+ }
+
+ controllerUpdateEnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateEnbGetNodebSuccessUpdateEnbFailure(t *testing.T) {
+ oldServedCells := generateServedCells("whatever1", "whatever2")
+ context := controllerUpdateEnbTestContext{
+ removeServedCellsParams: &removeServedCellsParams{
+ err: nil,
+ servedCellInfo: oldServedCells,
+ },
+ updateEnbCellsParams: &updateEnbCellsParams{
+ err: common.NewInternalError(errors.New("#writer.UpdateEnb - Internal Error")),
+ },
+ 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_ENB}},
+ },
+ rnibError: nil,
+ },
+ requestBody: getUpdateEnbRequest(""),
+ expectedStatusCode: http.StatusInternalServerError,
+ expectedJsonResponse: RnibErrorJson,
+ }
+
+ controllerUpdateEnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateEnbExistingEmptyCellsSuccess(t *testing.T) {
+ oldServedCells := []*entities.ServedCellInfo{}
+ context := controllerUpdateEnbTestContext{
+ 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_MACRO_ENB}},
+ },
+ rnibError: nil,
+ },
+ requestBody: getUpdateEnbRequest(""),
+ expectedStatusCode: http.StatusOK,
+ expectedJsonResponse: "{\"ranName\":\"test\",\"connectionStatus\":\"CONNECTED\",\"nodeType\":\"ENB\",\"enb\":{\"enbType\":\"MACRO_ENB\",\"servedCells\":[{\"pci\":1,\"cellId\":\"whatever\",\"tac\":\"whatever3\",\"broadcastPlmns\":[\"whatever\"],\"choiceEutraMode\":{\"fdd\":{}},\"eutraMode\":\"FDD\"}]},\"associatedE2tInstanceAddress\":\"10.0.2.15:38000\"}",
+ }
+
+ controllerUpdateEnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateEnbNgEnbFailure(t *testing.T) {
+
+ requestBody := map[string]interface{}{
+ "enb": map[string]interface{}{
+ "enbType": 5,
+ "servedCells": []interface{}{
+ buildServedCell(""),
+ }},
+ }
+
+ context := controllerUpdateEnbTestContext{
+ getNodebInfoResult: nil,
+ requestBody: requestBody,
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerUpdateEnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateEnbSuccessSetupFromNwFalse(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}},
+ },
+ rnibError: nil,
+ },
+ requestBody: getUpdateEnbRequest(""),
+ expectedStatusCode: http.StatusOK,
+ expectedJsonResponse: "{\"ranName\":\"test\",\"connectionStatus\":\"CONNECTED\",\"nodeType\":\"ENB\",\"enb\":{\"enbType\":\"MACRO_ENB\",\"servedCells\":[{\"pci\":1,\"cellId\":\"whatever\",\"tac\":\"whatever3\",\"broadcastPlmns\":[\"whatever\"],\"choiceEutraMode\":{\"fdd\":{}},\"eutraMode\":\"FDD\"}]},\"associatedE2tInstanceAddress\":\"10.0.2.15:38000\"}",
+ }
+
+ 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
+*/
+
+func TestControllerAddEnbGetNodebInternalError(t *testing.T) {
+ context := controllerAddEnbTestContext{
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: nil,
+ rnibError: common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
+ },
+ requestBody: getAddEnbRequest(""),
+ expectedStatusCode: http.StatusInternalServerError,
+ expectedJsonResponse: RnibErrorJson,
+ }
+
+ controllerAddEnbTestExecuter(t, &context)
+}
+
+func TestControllerAddEnbNodebExistsFailure(t *testing.T) {
+ context := controllerAddEnbTestContext{
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: &entities.NodebInfo{},
+ rnibError: nil,
+ },
+ requestBody: getAddEnbRequest(""),
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: NodebExistsJson,
+ }
+
+ controllerAddEnbTestExecuter(t, &context)
+}
+
+func TestControllerAddEnbSaveNodebFailure(t *testing.T) {
+ context := controllerAddEnbTestContext{
+ addEnbParams: &addEnbParams{
+ err: common.NewInternalError(errors.New("#reader.AddEnb - Internal Error")),
+ },
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: nil,
+ rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
+ },
+ requestBody: getAddEnbRequest(""),
+ expectedStatusCode: http.StatusInternalServerError,
+ expectedJsonResponse: RnibErrorJson,
+ }
+
+ controllerAddEnbTestExecuter(t, &context)
+}
+
+func TestControllerAddEnbAddNbIdentityFailure(t *testing.T) {
+ context := controllerAddEnbTestContext{
+ addEnbParams: &addEnbParams{
+ err: nil,
+ },
+ addNbIdentityParams: &addNbIdentityParams{
+ err: common.NewInternalError(errors.New("#writer.addNbIdentity - Internal Error")),
+ },
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: nil,
+ rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
+ },
+ requestBody: getAddEnbRequest(""),
+ expectedStatusCode: http.StatusInternalServerError,
+ expectedJsonResponse: RnibErrorJson,
+ }
+
+ controllerAddEnbTestExecuter(t, &context)
+}
+
+func TestControllerAddEnbMissingRequiredRequestProps(t *testing.T) {
+
+ for _, v := range AddEnbRequestRequiredFields {
+ context := controllerAddEnbTestContext{
+ requestBody: getAddEnbRequest(v),
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerAddEnbTestExecuter(t, &context)
+ }
+}
+
+func TestControllerAddEnbInvalidRequest(t *testing.T) {
+ controller, _, _, _, _, _ := setupControllerTest(t)
+ writer := httptest.NewRecorder()
+
+ // Invalid json: attribute name without quotes (should be "cause":).
+ invalidJson := strings.NewReader("{ranName:\"whatever\"")
+ req, _ := http.NewRequest(http.MethodPost, AddEnbUrl, invalidJson)
+
+ controller.AddEnb(writer, req)
+ assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
+ bodyBytes, _ := ioutil.ReadAll(writer.Body)
+ assert.Equal(t, CorruptedJson, string(bodyBytes))
+
+}
+
+func TestControllerAddEnbMissingRequiredGlobalNbIdProps(t *testing.T) {
+
+ r := getAddEnbRequest("")
+
+ for _, v := range GlobalIdRequiredFields {
+ r["globalNbId"] = buildGlobalNbId(v)
+
+ context := controllerAddEnbTestContext{
+ requestBody: r,
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerAddEnbTestExecuter(t, &context)
+ }
+}
+
+func TestControllerAddEnbMissingRequiredEnbProps(t *testing.T) {
+
+ r := getAddEnbRequest("")
+
+ for _, v := range EnbRequiredFields {
+ r["enb"] = buildEnb(v)
+
+ context := controllerAddEnbTestContext{
+ requestBody: r,
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerAddEnbTestExecuter(t, &context)
+ }
+}
+
+func TestControllerAddEnbMissingRequiredServedCellProps(t *testing.T) {
+
+ r := getAddEnbRequest("")
+
+ for _, v := range ServedCellRequiredFields {
+ enb := r["enb"]
+
+ enbMap, _ := enb.(map[string]interface{})
+
+ enbMap["servedCells"] = []interface{}{
+ buildServedCell(v),
+ }
+
+ context := controllerAddEnbTestContext{
+ requestBody: r,
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ controllerAddEnbTestExecuter(t, &context)
+ }
+}
+
+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{
+ addEnbParams: &addEnbParams{
+ err: nil,
+ },
+ addNbIdentityParams: &addNbIdentityParams{
+ err: nil,
+ },
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: nil,
+ rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
+ },
+ requestBody: getAddEnbRequest(""),
+ expectedStatusCode: http.StatusCreated,
+ expectedJsonResponse: "{\"ranName\":\"test\",\"connectionStatus\":\"DISCONNECTED\",\"globalNbId\":{\"plmnId\":\"whatever\",\"nbId\":\"whatever2\"},\"nodeType\":\"ENB\",\"enb\":{\"enbType\":\"MACRO_ENB\",\"servedCells\":[{\"pci\":1,\"cellId\":\"whatever\",\"tac\":\"whatever3\",\"broadcastPlmns\":[\"whatever\"],\"choiceEutraMode\":{\"fdd\":{}},\"eutraMode\":\"FDD\"}]}}",
+ }
+
+ controllerAddEnbTestExecuter(t, &context)
+}
+
+func TestControllerDeleteEnbGetNodebInternalError(t *testing.T) {
+ context := controllerDeleteEnbTestContext{
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: nil,
+ rnibError: common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
+ },
+ expectedStatusCode: http.StatusInternalServerError,
+ expectedJsonResponse: RnibErrorJson,
+ }
+
+ controllerDeleteEnbTestExecuter(t, &context, false)
+}
+
+/*
+DeleteEnb UTs
+*/
+
+func TestControllerDeleteEnbNodebNotExistsFailure(t *testing.T) {
+ context := controllerDeleteEnbTestContext{
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: nil,
+ rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found"),
+ },
+ expectedStatusCode: http.StatusNotFound,
+ expectedJsonResponse: ResourceNotFoundJson,
+ }
+
+ controllerDeleteEnbTestExecuter(t, &context, false)
+}
+
+func TestControllerDeleteEnbNodebNotEnb(t *testing.T) {
+ context := controllerDeleteEnbTestContext{
+ getNodebInfoResult: &getNodebInfoResult{
+ nodebInfo: &entities.NodebInfo{RanName: "ran1", NodeType: entities.Node_GNB, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED},
+ rnibError: nil,
+ },
+ expectedStatusCode: http.StatusBadRequest,
+ expectedJsonResponse: ValidationFailureJson,
+ }
+
+ 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{
+ nodebInfo: &entities.NodebInfo{RanName: RanName, NodeType: entities.Node_ENB, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED},
+ rnibError: nil,
+ },
+ expectedStatusCode: http.StatusNoContent,
+ expectedJsonResponse: "",
+ }
+ controllerDeleteEnbTestExecuter(t, &context, true)
+}
+
+func getJsonRequestAsBuffer(requestJson map[string]interface{}) *bytes.Buffer {
+ b := new(bytes.Buffer)
+ _ = json.NewEncoder(b).Encode(requestJson)
+ return b
+}
+
+/*
+GetNodeb UTs
+*/
+