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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
27 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
29 "github.com/golang/protobuf/proto"
30 "github.com/stretchr/testify/assert"
35 var namespace = "namespace"
40 func initSdlInstanceMock(namespace string) (w RNibWriter, sdlInstanceMock *mocks.MockSdlInstance) {
41 sdlInstanceMock = new(mocks.MockSdlInstance)
42 w = GetRNibWriter(sdlInstanceMock)
46 func generateNodebInfo(inventoryName string, nodeType entities.Node_Type, plmnId string, nbId string) *entities.NodebInfo {
47 nodebInfo := &entities.NodebInfo{
48 RanName: inventoryName,
49 GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId},
51 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
54 if nodeType == entities.Node_ENB {
55 nodebInfo.Configuration = &entities.NodebInfo_Enb{
58 } else if nodeType == entities.Node_GNB {
59 nodebInfo.Configuration = &entities.NodebInfo_Gnb{
67 func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell {
69 servedNrCells := []*entities.ServedNRCell{}
71 for i, v := range cellIds {
72 servedNrCells = append(servedNrCells, &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{
74 ChoiceNrMode: &entities.ServedNRCellInformation_ChoiceNRMode{
75 Fdd: &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{
79 NrMode: entities.Nr_FDD,
81 ServedPlmns: []string{"whatever"},
88 func TestRemoveServedNrCellsSuccess(t *testing.T) {
89 w, sdlInstanceMock := initSdlInstanceMock(namespace)
90 servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2")
91 sdlInstanceMock.On("Remove", buildCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(nil)
92 err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove)
96 func TestRemoveServedNrCellsFailure(t *testing.T) {
97 w, sdlInstanceMock := initSdlInstanceMock(namespace)
98 servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2")
99 sdlInstanceMock.On("Remove", buildCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(errors.New("expected error"))
100 err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove)
101 assert.IsType(t, &common.InternalError{}, err)
104 func TestUpdateGnbCellsInvalidNodebInfoFailure(t *testing.T) {
105 w, sdlInstanceMock := initSdlInstanceMock(namespace)
106 servedNrCells := generateServedNrCells("test1", "test2")
107 nodebInfo := &entities.NodebInfo{}
108 sdlInstanceMock.AssertNotCalled(t, "Set")
109 rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
110 assert.IsType(t, &common.ValidationError{}, rNibErr)
113 func TestUpdateGnbCellsInvalidCellFailure(t *testing.T) {
114 inventoryName := "name"
117 w, sdlInstanceMock := initSdlInstanceMock(namespace)
118 servedNrCells := []*entities.ServedNRCell{{ServedNrCellInformation: &entities.ServedNRCellInformation{}}}
119 nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
120 nodebInfo.GetGnb().ServedNrCells = servedNrCells
121 sdlInstanceMock.AssertNotCalled(t, "Set")
122 rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
123 assert.IsType(t, &common.ValidationError{}, rNibErr)
126 func getUpdateGnbCellsSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) []interface{} {
128 nodebInfoData, err := proto.Marshal(nodebInfo)
130 t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal NodeB entity. Error: %s", err)
133 nodebNameKey, _ := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
134 nodebIdKey, _ := common.ValidateAndBuildNodeBIdKey(nodebInfo.NodeType.String(), nodebInfo.GlobalNbId.PlmnId, nodebInfo.GlobalNbId.NbId)
135 setExpected := []interface{}{nodebNameKey, nodebInfoData, nodebIdKey, nodebInfoData}
137 for _, v := range servedNrCells {
139 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: v}}
140 cellData, err := proto.Marshal(&cellEntity)
143 t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal cell entity. Error: %s", err)
146 nrCellIdKey, _ := common.ValidateAndBuildNrCellIdKey(v.GetServedNrCellInformation().GetCellId())
147 cellNamePciKey, _ := common.ValidateAndBuildCellNamePciKey(nodebInfo.RanName, v.GetServedNrCellInformation().GetNrPci())
148 setExpected = append(setExpected, nrCellIdKey, cellData, cellNamePciKey, cellData)
154 func TestUpdateGnbCellsSdlFailure(t *testing.T) {
155 inventoryName := "name"
158 w, sdlInstanceMock := initSdlInstanceMock(namespace)
159 servedNrCells := generateServedNrCells("test1", "test2")
160 nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
161 nodebInfo.GetGnb().ServedNrCells = servedNrCells
162 setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
163 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(errors.New("expected error"))
164 rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
165 assert.IsType(t, &common.InternalError{}, rNibErr)
168 func TestUpdateGnbCellsSuccess(t *testing.T) {
169 inventoryName := "name"
172 w, sdlInstanceMock := initSdlInstanceMock(namespace)
173 servedNrCells := generateServedNrCells("test1", "test2")
174 nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
175 nodebInfo.GetGnb().ServedNrCells = servedNrCells
176 setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
178 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
179 rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
180 assert.Nil(t, rNibErr)
183 func TestUpdateNodebInfoSuccess(t *testing.T) {
184 inventoryName := "name"
187 w, sdlInstanceMock := initSdlInstanceMock(namespace)
188 nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
189 data, err := proto.Marshal(nodebInfo)
191 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
194 var setExpected []interface{}
196 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
197 nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
198 setExpected = append(setExpected, nodebNameKey, data)
199 setExpected = append(setExpected, nodebIdKey, data)
201 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
203 rNibErr := w.UpdateNodebInfo(nodebInfo)
204 assert.Nil(t, rNibErr)
207 func TestUpdateNodebInfoMissingInventoryNameFailure(t *testing.T) {
208 inventoryName := "name"
211 w, sdlInstanceMock := initSdlInstanceMock(namespace)
212 nodebInfo := &entities.NodebInfo{}
213 data, err := proto.Marshal(nodebInfo)
215 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
218 var setExpected []interface{}
220 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
221 nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
222 setExpected = append(setExpected, nodebNameKey, data)
223 setExpected = append(setExpected, nodebIdKey, data)
225 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
227 rNibErr := w.UpdateNodebInfo(nodebInfo)
229 assert.NotNil(t, rNibErr)
230 assert.IsType(t, &common.ValidationError{}, rNibErr)
233 func TestUpdateNodebInfoMissingGlobalNbId(t *testing.T) {
234 inventoryName := "name"
235 w, sdlInstanceMock := initSdlInstanceMock(namespace)
236 nodebInfo := &entities.NodebInfo{}
237 nodebInfo.RanName = inventoryName
238 data, err := proto.Marshal(nodebInfo)
240 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
243 var setExpected []interface{}
245 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
246 setExpected = append(setExpected, nodebNameKey, data)
247 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
249 rNibErr := w.UpdateNodebInfo(nodebInfo)
251 assert.Nil(t, rNibErr)
254 func TestSaveEnb(t *testing.T) {
256 ranName := "RAN:" + name
257 w, sdlInstanceMock := initSdlInstanceMock(namespace)
258 nb := entities.NodebInfo{}
259 nb.NodeType = entities.Node_ENB
260 nb.ConnectionStatus = 1
263 enb := entities.Enb{}
264 cell := &entities.ServedCellInfo{CellId: "aaff", Pci: 3}
265 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
266 enb.ServedCells = []*entities.ServedCellInfo{cell}
267 nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
268 data, err := proto.Marshal(&nb)
270 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
274 cellData, err := proto.Marshal(&cellEntity)
276 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal Cell entity. Error: %v", err)
278 var setExpected []interface{}
279 setExpected = append(setExpected, ranName, data)
280 setExpected = append(setExpected, "ENB:02f829:4a952a0a", data)
281 setExpected = append(setExpected, fmt.Sprintf("CELL:%s", cell.GetCellId()), cellData)
282 setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetPci()), cellData)
284 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
286 nbIdData, err := proto.Marshal(&entities.NbIdentity{InventoryName: name})
288 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
290 sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
292 nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
293 nbIdData, err = proto.Marshal(nbIdentity)
295 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB Identity entity. Error: %v", err)
297 sdlInstanceMock.On("AddMember", "ENB", []interface{}{nbIdData}).Return(e)
299 rNibErr := w.SaveNodeb(nbIdentity, &nb)
300 assert.Nil(t, rNibErr)
303 func TestSaveEnbCellIdValidationFailure(t *testing.T) {
305 w, _ := initSdlInstanceMock(namespace)
306 nb := entities.NodebInfo{}
307 nb.NodeType = entities.Node_ENB
308 nb.ConnectionStatus = 1
311 enb := entities.Enb{}
312 cell := &entities.ServedCellInfo{Pci: 3}
313 enb.ServedCells = []*entities.ServedCellInfo{cell}
314 nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
316 nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
317 rNibErr := w.SaveNodeb(nbIdentity, &nb)
318 assert.NotNil(t, rNibErr)
319 assert.IsType(t, &common.ValidationError{}, rNibErr)
320 assert.Equal(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", rNibErr.Error())
323 func TestSaveEnbInventoryNameValidationFailure(t *testing.T) {
324 w, _ := initSdlInstanceMock(namespace)
325 nb := entities.NodebInfo{}
326 nb.NodeType = entities.Node_ENB
327 nb.ConnectionStatus = 1
330 enb := entities.Enb{}
331 cell := &entities.ServedCellInfo{CellId: "aaa", Pci: 3}
332 enb.ServedCells = []*entities.ServedCellInfo{cell}
333 nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
335 nbIdentity := &entities.NbIdentity{InventoryName: "", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
336 rNibErr := w.SaveNodeb(nbIdentity, &nb)
337 assert.NotNil(t, rNibErr)
338 assert.IsType(t, &common.ValidationError{}, rNibErr)
339 assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error())
342 func TestSaveGnbCellIdValidationFailure(t *testing.T) {
344 w, _ := initSdlInstanceMock(namespace)
345 nb := entities.NodebInfo{}
346 nb.NodeType = entities.Node_GNB
347 nb.ConnectionStatus = 1
350 gnb := entities.Gnb{}
351 cellInfo := &entities.ServedNRCellInformation{NrPci: 2}
352 cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
353 gnb.ServedNrCells = []*entities.ServedNRCell{cell}
354 nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
356 nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
357 rNibErr := w.SaveNodeb(nbIdentity, &nb)
358 assert.NotNil(t, rNibErr)
359 assert.IsType(t, &common.ValidationError{}, rNibErr)
360 assert.Equal(t, "#utils.ValidateAndBuildNrCellIdKey - an empty cell id received", rNibErr.Error())
363 func TestSaveGnb(t *testing.T) {
365 ranName := "RAN:" + name
366 w, sdlInstanceMock := initSdlInstanceMock(namespace)
367 nb := entities.NodebInfo{}
368 nb.NodeType = entities.Node_GNB
369 nb.ConnectionStatus = 1
372 gnb := entities.Gnb{}
373 cellInfo := &entities.ServedNRCellInformation{NrPci: 2, CellId: "ccdd"}
374 cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
375 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
376 gnb.ServedNrCells = []*entities.ServedNRCell{cell}
377 nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
378 data, err := proto.Marshal(&nb)
380 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB entity. Error: %v", err)
384 cellData, err := proto.Marshal(&cellEntity)
386 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal Cell entity. Error: %v", err)
388 var setExpected []interface{}
389 setExpected = append(setExpected, ranName, data)
390 setExpected = append(setExpected, "GNB:02f829:4a952a0a", data)
391 setExpected = append(setExpected, fmt.Sprintf("NRCELL:%s", cell.GetServedNrCellInformation().GetCellId()), cellData)
392 setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetServedNrCellInformation().GetNrPci()), cellData)
394 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
395 nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
396 nbIdData, err := proto.Marshal(nbIdentity)
398 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB Identity entity. Error: %v", err)
400 sdlInstanceMock.On("AddMember", "GNB", []interface{}{nbIdData}).Return(e)
402 nbIdData, err = proto.Marshal(&entities.NbIdentity{InventoryName: name})
404 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
406 sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
408 rNibErr := w.SaveNodeb(nbIdentity, &nb)
409 assert.Nil(t, rNibErr)
412 func TestSaveRanLoadInformationSuccess(t *testing.T) {
413 inventoryName := "name"
414 loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
416 if validationErr != nil {
417 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
420 w, sdlInstanceMock := initSdlInstanceMock(namespace)
422 ranLoadInformation := generateRanLoadInformation()
423 data, err := proto.Marshal(ranLoadInformation)
426 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
430 var setExpected []interface{}
431 setExpected = append(setExpected, loadKey, data)
432 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
434 rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
435 assert.Nil(t, rNibErr)
438 func TestSaveRanLoadInformationMarshalNilFailure(t *testing.T) {
439 inventoryName := "name2"
440 w, _ := initSdlInstanceMock(namespace)
442 expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
443 err := w.SaveRanLoadInformation(inventoryName, nil)
444 assert.Equal(t, expectedErr, err)
447 func TestSaveRanLoadInformationEmptyInventoryNameFailure(t *testing.T) {
449 w, _ := initSdlInstanceMock(namespace)
451 err := w.SaveRanLoadInformation(inventoryName, nil)
452 assert.NotNil(t, err)
453 assert.IsType(t, &common.ValidationError{}, err)
456 func TestSaveRanLoadInformationSdlFailure(t *testing.T) {
457 inventoryName := "name2"
459 loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
461 if validationErr != nil {
462 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
465 w, sdlInstanceMock := initSdlInstanceMock(namespace)
467 ranLoadInformation := generateRanLoadInformation()
468 data, err := proto.Marshal(ranLoadInformation)
471 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
474 expectedErr := errors.New("expected error")
475 var setExpected []interface{}
476 setExpected = append(setExpected, loadKey, data)
477 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
479 rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
480 assert.NotNil(t, rNibErr)
481 assert.IsType(t, &common.InternalError{}, rNibErr)
484 func generateCellLoadInformation() *entities.CellLoadInformation {
485 cellLoadInformation := entities.CellLoadInformation{}
487 cellLoadInformation.CellId = "123"
489 ulInterferenceOverloadIndication := entities.UlInterferenceOverloadIndication_HIGH_INTERFERENCE
490 cellLoadInformation.UlInterferenceOverloadIndications = []entities.UlInterferenceOverloadIndication{ulInterferenceOverloadIndication}
492 ulHighInterferenceInformation := entities.UlHighInterferenceInformation{
494 UlHighInterferenceIndication: "xxx",
497 cellLoadInformation.UlHighInterferenceInfos = []*entities.UlHighInterferenceInformation{&ulHighInterferenceInformation}
499 cellLoadInformation.RelativeNarrowbandTxPower = &entities.RelativeNarrowbandTxPower{
501 RntpThreshold: entities.RntpThreshold_NEG_4,
502 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V1_ANT_PRT,
504 PdcchInterferenceImpact: 2,
505 EnhancedRntp: &entities.EnhancedRntp{
506 EnhancedRntpBitmap: "xxx",
507 RntpHighPowerThreshold: entities.RntpThreshold_NEG_2,
508 EnhancedRntpStartTime: &entities.StartTime{StartSfn: 500, StartSubframeNumber: 5},
512 cellLoadInformation.AbsInformation = &entities.AbsInformation{
513 Mode: entities.AbsInformationMode_ABS_INFO_FDD,
514 AbsPatternInfo: "xxx",
515 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V2_ANT_PRT,
516 MeasurementSubset: "xxx",
519 cellLoadInformation.InvokeIndication = entities.InvokeIndication_ABS_INFORMATION
521 cellLoadInformation.ExtendedUlInterferenceOverloadInfo = &entities.ExtendedUlInterferenceOverloadInfo{
522 AssociatedSubframes: "xxx",
523 ExtendedUlInterferenceOverloadIndications: cellLoadInformation.UlInterferenceOverloadIndications,
526 compInformationItem := &entities.CompInformationItem{
527 CompHypothesisSets: []*entities.CompHypothesisSet{{CellId: "789", CompHypothesis: "xxx"}},
531 cellLoadInformation.CompInformation = &entities.CompInformation{
532 CompInformationItems: []*entities.CompInformationItem{compInformationItem},
533 CompInformationStartTime: &entities.StartTime{StartSfn: 123, StartSubframeNumber: 456},
536 cellLoadInformation.DynamicDlTransmissionInformation = &entities.DynamicDlTransmissionInformation{
537 State: entities.NaicsState_NAICS_ACTIVE,
538 TransmissionModes: "xxx",
540 PAList: []entities.PA{entities.PA_DB_NEG_3},
543 return &cellLoadInformation
546 func generateRanLoadInformation() *entities.RanLoadInformation {
547 ranLoadInformation := entities.RanLoadInformation{}
549 ranLoadInformation.LoadTimestamp = uint64(time.Now().UnixNano())
551 cellLoadInformation := generateCellLoadInformation()
552 ranLoadInformation.CellLoadInfos = []*entities.CellLoadInformation{cellLoadInformation}
554 return &ranLoadInformation
557 func TestSaveNilEntityFailure(t *testing.T) {
558 w, _ := initSdlInstanceMock(namespace)
559 expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
560 nbIdentity := &entities.NbIdentity{}
561 actualErr := w.SaveNodeb(nbIdentity, nil)
562 assert.Equal(t, expectedErr, actualErr)
565 func TestSaveUnknownTypeEntityFailure(t *testing.T) {
566 w, _ := initSdlInstanceMock(namespace)
567 nbIdentity := &entities.NbIdentity{InventoryName: "name", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
568 nb := &entities.NodebInfo{}
571 actualErr := w.SaveNodeb(nbIdentity, nb)
572 assert.IsType(t, &common.ValidationError{}, actualErr)
575 func TestSaveEntityFailure(t *testing.T) {
580 w, sdlInstanceMock := initSdlInstanceMock(namespace)
581 gnb := entities.NodebInfo{}
582 gnb.NodeType = entities.Node_GNB
583 data, err := proto.Marshal(&gnb)
585 t.Errorf("#rNibWriter_test.TestSaveEntityFailure - Failed to marshal NodeB entity. Error: %v", err)
587 nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}}
588 setExpected := []interface{}{"RAN:" + name, data}
589 setExpected = append(setExpected, "GNB:"+plmnId+":"+nbId, data)
590 expectedErr := errors.New("expected error")
591 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
592 rNibErr := w.SaveNodeb(nbIdentity, &gnb)
593 assert.NotEmpty(t, rNibErr)
596 func TestGetRNibWriter(t *testing.T) {
597 received, _ := initSdlInstanceMock(namespace)
598 assert.NotEmpty(t, received)
601 func TestSaveE2TInstanceSuccess(t *testing.T) {
602 address := "10.10.2.15:9800"
603 loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
605 if validationErr != nil {
606 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to build E2T Instance key. Error: %v", validationErr)
609 w, sdlInstanceMock := initSdlInstanceMock(namespace)
611 e2tInstance := generateE2tInstance(address)
612 data, err := json.Marshal(e2tInstance)
615 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to marshal E2tInstance entity. Error: %v", err)
619 var setExpected []interface{}
620 setExpected = append(setExpected, loadKey, data)
621 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
623 rNibErr := w.SaveE2TInstance(e2tInstance)
624 assert.Nil(t, rNibErr)
627 func TestSaveE2TInstanceNullE2tInstanceFailure(t *testing.T) {
628 w, _ := initSdlInstanceMock(namespace)
630 e2tInstance := entities.NewE2TInstance(address, "test")
631 err := w.SaveE2TInstance(e2tInstance)
632 assert.NotNil(t, err)
633 assert.IsType(t, &common.ValidationError{}, err)
636 func TestSaveE2TInstanceSdlFailure(t *testing.T) {
637 address := "10.10.2.15:9800"
638 loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
640 if validationErr != nil {
641 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to build E2T Instance key. Error: %v", validationErr)
644 w, sdlInstanceMock := initSdlInstanceMock(namespace)
646 e2tInstance := generateE2tInstance(address)
647 data, err := json.Marshal(e2tInstance)
650 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to marshal E2tInstance entity. Error: %v", err)
653 expectedErr := errors.New("expected error")
654 var setExpected []interface{}
655 setExpected = append(setExpected, loadKey, data)
656 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
658 rNibErr := w.SaveE2TInstance(e2tInstance)
659 assert.NotNil(t, rNibErr)
660 assert.IsType(t, &common.InternalError{}, rNibErr)
663 func generateE2tInstance(address string) *entities.E2TInstance {
664 e2tInstance := entities.NewE2TInstance(address, "pod test")
666 e2tInstance.AssociatedRanList = []string{"test1", "test2"}
671 func TestSaveE2TAddressesSuccess(t *testing.T) {
672 address := "10.10.2.15:9800"
673 w, sdlInstanceMock := initSdlInstanceMock(namespace)
675 e2tAddresses := []string{address}
676 data, err := json.Marshal(e2tAddresses)
679 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSuccess - Failed to marshal E2TInfoList. Error: %v", err)
683 var setExpected []interface{}
684 setExpected = append(setExpected, E2TAddressesKey, data)
685 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
687 rNibErr := w.SaveE2TAddresses(e2tAddresses)
688 assert.Nil(t, rNibErr)
691 func TestSaveE2TAddressesSdlFailure(t *testing.T) {
692 address := "10.10.2.15:9800"
693 w, sdlInstanceMock := initSdlInstanceMock(namespace)
695 e2tAddresses := []string{address}
696 data, err := json.Marshal(e2tAddresses)
699 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSdlFailure - Failed to marshal E2TInfoList. Error: %v", err)
702 expectedErr := errors.New("expected error")
703 var setExpected []interface{}
704 setExpected = append(setExpected, E2TAddressesKey, data)
705 sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
707 rNibErr := w.SaveE2TAddresses(e2tAddresses)
708 assert.NotNil(t, rNibErr)
709 assert.IsType(t, &common.InternalError{}, rNibErr)
712 func TestRemoveE2TInstanceSuccess(t *testing.T) {
713 address := "10.10.2.15:9800"
714 w, sdlInstanceMock := initSdlInstanceMock(namespace)
716 e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
718 sdlInstanceMock.On("Remove", e2tAddresses).Return(e)
720 rNibErr := w.RemoveE2TInstance(address)
721 assert.Nil(t, rNibErr)
722 sdlInstanceMock.AssertExpectations(t)
725 func TestRemoveE2TInstanceSdlFailure(t *testing.T) {
726 address := "10.10.2.15:9800"
727 w, sdlInstanceMock := initSdlInstanceMock(namespace)
729 e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
730 expectedErr := errors.New("expected error")
731 sdlInstanceMock.On("Remove", e2tAddresses).Return(expectedErr)
733 rNibErr := w.RemoveE2TInstance(address)
734 assert.IsType(t, &common.InternalError{}, rNibErr)
737 func TestRemoveE2TInstanceEmptyAddressFailure(t *testing.T) {
738 w, sdlInstanceMock := initSdlInstanceMock(namespace)
740 rNibErr := w.RemoveE2TInstance("")
741 assert.IsType(t, &common.ValidationError{}, rNibErr)
742 sdlInstanceMock.AssertExpectations(t)
745 func TestUpdateNodebInfoOnConnectionStatusInversionSuccess(t *testing.T) {
746 inventoryName := "name"
749 channelName := "RAN_CONNECT_STATE_CHANGE"
750 eventName := inventoryName + "_" + "CONNECTED"
751 w, sdlInstanceMock := initSdlInstanceMock(namespace)
752 nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
753 data, err := proto.Marshal(nodebInfo)
755 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionSuccess - Failed to marshal NodeB entity. Error: %v", err)
758 var setExpected []interface{}
760 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
761 nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
762 setExpected = append(setExpected, nodebNameKey, data)
763 setExpected = append(setExpected, nodebIdKey, data)
765 sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
767 rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
768 assert.Nil(t, rNibErr)
771 func TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure(t *testing.T) {
772 inventoryName := "name"
775 channelName := "RAN_CONNECT_STATE_CHANGE"
776 eventName := inventoryName + "_" + "CONNECTED"
777 w, sdlInstanceMock := initSdlInstanceMock(namespace)
778 nodebInfo := &entities.NodebInfo{}
779 data, err := proto.Marshal(nodebInfo)
781 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
784 var setExpected []interface{}
786 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
787 nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
788 setExpected = append(setExpected, nodebNameKey, data)
789 setExpected = append(setExpected, nodebIdKey, data)
791 sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
793 rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
795 assert.NotNil(t, rNibErr)
796 assert.IsType(t, &common.ValidationError{}, rNibErr)
799 func TestUpdateNodebInfoOnConnectionStatusInversionMissingGlobalNbId(t *testing.T) {
800 inventoryName := "name"
801 channelName := "RAN_CONNECT_STATE_CHANGE"
802 eventName := inventoryName + "_" + "CONNECTED"
803 w, sdlInstanceMock := initSdlInstanceMock(namespace)
804 nodebInfo := &entities.NodebInfo{}
805 nodebInfo.RanName = inventoryName
806 data, err := proto.Marshal(nodebInfo)
808 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
811 var setExpected []interface{}
813 nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
814 setExpected = append(setExpected, nodebNameKey, data)
815 sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
817 rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
819 assert.Nil(t, rNibErr)
822 func TestSaveGeneralConfiguration(t *testing.T) {
823 w, sdlInstanceMock := initSdlInstanceMock(namespace)
825 key := common.BuildGeneralConfigurationKey()
826 configurationData := "{\"enableRic\":true}"
827 configuration := &entities.GeneralConfiguration{}
828 configuration.EnableRic = true
830 sdlInstanceMock.On("Set",[]interface{}{[]interface{}{key, []byte(configurationData)}}).Return(nil)
831 rNibErr := w.SaveGeneralConfiguration(configuration)
833 assert.Nil(t, rNibErr)
834 sdlInstanceMock.AssertExpectations(t)
837 func TestSaveGeneralConfigurationDbError(t *testing.T) {
838 w, sdlInstanceMock := initSdlInstanceMock(namespace)
840 key := common.BuildGeneralConfigurationKey()
841 configurationData := "{\"enableRic\":true}"
842 configuration := &entities.GeneralConfiguration{}
843 configuration.EnableRic = true
845 expectedErr := errors.New("expected error")
847 sdlInstanceMock.On("Set",[]interface{}{[]interface{}{key, []byte(configurationData)}}).Return(expectedErr)
848 rNibErr := w.SaveGeneralConfiguration(configuration)
850 assert.NotNil(t, rNibErr)
855 //func TestSaveEnbGnbInteg(t *testing.T){
856 // for i := 0; i<10; i++{
857 // Init("e2Manager", 1)
858 // w := GetRNibWriter()
859 // nb := entities.NodebInfo{}
860 // nb.NodeType = entities.Node_ENB
861 // nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED
862 // nb.Ip = "localhost"
863 // nb.Port = uint32(5656 + i)
864 // enb := entities.Enb{}
865 // cell1 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",111 + i), Pci:uint32(11 + i)}
866 // cell2 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",222 + i), Pci:uint32(22 + i)}
867 // cell3 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",333 + i), Pci:uint32(33 + i)}
868 // enb.ServedCells = []*entities.ServedCellInfo{cell1, cell2, cell3}
869 // nb.Configuration = &entities.NodebInfo_Enb{Enb:&enb}
870 // plmnId := 0x02f828
871 // nbId := 0x4a952a0a
872 // nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameEnb%d" ,i), GlobalNbId:&entities.GlobalNbId{RicId:fmt.Sprintf("%02x", plmnId + i), NbId:fmt.Sprintf("%02x", nbId + i)}}
873 // err := w.SaveNodeb(nbIdentity, &nb)
875 // t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
878 // nb1 := entities.NodebInfo{}
879 // nb1.NodeType = entities.Node_GNB
880 // nb1.ConnectionStatus = entities.ConnectionStatus_CONNECTED
881 // nb1.Ip = "localhost"
882 // nb1.Port = uint32(6565 + i)
883 // gnb := entities.Gnb{}
884 // gCell1 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",1111 + i), NrPci:uint32(1 + i)}}
885 // gCell2 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",2222 + i), NrPci:uint32(2 + i)}}
886 // gCell3 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",3333 + i), NrPci:uint32(3 + i)}}
887 // gnb.ServedNrCells = []*entities.ServedNRCell{gCell1, gCell2, gCell3,}
888 // nb1.Configuration = &entities.NodebInfo_Gnb{Gnb:&gnb}
889 // nbIdentity = &entities.NbIdentity{InventoryName: fmt.Sprintf("nameGnb%d" ,i), GlobalNbId:&entities.GlobalNbId{RicId:fmt.Sprintf("%02x", plmnId - i), NbId:fmt.Sprintf("%02x", nbId - i)}}
890 // err = w.SaveNodeb(nbIdentity, &nb1)
892 // t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
897 //func TestSaveNbRanNamesInteg(t *testing.T){
898 // for i := 0; i<10; i++{
899 // Init("e2Manager", 1)
900 // w := GetRNibWriter()
901 // nb := entities.NodebInfo{}
902 // nb.ConnectionStatus = entities.ConnectionStatus_CONNECTING
903 // nb.Ip = "localhost"
904 // nb.Port = uint32(5656 + i)
905 // nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameOnly%d" ,i)}
906 // err := w.SaveNodeb(nbIdentity, &nb)
908 // t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
913 //func TestSaveRanLoadInformationInteg(t *testing.T){
914 // Init("e2Manager", 1)
915 // w := GetRNibWriter()
916 // ranLoadInformation := generateRanLoadInformation()
917 // err := w.SaveRanLoadInformation("ran_integ", ranLoadInformation)
919 // t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationInteg - Failed to save RanLoadInformation entity. Error: %v", err)