RIC-11565:Add support for Multiple E2 Nodes: CU/DU for the case having same GNBId
[ric-plt/e2mgr.git] / E2Manager / rNibWriter / rNibWriter_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 // Copyright 2023 Capgemini
5 //
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.
17
18 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 //  platform project (RICP).
20
21 package rNibWriter
22
23 import (
24         "e2mgr/configuration"
25         "e2mgr/mocks"
26         "encoding/json"
27         "errors"
28         "fmt"
29         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
30         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
31         "github.com/golang/protobuf/proto"
32         "github.com/stretchr/testify/assert"
33         "testing"
34         "time"
35 )
36
37 var namespace = common.GetRNibNamespace()
38
39 const (
40         RanName = "test"
41         PlmnId  = "02f829"
42         NbId    = "4a952a0a"
43 )
44
45 func initSdlMock() (w RNibWriter, sdlMock *mocks.MockSdlSyncStorage) {
46         sdlMock = new(mocks.MockSdlSyncStorage)
47         w = GetRNibWriter(sdlMock, configuration.RnibWriterConfig{StateChangeMessageChannel: "RAN_CONNECTION_STATUS_CHANGE", RanManipulationMessageChannel: "RAN_MANIPULATION"})
48         return
49 }
50
51 func generateNodebInfo(inventoryName string, nodeType entities.Node_Type, plmnId string, nbId string) *entities.NodebInfo {
52         nodebInfo := &entities.NodebInfo{
53                 RanName:          inventoryName,
54                 GlobalNbId:       &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId},
55                 NodeType:         nodeType,
56                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
57         }
58
59         if nodeType == entities.Node_ENB {
60                 nodebInfo.Configuration = &entities.NodebInfo_Enb{
61                         Enb: &entities.Enb{},
62                 }
63         } else if nodeType == entities.Node_GNB {
64                 nodebInfo.Configuration = &entities.NodebInfo_Gnb{
65                         Gnb: &entities.Gnb{},
66                 }
67         }
68
69         return nodebInfo
70 }
71
72 func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell {
73
74         var servedNrCells []*entities.ServedNRCell
75
76         for i, v := range cellIds {
77                 servedNrCells = append(servedNrCells, &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{
78                         CellId: v,
79                         ChoiceNrMode: &entities.ServedNRCellInformation_ChoiceNRMode{
80                                 Fdd: &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{},
81                         },
82                         NrMode:      entities.Nr_FDD,
83                         NrPci:       uint32(i + 1),
84                         ServedPlmns: []string{"whatever"},
85                 }})
86         }
87
88         return servedNrCells
89 }
90
91 func generateServedCells(cellIds ...string) []*entities.ServedCellInfo {
92
93         var servedCells []*entities.ServedCellInfo
94
95         for i, v := range cellIds {
96                 servedCells = append(servedCells, &entities.ServedCellInfo{
97                         CellId: v,
98                         ChoiceEutraMode: &entities.ChoiceEUTRAMode{
99                                 Fdd: &entities.FddInfo{},
100                         },
101                         Pci:            uint32(i + 1),
102                         BroadcastPlmns: []string{"whatever"},
103                 })
104         }
105
106         return servedCells
107 }
108
109 func generateServedCellInfos(cellIds ...string) []*entities.ServedCellInfo {
110
111         servedCells := []*entities.ServedCellInfo{}
112
113         for i, v := range cellIds {
114                 servedCells = append(servedCells, &entities.ServedCellInfo{
115                         CellId: v,
116                         Pci:    uint32(i + 1),
117                 })
118         }
119
120         return servedCells
121 }
122
123 func TestRemoveServedNrCellsSuccess(t *testing.T) {
124         w, sdlMock := initSdlMock()
125         servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2")
126         sdlMock.On("Remove", namespace, buildServedNRCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(nil)
127         err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove)
128         assert.Nil(t, err)
129 }
130
131 func TestRemoveServedNrCellsFailure(t *testing.T) {
132         w, sdlMock := initSdlMock()
133         servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2")
134         sdlMock.On("Remove", namespace, buildServedNRCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(errors.New("expected error"))
135         err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove)
136         assert.IsType(t, &common.InternalError{}, err)
137 }
138
139 func TestUpdateGnbCellsInvalidNodebInfoFailure(t *testing.T) {
140         w, sdlMock := initSdlMock()
141         servedNrCells := generateServedNrCells("test1", "test2")
142         nodebInfo := &entities.NodebInfo{}
143         sdlMock.AssertNotCalled(t, "SetAndPublish")
144         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
145         assert.IsType(t, &common.ValidationError{}, rNibErr)
146 }
147
148 func TestAddNbIdentitySuccess(t *testing.T) {
149         w, sdlMock := initSdlMock()
150
151         nbIdentity := &entities.NbIdentity{InventoryName: RanName, GlobalNbId: &entities.GlobalNbId{PlmnId: PlmnId, NbId: NbId}}
152         nbIdData, err := proto.Marshal(nbIdentity)
153         if err != nil {
154                 t.Fatalf("#rNibWriter_test.TestAddNbIdentitySuccess - Failed to marshal NodeB Identity entity. Error: %v", err)
155         }
156
157         sdlMock.On("AddMember", namespace, "ENB", []interface{}{nbIdData}).Return(nil)
158         rNibErr := w.AddNbIdentity(entities.Node_ENB, nbIdentity)
159         assert.Nil(t, rNibErr)
160 }
161
162 func TestAddNbIdentityMarshalNilFailure(t *testing.T) {
163         w, _ := initSdlMock()
164
165         rNibErr := w.AddNbIdentity(entities.Node_ENB, nil)
166         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
167         assert.Equal(t, expectedErr, rNibErr)
168 }
169
170 func TestAddNbIdentitySdlFailure(t *testing.T) {
171         w, sdlMock := initSdlMock()
172
173         nbIdentity := &entities.NbIdentity{InventoryName: RanName, GlobalNbId: &entities.GlobalNbId{PlmnId: PlmnId, NbId: NbId}}
174         nbIdData, err := proto.Marshal(nbIdentity)
175         if err != nil {
176                 t.Fatalf("#rNibWriter_test.TestAddNbIdentitySdlFailure - Failed to marshal NodeB Identity entity. Error: %v", err)
177         }
178
179         sdlMock.On("AddMember", namespace, "ENB", []interface{}{nbIdData}).Return(errors.New("expected error"))
180         rNibErr := w.AddNbIdentity(entities.Node_ENB, nbIdentity)
181         assert.IsType(t, &common.InternalError{}, rNibErr)
182 }
183
184 func TestUpdateGnbCellsInvalidCellFailure(t *testing.T) {
185         inventoryName := "name"
186         plmnId := "02f829"
187         nbId := "4a952a0a"
188         w, sdlMock := initSdlMock()
189         servedNrCells := []*entities.ServedNRCell{{ServedNrCellInformation: &entities.ServedNRCellInformation{}}}
190         nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
191         nodebInfo.GetGnb().ServedNrCells = servedNrCells
192         sdlMock.AssertNotCalled(t, "SetAndPublish")
193         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
194         assert.IsType(t, &common.ValidationError{}, rNibErr)
195 }
196
197 func getUpdateEnbCellsSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedCells []*entities.ServedCellInfo) []interface{} {
198
199         nodebInfoData, err := proto.Marshal(nodebInfo)
200         if err != nil {
201                 t.Fatalf("#rNibWriter_test.getUpdateEnbCellsSetExpected - Failed to marshal NodeB entity. Error: %s", err)
202         }
203
204         nodebNameKey, _ := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
205         nodebIdKey, _ := common.ValidateAndBuildNodeBIdKey(nodebInfo.NodeType.String(), nodebInfo.GlobalNbId.PlmnId, nodebInfo.GlobalNbId.NbId,nodebInfo.CuUpId,nodebInfo.DuId)
206         setExpected := []interface{}{nodebNameKey, nodebInfoData, nodebIdKey, nodebInfoData}
207
208         for _, cell := range servedCells {
209
210                 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
211                 cellData, err := proto.Marshal(&cellEntity)
212
213                 if err != nil {
214                         t.Fatalf("#rNibWriter_test.getUpdateEnbCellsSetExpected - Failed to marshal cell entity. Error: %s", err)
215                 }
216
217                 nrCellIdKey, _ := common.ValidateAndBuildCellIdKey(cell.GetCellId())
218                 cellNamePciKey, _ := common.ValidateAndBuildCellNamePciKey(nodebInfo.RanName, cell.GetPci())
219                 setExpected = append(setExpected, nrCellIdKey, cellData, cellNamePciKey, cellData)
220         }
221
222         return setExpected
223 }
224
225 func getUpdateGnbCellsSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) []interface{} {
226
227         nodebInfoData, err := proto.Marshal(nodebInfo)
228         if err != nil {
229                 t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal NodeB entity. Error: %s", err)
230         }
231
232         nodebNameKey, _ := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
233         nodebIdKey, _ := common.ValidateAndBuildNodeBIdKey(nodebInfo.NodeType.String(), nodebInfo.GlobalNbId.PlmnId, nodebInfo.GlobalNbId.NbId,nodebInfo.CuUpId,nodebInfo.DuId)
234         setExpected := []interface{}{nodebNameKey, nodebInfoData, nodebIdKey, nodebInfoData}
235
236         for _, v := range servedNrCells {
237
238                 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: v}}
239                 cellData, err := proto.Marshal(&cellEntity)
240
241                 if err != nil {
242                         t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal cell entity. Error: %s", err)
243                 }
244
245                 nrCellIdKey, _ := common.ValidateAndBuildNrCellIdKey(v.GetServedNrCellInformation().GetCellId())
246                 cellNamePciKey, _ := common.ValidateAndBuildCellNamePciKey(nodebInfo.RanName, v.GetServedNrCellInformation().GetNrPci())
247                 setExpected = append(setExpected, nrCellIdKey, cellData, cellNamePciKey, cellData)
248         }
249
250         return setExpected
251 }
252
253 func TestUpdateGnbCellsSdlFailure(t *testing.T) {
254         inventoryName := "name"
255         plmnId := "02f829"
256         nbId := "4a952a0a"
257         w, sdlMock := initSdlMock()
258         servedNrCells := generateServedNrCells("test1", "test2")
259         nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
260         nodebInfo.GetGnb().ServedNrCells = servedNrCells
261         setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
262         sdlMock.On("SetAndPublish", namespace, []string{"RAN_MANIPULATION", inventoryName + "_" + RanUpdatedEvent}, []interface{}{setExpected}).Return(errors.New("expected error"))
263         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
264         assert.IsType(t, &common.InternalError{}, rNibErr)
265 }
266
267 func TestUpdateGnbCellsRnibKeyValidationError(t *testing.T) {
268         //Empty RAN name fails RNIB validation
269         inventoryName := ""
270         plmnId := "02f829"
271         nbId := "4a952a0a"
272         w, _ := initSdlMock()
273         servedNrCells := generateServedNrCells("test1", "test2")
274         nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
275         nodebInfo.GetGnb().ServedNrCells = servedNrCells
276
277         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
278         assert.IsType(t, &common.ValidationError{}, rNibErr)
279 }
280
281 func TestUpdateGnbCellsSuccess(t *testing.T) {
282         inventoryName := "name"
283         plmnId := "02f829"
284         nbId := "4a952a0a"
285         w, sdlMock := initSdlMock()
286         servedNrCells := generateServedNrCells("test1", "test2")
287         nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
288         nodebInfo.GetGnb().ServedNrCells = servedNrCells
289         setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
290         var e error
291         sdlMock.On("SetAndPublish", namespace, []string{"RAN_MANIPULATION", inventoryName + "_" + RanUpdatedEvent}, []interface{}{setExpected}).Return(e)
292         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
293         assert.Nil(t, rNibErr)
294 }
295
296 func TestUpdateNodebInfoSuccess(t *testing.T) {
297         inventoryName := "name"
298         plmnId := "02f829"
299         nbId := "4a952a0a"
300         w, sdlMock := initSdlMock()
301         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
302         data, err := proto.Marshal(nodebInfo)
303         if err != nil {
304                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
305         }
306         var e error
307         var setExpected []interface{}
308
309         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
310         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
311         setExpected = append(setExpected, nodebNameKey, data)
312         setExpected = append(setExpected, nodebIdKey, data)
313
314         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(e)
315
316         rNibErr := w.UpdateNodebInfo(nodebInfo)
317         assert.Nil(t, rNibErr)
318         sdlMock.AssertExpectations(t)
319 }
320
321 func TestUpdateNodebInfoAndPublishSuccess(t *testing.T) {
322         inventoryName := "name"
323         plmnId := "02f829"
324         nbId := "4a952a0a"
325         w, sdlMock := initSdlMock()
326         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
327         data, err := proto.Marshal(nodebInfo)
328         if err != nil {
329                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
330         }
331         var e error
332         var setExpected []interface{}
333
334         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
335         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
336         setExpected = append(setExpected, nodebNameKey, data)
337         setExpected = append(setExpected, nodebIdKey, data)
338
339         sdlMock.On("SetAndPublish", namespace, []string{"RAN_MANIPULATION", inventoryName + "_" + RanUpdatedEvent}, []interface{}{setExpected}).Return(e)
340
341         rNibErr := w.UpdateNodebInfoAndPublish(nodebInfo)
342         assert.Nil(t, rNibErr)
343         sdlMock.AssertExpectations(t)
344 }
345
346 func TestUpdateNodebInfoMissingInventoryNameFailure(t *testing.T) {
347         inventoryName := "name"
348         plmnId := "02f829"
349         nbId := "4a952a0a"
350         w, sdlMock := initSdlMock()
351         nodebInfo := &entities.NodebInfo{}
352         data, err := proto.Marshal(nodebInfo)
353         if err != nil {
354                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
355         }
356         var e error
357         var setExpected []interface{}
358
359         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
360         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
361         setExpected = append(setExpected, nodebNameKey, data)
362         setExpected = append(setExpected, nodebIdKey, data)
363
364         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(e)
365
366         rNibErr := w.UpdateNodebInfo(nodebInfo)
367
368         assert.NotNil(t, rNibErr)
369         assert.IsType(t, &common.ValidationError{}, rNibErr)
370 }
371
372 func TestUpdateNodebInfoMissingGlobalNbId(t *testing.T) {
373         inventoryName := "name"
374         w, sdlMock := initSdlMock()
375         nodebInfo := &entities.NodebInfo{}
376         nodebInfo.RanName = inventoryName
377         data, err := proto.Marshal(nodebInfo)
378         if err != nil {
379                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
380         }
381         var e error
382         var setExpected []interface{}
383
384         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
385         setExpected = append(setExpected, nodebNameKey, data)
386         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(e)
387
388         rNibErr := w.UpdateNodebInfo(nodebInfo)
389
390         assert.Nil(t, rNibErr)
391 }
392
393 func TestUpdateNodebInfoSdlSetFailure(t *testing.T) {
394         inventoryName := "name"
395         plmnId := "02f829"
396         nbId := "4a952a0a"
397         w, sdlMock := initSdlMock()
398         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
399         data, err := proto.Marshal(nodebInfo)
400         if err != nil {
401                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
402         }
403         e := errors.New("expected error")
404         var setExpected []interface{}
405
406         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
407         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
408         setExpected = append(setExpected, nodebNameKey, data)
409         setExpected = append(setExpected, nodebIdKey, data)
410
411         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(e)
412
413         rNibErr := w.UpdateNodebInfo(nodebInfo)
414         assert.NotNil(t, rNibErr)
415         assert.IsType(t, &common.InternalError{}, rNibErr)
416         sdlMock.AssertExpectations(t)
417 }
418
419 func TestSaveEnb(t *testing.T) {
420         ranName := "RAN:" + RanName
421         w, sdlMock := initSdlMock()
422         nb := entities.NodebInfo{
423                 RanName:          RanName,
424                 NodeType:         entities.Node_ENB,
425                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
426                 Ip:               "localhost",
427                 Port:             5656,
428                 GlobalNbId: &entities.GlobalNbId{
429                         NbId:   "4a952a0a",
430                         PlmnId: "02f829",
431                 },
432         }
433
434         enb := entities.Enb{}
435         cell := &entities.ServedCellInfo{CellId: "aaff", Pci: 3}
436         cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
437         enb.ServedCells = []*entities.ServedCellInfo{cell}
438         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
439         data, err := proto.Marshal(&nb)
440         if err != nil {
441                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
442         }
443         var e error
444
445         cellData, err := proto.Marshal(&cellEntity)
446         if err != nil {
447                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal Cell entity. Error: %v", err)
448         }
449         var setExpected []interface{}
450         setExpected = append(setExpected, ranName, data)
451         setExpected = append(setExpected, "ENB:02f829:4a952a0a", data)
452         setExpected = append(setExpected, fmt.Sprintf("CELL:%s", cell.GetCellId()), cellData)
453         setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", RanName, cell.GetPci()), cellData)
454
455         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(e)
456         rNibErr := w.SaveNodeb(&nb)
457         assert.Nil(t, rNibErr)
458 }
459
460 func TestSaveEnbCellIdValidationFailure(t *testing.T) {
461         w, _ := initSdlMock()
462         nb := entities.NodebInfo{}
463         nb.RanName = "name"
464         nb.NodeType = entities.Node_ENB
465         nb.ConnectionStatus = 1
466         nb.Ip = "localhost"
467         nb.Port = 5656
468         enb := entities.Enb{}
469         cell := &entities.ServedCellInfo{Pci: 3}
470         enb.ServedCells = []*entities.ServedCellInfo{cell}
471         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
472         rNibErr := w.SaveNodeb(&nb)
473         assert.NotNil(t, rNibErr)
474         assert.IsType(t, &common.ValidationError{}, rNibErr)
475         assert.Equal(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", rNibErr.Error())
476 }
477
478 func TestSaveEnbInventoryNameValidationFailure(t *testing.T) {
479         w, _ := initSdlMock()
480         nb := entities.NodebInfo{
481                 NodeType:         entities.Node_ENB,
482                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
483                 Ip:               "localhost",
484                 Port:             5656,
485                 GlobalNbId: &entities.GlobalNbId{
486                         NbId:   "4a952a0a",
487                         PlmnId: "02f829",
488                 },
489         }
490         enb := entities.Enb{}
491         cell := &entities.ServedCellInfo{CellId: "aaa", Pci: 3}
492         enb.ServedCells = []*entities.ServedCellInfo{cell}
493         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
494         rNibErr := w.SaveNodeb(&nb)
495         assert.NotNil(t, rNibErr)
496         assert.IsType(t, &common.ValidationError{}, rNibErr)
497         assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error())
498 }
499
500 func TestSaveEnbGlobalNbIdPlmnValidationFailure(t *testing.T) {
501         w, _ := initSdlMock()
502         nb := entities.NodebInfo{
503                 RanName:          RanName,
504                 NodeType:         entities.Node_ENB,
505                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
506                 Ip:               "localhost",
507                 Port:             5656,
508                 GlobalNbId: &entities.GlobalNbId{
509                         NbId: "4a952a0a",
510                         //Empty PLMNID fails RNIB validation
511                         PlmnId: "",
512                 },
513         }
514         enb := entities.Enb{}
515         cell := &entities.ServedCellInfo{CellId: "aaa", Pci: 3}
516         enb.ServedCells = []*entities.ServedCellInfo{cell}
517         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
518         rNibErr := w.SaveNodeb(&nb)
519         assert.NotNil(t, rNibErr)
520         assert.IsType(t, &common.ValidationError{}, rNibErr)
521         assert.Equal(t, "#utils.ValidateAndBuildNodeBIdKey - an empty plmnId received", rNibErr.Error())
522 }
523
524 func TestSaveGnbCellIdValidationFailure(t *testing.T) {
525         w, _ := initSdlMock()
526         nb := entities.NodebInfo{}
527         nb.RanName = "name"
528         nb.NodeType = entities.Node_GNB
529         nb.ConnectionStatus = 1
530         nb.Ip = "localhost"
531         nb.Port = 5656
532         gnb := entities.Gnb{}
533         cellInfo := &entities.ServedNRCellInformation{NrPci: 2}
534         cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
535         gnb.ServedNrCells = []*entities.ServedNRCell{cell}
536         nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
537
538         rNibErr := w.SaveNodeb(&nb)
539         assert.NotNil(t, rNibErr)
540         assert.IsType(t, &common.ValidationError{}, rNibErr)
541         assert.Equal(t, "#utils.ValidateAndBuildNrCellIdKey - an empty cell id received", rNibErr.Error())
542 }
543
544 func TestSaveGnb(t *testing.T) {
545         ranName := "RAN:" + RanName
546         w, sdlMock := initSdlMock()
547         nb := entities.NodebInfo{
548                 RanName:          RanName,
549                 NodeType:         entities.Node_GNB,
550                 ConnectionStatus: 1,
551                 GlobalNbId: &entities.GlobalNbId{
552                         NbId:   "4a952a0a",
553                         PlmnId: "02f829",
554                 },
555                 Ip:   "localhost",
556                 Port: 5656,
557         }
558
559         gnb := entities.Gnb{}
560         cellInfo := &entities.ServedNRCellInformation{NrPci: 2, CellId: "ccdd"}
561         cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
562         cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
563         gnb.ServedNrCells = []*entities.ServedNRCell{cell}
564         nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
565         data, err := proto.Marshal(&nb)
566         if err != nil {
567                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB entity. Error: %v", err)
568         }
569         var e error
570
571         cellData, err := proto.Marshal(&cellEntity)
572         if err != nil {
573                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal Cell entity. Error: %v", err)
574         }
575         var setExpected []interface{}
576         setExpected = append(setExpected, ranName, data)
577         setExpected = append(setExpected, "GNB:02f829:4a952a0a", data)
578         setExpected = append(setExpected, fmt.Sprintf("NRCELL:%s", cell.GetServedNrCellInformation().GetCellId()), cellData)
579         setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", RanName, cell.GetServedNrCellInformation().GetNrPci()), cellData)
580
581         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(e)
582         rNibErr := w.SaveNodeb(&nb)
583         assert.Nil(t, rNibErr)
584 }
585
586 func TestSaveRanLoadInformationSuccess(t *testing.T) {
587         inventoryName := "name"
588         loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
589
590         if validationErr != nil {
591                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
592         }
593
594         w, sdlMock := initSdlMock()
595
596         ranLoadInformation := generateRanLoadInformation()
597         data, err := proto.Marshal(ranLoadInformation)
598
599         if err != nil {
600                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
601         }
602
603         var e error
604         var setExpected []interface{}
605         setExpected = append(setExpected, loadKey, data)
606         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(e)
607
608         rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
609         assert.Nil(t, rNibErr)
610 }
611
612 func TestSaveRanLoadInformationMarshalNilFailure(t *testing.T) {
613         inventoryName := "name2"
614         w, _ := initSdlMock()
615
616         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
617         err := w.SaveRanLoadInformation(inventoryName, nil)
618         assert.Equal(t, expectedErr, err)
619 }
620
621 func TestSaveRanLoadInformationEmptyInventoryNameFailure(t *testing.T) {
622         inventoryName := ""
623         w, _ := initSdlMock()
624
625         err := w.SaveRanLoadInformation(inventoryName, nil)
626         assert.NotNil(t, err)
627         assert.IsType(t, &common.ValidationError{}, err)
628 }
629
630 func TestSaveRanLoadInformationSdlFailure(t *testing.T) {
631         inventoryName := "name2"
632
633         loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
634
635         if validationErr != nil {
636                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
637         }
638
639         w, sdlMock := initSdlMock()
640
641         ranLoadInformation := generateRanLoadInformation()
642         data, err := proto.Marshal(ranLoadInformation)
643
644         if err != nil {
645                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
646         }
647
648         expectedErr := errors.New("expected error")
649         var setExpected []interface{}
650         setExpected = append(setExpected, loadKey, data)
651         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(expectedErr)
652
653         rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
654         assert.NotNil(t, rNibErr)
655         assert.IsType(t, &common.InternalError{}, rNibErr)
656 }
657
658 func generateCellLoadInformation() *entities.CellLoadInformation {
659         cellLoadInformation := entities.CellLoadInformation{}
660
661         cellLoadInformation.CellId = "123"
662
663         ulInterferenceOverloadIndication := entities.UlInterferenceOverloadIndication_HIGH_INTERFERENCE
664         cellLoadInformation.UlInterferenceOverloadIndications = []entities.UlInterferenceOverloadIndication{ulInterferenceOverloadIndication}
665
666         ulHighInterferenceInformation := entities.UlHighInterferenceInformation{
667                 TargetCellId:                 "456",
668                 UlHighInterferenceIndication: "xxx",
669         }
670
671         cellLoadInformation.UlHighInterferenceInfos = []*entities.UlHighInterferenceInformation{&ulHighInterferenceInformation}
672
673         cellLoadInformation.RelativeNarrowbandTxPower = &entities.RelativeNarrowbandTxPower{
674                 RntpPerPrb:                       "xxx",
675                 RntpThreshold:                    entities.RntpThreshold_NEG_4,
676                 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V1_ANT_PRT,
677                 PB:                               1,
678                 PdcchInterferenceImpact:          2,
679                 EnhancedRntp: &entities.EnhancedRntp{
680                         EnhancedRntpBitmap:     "xxx",
681                         RntpHighPowerThreshold: entities.RntpThreshold_NEG_2,
682                         EnhancedRntpStartTime:  &entities.StartTime{StartSfn: 500, StartSubframeNumber: 5},
683                 },
684         }
685
686         cellLoadInformation.AbsInformation = &entities.AbsInformation{
687                 Mode:                             entities.AbsInformationMode_ABS_INFO_FDD,
688                 AbsPatternInfo:                   "xxx",
689                 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V2_ANT_PRT,
690                 MeasurementSubset:                "xxx",
691         }
692
693         cellLoadInformation.InvokeIndication = entities.InvokeIndication_ABS_INFORMATION
694
695         cellLoadInformation.ExtendedUlInterferenceOverloadInfo = &entities.ExtendedUlInterferenceOverloadInfo{
696                 AssociatedSubframes:                       "xxx",
697                 ExtendedUlInterferenceOverloadIndications: cellLoadInformation.UlInterferenceOverloadIndications,
698         }
699
700         compInformationItem := &entities.CompInformationItem{
701                 CompHypothesisSets: []*entities.CompHypothesisSet{{CellId: "789", CompHypothesis: "xxx"}},
702                 BenefitMetric:      50,
703         }
704
705         cellLoadInformation.CompInformation = &entities.CompInformation{
706                 CompInformationItems:     []*entities.CompInformationItem{compInformationItem},
707                 CompInformationStartTime: &entities.StartTime{StartSfn: 123, StartSubframeNumber: 456},
708         }
709
710         cellLoadInformation.DynamicDlTransmissionInformation = &entities.DynamicDlTransmissionInformation{
711                 State:             entities.NaicsState_NAICS_ACTIVE,
712                 TransmissionModes: "xxx",
713                 PB:                2,
714                 PAList:            []entities.PA{entities.PA_DB_NEG_3},
715         }
716
717         return &cellLoadInformation
718 }
719
720 func generateRanLoadInformation() *entities.RanLoadInformation {
721         ranLoadInformation := entities.RanLoadInformation{}
722
723         ranLoadInformation.LoadTimestamp = uint64(time.Now().UnixNano())
724
725         cellLoadInformation := generateCellLoadInformation()
726         ranLoadInformation.CellLoadInfos = []*entities.CellLoadInformation{cellLoadInformation}
727
728         return &ranLoadInformation
729 }
730
731 func TestSaveNilEntityFailure(t *testing.T) {
732         w, _ := initSdlMock()
733         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
734         actualErr := w.SaveNodeb(nil)
735         assert.Equal(t, expectedErr, actualErr)
736 }
737
738 func TestSaveUnknownTypeEntityFailure(t *testing.T) {
739         w, _ := initSdlMock()
740         nb := &entities.NodebInfo{}
741         nb.Port = 5656
742         nb.Ip = "localhost"
743         actualErr := w.SaveNodeb(nb)
744         assert.IsType(t, &common.ValidationError{}, actualErr)
745 }
746
747 func TestSaveEntitySetFailure(t *testing.T) {
748         name := "name"
749         plmnId := "02f829"
750         nbId := "4a952a0a"
751
752         w, sdlMock := initSdlMock()
753         gnb := entities.NodebInfo{
754                 RanName:          name,
755                 NodeType:         entities.Node_GNB,
756                 ConnectionStatus: 1,
757                 GlobalNbId: &entities.GlobalNbId{
758                         NbId:   nbId,
759                         PlmnId: plmnId,
760                 },
761                 Ip:   "localhost",
762                 Port: 5656,
763         }
764         data, err := proto.Marshal(&gnb)
765         if err != nil {
766                 t.Errorf("#rNibWriter_test.TestSaveEntityFailure - Failed to marshal NodeB entity. Error: %v", err)
767         }
768         setExpected := []interface{}{"RAN:" + name, data}
769         setExpected = append(setExpected, "GNB:"+plmnId+":"+nbId, data)
770         expectedErr := errors.New("expected error")
771         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(expectedErr)
772         rNibErr := w.SaveNodeb(&gnb)
773         assert.NotEmpty(t, rNibErr)
774 }
775
776 func TestSaveEntitySetAndPublishFailure(t *testing.T) {
777         name := "name"
778         plmnId := "02f829"
779         nbId := "4a952a0a"
780
781         w, sdlMock := initSdlMock()
782         enb := entities.NodebInfo{
783                 RanName:          name,
784                 NodeType:         entities.Node_ENB,
785                 ConnectionStatus: 1,
786                 GlobalNbId: &entities.GlobalNbId{
787                         NbId:   nbId,
788                         PlmnId: plmnId,
789                 },
790                 Ip:   "localhost",
791                 Port: 5656,
792         }
793         data, err := proto.Marshal(&enb)
794         if err != nil {
795                 t.Errorf("#rNibWriter_test.TestSaveEntityFailure - Failed to marshal NodeB entity. Error: %v", err)
796         }
797         setExpected := []interface{}{"RAN:" + name, data}
798         setExpected = append(setExpected, "ENB:"+plmnId+":"+nbId, data)
799         expectedErr := errors.New("expected error")
800         sdlMock.On("SetAndPublish", namespace, []string{"RAN_MANIPULATION", name + "_" + RanAddedEvent}, []interface{}{setExpected}).Return(expectedErr)
801         rNibErr := w.AddEnb(&enb)
802         assert.NotEmpty(t, rNibErr)
803 }
804
805 func TestGetRNibWriter(t *testing.T) {
806         received, _ := initSdlMock()
807         assert.NotEmpty(t, received)
808 }
809
810 func TestSaveE2TInstanceSuccess(t *testing.T) {
811         address := "10.10.2.15:9800"
812         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
813
814         if validationErr != nil {
815                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to build E2T Instance key. Error: %v", validationErr)
816         }
817
818         w, sdlMock := initSdlMock()
819
820         e2tInstance := generateE2tInstance(address)
821         data, err := json.Marshal(e2tInstance)
822
823         if err != nil {
824                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to marshal E2tInstance entity. Error: %v", err)
825         }
826
827         var e error
828         var setExpected []interface{}
829         setExpected = append(setExpected, loadKey, data)
830         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(e)
831
832         rNibErr := w.SaveE2TInstance(e2tInstance)
833         assert.Nil(t, rNibErr)
834 }
835
836 func TestSaveE2TInstanceNullE2tInstanceFailure(t *testing.T) {
837         w, _ := initSdlMock()
838         var address string
839         e2tInstance := entities.NewE2TInstance(address, "test")
840         err := w.SaveE2TInstance(e2tInstance)
841         assert.NotNil(t, err)
842         assert.IsType(t, &common.ValidationError{}, err)
843 }
844
845 func TestSaveE2TInstanceSdlFailure(t *testing.T) {
846         address := "10.10.2.15:9800"
847         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
848
849         if validationErr != nil {
850                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to build E2T Instance key. Error: %v", validationErr)
851         }
852
853         w, sdlMock := initSdlMock()
854
855         e2tInstance := generateE2tInstance(address)
856         data, err := json.Marshal(e2tInstance)
857
858         if err != nil {
859                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to marshal E2tInstance entity. Error: %v", err)
860         }
861
862         expectedErr := errors.New("expected error")
863         var setExpected []interface{}
864         setExpected = append(setExpected, loadKey, data)
865         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(expectedErr)
866
867         rNibErr := w.SaveE2TInstance(e2tInstance)
868         assert.NotNil(t, rNibErr)
869         assert.IsType(t, &common.InternalError{}, rNibErr)
870 }
871
872 func generateE2tInstance(address string) *entities.E2TInstance {
873         e2tInstance := entities.NewE2TInstance(address, "pod test")
874
875         e2tInstance.AssociatedRanList = []string{"test1", "test2"}
876
877         return e2tInstance
878 }
879
880 func TestSaveE2TAddressesSuccess(t *testing.T) {
881         address := "10.10.2.15:9800"
882         w, sdlMock := initSdlMock()
883
884         e2tAddresses := []string{address}
885         data, err := json.Marshal(e2tAddresses)
886
887         if err != nil {
888                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSuccess - Failed to marshal E2TInfoList. Error: %v", err)
889         }
890
891         var e error
892         var setExpected []interface{}
893         setExpected = append(setExpected, E2TAddressesKey, data)
894         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(e)
895
896         rNibErr := w.SaveE2TAddresses(e2tAddresses)
897         assert.Nil(t, rNibErr)
898 }
899
900 func TestSaveE2TAddressesSdlFailure(t *testing.T) {
901         address := "10.10.2.15:9800"
902         w, sdlMock := initSdlMock()
903
904         e2tAddresses := []string{address}
905         data, err := json.Marshal(e2tAddresses)
906
907         if err != nil {
908                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSdlFailure - Failed to marshal E2TInfoList. Error: %v", err)
909         }
910
911         expectedErr := errors.New("expected error")
912         var setExpected []interface{}
913         setExpected = append(setExpected, E2TAddressesKey, data)
914         sdlMock.On("Set", namespace, []interface{}{setExpected}).Return(expectedErr)
915
916         rNibErr := w.SaveE2TAddresses(e2tAddresses)
917         assert.NotNil(t, rNibErr)
918         assert.IsType(t, &common.InternalError{}, rNibErr)
919 }
920
921 func TestRemoveE2TInstanceSuccess(t *testing.T) {
922         address := "10.10.2.15:9800"
923         w, sdlMock := initSdlMock()
924
925         e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
926         var e error
927         sdlMock.On("Remove", namespace, e2tAddresses).Return(e)
928
929         rNibErr := w.RemoveE2TInstance(address)
930         assert.Nil(t, rNibErr)
931         sdlMock.AssertExpectations(t)
932 }
933
934 func TestRemoveE2TInstanceSdlFailure(t *testing.T) {
935         address := "10.10.2.15:9800"
936         w, sdlMock := initSdlMock()
937
938         e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
939         expectedErr := errors.New("expected error")
940         sdlMock.On("Remove", namespace, e2tAddresses).Return(expectedErr)
941
942         rNibErr := w.RemoveE2TInstance(address)
943         assert.IsType(t, &common.InternalError{}, rNibErr)
944 }
945
946 func TestRemoveE2TInstanceEmptyAddressFailure(t *testing.T) {
947         w, sdlMock := initSdlMock()
948
949         rNibErr := w.RemoveE2TInstance("")
950         assert.IsType(t, &common.ValidationError{}, rNibErr)
951         sdlMock.AssertExpectations(t)
952 }
953
954 func TestUpdateNodebInfoOnConnectionStatusInversionSuccess(t *testing.T) {
955         inventoryName := "name"
956         plmnId := "02f829"
957         nbId := "4a952a0a"
958         channelName := "RAN_CONNECTION_STATUS_CHANGE"
959         eventName := inventoryName + "_" + "CONNECTED"
960         w, sdlMock := initSdlMock()
961         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
962         data, err := proto.Marshal(nodebInfo)
963         if err != nil {
964                 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionSuccess - Failed to marshal NodeB entity. Error: %v", err)
965         }
966         var e error
967         var setExpected []interface{}
968
969         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
970         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
971         setExpected = append(setExpected, nodebNameKey, data)
972         setExpected = append(setExpected, nodebIdKey, data)
973
974         sdlMock.On("SetAndPublish", namespace, []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
975
976         rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, eventName)
977         assert.Nil(t, rNibErr)
978 }
979
980 func TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure(t *testing.T) {
981         inventoryName := "name"
982         plmnId := "02f829"
983         nbId := "4a952a0a"
984         channelName := "RAN_CONNECTION_STATUS_CHANGE"
985         eventName := inventoryName + "_" + "CONNECTED"
986         w, sdlMock := initSdlMock()
987         nodebInfo := &entities.NodebInfo{}
988         data, err := proto.Marshal(nodebInfo)
989         if err != nil {
990                 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
991         }
992         var e error
993         var setExpected []interface{}
994
995         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
996         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
997         setExpected = append(setExpected, nodebNameKey, data)
998         setExpected = append(setExpected, nodebIdKey, data)
999
1000         sdlMock.On("SetAndPublish", namespace, []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
1001
1002         rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, eventName)
1003
1004         assert.NotNil(t, rNibErr)
1005         assert.IsType(t, &common.ValidationError{}, rNibErr)
1006 }
1007
1008 func TestUpdateNodebInfoOnConnectionStatusInversionMissingGlobalNbId(t *testing.T) {
1009         inventoryName := "name"
1010         channelName := "RAN_CONNECTION_STATUS_CHANGE"
1011         eventName := inventoryName + "_" + "CONNECTED"
1012         w, sdlMock := initSdlMock()
1013         nodebInfo := &entities.NodebInfo{}
1014         nodebInfo.RanName = inventoryName
1015         data, err := proto.Marshal(nodebInfo)
1016         if err != nil {
1017                 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
1018         }
1019         var e error
1020         var setExpected []interface{}
1021
1022         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
1023         setExpected = append(setExpected, nodebNameKey, data)
1024         sdlMock.On("SetAndPublish", namespace, []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
1025
1026         rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, eventName)
1027
1028         assert.Nil(t, rNibErr)
1029 }
1030
1031 func TestUpdateNodebInfoOnConnectionStatusInversionSdlFailure(t *testing.T) {
1032         inventoryName := "name"
1033         plmnId := "02f829"
1034         nbId := "4a952a0a"
1035         channelName := "RAN_CONNECTION_STATUS_CHANGE"
1036         eventName := inventoryName + "_" + "CONNECTED"
1037         w, sdlMock := initSdlMock()
1038         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
1039         data, err := proto.Marshal(nodebInfo)
1040         if err != nil {
1041                 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionSuccess - Failed to marshal NodeB entity. Error: %v", err)
1042         }
1043         e := errors.New("expected error")
1044         var setExpected []interface{}
1045
1046         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
1047         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
1048         setExpected = append(setExpected, nodebNameKey, data)
1049         setExpected = append(setExpected, nodebIdKey, data)
1050
1051         sdlMock.On("SetAndPublish", namespace, []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
1052
1053         rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, eventName)
1054         assert.NotNil(t, rNibErr)
1055         assert.IsType(t, &common.InternalError{}, rNibErr)
1056 }
1057
1058 func TestSaveGeneralConfiguration(t *testing.T) {
1059         w, sdlMock := initSdlMock()
1060
1061         key := common.BuildGeneralConfigurationKey()
1062         configurationData := "{\"enableRic\":true}"
1063         configuration := &entities.GeneralConfiguration{}
1064         configuration.EnableRic = true
1065
1066         sdlMock.On("Set", namespace, []interface{}{[]interface{}{key, []byte(configurationData)}}).Return(nil)
1067         rNibErr := w.SaveGeneralConfiguration(configuration)
1068
1069         assert.Nil(t, rNibErr)
1070         sdlMock.AssertExpectations(t)
1071 }
1072
1073 func TestSaveGeneralConfigurationDbError(t *testing.T) {
1074         w, sdlMock := initSdlMock()
1075
1076         key := common.BuildGeneralConfigurationKey()
1077         configurationData := "{\"enableRic\":true}"
1078         configuration := &entities.GeneralConfiguration{}
1079         configuration.EnableRic = true
1080
1081         expectedErr := errors.New("expected error")
1082
1083         sdlMock.On("Set", namespace, []interface{}{[]interface{}{key, []byte(configurationData)}}).Return(expectedErr)
1084         rNibErr := w.SaveGeneralConfiguration(configuration)
1085
1086         assert.NotNil(t, rNibErr)
1087 }
1088
1089 func TestRemoveServedCellsFailure(t *testing.T) {
1090         w, sdlMock := initSdlMock()
1091         servedCellsToRemove := generateServedCells("whatever1", "whatever2")
1092         expectedErr := errors.New("expected error")
1093         sdlMock.On("Remove", namespace, buildServedCellInfoKeysToRemove(RanName, servedCellsToRemove)).Return(expectedErr)
1094
1095         rNibErr := w.RemoveServedCells(RanName, servedCellsToRemove)
1096
1097         assert.NotNil(t, rNibErr)
1098 }
1099
1100 func TestRemoveServedCellsSuccess(t *testing.T) {
1101         w, sdlMock := initSdlMock()
1102         servedCellsToRemove := generateServedCells("whatever1", "whatever2")
1103         sdlMock.On("Remove", namespace, buildServedCellInfoKeysToRemove(RanName, servedCellsToRemove)).Return(nil)
1104         err := w.RemoveServedCells(RanName, servedCellsToRemove)
1105         assert.Nil(t, err)
1106 }
1107
1108 func TestUpdateEnbInvalidNodebInfoFailure(t *testing.T) {
1109         w, sdlMock := initSdlMock()
1110         servedCells := generateServedCells("test1", "test2")
1111         nodebInfo := &entities.NodebInfo{}
1112         sdlMock.AssertNotCalled(t, "SetAndPublish")
1113         rNibErr := w.UpdateEnb(nodebInfo, servedCells)
1114         assert.IsType(t, &common.ValidationError{}, rNibErr)
1115 }
1116
1117 func TestUpdateEnbInvalidCellFailure(t *testing.T) {
1118         inventoryName := "name"
1119         plmnId := "02f829"
1120         nbId := "4a952a0a"
1121         w, sdlMock := initSdlMock()
1122         servedCells := []*entities.ServedCellInfo{{CellId: ""}}
1123         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
1124         nodebInfo.GetEnb().ServedCells = servedCells
1125         sdlMock.AssertNotCalled(t, "SetAndPublish")
1126         rNibErr := w.UpdateEnb(nodebInfo, servedCells)
1127         assert.IsType(t, &common.ValidationError{}, rNibErr)
1128 }
1129
1130 func TestUpdateEnbRnibKeyValidationError(t *testing.T) {
1131         //Empty RAN name fails RNIB validation
1132         inventoryName := ""
1133         plmnId := "02f829"
1134         nbId := "4a952a0a"
1135         w, _ := initSdlMock()
1136         servedCells := generateServedCells("test1", "test2")
1137         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
1138         nodebInfo.GetEnb().ServedCells = servedCells
1139
1140         rNibErr := w.UpdateEnb(nodebInfo, servedCells)
1141         assert.IsType(t, &common.ValidationError{}, rNibErr)
1142 }
1143
1144 func TestUpdateEnbSdlFailure(t *testing.T) {
1145         inventoryName := "name"
1146         plmnId := "02f829"
1147         nbId := "4a952a0a"
1148         w, sdlMock := initSdlMock()
1149         servedCells := generateServedCells("test1", "test2")
1150         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
1151         nodebInfo.GetEnb().ServedCells = servedCells
1152         setExpected := getUpdateEnbCellsSetExpected(t, nodebInfo, servedCells)
1153         sdlMock.On("SetAndPublish", namespace, []string{"RAN_MANIPULATION", inventoryName + "_" + RanUpdatedEvent}, []interface{}{setExpected}).Return(errors.New("expected error"))
1154         rNibErr := w.UpdateEnb(nodebInfo, servedCells)
1155         assert.IsType(t, &common.InternalError{}, rNibErr)
1156 }
1157
1158 func TestUpdateEnbSuccess(t *testing.T) {
1159         inventoryName := "name"
1160         plmnId := "02f829"
1161         nbId := "4a952a0a"
1162         w, sdlMock := initSdlMock()
1163         servedCells := generateServedCells("test1", "test2")
1164         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
1165         nodebInfo.GetEnb().ServedCells = servedCells
1166         setExpected := getUpdateEnbCellsSetExpected(t, nodebInfo, servedCells)
1167
1168         var e error
1169         sdlMock.On("SetAndPublish", namespace, []string{"RAN_MANIPULATION", inventoryName + "_" + RanUpdatedEvent}, []interface{}{setExpected}).Return(e)
1170         rNibErr := w.UpdateEnb(nodebInfo, servedCells)
1171         assert.Nil(t, rNibErr)
1172 }
1173
1174 func getUpdateEnbSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedCells []*entities.ServedCellInfo) []interface{} {
1175
1176         nodebInfoData, err := proto.Marshal(nodebInfo)
1177         if err != nil {
1178                 t.Fatalf("#rNibWriter_test.getUpdateEnbSetExpected - Failed to marshal NodeB entity. Error: %s", err)
1179         }
1180
1181         nodebNameKey, _ := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
1182         nodebIdKey, _ := common.ValidateAndBuildNodeBIdKey(nodebInfo.NodeType.String(), nodebInfo.GlobalNbId.PlmnId, nodebInfo.GlobalNbId.NbId,nodebInfo.CuUpId,nodebInfo.DuId)
1183         setExpected := []interface{}{nodebNameKey, nodebInfoData, nodebIdKey, nodebInfoData}
1184
1185         for _, v := range servedCells {
1186
1187                 cellEntity := entities.ServedCellInfo{CellId: "some cell id", EutraMode: entities.Eutra_FDD, CsgId: "some csg id"}
1188                 cellData, err := proto.Marshal(&cellEntity)
1189
1190                 if err != nil {
1191                         t.Fatalf("#rNibWriter_test.getUpdateEnbSetExpected - Failed to marshal cell entity. Error: %s", err)
1192                 }
1193
1194                 nrCellIdKey, _ := common.ValidateAndBuildNrCellIdKey(v.GetCellId())
1195                 cellNamePciKey, _ := common.ValidateAndBuildCellNamePciKey(nodebInfo.RanName, v.GetPci())
1196                 setExpected = append(setExpected, nrCellIdKey, cellData, cellNamePciKey, cellData)
1197         }
1198         return setExpected
1199 }
1200
1201 func TestRemoveEnbSuccess(t *testing.T) {
1202         inventoryName := "name"
1203         plmnId := "02f829"
1204         nbId := "4a952a0a"
1205         channelName := "RAN_MANIPULATION"
1206         eventName := inventoryName + "_" + "DELETED"
1207         w, sdlMock := initSdlMock()
1208         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
1209         nodebInfo.GetEnb().ServedCells = generateServedCellInfos("cell1", "cell2")
1210
1211         var e error
1212
1213         expectedKeys := []string{}
1214         cell1Key := fmt.Sprintf("CELL:%s", nodebInfo.GetEnb().ServedCells[0].CellId)
1215         cell1PciKey := fmt.Sprintf("PCI:%s:%02x", inventoryName, nodebInfo.GetEnb().ServedCells[0].Pci)
1216         cell2Key := fmt.Sprintf("CELL:%s", nodebInfo.GetEnb().ServedCells[1].CellId)
1217         cell2PciKey := fmt.Sprintf("PCI:%s:%02x", inventoryName, nodebInfo.GetEnb().ServedCells[1].Pci)
1218         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
1219         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
1220         expectedKeys = append(expectedKeys, cell1Key, cell1PciKey, cell2Key, cell2PciKey, nodebNameKey, nodebIdKey)
1221         sdlMock.On("RemoveAndPublish", namespace, []string{channelName, eventName}, expectedKeys).Return(e)
1222
1223         rNibErr := w.RemoveEnb(nodebInfo)
1224         assert.Nil(t, rNibErr)
1225         sdlMock.AssertExpectations(t)
1226 }
1227
1228 func TestRemoveEnbRnibKeyValidationError(t *testing.T) {
1229         //Empty RAN name fails RNIB key validation
1230         inventoryName := ""
1231         plmnId := "02f829"
1232         nbId := "4a952a0a"
1233         w, _ := initSdlMock()
1234         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
1235         nodebInfo.GetEnb().ServedCells = generateServedCellInfos("cell1", "cell2")
1236
1237         rNibErr := w.RemoveEnb(nodebInfo)
1238         assert.NotNil(t, rNibErr)
1239 }
1240
1241 func TestRemoveEnbRemoveAndPublishError(t *testing.T) {
1242         inventoryName := "name"
1243         plmnId := "02f829"
1244         nbId := "4a952a0a"
1245         channelName := "RAN_MANIPULATION"
1246         eventName := inventoryName + "_" + "DELETED"
1247         w, sdlMock := initSdlMock()
1248         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
1249         nodebInfo.GetEnb().ServedCells = generateServedCellInfos("cell1", "cell2")
1250
1251         expectedKeys := []string{}
1252         cell1Key := fmt.Sprintf("CELL:%s", nodebInfo.GetEnb().ServedCells[0].CellId)
1253         cell1PciKey := fmt.Sprintf("PCI:%s:%02x", inventoryName, nodebInfo.GetEnb().ServedCells[0].Pci)
1254         cell2Key := fmt.Sprintf("CELL:%s", nodebInfo.GetEnb().ServedCells[1].CellId)
1255         cell2PciKey := fmt.Sprintf("PCI:%s:%02x", inventoryName, nodebInfo.GetEnb().ServedCells[1].Pci)
1256         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
1257         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
1258         expectedKeys = append(expectedKeys, cell1Key, cell1PciKey, cell2Key, cell2PciKey, nodebNameKey, nodebIdKey)
1259         sdlMock.On("RemoveAndPublish", namespace, []string{channelName, eventName}, expectedKeys).Return(errors.New("for test"))
1260
1261         rNibErr := w.RemoveEnb(nodebInfo)
1262         assert.NotNil(t, rNibErr)
1263         sdlMock.AssertExpectations(t)
1264 }
1265
1266 func TestRemoveNbIdentitySuccess(t *testing.T) {
1267         w, sdlMock := initSdlMock()
1268         nbIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
1269         nbIdData, err := proto.Marshal(nbIdentity)
1270         if err != nil {
1271                 t.Errorf("#TestRemoveNbIdentitySuccess - failed to Marshal NbIdentity")
1272         }
1273
1274         sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), []interface{}{nbIdData}).Return(nil)
1275
1276         rNibErr := w.RemoveNbIdentity(entities.Node_ENB, nbIdentity)
1277         assert.Nil(t, rNibErr)
1278         sdlMock.AssertExpectations(t)
1279 }
1280
1281 func TestRemoveNbIdentityMarshalNilFailure(t *testing.T) {
1282         w, _ := initSdlMock()
1283
1284         rNibErr := w.RemoveNbIdentity(entities.Node_ENB, nil)
1285         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
1286         assert.Equal(t, expectedErr, rNibErr)
1287 }
1288
1289 func TestRemoveNbIdentityError(t *testing.T) {
1290         w, sdlMock := initSdlMock()
1291         nbIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
1292         nbIdData, err := proto.Marshal(nbIdentity)
1293         if err != nil {
1294                 t.Errorf("#TestRemoveNbIdentitySuccess - failed to Marshal NbIdentity")
1295         }
1296
1297         sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), []interface{}{nbIdData}).Return(fmt.Errorf("for test"))
1298
1299         rNibErr := w.RemoveNbIdentity(entities.Node_ENB, nbIdentity)
1300         assert.NotNil(t, rNibErr)
1301         sdlMock.AssertExpectations(t)
1302 }
1303
1304 func TestAddEnb(t *testing.T) {
1305         ranName := "RAN:" + RanName
1306         w, sdlMock := initSdlMock()
1307         nb := entities.NodebInfo{
1308                 RanName:          RanName,
1309                 NodeType:         entities.Node_ENB,
1310                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
1311                 Ip:               "localhost",
1312                 Port:             5656,
1313                 GlobalNbId: &entities.GlobalNbId{
1314                         NbId:   "4a952a0a",
1315                         PlmnId: "02f829",
1316                 },
1317         }
1318
1319         enb := entities.Enb{}
1320         cell := &entities.ServedCellInfo{CellId: "aaff", Pci: 3}
1321         cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
1322         enb.ServedCells = []*entities.ServedCellInfo{cell}
1323         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
1324         data, err := proto.Marshal(&nb)
1325         if err != nil {
1326                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
1327         }
1328         var e error
1329
1330         cellData, err := proto.Marshal(&cellEntity)
1331         if err != nil {
1332                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal Cell entity. Error: %v", err)
1333         }
1334         var setExpected []interface{}
1335         setExpected = append(setExpected, ranName, data)
1336         setExpected = append(setExpected, "ENB:02f829:4a952a0a", data)
1337         setExpected = append(setExpected, fmt.Sprintf("CELL:%s", cell.GetCellId()), cellData)
1338         setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", RanName, cell.GetPci()), cellData)
1339
1340         sdlMock.On("SetAndPublish", namespace, []string{"RAN_MANIPULATION", RanName + "_" + RanAddedEvent}, []interface{}{setExpected}).Return(e)
1341
1342         rNibErr := w.AddEnb(&nb)
1343         assert.Nil(t, rNibErr)
1344 }
1345
1346 func TestAddEnbMarshalNilFailure(t *testing.T) {
1347         w, _ := initSdlMock()
1348
1349         rNibErr := w.AddEnb(nil)
1350         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
1351         assert.Equal(t, expectedErr, rNibErr)
1352 }
1353
1354 func TestAddEnbCellIdValidationFailure(t *testing.T) {
1355         w, _ := initSdlMock()
1356         nb := entities.NodebInfo{}
1357         nb.RanName = "name"
1358         nb.NodeType = entities.Node_ENB
1359         nb.ConnectionStatus = 1
1360         nb.Ip = "localhost"
1361         nb.Port = 5656
1362         enb := entities.Enb{}
1363         cell := &entities.ServedCellInfo{Pci: 3}
1364         enb.ServedCells = []*entities.ServedCellInfo{cell}
1365         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
1366         rNibErr := w.AddEnb(&nb)
1367         assert.NotNil(t, rNibErr)
1368         assert.IsType(t, &common.ValidationError{}, rNibErr)
1369         assert.Equal(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", rNibErr.Error())
1370 }
1371
1372 func TestAddEnbInventoryNameValidationFailure(t *testing.T) {
1373         w, _ := initSdlMock()
1374         nb := entities.NodebInfo{
1375                 NodeType:         entities.Node_ENB,
1376                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
1377                 Ip:               "localhost",
1378                 Port:             5656,
1379                 GlobalNbId: &entities.GlobalNbId{
1380                         NbId:   "4a952a0a",
1381                         PlmnId: "02f829",
1382                 },
1383         }
1384         enb := entities.Enb{}
1385         cell := &entities.ServedCellInfo{CellId: "aaa", Pci: 3}
1386         enb.ServedCells = []*entities.ServedCellInfo{cell}
1387         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
1388         rNibErr := w.AddEnb(&nb)
1389         assert.NotNil(t, rNibErr)
1390         assert.IsType(t, &common.ValidationError{}, rNibErr)
1391         assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error())
1392 }
1393
1394 func TestAddEnbGlobalNbIdPlmnValidationFailure(t *testing.T) {
1395         w, _ := initSdlMock()
1396         nb := entities.NodebInfo{
1397                 RanName:          "name",
1398                 NodeType:         entities.Node_ENB,
1399                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
1400                 Ip:               "localhost",
1401                 Port:             5656,
1402                 GlobalNbId: &entities.GlobalNbId{
1403                         NbId: "4a952a0a",
1404                         //Empty PLMNID fails RNIB validation
1405                         PlmnId: "",
1406                 },
1407         }
1408         enb := entities.Enb{}
1409         cell := &entities.ServedCellInfo{CellId: "aaa", Pci: 3}
1410         enb.ServedCells = []*entities.ServedCellInfo{cell}
1411         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
1412         rNibErr := w.AddEnb(&nb)
1413         assert.NotNil(t, rNibErr)
1414         assert.IsType(t, &common.ValidationError{}, rNibErr)
1415         assert.Equal(t, "#utils.ValidateAndBuildNodeBIdKey - an empty plmnId received", rNibErr.Error())
1416 }
1417
1418 func TestUpdateNbIdentityOneMemberSuccess(t *testing.T) {
1419         w, sdlMock := initSdlMock()
1420
1421         proto, nbIdentity := createNbIdentityProto(t, "ran1", "plmnId1", "nbId1", entities.ConnectionStatus_DISCONNECTED)
1422         val := []interface{}{proto}
1423
1424         sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), val).Return(nil)
1425
1426         protoAdd, nbIdentityAdd := createNbIdentityProto(t, "ran1_add", "plmnId1_add", "nbId1_add", entities.ConnectionStatus_CONNECTED)
1427         sdlMock.On("AddMember", namespace, entities.Node_ENB.String(), []interface{}{protoAdd}).Return(nil)
1428
1429         newNbIdIdentities := []*entities.NbIdentity{nbIdentityAdd}
1430         oldNbIdIdentities := []*entities.NbIdentity{nbIdentity}
1431
1432         rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
1433         assert.Nil(t, rNibErr)
1434         sdlMock.AssertExpectations(t)
1435 }
1436
1437 func TestUpdateNbIdentitySuccess(t *testing.T) {
1438         w, sdlMock := initSdlMock()
1439
1440         var nbIdIdentitiesProtoToRemove []interface{}
1441         protoRan1, _ := createNbIdentityProto(t, "ran1", "plmnId1", "nbId1", entities.ConnectionStatus_DISCONNECTED)
1442         protoRan2, _ := createNbIdentityProto(t, "ran2", "plmnId2", "nbId2", entities.ConnectionStatus_DISCONNECTED)
1443         nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan1)
1444         nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan2)
1445         sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToRemove).Return(nil)
1446
1447         var nbIdIdentitiesProtoToAdd []interface{}
1448         protoRan1Add, _ := createNbIdentityProto(t, "ran1_add", "plmnId1_add", "nbId1_add", entities.ConnectionStatus_CONNECTED)
1449         protoRan2Add, _ := createNbIdentityProto(t, "ran2_add", "plmnId2_add", "nbId2_add", entities.ConnectionStatus_CONNECTED)
1450         nbIdIdentitiesProtoToAdd = append(nbIdIdentitiesProtoToAdd, protoRan1Add)
1451         nbIdIdentitiesProtoToAdd = append(nbIdIdentitiesProtoToAdd, protoRan2Add)
1452         sdlMock.On("AddMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToAdd).Return(nil)
1453
1454         var newNbIdIdentities []*entities.NbIdentity
1455         firstNewNbIdIdentity := &entities.NbIdentity{InventoryName: "ran1_add", ConnectionStatus: entities.ConnectionStatus_CONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1_add", NbId: "nbId1_add"}}
1456         secondNewNbIdIdentity := &entities.NbIdentity{InventoryName: "ran2_add", ConnectionStatus: entities.ConnectionStatus_CONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId2_add", NbId: "nbId2_add"}}
1457         newNbIdIdentities = append(newNbIdIdentities, firstNewNbIdIdentity)
1458         newNbIdIdentities = append(newNbIdIdentities, secondNewNbIdIdentity)
1459
1460         var oldNbIdIdentities []*entities.NbIdentity
1461         firstOldNbIdIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
1462         secondOldNbIdIdentity := &entities.NbIdentity{InventoryName: "ran2", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId2", NbId: "nbId2"}}
1463         oldNbIdIdentities = append(oldNbIdIdentities, firstOldNbIdIdentity)
1464         oldNbIdIdentities = append(oldNbIdIdentities, secondOldNbIdIdentity)
1465
1466         rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
1467         assert.Nil(t, rNibErr)
1468         sdlMock.AssertExpectations(t)
1469 }
1470
1471 func TestUpdateNbIdentityOldIdentityMarshalNilFailure(t *testing.T) {
1472         w, _ := initSdlMock()
1473
1474         oldNbIdIdentities := []*entities.NbIdentity{nil}
1475         newNbIdIdentities := []*entities.NbIdentity{
1476                 &entities.NbIdentity{
1477                         InventoryName:    "ran1_add",
1478                         ConnectionStatus: entities.ConnectionStatus_CONNECTED,
1479                         GlobalNbId:       &entities.GlobalNbId{PlmnId: "plmnId1_add", NbId: "nbId1_add"},
1480                 },
1481         }
1482
1483         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
1484         rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
1485         assert.Equal(t, expectedErr, rNibErr)
1486 }
1487
1488 func TestUpdateNbIdentityNewIdentityMarshalNilFailure(t *testing.T) {
1489         w, sdlMock := initSdlMock()
1490
1491         var nbIdIdentitiesProtoToRemove []interface{}
1492         protoRan1, _ := createNbIdentityProto(t, "ran1", "plmnId1", "nbId1", entities.ConnectionStatus_DISCONNECTED)
1493         nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan1)
1494         sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToRemove).Return(nil)
1495
1496         oldNbIdIdentities := []*entities.NbIdentity{
1497                 &entities.NbIdentity{
1498                         InventoryName:    "ran1",
1499                         ConnectionStatus: entities.ConnectionStatus_DISCONNECTED,
1500                         GlobalNbId:       &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"},
1501                 },
1502         }
1503         newNbIdIdentities := []*entities.NbIdentity{nil}
1504
1505         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
1506         rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
1507         assert.Equal(t, expectedErr, rNibErr)
1508 }
1509
1510 func TestUpdateNbIdentityRemoveFailure(t *testing.T) {
1511         w, sdlMock := initSdlMock()
1512
1513         var nbIdIdentitiesProtoToRemove []interface{}
1514         protoRan1, _ := createNbIdentityProto(t, "ran1", "plmnId1", "nbId1", entities.ConnectionStatus_DISCONNECTED)
1515         nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan1)
1516         protoRan2, _ := createNbIdentityProto(t, "ran2", "plmnId2", "nbId2", entities.ConnectionStatus_DISCONNECTED)
1517         nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan2)
1518
1519         sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToRemove).Return(fmt.Errorf("for test"))
1520
1521         var oldNbIdIdentities []*entities.NbIdentity
1522         firstOldNbIdIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
1523         secondOldNbIdIdentity := &entities.NbIdentity{InventoryName: "ran2", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId2", NbId: "nbId2"}}
1524         oldNbIdIdentities = append(oldNbIdIdentities, firstOldNbIdIdentity)
1525         oldNbIdIdentities = append(oldNbIdIdentities, secondOldNbIdIdentity)
1526
1527         var newNbIdIdentities []*entities.NbIdentity
1528
1529         rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
1530         assert.NotNil(t, rNibErr)
1531         sdlMock.AssertExpectations(t)
1532 }
1533
1534 func TestUpdateNbIdentitySdlAddMemberFailure(t *testing.T) {
1535         w, sdlMock := initSdlMock()
1536
1537         var nbIdIdentitiesProtoToRemove []interface{}
1538         protoRan1, _ := createNbIdentityProto(t, "ran1", "plmnId1", "nbId1", entities.ConnectionStatus_DISCONNECTED)
1539         nbIdIdentitiesProtoToRemove = append(nbIdIdentitiesProtoToRemove, protoRan1)
1540         sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToRemove).Return(nil)
1541
1542         var nbIdIdentitiesProtoToAdd []interface{}
1543         protoRan1Add, _ := createNbIdentityProto(t, "ran1_add", "plmnId1_add", "nbId1_add", entities.ConnectionStatus_CONNECTED)
1544         nbIdIdentitiesProtoToAdd = append(nbIdIdentitiesProtoToAdd, protoRan1Add)
1545         sdlMock.On("AddMember", namespace, entities.Node_ENB.String(), nbIdIdentitiesProtoToAdd).Return(fmt.Errorf("for test"))
1546
1547         var oldNbIdIdentities []*entities.NbIdentity
1548         firstOldNbIdIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
1549         oldNbIdIdentities = append(oldNbIdIdentities, firstOldNbIdIdentity)
1550
1551         var newNbIdIdentities []*entities.NbIdentity
1552         firstNewNbIdIdentity := &entities.NbIdentity{InventoryName: "ran1_add", ConnectionStatus: entities.ConnectionStatus_CONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1_add", NbId: "nbId1_add"}}
1553         newNbIdIdentities = append(newNbIdIdentities, firstNewNbIdIdentity)
1554
1555         rNibErr := w.UpdateNbIdentities(entities.Node_ENB, oldNbIdIdentities, newNbIdIdentities)
1556         assert.NotNil(t, rNibErr)
1557         sdlMock.AssertExpectations(t)
1558 }
1559
1560 func TestUpdateNbIdentityAddFailure(t *testing.T) {
1561         w, sdlMock := initSdlMock()
1562         nbIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
1563         nbIdData, err := proto.Marshal(nbIdentity)
1564         if err != nil {
1565                 t.Errorf("#TestRemoveNbIdentitySuccess - failed to Marshal NbIdentity")
1566         }
1567         sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), []interface{}{nbIdData}).Return(fmt.Errorf("for test"))
1568
1569         rNibErr := w.RemoveNbIdentity(entities.Node_ENB, nbIdentity)
1570         assert.NotNil(t, rNibErr)
1571         sdlMock.AssertExpectations(t)
1572 }
1573
1574 func TestUpdateNbIdentityNoNbIdentityToRemove(t *testing.T) {
1575         w, sdlMock := initSdlMock()
1576         nbIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
1577         nbIdData, err := proto.Marshal(nbIdentity)
1578         if err != nil {
1579                 t.Errorf("#TestRemoveNbIdentitySuccess - failed to Marshal NbIdentity")
1580         }
1581         sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), []interface{}{nbIdData}).Return(fmt.Errorf("for test"))
1582
1583         rNibErr := w.RemoveNbIdentity(entities.Node_ENB, nbIdentity)
1584         assert.NotNil(t, rNibErr)
1585         sdlMock.AssertExpectations(t)
1586 }
1587
1588 func TestUpdateNbIdentityNoNbIdentityToAdd(t *testing.T) {
1589         w, sdlMock := initSdlMock()
1590         nbIdentity := &entities.NbIdentity{InventoryName: "ran1", ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, GlobalNbId: &entities.GlobalNbId{PlmnId: "plmnId1", NbId: "nbId1"}}
1591         nbIdData, err := proto.Marshal(nbIdentity)
1592         if err != nil {
1593                 t.Errorf("#TestRemoveNbIdentitySuccess - failed to Marshal NbIdentity")
1594         }
1595         sdlMock.On("RemoveMember", namespace, entities.Node_ENB.String(), []interface{}{nbIdData}).Return(fmt.Errorf("for test"))
1596
1597         rNibErr := w.RemoveNbIdentity(entities.Node_ENB, nbIdentity)
1598         assert.NotNil(t, rNibErr)
1599         sdlMock.AssertExpectations(t)
1600 }
1601
1602 func createNbIdentityProto(t *testing.T, ranName string, plmnId string, nbId string, connectionStatus entities.ConnectionStatus) ([]byte, *entities.NbIdentity) {
1603         nbIdentity := &entities.NbIdentity{InventoryName: ranName, ConnectionStatus: connectionStatus, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}}
1604         nbIdData, err := proto.Marshal(nbIdentity)
1605         if err != nil {
1606                 t.Errorf("#createNbIdentityProto - failed to Marshal NbIdentity")
1607         }
1608         return nbIdData, nbIdentity
1609 }