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