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