c09ba79c83d80f41ae2cf8c5bd09be396d768247
[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 func TestSaveE2TInstanceSuccess(t *testing.T) {
467         address := "10.10.2.15:9800"
468         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
469
470         if validationErr != nil {
471                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to build E2T Instance key. Error: %v", validationErr)
472         }
473
474         w, sdlInstanceMock := initSdlInstanceMock(namespace)
475
476         e2tInstance := generateE2tInstance(address)
477         data, err := json.Marshal(e2tInstance)
478
479         if err != nil {
480                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to marshal E2tInstance entity. Error: %v", err)
481         }
482
483         var e error
484         var setExpected []interface{}
485         setExpected = append(setExpected, loadKey, data)
486         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
487
488         rNibErr := w.SaveE2TInstance(e2tInstance)
489         assert.Nil(t, rNibErr)
490 }
491
492 func TestSaveE2TInstanceNullE2tInstanceFailure(t *testing.T) {
493         w, _ := initSdlInstanceMock(namespace)
494         var address string
495         e2tInstance := entities.NewE2TInstance(address)
496         err := w.SaveE2TInstance(e2tInstance)
497         assert.NotNil(t, err)
498         assert.IsType(t, &common.ValidationError{}, err)
499 }
500
501 func TestSaveE2TInstanceSdlFailure(t *testing.T) {
502         address := "10.10.2.15:9800"
503         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
504
505         if validationErr != nil {
506                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to build E2T Instance key. Error: %v", validationErr)
507         }
508
509         w, sdlInstanceMock := initSdlInstanceMock(namespace)
510
511         e2tInstance := generateE2tInstance(address)
512         data, err := json.Marshal(e2tInstance)
513
514         if err != nil {
515                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to marshal E2tInstance entity. Error: %v", err)
516         }
517
518         expectedErr := errors.New("expected error")
519         var setExpected []interface{}
520         setExpected = append(setExpected, loadKey, data)
521         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
522
523         rNibErr := w.SaveE2TInstance(e2tInstance)
524         assert.NotNil(t, rNibErr)
525         assert.IsType(t, &common.InternalError{}, rNibErr)
526 }
527
528 func generateE2tInstance(address string) *entities.E2TInstance {
529         e2tInstance := entities.NewE2TInstance(address)
530
531         e2tInstance.AssociatedRanList = []string{"test1", "test2"}
532
533         return e2tInstance
534 }
535
536 func TestSaveE2TInfoListSuccess(t *testing.T) {
537         address := "10.10.2.15:9800"
538         w, sdlInstanceMock := initSdlInstanceMock(namespace)
539
540         e2tAddresses := []string{address}
541         data, err := json.Marshal(e2tAddresses)
542
543         if err != nil {
544                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSuccess - Failed to marshal E2TInfoList. Error: %v", err)
545         }
546
547         var e error
548         var setExpected []interface{}
549         setExpected = append(setExpected, E2TAddressesKey, data)
550         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
551
552         rNibErr := w.SaveE2TAddresses(e2tAddresses)
553         assert.Nil(t, rNibErr)
554 }
555
556 func TestSaveE2TInfoListSdlFailure(t *testing.T) {
557         address := "10.10.2.15:9800"
558         w, sdlInstanceMock := initSdlInstanceMock(namespace)
559
560         e2tAddresses := []string{address}
561         data, err := json.Marshal(e2tAddresses)
562
563         if err != nil {
564                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSdlFailure - Failed to marshal E2TInfoList. Error: %v", err)
565         }
566
567         expectedErr := errors.New("expected error")
568         var setExpected []interface{}
569         setExpected = append(setExpected, E2TAddressesKey, data)
570         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
571
572         rNibErr := w.SaveE2TAddresses(e2tAddresses)
573         assert.NotNil(t, rNibErr)
574         assert.IsType(t, &common.InternalError{}, rNibErr)
575 }
576
577 //Integration tests
578 //
579 //func TestSaveEnbGnbInteg(t *testing.T){
580 //      for i := 0; i<10; i++{
581 //              Init("e2Manager", 1)
582 //              w := GetRNibWriter()
583 //              nb := entities.NodebInfo{}
584 //              nb.NodeType = entities.Node_ENB
585 //              nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED
586 //              nb.Ip = "localhost"
587 //              nb.Port = uint32(5656 + i)
588 //              enb := entities.Enb{}
589 //              cell1 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",111 + i), Pci:uint32(11 + i)}
590 //              cell2 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",222 + i), Pci:uint32(22 + i)}
591 //              cell3 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",333 + i), Pci:uint32(33 + i)}
592 //              enb.ServedCells = []*entities.ServedCellInfo{cell1, cell2, cell3}
593 //              nb.Configuration = &entities.NodebInfo_Enb{Enb:&enb}
594 //              plmnId := 0x02f828
595 //              nbId := 0x4a952a0a
596 //              nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameEnb%d" ,i), GlobalNbId:&entities.GlobalNbId{PlmnId:fmt.Sprintf("%02x", plmnId + i), NbId:fmt.Sprintf("%02x", nbId + i)}}
597 //              err := w.SaveNodeb(nbIdentity, &nb)
598 //              if err != nil{
599 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
600 //              }
601 //
602 //              nb1 := entities.NodebInfo{}
603 //              nb1.NodeType = entities.Node_GNB
604 //              nb1.ConnectionStatus = entities.ConnectionStatus_CONNECTED
605 //              nb1.Ip = "localhost"
606 //              nb1.Port =  uint32(6565 + i)
607 //              gnb := entities.Gnb{}
608 //              gCell1 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",1111 + i), NrPci:uint32(1 + i)}}
609 //              gCell2 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",2222 + i), NrPci:uint32(2 + i)}}
610 //              gCell3 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",3333 + i), NrPci:uint32(3 + i)}}
611 //              gnb.ServedNrCells = []*entities.ServedNRCell{gCell1, gCell2, gCell3,}
612 //              nb1.Configuration = &entities.NodebInfo_Gnb{Gnb:&gnb}
613 //              nbIdentity = &entities.NbIdentity{InventoryName: fmt.Sprintf("nameGnb%d" ,i), GlobalNbId:&entities.GlobalNbId{PlmnId:fmt.Sprintf("%02x", plmnId - i), NbId:fmt.Sprintf("%02x", nbId - i)}}
614 //              err = w.SaveNodeb(nbIdentity, &nb1)
615 //              if err != nil{
616 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
617 //              }
618 //      }
619 //}
620 //
621 //func TestSaveNbRanNamesInteg(t *testing.T){
622 //      for i := 0; i<10; i++{
623 //              Init("e2Manager", 1)
624 //              w := GetRNibWriter()
625 //              nb := entities.NodebInfo{}
626 //              nb.ConnectionStatus = entities.ConnectionStatus_CONNECTING
627 //              nb.Ip = "localhost"
628 //              nb.Port = uint32(5656 + i)
629 //              nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameOnly%d" ,i)}
630 //              err := w.SaveNodeb(nbIdentity, &nb)
631 //              if err != nil{
632 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
633 //              }
634 //      }
635 //}
636 //
637 //func TestSaveRanLoadInformationInteg(t *testing.T){
638 //              Init("e2Manager", 1)
639 //              w := GetRNibWriter()
640 //              ranLoadInformation := generateRanLoadInformation()
641 //              err := w.SaveRanLoadInformation("ran_integ", ranLoadInformation)
642 //              if err != nil{
643 //                      t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationInteg - Failed to save RanLoadInformation entity. Error: %v", err)
644 //              }
645 //}