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