[RIC-433] Add unit tests + increment container-tag
[ric-plt/e2mgr.git] / E2Manager / controllers / nodeb_controller_test.go
index 7ee44d7..d128dad 100644 (file)
@@ -124,6 +124,12 @@ type controllerAddEnbTestContext struct {
        expectedJsonResponse string
 }
 
+type controllerDeleteEnbTestContext struct {
+       getNodebInfoResult   *getNodebInfoResult
+       expectedStatusCode   int
+       expectedJsonResponse string
+}
+
 func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell {
 
        servedNrCells := []*entities.ServedNRCell{}
@@ -374,6 +380,14 @@ func assertControllerAddEnb(t *testing.T, context *controllerAddEnbTestContext,
        writerMock.AssertExpectations(t)
 }
 
+func assertControllerDeleteEnb(t *testing.T, context *controllerDeleteEnbTestContext, 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)
+}
+
 func buildUpdateGnbRequest(context *controllerUpdateGnbTestContext) *http.Request {
        updateGnbUrl := fmt.Sprintf("/nodeb/%s/update", RanName)
        requestBody := getJsonRequestAsBuffer(context.requestBody)
@@ -438,6 +452,20 @@ func controllerAddEnbTestExecuter(t *testing.T, context *controllerAddEnbTestCon
        assertControllerAddEnb(t, context, writer, readerMock, writerMock)
 }
 
+func controllerDeleteEnbTestExecuter(t *testing.T, context *controllerDeleteEnbTestContext) {
+       controller, readerMock, writerMock, _, _ := setupControllerTest(t)
+       readerMock.On("GetNodeb", RanName).Return(context.getNodebInfoResult.nodebInfo, context.getNodebInfoResult.rnibError)
+       if context.getNodebInfoResult.rnibError == nil && context.getNodebInfoResult.nodebInfo.GetNodeType() == entities.Node_ENB {
+               writerMock.On("RemoveEnb", context.getNodebInfoResult.nodebInfo).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)
+}
+
 func TestControllerUpdateGnbEmptyServedNrCells(t *testing.T) {
        context := controllerUpdateGnbTestContext{
                getNodebInfoResult: nil,
@@ -883,6 +911,57 @@ func TestControllerAddEnbSuccess(t *testing.T) {
        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)
+}
+
+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)
+}
+
+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)
+}
+
+func TestControllerDeleteEnbSuccess(t *testing.T) {
+       context := controllerDeleteEnbTestContext{
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: &entities.NodebInfo{RanName: "ran1", NodeType: entities.Node_ENB, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED},
+                       rnibError: nil,
+               },
+               expectedStatusCode: http.StatusNoContent,
+               expectedJsonResponse: "",
+       }
+       controllerDeleteEnbTestExecuter(t, &context)
+}
+
 func getJsonRequestAsBuffer(requestJson map[string]interface{}) *bytes.Buffer {
        b := new(bytes.Buffer)
        _ = json.NewEncoder(b).Encode(requestJson)