2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
25 "e2mgr/providers/httpmsghandlerprovider"
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
29 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
30 "github.com/magiconair/properties/assert"
31 "github.com/pkg/errors"
38 const E2TAddress string = "10.0.2.15:38000"
39 const E2TAddress2 string = "10.0.2.16:38001"
41 type controllerE2TInstancesTestContext struct {
43 e2tInstances []*entities.E2TInstance
45 expectedStatusCode int
46 expectedJsonResponse string
49 func setupE2TControllerTest(t *testing.T) (*E2TController, *mocks.RnibReaderMock) {
51 config := configuration.ParseConfiguration()
53 readerMock := &mocks.RnibReaderMock{}
55 rnibDataService := services.NewRnibDataService(log, config, readerMock, nil)
56 e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, log)
58 ranListManager := managers.NewRanListManager(log)
59 ranAlarmService := services.NewRanAlarmService(log, config)
60 ranConnectStatusChangeManager := managers.NewRanConnectStatusChangeManager(log, rnibDataService,ranListManager, ranAlarmService)
62 handlerProvider := httpmsghandlerprovider.NewIncomingRequestHandlerProvider(log, nil, config, rnibDataService, nil, e2tInstancesManager, &managers.E2TAssociationManager{}, nil, ranConnectStatusChangeManager)
63 controller := NewE2TController(log, handlerProvider)
64 return controller, readerMock
67 func controllerGetE2TInstancesTestExecuter(t *testing.T, context *controllerE2TInstancesTestContext) {
68 controller, readerMock := setupE2TControllerTest(t)
69 writer := httptest.NewRecorder()
70 readerMock.On("GetE2TAddresses").Return(context.e2tAddresses, context.error)
72 if context.e2tInstances != nil {
73 readerMock.On("GetE2TInstances", context.e2tAddresses).Return(context.e2tInstances, context.error)
76 req, _ := http.NewRequest("GET", "/e2t/list", nil)
77 controller.GetE2TInstances(writer, req)
78 assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
79 bodyBytes, _ := ioutil.ReadAll(writer.Body)
80 assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
81 readerMock.AssertExpectations(t)
84 func TestControllerGetE2TInstancesSuccess(t *testing.T) {
85 ranNames1 := []string{"test1", "test2", "test3"}
86 e2tInstanceResponseModel1 := models.NewE2TInstanceResponseModel(E2TAddress, ranNames1)
87 e2tInstanceResponseModel2 := models.NewE2TInstanceResponseModel(E2TAddress2, []string{})
88 e2tInstancesResponse := models.E2TInstancesResponse{e2tInstanceResponseModel1, e2tInstanceResponseModel2}
89 bytes, _ := json.Marshal(e2tInstancesResponse)
91 context := controllerE2TInstancesTestContext{
92 e2tAddresses: []string{E2TAddress, E2TAddress2},
93 e2tInstances: []*entities.E2TInstance{{Address: E2TAddress, AssociatedRanList: ranNames1}, {Address: E2TAddress2, AssociatedRanList: []string{}}},
95 expectedStatusCode: http.StatusOK,
96 expectedJsonResponse: string(bytes),
99 controllerGetE2TInstancesTestExecuter(t, &context)
102 func TestControllerGetE2TInstancesEmptySuccess(t *testing.T) {
103 e2tInstancesResponse := models.E2TInstancesResponse{}
104 bytes, _ := json.Marshal(e2tInstancesResponse)
106 context := controllerE2TInstancesTestContext{
107 e2tAddresses: []string{},
110 expectedStatusCode: http.StatusOK,
111 expectedJsonResponse: string(bytes),
114 controllerGetE2TInstancesTestExecuter(t, &context)
117 func TestControllerGetE2TInstancesInternal(t *testing.T) {
118 context := controllerE2TInstancesTestContext{
121 error: common.NewInternalError(errors.New("error")),
122 expectedStatusCode: http.StatusInternalServerError,
123 expectedJsonResponse: "{\"errorCode\":500,\"errorMessage\":\"RNIB error\"}",
126 controllerGetE2TInstancesTestExecuter(t, &context)
129 func TestInvalidRequestName(t *testing.T) {
130 controller, _ := setupE2TControllerTest(t)
132 writer := httptest.NewRecorder()
134 header := &http.Header{}
136 controller.handleRequest(writer, header, "", nil, true)
138 var errorResponse = parseJsonRequest(t, writer.Body)
140 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
141 assert.Equal(t, errorResponse.Code, 501)