is005q | 33591a3 | 2019-12-25 09:08:28 +0200 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2019 AT&T Intellectual Property |
| 3 | // Copyright 2019 Nokia |
| 4 | // |
| 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 |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 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. |
| 16 | // |
| 17 | |
| 18 | package controllers |
| 19 | |
| 20 | import ( |
| 21 | "e2mgr/configuration" |
| 22 | "e2mgr/managers" |
| 23 | "e2mgr/mocks" |
| 24 | "e2mgr/models" |
| 25 | "e2mgr/providers/httpmsghandlerprovider" |
| 26 | "e2mgr/services" |
| 27 | "encoding/json" |
| 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" |
| 32 | "io/ioutil" |
| 33 | "net/http" |
| 34 | "net/http/httptest" |
| 35 | "testing" |
| 36 | ) |
| 37 | |
| 38 | const E2TAddress string = "10.0.2.15:38000" |
| 39 | const E2TAddress2 string = "10.0.2.16:38001" |
| 40 | |
| 41 | type controllerE2TInstancesTestContext struct { |
| 42 | e2tAddresses []string |
| 43 | e2tInstances []*entities.E2TInstance |
| 44 | error error |
| 45 | expectedStatusCode int |
| 46 | expectedJsonResponse string |
| 47 | } |
| 48 | |
| 49 | func setupE2TControllerTest(t *testing.T) (*E2TController, *mocks.RnibReaderMock) { |
| 50 | log := initLog(t) |
| 51 | config := configuration.ParseConfiguration() |
| 52 | |
| 53 | readerMock := &mocks.RnibReaderMock{} |
| 54 | |
| 55 | rnibDataService := services.NewRnibDataService(log, config, readerMock, nil) |
| 56 | e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, log) |
Irina | db03807 | 2020-06-21 13:09:56 +0300 | [diff] [blame] | 57 | |
| 58 | ranListManager := managers.NewRanListManager(log) |
| 59 | ranAlarmService := services.NewRanAlarmService(log, config) |
| 60 | ranConnectStatusChangeManager := managers.NewRanConnectStatusChangeManager(log, rnibDataService,ranListManager, ranAlarmService) |
Irina | b2f2743 | 2020-07-07 11:17:10 +0000 | [diff] [blame] | 61 | nodebValidator := managers.NewNodebValidator() |
Irina | 5830d4d | 2020-07-08 18:11:04 +0000 | [diff] [blame^] | 62 | updateEnbManager := managers.NewUpdateEnbManager(log, rnibDataService, nodebValidator) |
Irina | db03807 | 2020-06-21 13:09:56 +0300 | [diff] [blame] | 63 | |
Irina | 5830d4d | 2020-07-08 18:11:04 +0000 | [diff] [blame^] | 64 | handlerProvider := httpmsghandlerprovider.NewIncomingRequestHandlerProvider(log, nil, config, rnibDataService, e2tInstancesManager, nil, ranConnectStatusChangeManager, nodebValidator, updateEnbManager) |
is005q | 33591a3 | 2019-12-25 09:08:28 +0200 | [diff] [blame] | 65 | controller := NewE2TController(log, handlerProvider) |
| 66 | return controller, readerMock |
| 67 | } |
| 68 | |
| 69 | func controllerGetE2TInstancesTestExecuter(t *testing.T, context *controllerE2TInstancesTestContext) { |
| 70 | controller, readerMock := setupE2TControllerTest(t) |
| 71 | writer := httptest.NewRecorder() |
| 72 | readerMock.On("GetE2TAddresses").Return(context.e2tAddresses, context.error) |
| 73 | |
| 74 | if context.e2tInstances != nil { |
| 75 | readerMock.On("GetE2TInstances", context.e2tAddresses).Return(context.e2tInstances, context.error) |
| 76 | } |
| 77 | |
| 78 | req, _ := http.NewRequest("GET", "/e2t/list", nil) |
| 79 | controller.GetE2TInstances(writer, req) |
| 80 | assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode) |
| 81 | bodyBytes, _ := ioutil.ReadAll(writer.Body) |
| 82 | assert.Equal(t, context.expectedJsonResponse, string(bodyBytes)) |
| 83 | readerMock.AssertExpectations(t) |
| 84 | } |
| 85 | |
| 86 | func TestControllerGetE2TInstancesSuccess(t *testing.T) { |
| 87 | ranNames1 := []string{"test1", "test2", "test3"} |
| 88 | e2tInstanceResponseModel1 := models.NewE2TInstanceResponseModel(E2TAddress, ranNames1) |
| 89 | e2tInstanceResponseModel2 := models.NewE2TInstanceResponseModel(E2TAddress2, []string{}) |
| 90 | e2tInstancesResponse := models.E2TInstancesResponse{e2tInstanceResponseModel1, e2tInstanceResponseModel2} |
| 91 | bytes, _ := json.Marshal(e2tInstancesResponse) |
| 92 | |
| 93 | context := controllerE2TInstancesTestContext{ |
| 94 | e2tAddresses: []string{E2TAddress, E2TAddress2}, |
| 95 | e2tInstances: []*entities.E2TInstance{{Address: E2TAddress, AssociatedRanList: ranNames1}, {Address: E2TAddress2, AssociatedRanList: []string{}}}, |
| 96 | error: nil, |
| 97 | expectedStatusCode: http.StatusOK, |
| 98 | expectedJsonResponse: string(bytes), |
| 99 | } |
| 100 | |
| 101 | controllerGetE2TInstancesTestExecuter(t, &context) |
| 102 | } |
| 103 | |
| 104 | func TestControllerGetE2TInstancesEmptySuccess(t *testing.T) { |
| 105 | e2tInstancesResponse := models.E2TInstancesResponse{} |
| 106 | bytes, _ := json.Marshal(e2tInstancesResponse) |
| 107 | |
| 108 | context := controllerE2TInstancesTestContext{ |
| 109 | e2tAddresses: []string{}, |
| 110 | e2tInstances: nil, |
| 111 | error: nil, |
| 112 | expectedStatusCode: http.StatusOK, |
| 113 | expectedJsonResponse: string(bytes), |
| 114 | } |
| 115 | |
| 116 | controllerGetE2TInstancesTestExecuter(t, &context) |
| 117 | } |
| 118 | |
| 119 | func TestControllerGetE2TInstancesInternal(t *testing.T) { |
| 120 | context := controllerE2TInstancesTestContext{ |
| 121 | e2tAddresses: nil, |
| 122 | e2tInstances: nil, |
| 123 | error: common.NewInternalError(errors.New("error")), |
| 124 | expectedStatusCode: http.StatusInternalServerError, |
| 125 | expectedJsonResponse: "{\"errorCode\":500,\"errorMessage\":\"RNIB error\"}", |
| 126 | } |
| 127 | |
| 128 | controllerGetE2TInstancesTestExecuter(t, &context) |
| 129 | } |
| 130 | |
| 131 | func TestInvalidRequestName(t *testing.T) { |
| 132 | controller, _ := setupE2TControllerTest(t) |
| 133 | |
| 134 | writer := httptest.NewRecorder() |
| 135 | |
| 136 | header := &http.Header{} |
| 137 | |
| 138 | controller.handleRequest(writer, header, "", nil, true) |
| 139 | |
| 140 | var errorResponse = parseJsonRequest(t, writer.Body) |
| 141 | |
| 142 | assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode) |
| 143 | assert.Equal(t, errorResponse.Code, 501) |
| 144 | } |