blob: 713fcde4ccc517a2f672806178dcaefdb0fd1c8a [file] [log] [blame]
ss412g07ef76d2019-08-12 17:26:40 +03001//
2// Copyright 2019 AT&T Intellectual Property
3// Copyright 2019 Nokia
Rahul Banerji5f208b12020-10-01 17:00:32 +05304// Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
ss412g07ef76d2019-08-12 17:26:40 +03005//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
nm755n15d39822019-11-28 16:56:00 +000017
18// This source code is part of the near-RT RIC (RAN Intelligent Controller)
19// platform project (RICP).
20
ss412g07ef76d2019-08-12 17:26:40 +030021package controllers
22
23import (
ss412gde190682019-10-24 09:29:26 +030024 "bytes"
Amichai804065d2019-12-24 14:52:24 +020025 "e2mgr/clients"
Amichaie3623cf2019-09-11 16:59:16 +030026 "e2mgr/configuration"
ss412gde190682019-10-24 09:29:26 +030027 "e2mgr/e2managererrors"
ss412g07ef76d2019-08-12 17:26:40 +030028 "e2mgr/logger"
ss412gde190682019-10-24 09:29:26 +030029 "e2mgr/managers"
ss412g07ef76d2019-08-12 17:26:40 +030030 "e2mgr/mocks"
31 "e2mgr/models"
ss412gde190682019-10-24 09:29:26 +030032 "e2mgr/providers/httpmsghandlerprovider"
ss412g07ef76d2019-08-12 17:26:40 +030033 "e2mgr/rmrCgo"
34 "e2mgr/services"
ss412gde190682019-10-24 09:29:26 +030035 "e2mgr/services/rmrsender"
ss412g07ef76d2019-08-12 17:26:40 +030036 "e2mgr/tests"
ss412gde190682019-10-24 09:29:26 +030037 "encoding/json"
38 "fmt"
ss412g07ef76d2019-08-12 17:26:40 +030039 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
40 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
idanshal08bbf912020-04-20 17:11:33 +030041 "github.com/golang/protobuf/jsonpb"
Amichai4db397d2019-08-28 19:23:15 +030042 "github.com/gorilla/mux"
ss412g07ef76d2019-08-12 17:26:40 +030043 "github.com/pkg/errors"
44 "github.com/stretchr/testify/assert"
ss412gde190682019-10-24 09:29:26 +030045 "github.com/stretchr/testify/mock"
46 "io"
ss412g07ef76d2019-08-12 17:26:40 +030047 "io/ioutil"
48 "net/http"
49 "net/http/httptest"
ss412gde190682019-10-24 09:29:26 +030050 "strings"
ss412g07ef76d2019-08-12 17:26:40 +030051 "testing"
ns019tb3805a92020-04-13 16:57:59 +030052 "unsafe"
ss412g07ef76d2019-08-12 17:26:40 +030053)
54
idanshalf1035e22020-04-13 11:09:19 +030055const (
56 RanName = "test"
57 AssociatedE2TInstanceAddress = "10.0.2.15:38000"
idanshale0106ff2020-07-07 16:04:00 +000058 CorruptedJson = "{\"errorCode\":401,\"errorMessage\":\"corrupted json\"}"
idanshalf1035e22020-04-13 11:09:19 +030059 ValidationFailureJson = "{\"errorCode\":402,\"errorMessage\":\"Validation error\"}"
60 ResourceNotFoundJson = "{\"errorCode\":404,\"errorMessage\":\"Resource not found\"}"
Idan Shalom14eb5132020-07-06 09:41:46 +000061 NodebExistsJson = "{\"errorCode\":406,\"errorMessage\":\"Nodeb already exists\"}"
idanshalf1035e22020-04-13 11:09:19 +030062 RnibErrorJson = "{\"errorCode\":500,\"errorMessage\":\"RNIB error\"}"
idanshal08bbf912020-04-20 17:11:33 +030063 InternalErrorJson = "{\"errorCode\":501,\"errorMessage\":\"Internal Server Error. Please try again later\"}"
Idan Shalom14eb5132020-07-06 09:41:46 +000064 AddEnbUrl = "/nodeb/enb"
idanshalf1035e22020-04-13 11:09:19 +030065)
66
67var (
idanshal13fff612020-07-07 11:53:39 +000068 ServedNrCellInformationRequiredFields = []string{"cellId", "choiceNrMode", "nrMode", "servedPlmns"}
69 NrNeighbourInformationRequiredFields = []string{"nrCgi", "choiceNrMode", "nrMode"}
70 AddEnbRequestRequiredFields = []string{"ranName", "enb", "globalNbId"}
Irina9377e3d2020-07-13 13:15:03 +030071 UpdateEnbRequestRequiredFields = []string{"enb"}
idanshale0106ff2020-07-07 16:04:00 +000072 GlobalIdRequiredFields = []string{"plmnId", "nbId"}
Idan Shalom14eb5132020-07-06 09:41:46 +000073 EnbRequiredFields = []string{"enbType", "servedCells"}
idanshal13fff612020-07-07 11:53:39 +000074 ServedCellRequiredFields = []string{"broadcastPlmns", "cellId", "choiceEutraMode", "eutraMode", "tac"}
idanshalf1035e22020-04-13 11:09:19 +030075)
76
ss412gde190682019-10-24 09:29:26 +030077type controllerGetNodebTestContext struct {
78 ranName string
79 nodebInfo *entities.NodebInfo
80 rnibError error
81 expectedStatusCode int
82 expectedJsonResponse string
83}
84
85type controllerGetNodebIdListTestContext struct {
86 nodebIdList []*entities.NbIdentity
87 rnibError error
88 expectedStatusCode int
89 expectedJsonResponse string
90}
91
idanshalf1035e22020-04-13 11:09:19 +030092type getNodebInfoResult struct {
93 nodebInfo *entities.NodebInfo
94 rnibError error
95}
96
idanshal08bbf912020-04-20 17:11:33 +030097type updateGnbCellsParams struct {
98 err error
99}
100
Irina9377e3d2020-07-13 13:15:03 +0300101type updateEnbCellsParams struct {
102 err error
103}
104
Amichai7ec48b22020-07-23 11:28:40 +0000105type addEnbParams struct {
idanshala269db82020-07-15 08:59:15 +0000106 err error
107}
108
109type addNbIdentityParams struct {
110 err error
Idan Shalom14eb5132020-07-06 09:41:46 +0000111}
112
Irina9377e3d2020-07-13 13:15:03 +0300113type removeServedCellsParams struct {
114 servedCellInfo []*entities.ServedCellInfo
115 err error
116}
117
idanshald916e2a2020-04-22 15:56:43 +0300118type removeServedNrCellsParams struct {
119 servedNrCells []*entities.ServedNRCell
idanshal087f53f2020-04-27 14:12:54 +0300120 err error
idanshald916e2a2020-04-22 15:56:43 +0300121}
122
Irina9377e3d2020-07-13 13:15:03 +0300123type controllerUpdateEnbTestContext struct {
124 getNodebInfoResult *getNodebInfoResult
125 removeServedCellsParams *removeServedCellsParams
126 updateEnbCellsParams *updateEnbCellsParams
127 requestBody map[string]interface{}
128 expectedStatusCode int
129 expectedJsonResponse string
130}
131
idanshalf1035e22020-04-13 11:09:19 +0300132type controllerUpdateGnbTestContext struct {
idanshal087f53f2020-04-27 14:12:54 +0300133 getNodebInfoResult *getNodebInfoResult
idanshald916e2a2020-04-22 15:56:43 +0300134 removeServedNrCellsParams *removeServedNrCellsParams
idanshal087f53f2020-04-27 14:12:54 +0300135 updateGnbCellsParams *updateGnbCellsParams
136 requestBody map[string]interface{}
137 expectedStatusCode int
138 expectedJsonResponse string
idanshalf1035e22020-04-13 11:09:19 +0300139}
140
Idan Shalom14eb5132020-07-06 09:41:46 +0000141type controllerAddEnbTestContext struct {
142 getNodebInfoResult *getNodebInfoResult
Amichai7ec48b22020-07-23 11:28:40 +0000143 addEnbParams *addEnbParams
idanshala269db82020-07-15 08:59:15 +0000144 addNbIdentityParams *addNbIdentityParams
Idan Shalom14eb5132020-07-06 09:41:46 +0000145 requestBody map[string]interface{}
146 expectedStatusCode int
147 expectedJsonResponse string
148}
149
Amichaic7018fe2020-07-09 10:18:52 +0000150type controllerDeleteEnbTestContext struct {
151 getNodebInfoResult *getNodebInfoResult
152 expectedStatusCode int
153 expectedJsonResponse string
154}
155
idanshald916e2a2020-04-22 15:56:43 +0300156func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell {
157
158 servedNrCells := []*entities.ServedNRCell{}
159
160 for _, v := range cellIds {
161 servedNrCells = append(servedNrCells, &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{
162 CellId: v,
163 ChoiceNrMode: &entities.ServedNRCellInformation_ChoiceNRMode{
Rahul Banerjife8d4812020-08-24 14:26:41 +0530164 Fdd: &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{},
idanshald916e2a2020-04-22 15:56:43 +0300165 },
166 NrMode: entities.Nr_FDD,
167 NrPci: 5,
168 ServedPlmns: []string{"whatever"},
169 }})
170 }
171
172 return servedNrCells
173}
174
Irina9377e3d2020-07-13 13:15:03 +0300175func generateServedCells(cellIds ...string) []*entities.ServedCellInfo {
176
177 var servedCells []*entities.ServedCellInfo
178
179 for i, v := range cellIds {
180 servedCells = append(servedCells, &entities.ServedCellInfo{
181 CellId: v,
182 ChoiceEutraMode: &entities.ChoiceEUTRAMode{
183 Fdd: &entities.FddInfo{},
184 },
185 Pci: uint32(i + 1),
186 BroadcastPlmns: []string{"whatever"},
187 })
188 }
189
190 return servedCells
191}
192
idanshalf1035e22020-04-13 11:09:19 +0300193func buildNrNeighbourInformation(propToOmit string) map[string]interface{} {
194 ret := map[string]interface{}{
195 "nrCgi": "whatever",
196 "choiceNrMode": map[string]interface{}{
197 "tdd": map[string]interface{}{},
198 },
199 "nrMode": 1,
200 "nrPci": 1,
201 }
202
203 if len(propToOmit) != 0 {
204 delete(ret, propToOmit)
205 }
206
207 return ret
208}
209
210func buildServedNrCellInformation(propToOmit string) map[string]interface{} {
211 ret := map[string]interface{}{
212 "cellId": "whatever",
213 "choiceNrMode": map[string]interface{}{
214 "fdd": map[string]interface{}{},
215 },
216 "nrMode": 1,
217 "nrPci": 1,
218 "servedPlmns": []interface{}{
219 "whatever",
220 },
221 }
222
223 if len(propToOmit) != 0 {
224 delete(ret, propToOmit)
225 }
226
227 return ret
228}
229
Idan Shalom14eb5132020-07-06 09:41:46 +0000230func buildServedCell(propToOmit string) map[string]interface{} {
231 ret := map[string]interface{}{
232 "cellId": "whatever",
233 "choiceEutraMode": map[string]interface{}{
234 "fdd": map[string]interface{}{},
235 },
236 "eutraMode": 1,
237 "pci": 1,
238 "tac": "whatever3",
239 "broadcastPlmns": []interface{}{
240 "whatever",
241 },
242 }
243
244 if len(propToOmit) != 0 {
245 delete(ret, propToOmit)
246 }
247
248 return ret
249}
250
Irina9377e3d2020-07-13 13:15:03 +0300251func getUpdateEnbRequest(propToOmit string) map[string]interface{} {
252 ret := map[string]interface{}{
idanshala269db82020-07-15 08:59:15 +0000253 "enb": buildEnb(propToOmit),
Irina9377e3d2020-07-13 13:15:03 +0300254 }
255
256 if len(propToOmit) != 0 {
257 delete(ret, propToOmit)
258 }
259
260 return ret
261}
262
idanshal13fff612020-07-07 11:53:39 +0000263func getAddEnbRequest(propToOmit string) map[string]interface{} {
264 ret := map[string]interface{}{
idanshale0106ff2020-07-07 16:04:00 +0000265 "ranName": RanName,
266 "globalNbId": buildGlobalNbId(""),
267 "enb": buildEnb(""),
idanshal13fff612020-07-07 11:53:39 +0000268 }
269
270 if len(propToOmit) != 0 {
271 delete(ret, propToOmit)
272 }
273
274 return ret
275}
276
277func buildEnb(propToOmit string) map[string]interface{} {
278 ret := map[string]interface{}{
279 "enbType": 1,
280 "servedCells": []interface{}{
281 buildServedCell(""),
282 }}
283
284 if len(propToOmit) != 0 {
285 delete(ret, propToOmit)
286 }
287
288 return ret
289}
290
idanshale0106ff2020-07-07 16:04:00 +0000291func buildGlobalNbId(propToOmit string) map[string]interface{} {
292 ret := map[string]interface{}{
293 "plmnId": "whatever",
294 "nbId": "whatever2",
295 }
296
297 if len(propToOmit) != 0 {
298 delete(ret, propToOmit)
299 }
300
301 return ret
302}
303
Irinaa5904172020-07-16 21:21:51 +0300304func setupControllerTest(t *testing.T) (*NodebController, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.RmrMessengerMock, *mocks.E2TInstancesManagerMock, managers.RanListManager) {
ss412gde190682019-10-24 09:29:26 +0300305 log := initLog(t)
306 config := configuration.ParseConfiguration()
307
308 rmrMessengerMock := &mocks.RmrMessengerMock{}
Amichaie3623cf2019-09-11 16:59:16 +0300309 readerMock := &mocks.RnibReaderMock{}
ss412gefcb4522019-12-02 16:59:19 +0200310
ss412gde190682019-10-24 09:29:26 +0300311 writerMock := &mocks.RnibWriterMock{}
ss412gefcb4522019-12-02 16:59:19 +0200312
313 rnibDataService := services.NewRnibDataService(log, config, readerMock, writerMock)
ss412gde190682019-10-24 09:29:26 +0300314 rmrSender := getRmrSender(rmrMessengerMock, log)
ss412gefcb4522019-12-02 16:59:19 +0200315 e2tInstancesManager := &mocks.E2TInstancesManagerMock{}
Amichai804065d2019-12-24 14:52:24 +0200316 httpClientMock := &mocks.HttpClientMock{}
317 rmClient := clients.NewRoutingManagerClient(log, config, httpClientMock)
idanshala269db82020-07-15 08:59:15 +0000318 ranListManager := managers.NewRanListManager(log, rnibDataService)
idanshal76709202020-06-17 14:53:06 +0300319 ranAlarmService := &mocks.RanAlarmServiceMock{}
320 ranConnectStatusChangeManager := managers.NewRanConnectStatusChangeManager(log, rnibDataService, ranListManager, ranAlarmService)
Irinab2f27432020-07-07 11:17:10 +0000321 nodebValidator := managers.NewNodebValidator()
Irina5830d4d2020-07-08 18:11:04 +0000322 updateEnbManager := managers.NewUpdateEnbManager(log, rnibDataService, nodebValidator)
idanshal70008802020-07-23 06:16:41 +0000323 updateGnbManager := managers.NewUpdateGnbManager(log, rnibDataService, nodebValidator)
324 handlerProvider := httpmsghandlerprovider.NewIncomingRequestHandlerProvider(log, rmrSender, config, rnibDataService, e2tInstancesManager, rmClient, ranConnectStatusChangeManager, nodebValidator, updateEnbManager, updateGnbManager, ranListManager)
ss412gde190682019-10-24 09:29:26 +0300325 controller := NewNodebController(log, handlerProvider)
Irinaa5904172020-07-16 21:21:51 +0300326 return controller, readerMock, writerMock, rmrMessengerMock, e2tInstancesManager, ranListManager
Amichaie3623cf2019-09-11 16:59:16 +0300327}
ss412g07ef76d2019-08-12 17:26:40 +0300328
Amichai1bf79dd2020-07-16 21:15:32 +0000329func setupDeleteEnbControllerTest(t *testing.T, preAddNbIdentity bool) (*NodebController, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *entities.NbIdentity) {
330 log := initLog(t)
331 config := configuration.ParseConfiguration()
332
333 rmrMessengerMock := &mocks.RmrMessengerMock{}
334 readerMock := &mocks.RnibReaderMock{}
335
336 writerMock := &mocks.RnibWriterMock{}
337
338 rnibDataService := services.NewRnibDataService(log, config, readerMock, writerMock)
339 rmrSender := getRmrSender(rmrMessengerMock, log)
340 e2tInstancesManager := &mocks.E2TInstancesManagerMock{}
341 httpClientMock := &mocks.HttpClientMock{}
342 rmClient := clients.NewRoutingManagerClient(log, config, httpClientMock)
343 ranListManager := managers.NewRanListManager(log, rnibDataService)
344 var nbIdentity *entities.NbIdentity
345 if preAddNbIdentity {
346 nbIdentity = &entities.NbIdentity{InventoryName: RanName, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
347 writerMock.On("AddNbIdentity", entities.Node_ENB, nbIdentity).Return(nil)
348 ranListManager.AddNbIdentity(entities.Node_ENB, nbIdentity)
349 }
350 ranAlarmService := &mocks.RanAlarmServiceMock{}
351 ranConnectStatusChangeManager := managers.NewRanConnectStatusChangeManager(log, rnibDataService, ranListManager, ranAlarmService)
352 nodebValidator := managers.NewNodebValidator()
353 updateEnbManager := managers.NewUpdateEnbManager(log, rnibDataService, nodebValidator)
idanshal70008802020-07-23 06:16:41 +0000354 updateGnbManager := managers.NewUpdateGnbManager(log, rnibDataService, nodebValidator)
Amichai1bf79dd2020-07-16 21:15:32 +0000355
idanshal70008802020-07-23 06:16:41 +0000356 handlerProvider := httpmsghandlerprovider.NewIncomingRequestHandlerProvider(log, rmrSender, config, rnibDataService, e2tInstancesManager, rmClient, ranConnectStatusChangeManager, nodebValidator, updateEnbManager, updateGnbManager, ranListManager)
Amichai1bf79dd2020-07-16 21:15:32 +0000357 controller := NewNodebController(log, handlerProvider)
358 return controller, readerMock, writerMock, nbIdentity
359}
360
ss412gde190682019-10-24 09:29:26 +0300361func TestShutdownHandlerRnibError(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +0300362 controller, _, _, _, e2tInstancesManagerMock, _ := setupControllerTest(t)
is005q90d22a62020-01-01 11:55:49 +0200363 e2tInstancesManagerMock.On("GetE2TAddresses").Return([]string{}, e2managererrors.NewRnibDbError())
ss412gde190682019-10-24 09:29:26 +0300364
365 writer := httptest.NewRecorder()
366
367 controller.Shutdown(writer, tests.GetHttpRequest())
368
369 var errorResponse = parseJsonRequest(t, writer.Body)
370
371 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
372 assert.Equal(t, errorResponse.Code, e2managererrors.NewRnibDbError().Code)
373}
374
ss412ga07b8592020-06-16 21:51:33 +0300375func TestSetGeneralConfigurationHandlerRnibError(t *testing.T) {
idanshal70008802020-07-23 06:16:41 +0000376 controller, readerMock, _, _, _, _ := setupControllerTest(t)
ss412ga07b8592020-06-16 21:51:33 +0300377
378 configuration := &entities.GeneralConfiguration{}
379 readerMock.On("GetGeneralConfiguration").Return(configuration, e2managererrors.NewRnibDbError())
380
381 writer := httptest.NewRecorder()
382
383 httpRequest, _ := http.NewRequest("PUT", "https://localhost:3800/v1/nodeb/parameters", strings.NewReader("{\"enableRic\":false}"))
384
385 controller.SetGeneralConfiguration(writer, httpRequest)
386
387 var errorResponse = parseJsonRequest(t, writer.Body)
388
389 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
390 assert.Equal(t, e2managererrors.NewRnibDbError().Code, errorResponse.Code)
391}
392
393func TestSetGeneralConfigurationInvalidJson(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +0300394 controller, _, _, _, _, _ := setupControllerTest(t)
ss412ga07b8592020-06-16 21:51:33 +0300395
396 writer := httptest.NewRecorder()
397
Irinad6d623c2020-06-17 15:01:21 +0300398 httpRequest, _ := http.NewRequest("PUT", "https://localhost:3800/v1/nodeb/parameters", strings.NewReader("{\"enableRic\":false, \"someValue\":false}"))
ss412ga07b8592020-06-16 21:51:33 +0300399
400 controller.SetGeneralConfiguration(writer, httpRequest)
401
402 var errorResponse = parseJsonRequest(t, writer.Body)
403
404 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
405 assert.Equal(t, e2managererrors.NewInvalidJsonError().Code, errorResponse.Code)
406}
407
ss412gde190682019-10-24 09:29:26 +0300408func controllerGetNodebTestExecuter(t *testing.T, context *controllerGetNodebTestContext) {
Irinaa5904172020-07-16 21:21:51 +0300409 controller, readerMock, _, _, _, _ := setupControllerTest(t)
ss412gde190682019-10-24 09:29:26 +0300410 writer := httptest.NewRecorder()
411 readerMock.On("GetNodeb", context.ranName).Return(context.nodebInfo, context.rnibError)
idanshalf1035e22020-04-13 11:09:19 +0300412 req, _ := http.NewRequest(http.MethodGet, "/nodeb", nil)
ss412gde190682019-10-24 09:29:26 +0300413 req = mux.SetURLVars(req, map[string]string{"ranName": context.ranName})
414 controller.GetNodeb(writer, req)
415 assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
416 bodyBytes, _ := ioutil.ReadAll(writer.Body)
417 assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
ss412g07ef76d2019-08-12 17:26:40 +0300418}
419
ss412gde190682019-10-24 09:29:26 +0300420func controllerGetNodebIdListTestExecuter(t *testing.T, context *controllerGetNodebIdListTestContext) {
Irinaa5904172020-07-16 21:21:51 +0300421 controller, readerMock, _, _, _, ranListManager := setupControllerTest(t)
ss412g07ef76d2019-08-12 17:26:40 +0300422 writer := httptest.NewRecorder()
ss412gde190682019-10-24 09:29:26 +0300423 readerMock.On("GetListNodebIds").Return(context.nodebIdList, context.rnibError)
Irinaa5904172020-07-16 21:21:51 +0300424
425 err := ranListManager.InitNbIdentityMap()
426 if err != nil {
427 t.Errorf("Error cannot init identity")
428 }
429
430 req, _ := http.NewRequest(http.MethodGet, "/nodeb/states", nil)
ss412gde190682019-10-24 09:29:26 +0300431 controller.GetNodebIdList(writer, req)
432 assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
433 bodyBytes, _ := ioutil.ReadAll(writer.Body)
Amichai7ec48b22020-07-23 11:28:40 +0000434 assert.Contains(t, context.expectedJsonResponse, string(bodyBytes))
ss412gde190682019-10-24 09:29:26 +0300435}
ss412g07ef76d2019-08-12 17:26:40 +0300436
idanshala269db82020-07-15 08:59:15 +0000437func activateControllerUpdateEnbMocks(context *controllerUpdateEnbTestContext, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock, updateEnbRequest *models.UpdateEnbRequest) {
Irina9377e3d2020-07-13 13:15:03 +0300438 if context.getNodebInfoResult != nil {
439 readerMock.On("GetNodeb", RanName).Return(context.getNodebInfoResult.nodebInfo, context.getNodebInfoResult.rnibError)
440 }
441
442 if context.removeServedCellsParams != nil {
443 writerMock.On("RemoveServedCells", RanName, context.removeServedCellsParams.servedCellInfo).Return(context.removeServedCellsParams.err)
444 }
445
446 if context.updateEnbCellsParams != nil {
447 updatedNodebInfo := *context.getNodebInfoResult.nodebInfo
idanshal885ce202020-08-05 06:49:57 +0000448
449 if context.getNodebInfoResult.nodebInfo.SetupFromNetwork {
450 updateEnbRequest.Enb.EnbType = context.getNodebInfoResult.nodebInfo.GetEnb().EnbType
451 }
452
Irina9377e3d2020-07-13 13:15:03 +0300453 updatedNodebInfo.Configuration = &entities.NodebInfo_Enb{Enb: updateEnbRequest.Enb}
454
455 writerMock.On("UpdateEnb", &updatedNodebInfo, updateEnbRequest.Enb.ServedCells).Return(context.updateEnbCellsParams.err)
456 }
457}
458
idanshal08bbf912020-04-20 17:11:33 +0300459func activateControllerUpdateGnbMocks(context *controllerUpdateGnbTestContext, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock) {
idanshalf1035e22020-04-13 11:09:19 +0300460 if context.getNodebInfoResult != nil {
461 readerMock.On("GetNodeb", RanName).Return(context.getNodebInfoResult.nodebInfo, context.getNodebInfoResult.rnibError)
462 }
463
idanshald916e2a2020-04-22 15:56:43 +0300464 if context.removeServedNrCellsParams != nil {
465 writerMock.On("RemoveServedNrCells", RanName, context.removeServedNrCellsParams.servedNrCells).Return(context.removeServedNrCellsParams.err)
466 }
467
idanshal08bbf912020-04-20 17:11:33 +0300468 if context.updateGnbCellsParams != nil {
469 updatedNodebInfo := *context.getNodebInfoResult.nodebInfo
470 gnb := entities.Gnb{}
471 _ = jsonpb.Unmarshal(getJsonRequestAsBuffer(context.requestBody), &gnb)
idanshald916e2a2020-04-22 15:56:43 +0300472 updatedGnb := *updatedNodebInfo.GetGnb()
473 updatedGnb.ServedNrCells = gnb.ServedNrCells
idanshal08bbf912020-04-20 17:11:33 +0300474 writerMock.On("UpdateGnbCells", &updatedNodebInfo, gnb.ServedNrCells).Return(context.updateGnbCellsParams.err)
475 }
476}
477
478func assertControllerUpdateGnb(t *testing.T, context *controllerUpdateGnbTestContext, writer *httptest.ResponseRecorder, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock) {
479 assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
480 bodyBytes, _ := ioutil.ReadAll(writer.Body)
481 assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
482 readerMock.AssertExpectations(t)
483 writerMock.AssertExpectations(t)
Idan Shalom14eb5132020-07-06 09:41:46 +0000484}
idanshal08bbf912020-04-20 17:11:33 +0300485
Irina9377e3d2020-07-13 13:15:03 +0300486func assertControllerUpdateEnb(t *testing.T, context *controllerUpdateEnbTestContext, writer *httptest.ResponseRecorder, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock) {
487 assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
488 bodyBytes, _ := ioutil.ReadAll(writer.Body)
489 assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
490 readerMock.AssertExpectations(t)
491 writerMock.AssertExpectations(t)
492}
493
Idan Shalom14eb5132020-07-06 09:41:46 +0000494func assertControllerAddEnb(t *testing.T, context *controllerAddEnbTestContext, writer *httptest.ResponseRecorder, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock) {
495 assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
496 bodyBytes, _ := ioutil.ReadAll(writer.Body)
497 assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
498 readerMock.AssertExpectations(t)
499 writerMock.AssertExpectations(t)
idanshal08bbf912020-04-20 17:11:33 +0300500}
501
Amichaic7018fe2020-07-09 10:18:52 +0000502func assertControllerDeleteEnb(t *testing.T, context *controllerDeleteEnbTestContext, writer *httptest.ResponseRecorder, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock) {
503 assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
504 bodyBytes, _ := ioutil.ReadAll(writer.Body)
505 assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
506 readerMock.AssertExpectations(t)
507 writerMock.AssertExpectations(t)
508}
509
Irina9377e3d2020-07-13 13:15:03 +0300510func buildUpdateEnbRequest(context *controllerUpdateEnbTestContext) *http.Request {
511 updateEnbUrl := fmt.Sprintf("/nodeb/enb/%s", RanName)
512 requestBody := getJsonRequestAsBuffer(context.requestBody)
513 req, _ := http.NewRequest(http.MethodPut, updateEnbUrl, requestBody)
514 req.Header.Set("Content-Type", "application/json")
515 req = mux.SetURLVars(req, map[string]string{"ranName": RanName})
516 return req
517}
518
idanshal08bbf912020-04-20 17:11:33 +0300519func buildUpdateGnbRequest(context *controllerUpdateGnbTestContext) *http.Request {
idanshal70008802020-07-23 06:16:41 +0000520 updateGnbUrl := fmt.Sprintf("/nodeb/gnb/%s", RanName)
idanshalf1035e22020-04-13 11:09:19 +0300521 requestBody := getJsonRequestAsBuffer(context.requestBody)
Idan Shalom14eb5132020-07-06 09:41:46 +0000522 req, _ := http.NewRequest(http.MethodPut, updateGnbUrl, requestBody)
idanshalf1035e22020-04-13 11:09:19 +0300523 req.Header.Set("Content-Type", "application/json")
524 req = mux.SetURLVars(req, map[string]string{"ranName": RanName})
idanshal08bbf912020-04-20 17:11:33 +0300525 return req
526}
527
Idan Shalom14eb5132020-07-06 09:41:46 +0000528func buildAddEnbRequest(context *controllerAddEnbTestContext) *http.Request {
529 requestBody := getJsonRequestAsBuffer(context.requestBody)
530 req, _ := http.NewRequest(http.MethodPost, AddEnbUrl, requestBody)
531 req.Header.Set("Content-Type", "application/json")
532 return req
533}
534
Irina9377e3d2020-07-13 13:15:03 +0300535func controllerUpdateEnbTestExecuter(t *testing.T, context *controllerUpdateEnbTestContext) {
Irinaa5904172020-07-16 21:21:51 +0300536 controller, readerMock, writerMock, _, _, _ := setupControllerTest(t)
Irina9377e3d2020-07-13 13:15:03 +0300537 writer := httptest.NewRecorder()
538
539 r := buildUpdateEnbRequest(context)
540 body, _ := ioutil.ReadAll(io.LimitReader(r.Body, LimitRequest))
541
542 updateEnbRequest := models.UpdateEnbRequest{}
543 _ = json.Unmarshal(body, &updateEnbRequest)
544
545 activateControllerUpdateEnbMocks(context, readerMock, writerMock, &updateEnbRequest)
546 r = buildUpdateEnbRequest(context)
547 defer r.Body.Close()
548
549 controller.UpdateEnb(writer, r)
550
551 assertControllerUpdateEnb(t, context, writer, readerMock, writerMock)
552}
553
idanshal08bbf912020-04-20 17:11:33 +0300554func controllerUpdateGnbTestExecuter(t *testing.T, context *controllerUpdateGnbTestContext) {
Irinaa5904172020-07-16 21:21:51 +0300555 controller, readerMock, writerMock, _, _, _ := setupControllerTest(t)
idanshal08bbf912020-04-20 17:11:33 +0300556 writer := httptest.NewRecorder()
557
558 activateControllerUpdateGnbMocks(context, readerMock, writerMock)
559 req := buildUpdateGnbRequest(context)
idanshalf1035e22020-04-13 11:09:19 +0300560 controller.UpdateGnb(writer, req)
idanshal08bbf912020-04-20 17:11:33 +0300561 assertControllerUpdateGnb(t, context, writer, readerMock, writerMock)
idanshalf1035e22020-04-13 11:09:19 +0300562}
563
Idan Shalom14eb5132020-07-06 09:41:46 +0000564func activateControllerAddEnbMocks(context *controllerAddEnbTestContext, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock, addEnbRequest *models.AddEnbRequest) {
565 if context.getNodebInfoResult != nil {
566 readerMock.On("GetNodeb", RanName).Return(context.getNodebInfoResult.nodebInfo, context.getNodebInfoResult.rnibError)
567 }
568
Amichai7ec48b22020-07-23 11:28:40 +0000569 if context.addEnbParams != nil {
Idan Shalom14eb5132020-07-06 09:41:46 +0000570 nodebInfo := entities.NodebInfo{
571 RanName: addEnbRequest.RanName,
572 Ip: addEnbRequest.Ip,
573 Port: addEnbRequest.Port,
idanshale0106ff2020-07-07 16:04:00 +0000574 NodeType: entities.Node_ENB,
Idan Shalom14eb5132020-07-06 09:41:46 +0000575 GlobalNbId: addEnbRequest.GlobalNbId,
576 Configuration: &entities.NodebInfo_Enb{Enb: addEnbRequest.Enb},
577 ConnectionStatus: entities.ConnectionStatus_DISCONNECTED,
578 }
579
Amichai7ec48b22020-07-23 11:28:40 +0000580 writerMock.On("AddEnb", &nodebInfo).Return(context.addEnbParams.err)
idanshala269db82020-07-15 08:59:15 +0000581 }
582
583 if context.addNbIdentityParams != nil {
584 nbIdentity := entities.NbIdentity{InventoryName: addEnbRequest.RanName, GlobalNbId: addEnbRequest.GlobalNbId, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED}
585 writerMock.On("AddNbIdentity", entities.Node_ENB, &nbIdentity).Return(context.addNbIdentityParams.err)
Idan Shalom14eb5132020-07-06 09:41:46 +0000586 }
587}
588
589func controllerAddEnbTestExecuter(t *testing.T, context *controllerAddEnbTestContext) {
Irinaa5904172020-07-16 21:21:51 +0300590 controller, readerMock, writerMock, _, _, _ := setupControllerTest(t)
Idan Shalom14eb5132020-07-06 09:41:46 +0000591 writer := httptest.NewRecorder()
592 r := buildAddEnbRequest(context)
Idan Shalom14eb5132020-07-06 09:41:46 +0000593 body, _ := ioutil.ReadAll(io.LimitReader(r.Body, LimitRequest))
594
595 addEnbRequest := models.AddEnbRequest{}
596
597 _ = json.Unmarshal(body, &addEnbRequest)
598 activateControllerAddEnbMocks(context, readerMock, writerMock, &addEnbRequest)
idanshal13fff612020-07-07 11:53:39 +0000599 r = buildAddEnbRequest(context)
600 defer r.Body.Close()
601 controller.AddEnb(writer, r)
Idan Shalom14eb5132020-07-06 09:41:46 +0000602 assertControllerAddEnb(t, context, writer, readerMock, writerMock)
603}
604
Amichai1bf79dd2020-07-16 21:15:32 +0000605func controllerDeleteEnbTestExecuter(t *testing.T, context *controllerDeleteEnbTestContext, preAddNbIdentity bool) {
606 controller, readerMock, writerMock, nbIdentity := setupDeleteEnbControllerTest(t, preAddNbIdentity)
Amichaic7018fe2020-07-09 10:18:52 +0000607 readerMock.On("GetNodeb", RanName).Return(context.getNodebInfoResult.nodebInfo, context.getNodebInfoResult.rnibError)
idanshal885ce202020-08-05 06:49:57 +0000608 if context.getNodebInfoResult.rnibError == nil && context.getNodebInfoResult.nodebInfo.GetNodeType() == entities.Node_ENB &&
609 !context.getNodebInfoResult.nodebInfo.SetupFromNetwork {
Amichaic7018fe2020-07-09 10:18:52 +0000610 writerMock.On("RemoveEnb", context.getNodebInfoResult.nodebInfo).Return(nil)
Amichai1bf79dd2020-07-16 21:15:32 +0000611 if preAddNbIdentity {
idanshal70008802020-07-23 06:16:41 +0000612 writerMock.On("RemoveNbIdentity", entities.Node_ENB, nbIdentity).Return(nil)
Amichai1bf79dd2020-07-16 21:15:32 +0000613 }
Amichaic7018fe2020-07-09 10:18:52 +0000614 }
615 writer := httptest.NewRecorder()
616 r, _ := http.NewRequest(http.MethodDelete, AddEnbUrl+"/"+RanName, nil)
617 r.Header.Set("Content-Type", "application/json")
618 r = mux.SetURLVars(r, map[string]string{"ranName": RanName})
619 controller.DeleteEnb(writer, r)
620 assertControllerDeleteEnb(t, context, writer, readerMock, writerMock)
621}
622
idanshal70008802020-07-23 06:16:41 +0000623/*
624UpdateGnb UTs
625*/
626
627// BEGIN - UpdateGnb Validation Failure UTs
628
idanshalf1035e22020-04-13 11:09:19 +0300629func TestControllerUpdateGnbEmptyServedNrCells(t *testing.T) {
630 context := controllerUpdateGnbTestContext{
631 getNodebInfoResult: nil,
632 requestBody: map[string]interface{}{
Rahul Banerjife8d4812020-08-24 14:26:41 +0530633 "servedNrCells": []interface{}{},
idanshalf1035e22020-04-13 11:09:19 +0300634 },
635 expectedStatusCode: http.StatusBadRequest,
636 expectedJsonResponse: ValidationFailureJson,
637 }
638
639 controllerUpdateGnbTestExecuter(t, &context)
640}
641
642func TestControllerUpdateGnbMissingServedNrCellInformation(t *testing.T) {
643 context := controllerUpdateGnbTestContext{
644 getNodebInfoResult: nil,
645 requestBody: map[string]interface{}{
646 "servedNrCells": []interface{}{
647 map[string]interface{}{
648 "servedNrCellInformation": nil,
649 },
650 },
651 },
652 expectedStatusCode: http.StatusBadRequest,
653 expectedJsonResponse: ValidationFailureJson,
654 }
655
656 controllerUpdateGnbTestExecuter(t, &context)
657}
658
659func TestControllerUpdateGnbMissingServedNrCellRequiredProp(t *testing.T) {
660
661 for _, v := range ServedNrCellInformationRequiredFields {
662 context := controllerUpdateGnbTestContext{
663 getNodebInfoResult: nil,
664 requestBody: map[string]interface{}{
665 "servedNrCells": []interface{}{
666 map[string]interface{}{
667 "servedNrCellInformation": buildServedNrCellInformation(v),
668 },
669 },
670 },
671 expectedStatusCode: http.StatusBadRequest,
672 expectedJsonResponse: ValidationFailureJson,
673 }
674
675 controllerUpdateGnbTestExecuter(t, &context)
676 }
677}
678
679func TestControllerUpdateGnbMissingServedNrCellFddOrTdd(t *testing.T) {
680
681 servedNrCellInformation := buildServedNrCellInformation("")
682 servedNrCellInformation["choiceNrMode"] = map[string]interface{}{}
683
684 context := controllerUpdateGnbTestContext{
685 getNodebInfoResult: nil,
686 requestBody: map[string]interface{}{
687 "servedNrCells": []interface{}{
688 map[string]interface{}{
689 "servedNrCellInformation": servedNrCellInformation,
690 },
691 },
692 },
693 expectedStatusCode: http.StatusBadRequest,
694 expectedJsonResponse: ValidationFailureJson,
695 }
696
697 controllerUpdateGnbTestExecuter(t, &context)
698}
699
700func TestControllerUpdateGnbMissingNeighbourInfoFddOrTdd(t *testing.T) {
701
702 nrNeighbourInfo := buildNrNeighbourInformation("")
703 nrNeighbourInfo["choiceNrMode"] = map[string]interface{}{}
704
705 context := controllerUpdateGnbTestContext{
706 getNodebInfoResult: nil,
707 requestBody: map[string]interface{}{
708 "servedNrCells": []interface{}{
709 map[string]interface{}{
710 "servedNrCellInformation": buildServedNrCellInformation(""),
idanshal08bbf912020-04-20 17:11:33 +0300711 "nrNeighbourInfos": []interface{}{
idanshalf1035e22020-04-13 11:09:19 +0300712 nrNeighbourInfo,
713 },
714 },
715 },
716 },
717 expectedStatusCode: http.StatusBadRequest,
718 expectedJsonResponse: ValidationFailureJson,
719 }
720
721 controllerUpdateGnbTestExecuter(t, &context)
722}
723
724func TestControllerUpdateGnbMissingNrNeighbourInformationRequiredProp(t *testing.T) {
725
726 for _, v := range NrNeighbourInformationRequiredFields {
727 context := controllerUpdateGnbTestContext{
728 getNodebInfoResult: nil,
729 requestBody: map[string]interface{}{
730 "servedNrCells": []interface{}{
731 map[string]interface{}{
732 "servedNrCellInformation": buildServedNrCellInformation(""),
idanshal08bbf912020-04-20 17:11:33 +0300733 "nrNeighbourInfos": []interface{}{
idanshalf1035e22020-04-13 11:09:19 +0300734 buildNrNeighbourInformation(v),
735 },
736 },
737 },
738 },
739 expectedStatusCode: http.StatusBadRequest,
740 expectedJsonResponse: ValidationFailureJson,
741 }
742
743 controllerUpdateGnbTestExecuter(t, &context)
744 }
745}
746
idanshal70008802020-07-23 06:16:41 +0000747// END - UpdateGnb Validation Failure UTs
748
idanshalf1035e22020-04-13 11:09:19 +0300749func TestControllerUpdateGnbValidServedNrCellInformationGetNodebNotFound(t *testing.T) {
750 context := controllerUpdateGnbTestContext{
751 getNodebInfoResult: &getNodebInfoResult{
752 nodebInfo: nil,
753 rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
754 },
755 requestBody: map[string]interface{}{
756 "servedNrCells": []interface{}{
757 map[string]interface{}{
758 "servedNrCellInformation": buildServedNrCellInformation(""),
759 },
760 },
761 },
762 expectedStatusCode: http.StatusNotFound,
763 expectedJsonResponse: ResourceNotFoundJson,
764 }
765
766 controllerUpdateGnbTestExecuter(t, &context)
767}
768
769func TestControllerUpdateGnbValidServedNrCellInformationGetNodebInternalError(t *testing.T) {
770 context := controllerUpdateGnbTestContext{
771 getNodebInfoResult: &getNodebInfoResult{
772 nodebInfo: nil,
773 rnibError: common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
774 },
775 requestBody: map[string]interface{}{
776 "servedNrCells": []interface{}{
777 map[string]interface{}{
778 "servedNrCellInformation": buildServedNrCellInformation(""),
779 },
780 },
781 },
782 expectedStatusCode: http.StatusInternalServerError,
783 expectedJsonResponse: RnibErrorJson,
784 }
785
786 controllerUpdateGnbTestExecuter(t, &context)
787}
788
idanshal08bbf912020-04-20 17:11:33 +0300789func TestControllerUpdateGnbGetNodebSuccessInvalidGnbConfiguration(t *testing.T) {
790 context := controllerUpdateGnbTestContext{
791 getNodebInfoResult: &getNodebInfoResult{
792 nodebInfo: &entities.NodebInfo{
793 RanName: RanName,
794 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
795 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
idanshal70008802020-07-23 06:16:41 +0000796 NodeType: entities.Node_ENB,
idanshal08bbf912020-04-20 17:11:33 +0300797 },
798 rnibError: nil,
799 },
800 requestBody: map[string]interface{}{
801 "servedNrCells": []interface{}{
802 map[string]interface{}{
803 "servedNrCellInformation": buildServedNrCellInformation(""),
804 "nrNeighbourInfos": []interface{}{
805 buildNrNeighbourInformation(""),
806 },
807 },
808 },
809 },
idanshal70008802020-07-23 06:16:41 +0000810 expectedStatusCode: http.StatusBadRequest,
811 expectedJsonResponse: ValidationFailureJson,
idanshal08bbf912020-04-20 17:11:33 +0300812 }
813
814 controllerUpdateGnbTestExecuter(t, &context)
815}
816
idanshald916e2a2020-04-22 15:56:43 +0300817func TestControllerUpdateGnbGetNodebSuccessRemoveServedNrCellsFailure(t *testing.T) {
idanshal087f53f2020-04-27 14:12:54 +0300818 oldServedNrCells := generateServedNrCells("whatever1", "whatever2")
idanshal08bbf912020-04-20 17:11:33 +0300819 context := controllerUpdateGnbTestContext{
idanshald916e2a2020-04-22 15:56:43 +0300820 removeServedNrCellsParams: &removeServedNrCellsParams{
idanshal087f53f2020-04-27 14:12:54 +0300821 err: common.NewInternalError(errors.New("#writer.UpdateGnbCells - Internal Error")),
idanshald916e2a2020-04-22 15:56:43 +0300822 servedNrCells: oldServedNrCells,
823 },
824 getNodebInfoResult: &getNodebInfoResult{
825 nodebInfo: &entities.NodebInfo{
826 RanName: RanName,
827 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
828 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
829 Configuration: &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{ServedNrCells: oldServedNrCells}},
idanshal70008802020-07-23 06:16:41 +0000830 NodeType: entities.Node_GNB,
idanshald916e2a2020-04-22 15:56:43 +0300831 },
832 rnibError: nil,
833 },
834 requestBody: map[string]interface{}{
835 "servedNrCells": []interface{}{
836 map[string]interface{}{
837 "servedNrCellInformation": buildServedNrCellInformation(""),
838 "nrNeighbourInfos": []interface{}{
839 buildNrNeighbourInformation(""),
840 },
841 },
842 },
843 },
844 expectedStatusCode: http.StatusInternalServerError,
845 expectedJsonResponse: RnibErrorJson,
846 }
847
848 controllerUpdateGnbTestExecuter(t, &context)
849}
850
851func TestControllerUpdateGnbGetNodebSuccessUpdateGnbCellsFailure(t *testing.T) {
idanshal087f53f2020-04-27 14:12:54 +0300852 oldServedNrCells := generateServedNrCells("whatever1", "whatever2")
idanshald916e2a2020-04-22 15:56:43 +0300853 context := controllerUpdateGnbTestContext{
854 removeServedNrCellsParams: &removeServedNrCellsParams{
idanshal087f53f2020-04-27 14:12:54 +0300855 err: nil,
idanshald916e2a2020-04-22 15:56:43 +0300856 servedNrCells: oldServedNrCells,
857 },
idanshal08bbf912020-04-20 17:11:33 +0300858 updateGnbCellsParams: &updateGnbCellsParams{
859 err: common.NewInternalError(errors.New("#writer.UpdateGnbCells - Internal Error")),
860 },
861 getNodebInfoResult: &getNodebInfoResult{
862 nodebInfo: &entities.NodebInfo{
863 RanName: RanName,
864 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
865 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
idanshald916e2a2020-04-22 15:56:43 +0300866 Configuration: &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{ServedNrCells: oldServedNrCells}},
idanshal70008802020-07-23 06:16:41 +0000867 NodeType: entities.Node_GNB,
idanshal08bbf912020-04-20 17:11:33 +0300868 },
869 rnibError: nil,
870 },
871 requestBody: map[string]interface{}{
872 "servedNrCells": []interface{}{
873 map[string]interface{}{
874 "servedNrCellInformation": buildServedNrCellInformation(""),
875 "nrNeighbourInfos": []interface{}{
876 buildNrNeighbourInformation(""),
877 },
878 },
879 },
880 },
881 expectedStatusCode: http.StatusInternalServerError,
882 expectedJsonResponse: RnibErrorJson,
883 }
884
885 controllerUpdateGnbTestExecuter(t, &context)
886}
887
idanshal70008802020-07-23 06:16:41 +0000888func TestControllerUpdateGnbExistingEmptyCellsSuccess(t *testing.T) {
889 oldServedNrCells := []*entities.ServedNRCell{}
890
idanshal08bbf912020-04-20 17:11:33 +0300891 context := controllerUpdateGnbTestContext{
892 updateGnbCellsParams: &updateGnbCellsParams{
893 err: nil,
894 },
895 getNodebInfoResult: &getNodebInfoResult{
896 nodebInfo: &entities.NodebInfo{
897 RanName: RanName,
898 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
899 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
idanshal70008802020-07-23 06:16:41 +0000900 Configuration: &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{ServedNrCells: oldServedNrCells}},
901 NodeType: entities.Node_GNB,
idanshal08bbf912020-04-20 17:11:33 +0300902 },
903 rnibError: nil,
904 },
905 requestBody: map[string]interface{}{
906 "servedNrCells": []interface{}{
907 map[string]interface{}{
908 "servedNrCellInformation": buildServedNrCellInformation(""),
909 "nrNeighbourInfos": []interface{}{
910 buildNrNeighbourInformation(""),
911 },
912 },
913 },
914 },
915 expectedStatusCode: http.StatusOK,
idanshal70008802020-07-23 06:16:41 +0000916 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\"}",
idanshal08bbf912020-04-20 17:11:33 +0300917 }
918
919 controllerUpdateGnbTestExecuter(t, &context)
920}
921
idanshal70008802020-07-23 06:16:41 +0000922func TestControllerUpdateGnbSuccess(t *testing.T) {
923 oldServedNrCells := generateServedNrCells("whatever1", "whatever2")
924
925 context := controllerUpdateGnbTestContext{
926 removeServedNrCellsParams: &removeServedNrCellsParams{
927 err: nil,
928 servedNrCells: oldServedNrCells,
929 },
930 updateGnbCellsParams: &updateGnbCellsParams{
931 err: nil,
932 },
933 getNodebInfoResult: &getNodebInfoResult{
934 nodebInfo: &entities.NodebInfo{
935 RanName: RanName,
936 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
937 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
938 Configuration: &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{ServedNrCells: oldServedNrCells}},
939 NodeType: entities.Node_GNB,
940 },
941 rnibError: nil,
942 },
943 requestBody: map[string]interface{}{
944 "servedNrCells": []interface{}{
945 map[string]interface{}{
946 "servedNrCellInformation": buildServedNrCellInformation(""),
947 "nrNeighbourInfos": []interface{}{
948 buildNrNeighbourInformation(""),
949 },
950 },
951 },
952 },
953 expectedStatusCode: http.StatusOK,
954 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\"}",
955 }
956
957 controllerUpdateGnbTestExecuter(t, &context)
958}
959
960/*
961UpdateEnb UTs
962*/
963
Irina9377e3d2020-07-13 13:15:03 +0300964func TestControllerUpdateEnbInvalidRequest(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +0300965 controller, _, _, _, _, _ := setupControllerTest(t)
Irina9377e3d2020-07-13 13:15:03 +0300966
967 writer := httptest.NewRecorder()
968 invalidJson := strings.NewReader("{enb:\"whatever\"")
969
970 updateEnbUrl := fmt.Sprintf("/nodeb/enb/%s", RanName)
971 req, _ := http.NewRequest(http.MethodPut, updateEnbUrl, invalidJson)
972 req.Header.Set("Content-Type", "application/json")
973 req = mux.SetURLVars(req, map[string]string{"ranName": RanName})
974
975 controller.UpdateEnb(writer, req)
976
977 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
978 bodyBytes, _ := ioutil.ReadAll(writer.Body)
979 assert.Equal(t, CorruptedJson, string(bodyBytes))
980}
981
982func TestControllerUpdateEnbEmptyEnbType(t *testing.T) {
983 context := controllerUpdateEnbTestContext{
idanshala269db82020-07-15 08:59:15 +0000984 getNodebInfoResult: nil,
985 requestBody: getUpdateEnbRequest(EnbRequiredFields[0]),
Irina9377e3d2020-07-13 13:15:03 +0300986 expectedStatusCode: http.StatusBadRequest,
987 expectedJsonResponse: ValidationFailureJson,
988 }
989
990 controllerUpdateEnbTestExecuter(t, &context)
991}
992
993func TestControllerUpdateEnbEmptyServedCells(t *testing.T) {
994 context := controllerUpdateEnbTestContext{
idanshala269db82020-07-15 08:59:15 +0000995 getNodebInfoResult: nil,
996 requestBody: getUpdateEnbRequest(EnbRequiredFields[1]),
Irina9377e3d2020-07-13 13:15:03 +0300997 expectedStatusCode: http.StatusBadRequest,
998 expectedJsonResponse: ValidationFailureJson,
999 }
1000
1001 controllerUpdateEnbTestExecuter(t, &context)
1002}
1003
1004func TestControllerUpdateEnbMissingEnb(t *testing.T) {
1005 context := controllerUpdateEnbTestContext{
idanshala269db82020-07-15 08:59:15 +00001006 getNodebInfoResult: nil,
1007 requestBody: getUpdateEnbRequest(UpdateEnbRequestRequiredFields[0]),
Irina9377e3d2020-07-13 13:15:03 +03001008 expectedStatusCode: http.StatusBadRequest,
1009 expectedJsonResponse: ValidationFailureJson,
1010 }
1011
1012 controllerUpdateEnbTestExecuter(t, &context)
1013}
1014
idanshal70008802020-07-23 06:16:41 +00001015func TestControllerUpdateEnbMissingRequiredServedCellProps(t *testing.T) {
1016
1017 r := getUpdateEnbRequest("")
1018
1019 for _, v := range ServedCellRequiredFields {
1020 enb := r["enb"]
1021
1022 enbMap, _ := enb.(map[string]interface{})
1023
1024 enbMap["servedCells"] = []interface{}{
1025 buildServedCell(v),
1026 }
1027
1028 context := controllerUpdateEnbTestContext{
1029 requestBody: r,
1030 expectedStatusCode: http.StatusBadRequest,
1031 expectedJsonResponse: ValidationFailureJson,
1032 }
1033
1034 controllerUpdateEnbTestExecuter(t, &context)
1035 }
1036}
1037
Irina9377e3d2020-07-13 13:15:03 +03001038func TestControllerUpdateEnbValidServedCellsGetNodebNotFound(t *testing.T) {
1039 context := controllerUpdateEnbTestContext{
1040 getNodebInfoResult: &getNodebInfoResult{
1041 nodebInfo: nil,
1042 rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
1043 },
idanshala269db82020-07-15 08:59:15 +00001044 requestBody: getUpdateEnbRequest(""),
Irina9377e3d2020-07-13 13:15:03 +03001045 expectedStatusCode: http.StatusNotFound,
1046 expectedJsonResponse: ResourceNotFoundJson,
1047 }
1048
1049 controllerUpdateEnbTestExecuter(t, &context)
1050}
1051
1052func TestControllerUpdateEnbValidServedCellsGetNodebInternalError(t *testing.T) {
1053 context := controllerUpdateEnbTestContext{
1054 getNodebInfoResult: &getNodebInfoResult{
1055 nodebInfo: nil,
1056 rnibError: common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
1057 },
idanshala269db82020-07-15 08:59:15 +00001058 requestBody: getUpdateEnbRequest(""),
Irina9377e3d2020-07-13 13:15:03 +03001059 expectedStatusCode: http.StatusInternalServerError,
1060 expectedJsonResponse: RnibErrorJson,
1061 }
1062
1063 controllerUpdateEnbTestExecuter(t, &context)
1064}
1065
1066func TestControllerUpdateEnbGetNodebSuccessGnbTypeFailure(t *testing.T) {
1067 oldServedCells := generateServedCells("whatever1", "whatever2")
1068 context := controllerUpdateEnbTestContext{
1069 getNodebInfoResult: &getNodebInfoResult{
1070 nodebInfo: &entities.NodebInfo{
1071 RanName: RanName,
1072 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
1073 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
1074 NodeType: entities.Node_GNB,
1075 Configuration: &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells}},
1076 },
1077 rnibError: nil,
1078 },
idanshala269db82020-07-15 08:59:15 +00001079 requestBody: getUpdateEnbRequest(""),
Irina9377e3d2020-07-13 13:15:03 +03001080 expectedStatusCode: http.StatusBadRequest,
1081 expectedJsonResponse: ValidationFailureJson,
1082 }
1083
1084 controllerUpdateEnbTestExecuter(t, &context)
1085}
1086
1087func TestControllerUpdateEnbGetNodebSuccessRemoveServedCellsFailure(t *testing.T) {
1088 oldServedCells := generateServedCells("whatever1", "whatever2")
1089 context := controllerUpdateEnbTestContext{
1090 removeServedCellsParams: &removeServedCellsParams{
1091 err: common.NewInternalError(errors.New("#writer.RemoveServedCells - Internal Error")),
1092 servedCellInfo: oldServedCells,
1093 },
1094 getNodebInfoResult: &getNodebInfoResult{
1095 nodebInfo: &entities.NodebInfo{
1096 RanName: RanName,
1097 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
1098 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
1099 NodeType: entities.Node_ENB,
1100 Configuration: &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells}},
1101 },
1102 rnibError: nil,
1103 },
idanshala269db82020-07-15 08:59:15 +00001104 requestBody: getUpdateEnbRequest(""),
Irina9377e3d2020-07-13 13:15:03 +03001105 expectedStatusCode: http.StatusInternalServerError,
1106 expectedJsonResponse: RnibErrorJson,
1107 }
1108
1109 controllerUpdateEnbTestExecuter(t, &context)
1110}
1111
1112func TestControllerUpdateEnbGetNodebSuccessUpdateEnbFailure(t *testing.T) {
1113 oldServedCells := generateServedCells("whatever1", "whatever2")
1114 context := controllerUpdateEnbTestContext{
1115 removeServedCellsParams: &removeServedCellsParams{
idanshala269db82020-07-15 08:59:15 +00001116 err: nil,
Irina9377e3d2020-07-13 13:15:03 +03001117 servedCellInfo: oldServedCells,
1118 },
1119 updateEnbCellsParams: &updateEnbCellsParams{
1120 err: common.NewInternalError(errors.New("#writer.UpdateEnb - Internal Error")),
1121 },
1122 getNodebInfoResult: &getNodebInfoResult{
1123 nodebInfo: &entities.NodebInfo{
1124 RanName: RanName,
1125 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
1126 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
1127 NodeType: entities.Node_ENB,
1128 Configuration: &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells, EnbType: entities.EnbType_MACRO_ENB}},
1129 },
1130 rnibError: nil,
1131 },
idanshala269db82020-07-15 08:59:15 +00001132 requestBody: getUpdateEnbRequest(""),
Irina9377e3d2020-07-13 13:15:03 +03001133 expectedStatusCode: http.StatusInternalServerError,
1134 expectedJsonResponse: RnibErrorJson,
1135 }
1136
1137 controllerUpdateEnbTestExecuter(t, &context)
1138}
1139
idanshal70008802020-07-23 06:16:41 +00001140func TestControllerUpdateEnbExistingEmptyCellsSuccess(t *testing.T) {
1141 oldServedCells := []*entities.ServedCellInfo{}
1142 context := controllerUpdateEnbTestContext{
1143 updateEnbCellsParams: &updateEnbCellsParams{
1144 err: nil,
1145 },
1146 getNodebInfoResult: &getNodebInfoResult{
1147 nodebInfo: &entities.NodebInfo{
1148 RanName: RanName,
1149 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
1150 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
1151 NodeType: entities.Node_ENB,
1152 Configuration: &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells, EnbType: entities.EnbType_MACRO_ENB}},
1153 },
1154 rnibError: nil,
1155 },
1156 requestBody: getUpdateEnbRequest(""),
1157 expectedStatusCode: http.StatusOK,
1158 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\"}",
1159 }
1160
1161 controllerUpdateEnbTestExecuter(t, &context)
1162}
1163
idanshal885ce202020-08-05 06:49:57 +00001164func TestControllerUpdateEnbNgEnbFailure(t *testing.T) {
idanshal70008802020-07-23 06:16:41 +00001165
idanshal885ce202020-08-05 06:49:57 +00001166 requestBody := map[string]interface{}{
1167 "enb": map[string]interface{}{
ss412g7bd44bd2020-08-06 21:20:09 +00001168 "enbType": 3,
idanshal885ce202020-08-05 06:49:57 +00001169 "servedCells": []interface{}{
1170 buildServedCell(""),
1171 }},
1172 }
1173
ss412g7bd44bd2020-08-06 21:20:09 +00001174 oldServedCells := generateServedCells("whatever1", "whatever2")
1175
idanshal885ce202020-08-05 06:49:57 +00001176 context := controllerUpdateEnbTestContext{
Rahul Banerjife8d4812020-08-24 14:26:41 +05301177 getNodebInfoResult: &getNodebInfoResult{
1178 nodebInfo: &entities.NodebInfo{
1179 RanName: RanName,
1180 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
1181 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
1182 NodeType: entities.Node_ENB,
1183 Configuration: &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells, EnbType: entities.EnbType_MACRO_NG_ENB}},
1184 },
1185 rnibError: nil,
1186 },
idanshal885ce202020-08-05 06:49:57 +00001187 requestBody: requestBody,
1188 expectedStatusCode: http.StatusBadRequest,
1189 expectedJsonResponse: ValidationFailureJson,
1190 }
1191
1192 controllerUpdateEnbTestExecuter(t, &context)
1193}
1194
1195func TestControllerUpdateEnbSuccessSetupFromNwFalse(t *testing.T) {
Irina9377e3d2020-07-13 13:15:03 +03001196 oldServedCells := generateServedCells("whatever1", "whatever2")
1197 context := controllerUpdateEnbTestContext{
1198 removeServedCellsParams: &removeServedCellsParams{
idanshala269db82020-07-15 08:59:15 +00001199 err: nil,
Irina9377e3d2020-07-13 13:15:03 +03001200 servedCellInfo: oldServedCells,
1201 },
1202 updateEnbCellsParams: &updateEnbCellsParams{
1203 err: nil,
1204 },
1205 getNodebInfoResult: &getNodebInfoResult{
1206 nodebInfo: &entities.NodebInfo{
1207 RanName: RanName,
1208 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
1209 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
1210 NodeType: entities.Node_ENB,
idanshal885ce202020-08-05 06:49:57 +00001211 Configuration: &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells, EnbType: entities.EnbType_LONG_MACRO_ENB}},
Irina9377e3d2020-07-13 13:15:03 +03001212 },
1213 rnibError: nil,
1214 },
idanshala269db82020-07-15 08:59:15 +00001215 requestBody: getUpdateEnbRequest(""),
Irina9377e3d2020-07-13 13:15:03 +03001216 expectedStatusCode: http.StatusOK,
1217 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\"}",
1218 }
1219
1220 controllerUpdateEnbTestExecuter(t, &context)
1221}
1222
idanshal885ce202020-08-05 06:49:57 +00001223func TestControllerUpdateEnbSuccessSetupFromNwTrue(t *testing.T) {
1224 oldServedCells := generateServedCells("whatever1", "whatever2")
1225 context := controllerUpdateEnbTestContext{
1226 removeServedCellsParams: &removeServedCellsParams{
1227 err: nil,
1228 servedCellInfo: oldServedCells,
1229 },
1230 updateEnbCellsParams: &updateEnbCellsParams{
1231 err: nil,
1232 },
1233 getNodebInfoResult: &getNodebInfoResult{
1234 nodebInfo: &entities.NodebInfo{
1235 RanName: RanName,
1236 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
1237 AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
1238 NodeType: entities.Node_ENB,
1239 Configuration: &entities.NodebInfo_Enb{Enb: &entities.Enb{ServedCells: oldServedCells, EnbType: entities.EnbType_LONG_MACRO_ENB}},
1240 SetupFromNetwork: true,
1241 },
1242 rnibError: nil,
1243 },
1244 requestBody: getUpdateEnbRequest(""),
1245 expectedStatusCode: http.StatusOK,
1246 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}",
1247 }
1248
1249 controllerUpdateEnbTestExecuter(t, &context)
1250}
1251
idanshal70008802020-07-23 06:16:41 +00001252/*
1253AddEnb UTs
1254*/
1255
idanshal13fff612020-07-07 11:53:39 +00001256func TestControllerAddEnbGetNodebInternalError(t *testing.T) {
1257 context := controllerAddEnbTestContext{
1258 getNodebInfoResult: &getNodebInfoResult{
1259 nodebInfo: nil,
1260 rnibError: common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
1261 },
1262 requestBody: getAddEnbRequest(""),
1263 expectedStatusCode: http.StatusInternalServerError,
1264 expectedJsonResponse: RnibErrorJson,
1265 }
1266
1267 controllerAddEnbTestExecuter(t, &context)
1268}
1269
1270func TestControllerAddEnbNodebExistsFailure(t *testing.T) {
1271 context := controllerAddEnbTestContext{
1272 getNodebInfoResult: &getNodebInfoResult{
1273 nodebInfo: &entities.NodebInfo{},
1274 rnibError: nil,
1275 },
1276 requestBody: getAddEnbRequest(""),
1277 expectedStatusCode: http.StatusBadRequest,
1278 expectedJsonResponse: NodebExistsJson,
1279 }
1280
1281 controllerAddEnbTestExecuter(t, &context)
1282}
1283
1284func TestControllerAddEnbSaveNodebFailure(t *testing.T) {
1285 context := controllerAddEnbTestContext{
Amichai7ec48b22020-07-23 11:28:40 +00001286 addEnbParams: &addEnbParams{
1287 err: common.NewInternalError(errors.New("#reader.AddEnb - Internal Error")),
idanshal13fff612020-07-07 11:53:39 +00001288 },
1289 getNodebInfoResult: &getNodebInfoResult{
1290 nodebInfo: nil,
1291 rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
1292 },
1293 requestBody: getAddEnbRequest(""),
1294 expectedStatusCode: http.StatusInternalServerError,
1295 expectedJsonResponse: RnibErrorJson,
1296 }
1297
1298 controllerAddEnbTestExecuter(t, &context)
1299}
1300
idanshala269db82020-07-15 08:59:15 +00001301func TestControllerAddEnbAddNbIdentityFailure(t *testing.T) {
1302 context := controllerAddEnbTestContext{
Amichai7ec48b22020-07-23 11:28:40 +00001303 addEnbParams: &addEnbParams{
idanshala269db82020-07-15 08:59:15 +00001304 err: nil,
1305 },
1306 addNbIdentityParams: &addNbIdentityParams{
1307 err: common.NewInternalError(errors.New("#writer.addNbIdentity - Internal Error")),
1308 },
1309 getNodebInfoResult: &getNodebInfoResult{
1310 nodebInfo: nil,
1311 rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
1312 },
1313 requestBody: getAddEnbRequest(""),
1314 expectedStatusCode: http.StatusInternalServerError,
1315 expectedJsonResponse: RnibErrorJson,
1316 }
1317
1318 controllerAddEnbTestExecuter(t, &context)
1319}
1320
idanshal13fff612020-07-07 11:53:39 +00001321func TestControllerAddEnbMissingRequiredRequestProps(t *testing.T) {
1322
1323 for _, v := range AddEnbRequestRequiredFields {
1324 context := controllerAddEnbTestContext{
1325 requestBody: getAddEnbRequest(v),
1326 expectedStatusCode: http.StatusBadRequest,
1327 expectedJsonResponse: ValidationFailureJson,
1328 }
1329
1330 controllerAddEnbTestExecuter(t, &context)
1331 }
1332}
1333
idanshale0106ff2020-07-07 16:04:00 +00001334func TestControllerAddEnbInvalidRequest(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001335 controller, _, _, _, _, _ := setupControllerTest(t)
idanshale0106ff2020-07-07 16:04:00 +00001336 writer := httptest.NewRecorder()
1337
1338 // Invalid json: attribute name without quotes (should be "cause":).
1339 invalidJson := strings.NewReader("{ranName:\"whatever\"")
1340 req, _ := http.NewRequest(http.MethodPost, AddEnbUrl, invalidJson)
1341
1342 controller.AddEnb(writer, req)
1343 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
1344 bodyBytes, _ := ioutil.ReadAll(writer.Body)
1345 assert.Equal(t, CorruptedJson, string(bodyBytes))
1346
1347}
1348
1349func TestControllerAddEnbMissingRequiredGlobalNbIdProps(t *testing.T) {
1350
1351 r := getAddEnbRequest("")
1352
1353 for _, v := range GlobalIdRequiredFields {
1354 r["globalNbId"] = buildGlobalNbId(v)
1355
1356 context := controllerAddEnbTestContext{
1357 requestBody: r,
1358 expectedStatusCode: http.StatusBadRequest,
1359 expectedJsonResponse: ValidationFailureJson,
1360 }
1361
1362 controllerAddEnbTestExecuter(t, &context)
1363 }
1364}
1365
idanshal13fff612020-07-07 11:53:39 +00001366func TestControllerAddEnbMissingRequiredEnbProps(t *testing.T) {
1367
1368 r := getAddEnbRequest("")
1369
1370 for _, v := range EnbRequiredFields {
1371 r["enb"] = buildEnb(v)
1372
1373 context := controllerAddEnbTestContext{
1374 requestBody: r,
1375 expectedStatusCode: http.StatusBadRequest,
1376 expectedJsonResponse: ValidationFailureJson,
1377 }
1378
1379 controllerAddEnbTestExecuter(t, &context)
1380 }
1381}
1382
1383func TestControllerAddEnbMissingRequiredServedCellProps(t *testing.T) {
1384
1385 r := getAddEnbRequest("")
1386
1387 for _, v := range ServedCellRequiredFields {
1388 enb := r["enb"]
1389
1390 enbMap, _ := enb.(map[string]interface{})
1391
1392 enbMap["servedCells"] = []interface{}{
1393 buildServedCell(v),
1394 }
1395
1396 context := controllerAddEnbTestContext{
1397 requestBody: r,
1398 expectedStatusCode: http.StatusBadRequest,
1399 expectedJsonResponse: ValidationFailureJson,
1400 }
1401
1402 controllerAddEnbTestExecuter(t, &context)
1403 }
1404}
1405
idanshal885ce202020-08-05 06:49:57 +00001406func TestControllerAddEnbNgEnbFailure(t *testing.T) {
1407
1408 requestBody := map[string]interface{}{
1409 "ranName": RanName,
1410 "globalNbId": buildGlobalNbId(""),
1411 "enb": map[string]interface{}{
1412 "enbType": 5,
1413 "servedCells": []interface{}{
1414 buildServedCell(""),
1415 },
1416 },
1417 }
1418
1419 context := controllerAddEnbTestContext{
1420 requestBody: requestBody,
1421 expectedStatusCode: http.StatusBadRequest,
1422 expectedJsonResponse: ValidationFailureJson,
1423 }
1424
1425 controllerAddEnbTestExecuter(t, &context)
1426}
1427
Idan Shalom14eb5132020-07-06 09:41:46 +00001428func TestControllerAddEnbSuccess(t *testing.T) {
1429 context := controllerAddEnbTestContext{
Amichai7ec48b22020-07-23 11:28:40 +00001430 addEnbParams: &addEnbParams{
Idan Shalom14eb5132020-07-06 09:41:46 +00001431 err: nil,
1432 },
idanshala269db82020-07-15 08:59:15 +00001433 addNbIdentityParams: &addNbIdentityParams{
1434 err: nil,
1435 },
Idan Shalom14eb5132020-07-06 09:41:46 +00001436 getNodebInfoResult: &getNodebInfoResult{
1437 nodebInfo: nil,
1438 rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
1439 },
idanshala269db82020-07-15 08:59:15 +00001440 requestBody: getAddEnbRequest(""),
Idan Shalom14eb5132020-07-06 09:41:46 +00001441 expectedStatusCode: http.StatusCreated,
idanshale0106ff2020-07-07 16:04:00 +00001442 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\"}]}}",
Idan Shalom14eb5132020-07-06 09:41:46 +00001443 }
1444
1445 controllerAddEnbTestExecuter(t, &context)
1446}
1447
Amichaic7018fe2020-07-09 10:18:52 +00001448func TestControllerDeleteEnbGetNodebInternalError(t *testing.T) {
1449 context := controllerDeleteEnbTestContext{
1450 getNodebInfoResult: &getNodebInfoResult{
1451 nodebInfo: nil,
1452 rnibError: common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
1453 },
Irina9377e3d2020-07-13 13:15:03 +03001454 expectedStatusCode: http.StatusInternalServerError,
Amichaic7018fe2020-07-09 10:18:52 +00001455 expectedJsonResponse: RnibErrorJson,
1456 }
1457
Amichai1bf79dd2020-07-16 21:15:32 +00001458 controllerDeleteEnbTestExecuter(t, &context, false)
Amichaic7018fe2020-07-09 10:18:52 +00001459}
1460
idanshal70008802020-07-23 06:16:41 +00001461/*
1462DeleteEnb UTs
1463*/
1464
Amichaic7018fe2020-07-09 10:18:52 +00001465func TestControllerDeleteEnbNodebNotExistsFailure(t *testing.T) {
1466 context := controllerDeleteEnbTestContext{
1467 getNodebInfoResult: &getNodebInfoResult{
1468 nodebInfo: nil,
1469 rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found"),
1470 },
1471 expectedStatusCode: http.StatusNotFound,
1472 expectedJsonResponse: ResourceNotFoundJson,
1473 }
1474
Amichai1bf79dd2020-07-16 21:15:32 +00001475 controllerDeleteEnbTestExecuter(t, &context, false)
Amichaic7018fe2020-07-09 10:18:52 +00001476}
1477
1478func TestControllerDeleteEnbNodebNotEnb(t *testing.T) {
1479 context := controllerDeleteEnbTestContext{
1480 getNodebInfoResult: &getNodebInfoResult{
1481 nodebInfo: &entities.NodebInfo{RanName: "ran1", NodeType: entities.Node_GNB, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED},
1482 rnibError: nil,
1483 },
1484 expectedStatusCode: http.StatusBadRequest,
1485 expectedJsonResponse: ValidationFailureJson,
1486 }
1487
Amichai1bf79dd2020-07-16 21:15:32 +00001488 controllerDeleteEnbTestExecuter(t, &context, false)
Amichaic7018fe2020-07-09 10:18:52 +00001489}
1490
idanshal885ce202020-08-05 06:49:57 +00001491func TestControllerDeleteEnbSetupFromNetworkTrueFailure(t *testing.T) {
1492 context := controllerDeleteEnbTestContext{
1493 getNodebInfoResult: &getNodebInfoResult{
1494 nodebInfo: &entities.NodebInfo{RanName: RanName, NodeType: entities.Node_ENB, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, SetupFromNetwork: true},
1495 rnibError: nil,
1496 },
1497 expectedStatusCode: http.StatusBadRequest,
1498 expectedJsonResponse: ValidationFailureJson,
1499 }
1500 controllerDeleteEnbTestExecuter(t, &context, true)
1501}
1502
Amichaic7018fe2020-07-09 10:18:52 +00001503func TestControllerDeleteEnbSuccess(t *testing.T) {
1504 context := controllerDeleteEnbTestContext{
1505 getNodebInfoResult: &getNodebInfoResult{
Amichai1bf79dd2020-07-16 21:15:32 +00001506 nodebInfo: &entities.NodebInfo{RanName: RanName, NodeType: entities.Node_ENB, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED},
Amichaic7018fe2020-07-09 10:18:52 +00001507 rnibError: nil,
1508 },
Irina9377e3d2020-07-13 13:15:03 +03001509 expectedStatusCode: http.StatusNoContent,
Amichaic7018fe2020-07-09 10:18:52 +00001510 expectedJsonResponse: "",
1511 }
Amichai1bf79dd2020-07-16 21:15:32 +00001512 controllerDeleteEnbTestExecuter(t, &context, true)
Amichaic7018fe2020-07-09 10:18:52 +00001513}
1514
idanshalf1035e22020-04-13 11:09:19 +03001515func getJsonRequestAsBuffer(requestJson map[string]interface{}) *bytes.Buffer {
1516 b := new(bytes.Buffer)
1517 _ = json.NewEncoder(b).Encode(requestJson)
idanshal087f53f2020-04-27 14:12:54 +03001518 return b
idanshalf1035e22020-04-13 11:09:19 +03001519}
1520
idanshal70008802020-07-23 06:16:41 +00001521/*
1522GetNodeb UTs
1523*/
1524
ss412gde190682019-10-24 09:29:26 +03001525func TestControllerGetNodebSuccess(t *testing.T) {
1526 ranName := "test"
irina006fbce2019-09-05 16:11:02 +03001527 var rnibError error
ss412gde190682019-10-24 09:29:26 +03001528 context := controllerGetNodebTestContext{
1529 ranName: ranName,
1530 nodebInfo: &entities.NodebInfo{RanName: ranName, Ip: "10.0.2.15", Port: 1234},
1531 rnibError: rnibError,
1532 expectedStatusCode: http.StatusOK,
1533 expectedJsonResponse: fmt.Sprintf("{\"ranName\":\"%s\",\"ip\":\"10.0.2.15\",\"port\":1234}", ranName),
1534 }
ss412g07ef76d2019-08-12 17:26:40 +03001535
ss412gde190682019-10-24 09:29:26 +03001536 controllerGetNodebTestExecuter(t, &context)
ss412g07ef76d2019-08-12 17:26:40 +03001537}
1538
ss412gde190682019-10-24 09:29:26 +03001539func TestControllerGetNodebNotFound(t *testing.T) {
ss412g07ef76d2019-08-12 17:26:40 +03001540
ss412gde190682019-10-24 09:29:26 +03001541 ranName := "test"
ss412g07ef76d2019-08-12 17:26:40 +03001542 var nodebInfo *entities.NodebInfo
ss412gde190682019-10-24 09:29:26 +03001543 context := controllerGetNodebTestContext{
1544 ranName: ranName,
1545 nodebInfo: nodebInfo,
1546 rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
1547 expectedStatusCode: http.StatusNotFound,
idanshalf1035e22020-04-13 11:09:19 +03001548 expectedJsonResponse: ResourceNotFoundJson,
ss412gde190682019-10-24 09:29:26 +03001549 }
ss412g07ef76d2019-08-12 17:26:40 +03001550
ss412gde190682019-10-24 09:29:26 +03001551 controllerGetNodebTestExecuter(t, &context)
ss412g07ef76d2019-08-12 17:26:40 +03001552}
1553
ss412gde190682019-10-24 09:29:26 +03001554func TestControllerGetNodebInternal(t *testing.T) {
1555 ranName := "test"
ss412g07ef76d2019-08-12 17:26:40 +03001556 var nodebInfo *entities.NodebInfo
ss412gde190682019-10-24 09:29:26 +03001557 context := controllerGetNodebTestContext{
1558 ranName: ranName,
1559 nodebInfo: nodebInfo,
1560 rnibError: common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
1561 expectedStatusCode: http.StatusInternalServerError,
idanshalf1035e22020-04-13 11:09:19 +03001562 expectedJsonResponse: RnibErrorJson,
ss412gde190682019-10-24 09:29:26 +03001563 }
ss412g07ef76d2019-08-12 17:26:40 +03001564
ss412gde190682019-10-24 09:29:26 +03001565 controllerGetNodebTestExecuter(t, &context)
ss412g07ef76d2019-08-12 17:26:40 +03001566}
1567
ss412gde190682019-10-24 09:29:26 +03001568func TestControllerGetNodebIdListSuccess(t *testing.T) {
irina006fbce2019-09-05 16:11:02 +03001569 var rnibError error
ss412gde190682019-10-24 09:29:26 +03001570 nodebIdList := []*entities.NbIdentity{
is005qffd085d2019-09-10 15:56:48 +03001571 {InventoryName: "test1", GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}},
1572 {InventoryName: "test2", GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId2", NbId: "nbId2"}},
Amichai1e5e5ae2019-09-01 16:46:42 +03001573 }
ss412g07ef76d2019-08-12 17:26:40 +03001574
ss412gde190682019-10-24 09:29:26 +03001575 context := controllerGetNodebIdListTestContext{
1576 nodebIdList: nodebIdList,
1577 rnibError: rnibError,
1578 expectedStatusCode: http.StatusOK,
Amichai7ec48b22020-07-23 11:28:40 +00001579 expectedJsonResponse: "[{\"inventoryName\":\"test1\",\"globalNbId\":{\"plmnId\":\"plmnId1\",\"nbId\":\"nbId1\"}},{\"inventoryName\":\"test2\",\"globalNbId\":{\"plmnId\":\"plmnId2\",\"nbId\":\"nbId2\"}}][{\"inventoryName\":\"test2\",\"globalNbId\":{\"plmnId\":\"plmnId2\",\"nbId\":\"nbId2\"}},{\"inventoryName\":\"test1\",\"globalNbId\":{\"plmnId\":\"plmnId1\",\"nbId\":\"nbId1\"}}]",
ss412gde190682019-10-24 09:29:26 +03001580 }
1581
1582 controllerGetNodebIdListTestExecuter(t, &context)
ss412g07ef76d2019-08-12 17:26:40 +03001583}
1584
ss412gde190682019-10-24 09:29:26 +03001585func TestControllerGetNodebIdListEmptySuccess(t *testing.T) {
1586 var rnibError error
Irina5830d4d2020-07-08 18:11:04 +00001587 var nodebIdList []*entities.NbIdentity
ss412gde190682019-10-24 09:29:26 +03001588
1589 context := controllerGetNodebIdListTestContext{
1590 nodebIdList: nodebIdList,
1591 rnibError: rnibError,
1592 expectedStatusCode: http.StatusOK,
1593 expectedJsonResponse: "[]",
1594 }
1595
1596 controllerGetNodebIdListTestExecuter(t, &context)
1597}
1598
ss412gde190682019-10-24 09:29:26 +03001599func TestHeaderValidationFailed(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001600 controller, _, _, _, _, _ := setupControllerTest(t)
ss412g07ef76d2019-08-12 17:26:40 +03001601
1602 writer := httptest.NewRecorder()
1603
ss412gde190682019-10-24 09:29:26 +03001604 header := &http.Header{}
1605
Idan Shalom14eb5132020-07-06 09:41:46 +00001606 controller.handleRequest(writer, header, httpmsghandlerprovider.ShutdownRequest, nil, true, 0)
ss412gde190682019-10-24 09:29:26 +03001607
1608 var errorResponse = parseJsonRequest(t, writer.Body)
1609 err := e2managererrors.NewHeaderValidationError()
1610
1611 assert.Equal(t, http.StatusUnsupportedMediaType, writer.Result().StatusCode)
1612 assert.Equal(t, errorResponse.Code, err.Code)
1613 assert.Equal(t, errorResponse.Message, err.Message)
1614}
1615
1616func TestShutdownStatusNoContent(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001617 controller, readerMock, _, _, e2tInstancesManagerMock, _ := setupControllerTest(t)
is005q90d22a62020-01-01 11:55:49 +02001618 e2tInstancesManagerMock.On("GetE2TAddresses").Return([]string{}, nil)
1619 readerMock.On("GetListNodebIds").Return([]*entities.NbIdentity{}, nil)
ss412g07ef76d2019-08-12 17:26:40 +03001620
ss412gde190682019-10-24 09:29:26 +03001621 writer := httptest.NewRecorder()
1622 controller.Shutdown(writer, tests.GetHttpRequest())
ss412g07ef76d2019-08-12 17:26:40 +03001623
ss412gde190682019-10-24 09:29:26 +03001624 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
ss412g07ef76d2019-08-12 17:26:40 +03001625}
1626
ss412gde190682019-10-24 09:29:26 +03001627func TestHandleInternalError(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001628 controller, _, _, _, _, _ := setupControllerTest(t)
ss412gde190682019-10-24 09:29:26 +03001629
1630 writer := httptest.NewRecorder()
1631 err := e2managererrors.NewInternalError()
1632
1633 controller.handleErrorResponse(err, writer)
1634 var errorResponse = parseJsonRequest(t, writer.Body)
1635
1636 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
1637 assert.Equal(t, errorResponse.Code, err.Code)
1638 assert.Equal(t, errorResponse.Message, err.Message)
1639}
1640
1641func TestHandleCommandAlreadyInProgressError(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001642 controller, _, _, _, _, _ := setupControllerTest(t)
ss412gde190682019-10-24 09:29:26 +03001643 writer := httptest.NewRecorder()
1644 err := e2managererrors.NewCommandAlreadyInProgressError()
1645
1646 controller.handleErrorResponse(err, writer)
1647 var errorResponse = parseJsonRequest(t, writer.Body)
1648
1649 assert.Equal(t, http.StatusMethodNotAllowed, writer.Result().StatusCode)
1650 assert.Equal(t, errorResponse.Code, err.Code)
1651 assert.Equal(t, errorResponse.Message, err.Message)
1652}
1653
is005q32d7f422020-01-02 16:23:06 +02001654func TestHandleRoutingManagerError(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001655 controller, _, _, _, _, _ := setupControllerTest(t)
is005q32d7f422020-01-02 16:23:06 +02001656 writer := httptest.NewRecorder()
1657 err := e2managererrors.NewRoutingManagerError()
1658
1659 controller.handleErrorResponse(err, writer)
1660 var errorResponse = parseJsonRequest(t, writer.Body)
1661
1662 assert.Equal(t, http.StatusServiceUnavailable, writer.Result().StatusCode)
1663 assert.Equal(t, errorResponse.Code, err.Code)
1664 assert.Equal(t, errorResponse.Message, err.Message)
1665}
1666
ss412gefcb4522019-12-02 16:59:19 +02001667func TestHandleE2TInstanceAbsenceError(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001668 controller, _, _, _, _, _ := setupControllerTest(t)
ss412gefcb4522019-12-02 16:59:19 +02001669
1670 writer := httptest.NewRecorder()
1671 err := e2managererrors.NewE2TInstanceAbsenceError()
1672
1673 controller.handleErrorResponse(err, writer)
1674 var errorResponse = parseJsonRequest(t, writer.Body)
1675
1676 assert.Equal(t, http.StatusServiceUnavailable, writer.Result().StatusCode)
1677 assert.Equal(t, errorResponse.Code, err.Code)
1678 assert.Equal(t, errorResponse.Message, err.Message)
1679}
1680
ss412gde190682019-10-24 09:29:26 +03001681func TestValidateHeaders(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001682 controller, _, _, _, _, _ := setupControllerTest(t)
ss412gde190682019-10-24 09:29:26 +03001683
1684 header := http.Header{}
1685 header.Set("Content-Type", "application/json")
1686 result := controller.validateRequestHeader(&header)
1687
1688 assert.Nil(t, result)
1689}
1690
1691func parseJsonRequest(t *testing.T, r io.Reader) models.ErrorResponse {
1692
1693 var errorResponse models.ErrorResponse
1694 body, err := ioutil.ReadAll(r)
1695 if err != nil {
1696 t.Errorf("Error cannot deserialize json request")
1697 }
idanshal087f53f2020-04-27 14:12:54 +03001698 _ = json.Unmarshal(body, &errorResponse)
ss412gde190682019-10-24 09:29:26 +03001699
1700 return errorResponse
1701}
1702
1703func initLog(t *testing.T) *logger.Logger {
rimverma4cd92f92023-04-25 11:51:14 +05301704 InfoLevel := int8(3)
1705 log, err := logger.InitLogger(InfoLevel)
ss412gde190682019-10-24 09:29:26 +03001706 if err != nil {
1707 t.Errorf("#delete_all_request_handler_test.TestHandleSuccessFlow - failed to initialize logger, error: %s", err)
1708 }
1709 return log
1710}
1711
1712func TestX2ResetHandleSuccessfulRequestedCause(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001713 controller, readerMock, _, rmrMessengerMock, _, _ := setupControllerTest(t)
ss412gde190682019-10-24 09:29:26 +03001714
1715 ranName := "test1"
1716 payload := []byte{0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x05, 0x40, 0x01, 0x40}
ss412gefcb4522019-12-02 16:59:19 +02001717 var xAction []byte
ns019tb3805a92020-04-13 16:57:59 +03001718 var msgSrc unsafe.Pointer
1719 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xAction, msgSrc)
ss412gde190682019-10-24 09:29:26 +03001720 rmrMessengerMock.On("SendMsg", msg, mock.Anything).Return(msg, nil)
ss412g07ef76d2019-08-12 17:26:40 +03001721
1722 writer := httptest.NewRecorder()
1723
ss412gde190682019-10-24 09:29:26 +03001724 var nodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED}
1725 readerMock.On("GetNodeb", ranName).Return(nodeb, nil)
ss412g07ef76d2019-08-12 17:26:40 +03001726
ss412gde190682019-10-24 09:29:26 +03001727 data4Req := map[string]interface{}{"cause": "protocol:transfer-syntax-error"}
1728 b := new(bytes.Buffer)
1729 _ = json.NewEncoder(b).Encode(data4Req)
1730 req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
1731 req = mux.SetURLVars(req, map[string]string{"ranName": ranName})
1732
1733 controller.X2Reset(writer, req)
1734 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
1735
1736}
1737
1738func TestX2ResetHandleSuccessfulRequestedDefault(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001739 controller, readerMock, _, rmrMessengerMock, _, _ := setupControllerTest(t)
ss412gde190682019-10-24 09:29:26 +03001740
1741 ranName := "test1"
1742 // o&m intervention
1743 payload := []byte{0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x05, 0x40, 0x01, 0x64}
ss412gefcb4522019-12-02 16:59:19 +02001744 var xAction []byte
ns019tb3805a92020-04-13 16:57:59 +03001745 var msgSrc unsafe.Pointer
1746 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xAction, msgSrc)
ss412gefcb4522019-12-02 16:59:19 +02001747 rmrMessengerMock.On("SendMsg", msg, true).Return(msg, nil)
ss412gde190682019-10-24 09:29:26 +03001748
1749 writer := httptest.NewRecorder()
1750
1751 var nodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED}
1752 readerMock.On("GetNodeb", ranName).Return(nodeb, nil)
1753
1754 // no body
1755 b := new(bytes.Buffer)
Idan Shalom14eb5132020-07-06 09:41:46 +00001756 data4Req := map[string]interface{}{}
1757 _ = json.NewEncoder(b).Encode(data4Req)
ss412gde190682019-10-24 09:29:26 +03001758 req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
1759 req = mux.SetURLVars(req, map[string]string{"ranName": ranName})
1760
1761 controller.X2Reset(writer, req)
1762 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
ss412gde190682019-10-24 09:29:26 +03001763}
1764
Timo Tietavainen0234dab2020-11-19 09:04:22 +02001765func TestX2ResetHandleFailureBodyReadError(t *testing.T) {
1766 controller, _, _, _, _, _ := setupControllerTest(t)
1767
1768 ranName := "test1"
1769 writer := httptest.NewRecorder()
1770
1771 // Fake reader to return reading error.
1772 req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", errReader(0))
1773 req = mux.SetURLVars(req, map[string]string{"ranName": ranName})
1774
1775 controller.X2Reset(writer, req)
1776 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
1777
1778}
1779
ss412gde190682019-10-24 09:29:26 +03001780func TestX2ResetHandleFailureInvalidBody(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001781 controller, _, _, _, _, _ := setupControllerTest(t)
ss412gde190682019-10-24 09:29:26 +03001782
1783 ranName := "test1"
1784
1785 writer := httptest.NewRecorder()
1786
1787 // Invalid json: attribute name without quotes (should be "cause":).
1788 b := strings.NewReader("{cause:\"protocol:transfer-syntax-error\"")
1789 req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
1790 req = mux.SetURLVars(req, map[string]string{"ranName": ranName})
1791
1792 controller.X2Reset(writer, req)
1793 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
1794
1795}
1796
Timo Tietavainen0234dab2020-11-19 09:04:22 +02001797/*
1798func TestControllerHealthCheckRequestSuccess(t *testing.T) {
1799 controller, readerMock, _, rmrMessengerMock, _, _ := setupControllerTest(t)
1800
1801 ranName := "test1"
1802 // o&m intervention
1803 payload := []byte{0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x05, 0x40, 0x01, 0x64}
1804 var xAction []byte
1805 var msgSrc unsafe.Pointer
1806 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xAction, msgSrc)
1807 rmrMessengerMock.On("SendMsg", msg, true).Return(msg, nil)
1808
1809 writer := httptest.NewRecorder()
1810
1811 var nodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED}
1812 readerMock.On("GetNodeb", ranName).Return(nodeb, nil)
1813
1814 data4Req := map[string]interface{}{"ranList": []string{"abcd"}}
1815 b := new(bytes.Buffer)
1816 _ = json.NewEncoder(b).Encode(data4Req)
1817 req, _ := http.NewRequest("PUT", "https://localhost:3800/v1/nodeb/health", b)
1818 req = mux.SetURLVars(req, map[string]string{"ranName": ranName})
1819
1820 controller.HealthCheckRequest(writer, req)
1821 assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
1822}
1823*/
1824
ss412gde190682019-10-24 09:29:26 +03001825func TestHandleErrorResponse(t *testing.T) {
Irinaa5904172020-07-16 21:21:51 +03001826 controller, _, _, _, _, _ := setupControllerTest(t)
ss412gde190682019-10-24 09:29:26 +03001827
1828 writer := httptest.NewRecorder()
1829 controller.handleErrorResponse(e2managererrors.NewRnibDbError(), writer)
1830 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
1831
1832 writer = httptest.NewRecorder()
1833 controller.handleErrorResponse(e2managererrors.NewCommandAlreadyInProgressError(), writer)
1834 assert.Equal(t, http.StatusMethodNotAllowed, writer.Result().StatusCode)
1835
1836 writer = httptest.NewRecorder()
1837 controller.handleErrorResponse(e2managererrors.NewHeaderValidationError(), writer)
1838 assert.Equal(t, http.StatusUnsupportedMediaType, writer.Result().StatusCode)
1839
1840 writer = httptest.NewRecorder()
1841 controller.handleErrorResponse(e2managererrors.NewWrongStateError("", ""), writer)
1842 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
1843
1844 writer = httptest.NewRecorder()
1845 controller.handleErrorResponse(e2managererrors.NewRequestValidationError(), writer)
1846 assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
1847
1848 writer = httptest.NewRecorder()
1849 controller.handleErrorResponse(e2managererrors.NewRmrError(), writer)
1850 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
1851
1852 writer = httptest.NewRecorder()
Rahul Banerjife8d4812020-08-24 14:26:41 +05301853 controller.handleErrorResponse(e2managererrors.NewNoConnectedRanError(), writer)
1854 assert.Equal(t, http.StatusNotFound, writer.Result().StatusCode)
1855
1856 writer = httptest.NewRecorder()
ss412gde190682019-10-24 09:29:26 +03001857 controller.handleErrorResponse(e2managererrors.NewResourceNotFoundError(), writer)
1858 assert.Equal(t, http.StatusNotFound, writer.Result().StatusCode)
1859
1860 writer = httptest.NewRecorder()
1861 controller.handleErrorResponse(fmt.Errorf("ErrorError"), writer)
1862 assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
1863}
1864
1865func getRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
1866 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
1867 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
ss412gefcb4522019-12-02 16:59:19 +02001868 return rmrsender.NewRmrSender(log, rmrMessenger)
is005qffd085d2019-09-10 15:56:48 +03001869}
Timo Tietavainen0234dab2020-11-19 09:04:22 +02001870
1871type errReader int
1872
1873func (errReader) Read(p []byte) (n int, err error) {
1874 return 0, errors.New("test error")
1875}