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