[RICPLT-2787] Complete UTs and Automation.... 08/2108/1
authoris005q <idan.shalom@intl.att.com>
Wed, 25 Dec 2019 07:08:28 +0000 (09:08 +0200)
committeris005q <idan.shalom@intl.att.com>
Wed, 25 Dec 2019 07:08:35 +0000 (09:08 +0200)
Change-Id: I44dad1919186c49b3b6fcf48078e808527f314b7
Signed-off-by: is005q <idan.shalom@intl.att.com>
Automation/Tests/Get_E2T_Instances/get_e2t_instances.robot [new file with mode: 0644]
Automation/Tests/Unhappy/Get_E2T_Instances_DB_Error.robot [new file with mode: 0644]
E2Manager/controllers/e2t_controller.go
E2Manager/controllers/e2t_controller_test.go [new file with mode: 0644]
E2Manager/controllers/nodeb_controller.go
E2Manager/go.mod

diff --git a/Automation/Tests/Get_E2T_Instances/get_e2t_instances.robot b/Automation/Tests/Get_E2T_Instances/get_e2t_instances.robot
new file mode 100644 (file)
index 0000000..dbafcfc
--- /dev/null
@@ -0,0 +1,59 @@
+##############################################################################
+#
+#   Copyright (c) 2019 AT&T Intellectual Property.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+##############################################################################
+
+*** Settings ***
+Suite Setup   Prepare Enviorment
+Resource   ../Resource/Keywords.robot
+Resource   ../Resource/resource.robot
+Library     REST      ${url}
+Library    RequestsLibrary
+Library    Collections
+Library    OperatingSystem
+Library    json
+
+
+*** Test Cases ***
+
+Get E2T instances 1st call
+    Create Session  getE2tInstances  ${url}
+    ${headers}=  Create Dictionary    Accept=application/json
+    ${resp}=    Get Request   getE2tInstances     /v1/e2t/list    headers=${headers}
+    Should Be Equal As Strings   ${resp.status_code}    200
+    Should Be Equal As Strings    ${resp.content}        [{"e2tAddress":"e2t.att.com:38000","ranNames":[]}]
+
+Get E2T instances after one setup
+
+    Post Request setup node b x-2
+    Integer     response status       204
+    Create Session  getE2tInstances  ${url}
+    ${headers}=  Create Dictionary    Accept=application/json
+    ${resp}=    Get Request   getE2tInstances     /v1/e2t/list    headers=${headers}
+    Should Be Equal As Strings   ${resp.status_code}    200
+    Should Be Equal As Strings    ${resp.content}        [{\"e2tAddress\":\"e2t.att.com:38000\",\"ranNames\":[\"test1\"]}]
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Automation/Tests/Unhappy/Get_E2T_Instances_DB_Error.robot b/Automation/Tests/Unhappy/Get_E2T_Instances_DB_Error.robot
new file mode 100644 (file)
index 0000000..9ab940f
--- /dev/null
@@ -0,0 +1,32 @@
+##############################################################################
+#
+#   Copyright (c) 2019 AT&T Intellectual Property.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+##############################################################################
+
+*** Settings ***
+Suite Setup   Prepare Enviorment
+Resource   ../Resource/Keywords.robot
+Resource   ../Resource/resource.robot
+Library     REST      ${url}
+Suite Teardown   Start Dbass
+
+*** Test Cases ***
+Get All E2T Instances DB Error - 500
+    Stop Dbass
+    GET      /v1/e2t/list
+    Integer  response status            500
+    Integer  response body errorCode            500
+    String   response body errorMessage     RNIB error
\ No newline at end of file
index 2d95542..e6ebafa 100644 (file)
@@ -65,12 +65,6 @@ func (c *E2TController) handleRequest(writer http.ResponseWriter, header *http.H
                return
        }
 
-       if response == nil {
-               writer.WriteHeader(http.StatusNoContent)
-               c.logger.Infof("[E2 Manager -> Client] #E2TController.handleRequest - status response: %v", http.StatusNoContent)
-               return
-       }
-
        result, err := response.Marshal()
 
        if err != nil {
@@ -108,10 +102,6 @@ func (c *E2TController) handleErrorResponse(err error, writer http.ResponseWrite
        writer.Header().Set("Content-Type", "application/json")
        writer.WriteHeader(httpError)
        _, err = writer.Write(errorResponse)
-
-       if err != nil {
-               c.logger.Errorf("#E2TController.handleErrorResponse - Cannot send response. writer:%v", writer)
-       }
 }
 
 func (c *E2TController) prettifyRequest(request *http.Request) string {
diff --git a/E2Manager/controllers/e2t_controller_test.go b/E2Manager/controllers/e2t_controller_test.go
new file mode 100644 (file)
index 0000000..1ed6934
--- /dev/null
@@ -0,0 +1,137 @@
+//
+// Copyright 2019 AT&T Intellectual Property
+// Copyright 2019 Nokia
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package controllers
+
+import (
+       "e2mgr/configuration"
+       "e2mgr/managers"
+       "e2mgr/mocks"
+       "e2mgr/models"
+       "e2mgr/providers/httpmsghandlerprovider"
+       "e2mgr/services"
+       "encoding/json"
+       "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/magiconair/properties/assert"
+       "github.com/pkg/errors"
+       "io/ioutil"
+       "net/http"
+       "net/http/httptest"
+       "testing"
+)
+
+const E2TAddress string = "10.0.2.15:38000"
+const E2TAddress2 string = "10.0.2.16:38001"
+
+type controllerE2TInstancesTestContext struct {
+       e2tAddresses         []string
+       e2tInstances         []*entities.E2TInstance
+       error                error
+       expectedStatusCode   int
+       expectedJsonResponse string
+}
+
+func setupE2TControllerTest(t *testing.T) (*E2TController, *mocks.RnibReaderMock) {
+       log := initLog(t)
+       config := configuration.ParseConfiguration()
+
+       readerMock := &mocks.RnibReaderMock{}
+
+       rnibDataService := services.NewRnibDataService(log, config, readerMock, nil)
+       e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, log)
+       handlerProvider := httpmsghandlerprovider.NewIncomingRequestHandlerProvider(log, nil, config, rnibDataService, nil, e2tInstancesManager, &managers.E2TAssociationManager{})
+       controller := NewE2TController(log, handlerProvider)
+       return controller, readerMock
+}
+
+func controllerGetE2TInstancesTestExecuter(t *testing.T, context *controllerE2TInstancesTestContext) {
+       controller, readerMock := setupE2TControllerTest(t)
+       writer := httptest.NewRecorder()
+       readerMock.On("GetE2TAddresses").Return(context.e2tAddresses, context.error)
+
+       if context.e2tInstances != nil {
+               readerMock.On("GetE2TInstances", context.e2tAddresses).Return(context.e2tInstances, context.error)
+       }
+
+       req, _ := http.NewRequest("GET", "/e2t/list", nil)
+       controller.GetE2TInstances(writer, req)
+       assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
+       bodyBytes, _ := ioutil.ReadAll(writer.Body)
+       assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
+       readerMock.AssertExpectations(t)
+}
+
+func TestControllerGetE2TInstancesSuccess(t *testing.T) {
+       ranNames1 := []string{"test1", "test2", "test3"}
+       e2tInstanceResponseModel1 := models.NewE2TInstanceResponseModel(E2TAddress, ranNames1)
+       e2tInstanceResponseModel2 := models.NewE2TInstanceResponseModel(E2TAddress2, []string{})
+       e2tInstancesResponse := models.E2TInstancesResponse{e2tInstanceResponseModel1, e2tInstanceResponseModel2}
+       bytes, _ := json.Marshal(e2tInstancesResponse)
+
+       context := controllerE2TInstancesTestContext{
+               e2tAddresses:         []string{E2TAddress, E2TAddress2},
+               e2tInstances:         []*entities.E2TInstance{{Address: E2TAddress, AssociatedRanList: ranNames1}, {Address: E2TAddress2, AssociatedRanList: []string{}}},
+               error:                nil,
+               expectedStatusCode:   http.StatusOK,
+               expectedJsonResponse: string(bytes),
+       }
+
+       controllerGetE2TInstancesTestExecuter(t, &context)
+}
+
+func TestControllerGetE2TInstancesEmptySuccess(t *testing.T) {
+       e2tInstancesResponse := models.E2TInstancesResponse{}
+       bytes, _ := json.Marshal(e2tInstancesResponse)
+
+       context := controllerE2TInstancesTestContext{
+               e2tAddresses:         []string{},
+               e2tInstances:         nil,
+               error:                nil,
+               expectedStatusCode:   http.StatusOK,
+               expectedJsonResponse: string(bytes),
+       }
+
+       controllerGetE2TInstancesTestExecuter(t, &context)
+}
+
+func TestControllerGetE2TInstancesInternal(t *testing.T) {
+       context := controllerE2TInstancesTestContext{
+               e2tAddresses:         nil,
+               e2tInstances:         nil,
+               error:                common.NewInternalError(errors.New("error")),
+               expectedStatusCode:   http.StatusInternalServerError,
+               expectedJsonResponse: "{\"errorCode\":500,\"errorMessage\":\"RNIB error\"}",
+       }
+
+       controllerGetE2TInstancesTestExecuter(t, &context)
+}
+
+func TestInvalidRequestName(t *testing.T) {
+       controller, _ := setupE2TControllerTest(t)
+
+       writer := httptest.NewRecorder()
+
+       header := &http.Header{}
+
+       controller.handleRequest(writer, header, "", nil, true)
+
+       var errorResponse = parseJsonRequest(t, writer.Body)
+
+       assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
+       assert.Equal(t, errorResponse.Code, 501)
+}
index 8b8c743..7b8a86f 100644 (file)
@@ -249,10 +249,6 @@ func (c *NodebController) handleErrorResponse(err error, writer http.ResponseWri
        writer.Header().Set("Content-Type", "application/json")
        writer.WriteHeader(httpError)
        _, err = writer.Write(errorResponse)
-
-       if err != nil {
-               c.logger.Errorf("#NodebController.handleErrorResponse - Cannot send response. writer:%v", writer)
-       }
 }
 
 func (c *NodebController) prettifyRequest(request *http.Request) string {
index 1fd1095..7d2e125 100644 (file)
@@ -9,7 +9,7 @@ require (
        github.com/go-ozzo/ozzo-validation v3.5.0+incompatible
        github.com/golang/protobuf v1.3.2
        github.com/gorilla/mux v1.7.0
-       github.com/magiconair/properties v1.8.1 // indirect
+       github.com/magiconair/properties v1.8.1
        github.com/pelletier/go-toml v1.5.0 // indirect
        github.com/pkg/errors v0.8.1
        github.com/spf13/afero v1.2.2 // indirect