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