[RIC-298] Add Update GNB impl.
[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/mocks"
24         "encoding/json"
25         "errors"
26         "fmt"
27         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
28         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
29         "github.com/golang/protobuf/proto"
30         "github.com/stretchr/testify/assert"
31         "testing"
32         "time"
33 )
34
35 var namespace = "namespace"
36
37 func initSdlInstanceMock(namespace string) (w RNibWriter, sdlInstanceMock *mocks.MockSdlInstance) {
38         sdlInstanceMock = new(mocks.MockSdlInstance)
39         w = GetRNibWriter(sdlInstanceMock)
40         return
41 }
42
43 func generateNodebInfo(inventoryName string, nodeType entities.Node_Type, plmnId string, nbId string) *entities.NodebInfo {
44         nodebInfo := &entities.NodebInfo{
45                 RanName:          inventoryName,
46                 GlobalNbId:       &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId},
47                 NodeType:         nodeType,
48                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
49         }
50
51         if nodeType == entities.Node_ENB {
52                 nodebInfo.Configuration = &entities.NodebInfo_Enb{
53                         Enb: &entities.Enb{},
54                 }
55         } else if nodeType == entities.Node_GNB {
56                 nodebInfo.Configuration = &entities.NodebInfo_Gnb{
57                         Gnb: &entities.Gnb{},
58                 }
59         }
60
61         return nodebInfo
62 }
63
64 func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell {
65
66         servedNrCells := []*entities.ServedNRCell{}
67
68         for _, v := range cellIds {
69                 servedNrCells = append(servedNrCells, &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{
70                         CellId: v,
71                         ChoiceNrMode: &entities.ServedNRCellInformation_ChoiceNRMode{
72                                 Fdd: &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{
73
74                                 },
75                         },
76                         NrMode:      entities.Nr_FDD,
77                         NrPci:       5,
78                         ServedPlmns: []string{"whatever"},
79                 }})
80         }
81
82         return servedNrCells
83 }
84
85 func TestUpdateGnbCellsInvalidNodebInfoFailure(t *testing.T) {
86         w, sdlInstanceMock := initSdlInstanceMock(namespace)
87         servedNrCells := generateServedNrCells("test1", "test2")
88         nodebInfo := &entities.NodebInfo{}
89         sdlInstanceMock.AssertNotCalled(t, "Set")
90         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
91         assert.IsType(t, &common.ValidationError{}, rNibErr)
92 }
93
94 func TestUpdateGnbCellsInvalidCellFailure(t *testing.T) {
95         inventoryName := "name"
96         plmnId := "02f829"
97         nbId := "4a952a0a"
98         w, sdlInstanceMock := initSdlInstanceMock(namespace)
99         servedNrCells := []*entities.ServedNRCell{{ServedNrCellInformation: &entities.ServedNRCellInformation{}}}
100         nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
101         nodebInfo.GetGnb().ServedNrCells = servedNrCells
102         sdlInstanceMock.AssertNotCalled(t, "Set")
103         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
104         assert.IsType(t, &common.ValidationError{}, rNibErr)
105 }
106
107 func getUpdateGnbCellsSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) []interface{} {
108
109         nodebInfoData, err := proto.Marshal(nodebInfo)
110         if err != nil {
111                 t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal NodeB entity. Error: %s", err)
112         }
113
114         nodebNameKey, _ := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
115         nodebIdKey, _ := common.ValidateAndBuildNodeBIdKey(nodebInfo.NodeType.String(), nodebInfo.GlobalNbId.PlmnId, nodebInfo.GlobalNbId.NbId)
116         setExpected := []interface{}{nodebNameKey, nodebInfoData, nodebIdKey, nodebInfoData}
117
118         for _, v := range servedNrCells {
119
120                 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: v}}
121                 cellData, err := proto.Marshal(&cellEntity)
122
123                 if err != nil {
124                         t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal cell entity. Error: %s", err)
125                 }
126
127                 nrCellIdKey, _ := common.ValidateAndBuildNrCellIdKey(v.GetServedNrCellInformation().GetCellId())
128                 cellNamePciKey, _ := common.ValidateAndBuildCellNamePciKey(nodebInfo.RanName, v.GetServedNrCellInformation().GetNrPci())
129                 setExpected = append(setExpected, nrCellIdKey, cellData, cellNamePciKey, cellData)
130         }
131
132         return setExpected
133 }
134
135 func TestUpdateGnbCellsSdlFailure(t *testing.T) {
136         inventoryName := "name"
137         plmnId := "02f829"
138         nbId := "4a952a0a"
139         w, sdlInstanceMock := initSdlInstanceMock(namespace)
140         servedNrCells := generateServedNrCells("test1", "test2")
141         nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
142         nodebInfo.GetGnb().ServedNrCells = servedNrCells
143         setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
144         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(errors.New("expected error"))
145         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
146         assert.IsType(t, &common.InternalError{}, rNibErr)
147 }
148
149 func TestUpdateGnbCellsSuccess(t *testing.T) {
150         inventoryName := "name"
151         plmnId := "02f829"
152         nbId := "4a952a0a"
153         w, sdlInstanceMock := initSdlInstanceMock(namespace)
154         servedNrCells := generateServedNrCells("test1", "test2")
155         nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
156         nodebInfo.GetGnb().ServedNrCells = servedNrCells
157         setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
158         var e error
159         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
160         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
161         assert.Nil(t, rNibErr)
162 }
163
164 func TestUpdateNodebInfoSuccess(t *testing.T) {
165         inventoryName := "name"
166         plmnId := "02f829"
167         nbId := "4a952a0a"
168         w, sdlInstanceMock := initSdlInstanceMock(namespace)
169         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
170         data, err := proto.Marshal(nodebInfo)
171         if err != nil {
172                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
173         }
174         var e error
175         var setExpected []interface{}
176
177         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
178         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
179         setExpected = append(setExpected, nodebNameKey, data)
180         setExpected = append(setExpected, nodebIdKey, data)
181
182         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
183
184         rNibErr := w.UpdateNodebInfo(nodebInfo)
185         assert.Nil(t, rNibErr)
186 }
187
188 func TestUpdateNodebInfoMissingInventoryNameFailure(t *testing.T) {
189         inventoryName := "name"
190         plmnId := "02f829"
191         nbId := "4a952a0a"
192         w, sdlInstanceMock := initSdlInstanceMock(namespace)
193         nodebInfo := &entities.NodebInfo{}
194         data, err := proto.Marshal(nodebInfo)
195         if err != nil {
196                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
197         }
198         var e error
199         var setExpected []interface{}
200
201         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
202         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
203         setExpected = append(setExpected, nodebNameKey, data)
204         setExpected = append(setExpected, nodebIdKey, data)
205
206         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
207
208         rNibErr := w.UpdateNodebInfo(nodebInfo)
209
210         assert.NotNil(t, rNibErr)
211         assert.IsType(t, &common.ValidationError{}, rNibErr)
212 }
213
214 func TestUpdateNodebInfoMissingGlobalNbId(t *testing.T) {
215         inventoryName := "name"
216         w, sdlInstanceMock := initSdlInstanceMock(namespace)
217         nodebInfo := &entities.NodebInfo{}
218         nodebInfo.RanName = inventoryName
219         data, err := proto.Marshal(nodebInfo)
220         if err != nil {
221                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
222         }
223         var e error
224         var setExpected []interface{}
225
226         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
227         setExpected = append(setExpected, nodebNameKey, data)
228         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
229
230         rNibErr := w.UpdateNodebInfo(nodebInfo)
231
232         assert.Nil(t, rNibErr)
233 }
234
235 func TestSaveEnb(t *testing.T) {
236         name := "name"
237         ranName := "RAN:" + name
238         w, sdlInstanceMock := initSdlInstanceMock(namespace)
239         nb := entities.NodebInfo{}
240         nb.NodeType = entities.Node_ENB
241         nb.ConnectionStatus = 1
242         nb.Ip = "localhost"
243         nb.Port = 5656
244         enb := entities.Enb{}
245         cell := &entities.ServedCellInfo{CellId: "aaff", Pci: 3}
246         cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
247         enb.ServedCells = []*entities.ServedCellInfo{cell}
248         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
249         data, err := proto.Marshal(&nb)
250         if err != nil {
251                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
252         }
253         var e error
254
255         cellData, err := proto.Marshal(&cellEntity)
256         if err != nil {
257                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal Cell entity. Error: %v", err)
258         }
259         var setExpected []interface{}
260         setExpected = append(setExpected, ranName, data)
261         setExpected = append(setExpected, "ENB:02f829:4a952a0a", data)
262         setExpected = append(setExpected, fmt.Sprintf("CELL:%s", cell.GetCellId()), cellData)
263         setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetPci()), cellData)
264
265         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
266
267         nbIdData, err := proto.Marshal(&entities.NbIdentity{InventoryName: name})
268         if err != nil {
269                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
270         }
271         sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
272
273         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
274         nbIdData, err = proto.Marshal(nbIdentity)
275         if err != nil {
276                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB Identity entity. Error: %v", err)
277         }
278         sdlInstanceMock.On("AddMember", "ENB", []interface{}{nbIdData}).Return(e)
279
280         rNibErr := w.SaveNodeb(nbIdentity, &nb)
281         assert.Nil(t, rNibErr)
282 }
283
284 func TestSaveEnbCellIdValidationFailure(t *testing.T) {
285         name := "name"
286         w, _ := initSdlInstanceMock(namespace)
287         nb := entities.NodebInfo{}
288         nb.NodeType = entities.Node_ENB
289         nb.ConnectionStatus = 1
290         nb.Ip = "localhost"
291         nb.Port = 5656
292         enb := entities.Enb{}
293         cell := &entities.ServedCellInfo{Pci: 3}
294         enb.ServedCells = []*entities.ServedCellInfo{cell}
295         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
296
297         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
298         rNibErr := w.SaveNodeb(nbIdentity, &nb)
299         assert.NotNil(t, rNibErr)
300         assert.IsType(t, &common.ValidationError{}, rNibErr)
301         assert.Equal(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", rNibErr.Error())
302 }
303
304 func TestSaveEnbInventoryNameValidationFailure(t *testing.T) {
305         w, _ := initSdlInstanceMock(namespace)
306         nb := entities.NodebInfo{}
307         nb.NodeType = entities.Node_ENB
308         nb.ConnectionStatus = 1
309         nb.Ip = "localhost"
310         nb.Port = 5656
311         enb := entities.Enb{}
312         cell := &entities.ServedCellInfo{CellId: "aaa", Pci: 3}
313         enb.ServedCells = []*entities.ServedCellInfo{cell}
314         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
315
316         nbIdentity := &entities.NbIdentity{InventoryName: "", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
317         rNibErr := w.SaveNodeb(nbIdentity, &nb)
318         assert.NotNil(t, rNibErr)
319         assert.IsType(t, &common.ValidationError{}, rNibErr)
320         assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error())
321 }
322
323 func TestSaveGnbCellIdValidationFailure(t *testing.T) {
324         name := "name"
325         w, _ := initSdlInstanceMock(namespace)
326         nb := entities.NodebInfo{}
327         nb.NodeType = entities.Node_GNB
328         nb.ConnectionStatus = 1
329         nb.Ip = "localhost"
330         nb.Port = 5656
331         gnb := entities.Gnb{}
332         cellInfo := &entities.ServedNRCellInformation{NrPci: 2}
333         cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
334         gnb.ServedNrCells = []*entities.ServedNRCell{cell}
335         nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
336
337         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
338         rNibErr := w.SaveNodeb(nbIdentity, &nb)
339         assert.NotNil(t, rNibErr)
340         assert.IsType(t, &common.ValidationError{}, rNibErr)
341         assert.Equal(t, "#utils.ValidateAndBuildNrCellIdKey - an empty cell id received", rNibErr.Error())
342 }
343
344 func TestSaveGnb(t *testing.T) {
345         name := "name"
346         ranName := "RAN:" + name
347         w, sdlInstanceMock := initSdlInstanceMock(namespace)
348         nb := entities.NodebInfo{}
349         nb.NodeType = entities.Node_GNB
350         nb.ConnectionStatus = 1
351         nb.Ip = "localhost"
352         nb.Port = 5656
353         gnb := entities.Gnb{}
354         cellInfo := &entities.ServedNRCellInformation{NrPci: 2, CellId: "ccdd"}
355         cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
356         cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
357         gnb.ServedNrCells = []*entities.ServedNRCell{cell}
358         nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
359         data, err := proto.Marshal(&nb)
360         if err != nil {
361                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB entity. Error: %v", err)
362         }
363         var e error
364
365         cellData, err := proto.Marshal(&cellEntity)
366         if err != nil {
367                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal Cell entity. Error: %v", err)
368         }
369         var setExpected []interface{}
370         setExpected = append(setExpected, ranName, data)
371         setExpected = append(setExpected, "GNB:02f829:4a952a0a", data)
372         setExpected = append(setExpected, fmt.Sprintf("NRCELL:%s", cell.GetServedNrCellInformation().GetCellId()), cellData)
373         setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetServedNrCellInformation().GetNrPci()), cellData)
374
375         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
376         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
377         nbIdData, err := proto.Marshal(nbIdentity)
378         if err != nil {
379                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB Identity entity. Error: %v", err)
380         }
381         sdlInstanceMock.On("AddMember", "GNB", []interface{}{nbIdData}).Return(e)
382
383         nbIdData, err = proto.Marshal(&entities.NbIdentity{InventoryName: name})
384         if err != nil {
385                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
386         }
387         sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
388
389         rNibErr := w.SaveNodeb(nbIdentity, &nb)
390         assert.Nil(t, rNibErr)
391 }
392
393 func TestSaveRanLoadInformationSuccess(t *testing.T) {
394         inventoryName := "name"
395         loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
396
397         if validationErr != nil {
398                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
399         }
400
401         w, sdlInstanceMock := initSdlInstanceMock(namespace)
402
403         ranLoadInformation := generateRanLoadInformation()
404         data, err := proto.Marshal(ranLoadInformation)
405
406         if err != nil {
407                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
408         }
409
410         var e error
411         var setExpected []interface{}
412         setExpected = append(setExpected, loadKey, data)
413         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
414
415         rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
416         assert.Nil(t, rNibErr)
417 }
418
419 func TestSaveRanLoadInformationMarshalNilFailure(t *testing.T) {
420         inventoryName := "name2"
421         w, _ := initSdlInstanceMock(namespace)
422
423         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
424         err := w.SaveRanLoadInformation(inventoryName, nil)
425         assert.Equal(t, expectedErr, err)
426 }
427
428 func TestSaveRanLoadInformationEmptyInventoryNameFailure(t *testing.T) {
429         inventoryName := ""
430         w, _ := initSdlInstanceMock(namespace)
431
432         err := w.SaveRanLoadInformation(inventoryName, nil)
433         assert.NotNil(t, err)
434         assert.IsType(t, &common.ValidationError{}, err)
435 }
436
437 func TestSaveRanLoadInformationSdlFailure(t *testing.T) {
438         inventoryName := "name2"
439
440         loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
441
442         if validationErr != nil {
443                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
444         }
445
446         w, sdlInstanceMock := initSdlInstanceMock(namespace)
447
448         ranLoadInformation := generateRanLoadInformation()
449         data, err := proto.Marshal(ranLoadInformation)
450
451         if err != nil {
452                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
453         }
454
455         expectedErr := errors.New("expected error")
456         var setExpected []interface{}
457         setExpected = append(setExpected, loadKey, data)
458         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
459
460         rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
461         assert.NotNil(t, rNibErr)
462         assert.IsType(t, &common.InternalError{}, rNibErr)
463 }
464
465 func generateCellLoadInformation() *entities.CellLoadInformation {
466         cellLoadInformation := entities.CellLoadInformation{}
467
468         cellLoadInformation.CellId = "123"
469
470         ulInterferenceOverloadIndication := entities.UlInterferenceOverloadIndication_HIGH_INTERFERENCE
471         cellLoadInformation.UlInterferenceOverloadIndications = []entities.UlInterferenceOverloadIndication{ulInterferenceOverloadIndication}
472
473         ulHighInterferenceInformation := entities.UlHighInterferenceInformation{
474                 TargetCellId:                 "456",
475                 UlHighInterferenceIndication: "xxx",
476         }
477
478         cellLoadInformation.UlHighInterferenceInfos = []*entities.UlHighInterferenceInformation{&ulHighInterferenceInformation}
479
480         cellLoadInformation.RelativeNarrowbandTxPower = &entities.RelativeNarrowbandTxPower{
481                 RntpPerPrb:                       "xxx",
482                 RntpThreshold:                    entities.RntpThreshold_NEG_4,
483                 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V1_ANT_PRT,
484                 PB:                               1,
485                 PdcchInterferenceImpact:          2,
486                 EnhancedRntp: &entities.EnhancedRntp{
487                         EnhancedRntpBitmap:     "xxx",
488                         RntpHighPowerThreshold: entities.RntpThreshold_NEG_2,
489                         EnhancedRntpStartTime:  &entities.StartTime{StartSfn: 500, StartSubframeNumber: 5},
490                 },
491         }
492
493         cellLoadInformation.AbsInformation = &entities.AbsInformation{
494                 Mode:                             entities.AbsInformationMode_ABS_INFO_FDD,
495                 AbsPatternInfo:                   "xxx",
496                 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V2_ANT_PRT,
497                 MeasurementSubset:                "xxx",
498         }
499
500         cellLoadInformation.InvokeIndication = entities.InvokeIndication_ABS_INFORMATION
501
502         cellLoadInformation.ExtendedUlInterferenceOverloadInfo = &entities.ExtendedUlInterferenceOverloadInfo{
503                 AssociatedSubframes:                       "xxx",
504                 ExtendedUlInterferenceOverloadIndications: cellLoadInformation.UlInterferenceOverloadIndications,
505         }
506
507         compInformationItem := &entities.CompInformationItem{
508                 CompHypothesisSets: []*entities.CompHypothesisSet{{CellId: "789", CompHypothesis: "xxx"}},
509                 BenefitMetric:      50,
510         }
511
512         cellLoadInformation.CompInformation = &entities.CompInformation{
513                 CompInformationItems:     []*entities.CompInformationItem{compInformationItem},
514                 CompInformationStartTime: &entities.StartTime{StartSfn: 123, StartSubframeNumber: 456},
515         }
516
517         cellLoadInformation.DynamicDlTransmissionInformation = &entities.DynamicDlTransmissionInformation{
518                 State:             entities.NaicsState_NAICS_ACTIVE,
519                 TransmissionModes: "xxx",
520                 PB:                2,
521                 PAList:            []entities.PA{entities.PA_DB_NEG_3},
522         }
523
524         return &cellLoadInformation
525 }
526
527 func generateRanLoadInformation() *entities.RanLoadInformation {
528         ranLoadInformation := entities.RanLoadInformation{}
529
530         ranLoadInformation.LoadTimestamp = uint64(time.Now().UnixNano())
531
532         cellLoadInformation := generateCellLoadInformation()
533         ranLoadInformation.CellLoadInfos = []*entities.CellLoadInformation{cellLoadInformation}
534
535         return &ranLoadInformation
536 }
537
538 func TestSaveNilEntityFailure(t *testing.T) {
539         w, _ := initSdlInstanceMock(namespace)
540         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
541         nbIdentity := &entities.NbIdentity{}
542         actualErr := w.SaveNodeb(nbIdentity, nil)
543         assert.Equal(t, expectedErr, actualErr)
544 }
545
546 func TestSaveUnknownTypeEntityFailure(t *testing.T) {
547         w, _ := initSdlInstanceMock(namespace)
548         expectedErr := common.NewValidationError("#rNibWriter.saveNodeB - Unknown responding node type, entity: ip:\"localhost\" port:5656 ")
549         nbIdentity := &entities.NbIdentity{InventoryName: "name", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
550         nb := &entities.NodebInfo{}
551         nb.Port = 5656
552         nb.Ip = "localhost"
553         actualErr := w.SaveNodeb(nbIdentity, nb)
554         assert.Equal(t, expectedErr, actualErr)
555 }
556
557 func TestSaveEntityFailure(t *testing.T) {
558         name := "name"
559         plmnId := "02f829"
560         nbId := "4a952a0a"
561
562         w, sdlInstanceMock := initSdlInstanceMock(namespace)
563         gnb := entities.NodebInfo{}
564         gnb.NodeType = entities.Node_GNB
565         data, err := proto.Marshal(&gnb)
566         if err != nil {
567                 t.Errorf("#rNibWriter_test.TestSaveEntityFailure - Failed to marshal NodeB entity. Error: %v", err)
568         }
569         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}}
570         setExpected := []interface{}{"RAN:" + name, data}
571         setExpected = append(setExpected, "GNB:"+plmnId+":"+nbId, data)
572         expectedErr := errors.New("expected error")
573         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
574         rNibErr := w.SaveNodeb(nbIdentity, &gnb)
575         assert.NotEmpty(t, rNibErr)
576 }
577
578 func TestGetRNibWriter(t *testing.T) {
579         received, _ := initSdlInstanceMock(namespace)
580         assert.NotEmpty(t, received)
581 }
582
583 func TestSaveE2TInstanceSuccess(t *testing.T) {
584         address := "10.10.2.15:9800"
585         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
586
587         if validationErr != nil {
588                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to build E2T Instance key. Error: %v", validationErr)
589         }
590
591         w, sdlInstanceMock := initSdlInstanceMock(namespace)
592
593         e2tInstance := generateE2tInstance(address)
594         data, err := json.Marshal(e2tInstance)
595
596         if err != nil {
597                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to marshal E2tInstance entity. Error: %v", err)
598         }
599
600         var e error
601         var setExpected []interface{}
602         setExpected = append(setExpected, loadKey, data)
603         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
604
605         rNibErr := w.SaveE2TInstance(e2tInstance)
606         assert.Nil(t, rNibErr)
607 }
608
609 func TestSaveE2TInstanceNullE2tInstanceFailure(t *testing.T) {
610         w, _ := initSdlInstanceMock(namespace)
611         var address string
612         e2tInstance := entities.NewE2TInstance(address, "test")
613         err := w.SaveE2TInstance(e2tInstance)
614         assert.NotNil(t, err)
615         assert.IsType(t, &common.ValidationError{}, err)
616 }
617
618 func TestSaveE2TInstanceSdlFailure(t *testing.T) {
619         address := "10.10.2.15:9800"
620         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
621
622         if validationErr != nil {
623                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to build E2T Instance key. Error: %v", validationErr)
624         }
625
626         w, sdlInstanceMock := initSdlInstanceMock(namespace)
627
628         e2tInstance := generateE2tInstance(address)
629         data, err := json.Marshal(e2tInstance)
630
631         if err != nil {
632                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to marshal E2tInstance entity. Error: %v", err)
633         }
634
635         expectedErr := errors.New("expected error")
636         var setExpected []interface{}
637         setExpected = append(setExpected, loadKey, data)
638         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
639
640         rNibErr := w.SaveE2TInstance(e2tInstance)
641         assert.NotNil(t, rNibErr)
642         assert.IsType(t, &common.InternalError{}, rNibErr)
643 }
644
645 func generateE2tInstance(address string) *entities.E2TInstance {
646         e2tInstance := entities.NewE2TInstance(address, "pod test")
647
648         e2tInstance.AssociatedRanList = []string{"test1", "test2"}
649
650         return e2tInstance
651 }
652
653 func TestSaveE2TAddressesSuccess(t *testing.T) {
654         address := "10.10.2.15:9800"
655         w, sdlInstanceMock := initSdlInstanceMock(namespace)
656
657         e2tAddresses := []string{address}
658         data, err := json.Marshal(e2tAddresses)
659
660         if err != nil {
661                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSuccess - Failed to marshal E2TInfoList. Error: %v", err)
662         }
663
664         var e error
665         var setExpected []interface{}
666         setExpected = append(setExpected, E2TAddressesKey, data)
667         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
668
669         rNibErr := w.SaveE2TAddresses(e2tAddresses)
670         assert.Nil(t, rNibErr)
671 }
672
673 func TestSaveE2TAddressesSdlFailure(t *testing.T) {
674         address := "10.10.2.15:9800"
675         w, sdlInstanceMock := initSdlInstanceMock(namespace)
676
677         e2tAddresses := []string{address}
678         data, err := json.Marshal(e2tAddresses)
679
680         if err != nil {
681                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSdlFailure - Failed to marshal E2TInfoList. Error: %v", err)
682         }
683
684         expectedErr := errors.New("expected error")
685         var setExpected []interface{}
686         setExpected = append(setExpected, E2TAddressesKey, data)
687         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
688
689         rNibErr := w.SaveE2TAddresses(e2tAddresses)
690         assert.NotNil(t, rNibErr)
691         assert.IsType(t, &common.InternalError{}, rNibErr)
692 }
693
694 func TestRemoveE2TInstanceSuccess(t *testing.T) {
695         address := "10.10.2.15:9800"
696         w, sdlInstanceMock := initSdlInstanceMock(namespace)
697
698         e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
699         var e error
700         sdlInstanceMock.On("Remove", e2tAddresses).Return(e)
701
702         rNibErr := w.RemoveE2TInstance(address)
703         assert.Nil(t, rNibErr)
704         sdlInstanceMock.AssertExpectations(t)
705 }
706
707 func TestRemoveE2TInstanceSdlFailure(t *testing.T) {
708         address := "10.10.2.15:9800"
709         w, sdlInstanceMock := initSdlInstanceMock(namespace)
710
711         e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
712         expectedErr := errors.New("expected error")
713         sdlInstanceMock.On("Remove", e2tAddresses).Return(expectedErr)
714
715         rNibErr := w.RemoveE2TInstance(address)
716         assert.IsType(t, &common.InternalError{}, rNibErr)
717 }
718
719 func TestRemoveE2TInstanceEmptyAddressFailure(t *testing.T) {
720         w, sdlInstanceMock := initSdlInstanceMock(namespace)
721
722         rNibErr := w.RemoveE2TInstance("")
723         assert.IsType(t, &common.ValidationError{}, rNibErr)
724         sdlInstanceMock.AssertExpectations(t)
725 }
726
727 //Integration tests
728 //
729 //func TestSaveEnbGnbInteg(t *testing.T){
730 //      for i := 0; i<10; i++{
731 //              Init("e2Manager", 1)
732 //              w := GetRNibWriter()
733 //              nb := entities.NodebInfo{}
734 //              nb.NodeType = entities.Node_ENB
735 //              nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED
736 //              nb.Ip = "localhost"
737 //              nb.Port = uint32(5656 + i)
738 //              enb := entities.Enb{}
739 //              cell1 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",111 + i), Pci:uint32(11 + i)}
740 //              cell2 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",222 + i), Pci:uint32(22 + i)}
741 //              cell3 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",333 + i), Pci:uint32(33 + i)}
742 //              enb.ServedCells = []*entities.ServedCellInfo{cell1, cell2, cell3}
743 //              nb.Configuration = &entities.NodebInfo_Enb{Enb:&enb}
744 //              plmnId := 0x02f828
745 //              nbId := 0x4a952a0a
746 //              nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameEnb%d" ,i), GlobalNbId:&entities.GlobalNbId{PlmnId:fmt.Sprintf("%02x", plmnId + i), NbId:fmt.Sprintf("%02x", nbId + i)}}
747 //              err := w.SaveNodeb(nbIdentity, &nb)
748 //              if err != nil{
749 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
750 //              }
751 //
752 //              nb1 := entities.NodebInfo{}
753 //              nb1.NodeType = entities.Node_GNB
754 //              nb1.ConnectionStatus = entities.ConnectionStatus_CONNECTED
755 //              nb1.Ip = "localhost"
756 //              nb1.Port =  uint32(6565 + i)
757 //              gnb := entities.Gnb{}
758 //              gCell1 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",1111 + i), NrPci:uint32(1 + i)}}
759 //              gCell2 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",2222 + i), NrPci:uint32(2 + i)}}
760 //              gCell3 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",3333 + i), NrPci:uint32(3 + i)}}
761 //              gnb.ServedNrCells = []*entities.ServedNRCell{gCell1, gCell2, gCell3,}
762 //              nb1.Configuration = &entities.NodebInfo_Gnb{Gnb:&gnb}
763 //              nbIdentity = &entities.NbIdentity{InventoryName: fmt.Sprintf("nameGnb%d" ,i), GlobalNbId:&entities.GlobalNbId{PlmnId:fmt.Sprintf("%02x", plmnId - i), NbId:fmt.Sprintf("%02x", nbId - i)}}
764 //              err = w.SaveNodeb(nbIdentity, &nb1)
765 //              if err != nil{
766 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
767 //              }
768 //      }
769 //}
770 //
771 //func TestSaveNbRanNamesInteg(t *testing.T){
772 //      for i := 0; i<10; i++{
773 //              Init("e2Manager", 1)
774 //              w := GetRNibWriter()
775 //              nb := entities.NodebInfo{}
776 //              nb.ConnectionStatus = entities.ConnectionStatus_CONNECTING
777 //              nb.Ip = "localhost"
778 //              nb.Port = uint32(5656 + i)
779 //              nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameOnly%d" ,i)}
780 //              err := w.SaveNodeb(nbIdentity, &nb)
781 //              if err != nil{
782 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
783 //              }
784 //      }
785 //}
786 //
787 //func TestSaveRanLoadInformationInteg(t *testing.T){
788 //              Init("e2Manager", 1)
789 //              w := GetRNibWriter()
790 //              ranLoadInformation := generateRanLoadInformation()
791 //              err := w.SaveRanLoadInformation("ran_integ", ranLoadInformation)
792 //              if err != nil{
793 //                      t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationInteg - Failed to save RanLoadInformation entity. Error: %v", err)
794 //              }
795 //}