Merge "[RICPLT-1898] - Replace http router to gorilla-mux + UT + Automation 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
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 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.Equal(t, common.VALIDATION_ERROR, rNibErr.GetCode())
227         assert.Equal(t, "3 VALIDATION_ERROR - #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.Equal(t, common.VALIDATION_ERROR, rNibErr.GetCode())
248         assert.Equal(t, "3 VALIDATION_ERROR - #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.Equal(t, common.VALIDATION_ERROR, rNibErr.GetCode())
295         assert.Equal(t, "3 VALIDATION_ERROR - #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.Equal(t, common.VALIDATION_ERROR, err.GetCode())
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.Equal(t, common.INTERNAL_ERROR, rNibErr.GetCode())
427         assert.Equal(t, expectedErr, rNibErr.GetError())
428 }
429
430 func generateCellLoadInformation() *entities.CellLoadInformation {
431         cellLoadInformation := entities.CellLoadInformation{}
432
433         cellLoadInformation.CellId = "123"
434
435         ulInterferenceOverloadIndication := entities.UlInterferenceOverloadIndication_HIGH_INTERFERENCE
436         cellLoadInformation.UlInterferenceOverloadIndications = []entities.UlInterferenceOverloadIndication{ulInterferenceOverloadIndication}
437
438         ulHighInterferenceInformation := entities.UlHighInterferenceInformation{
439                 TargetCellId:                 "456",
440                 UlHighInterferenceIndication: "xxx",
441         }
442
443         cellLoadInformation.UlHighInterferenceInfos = []*entities.UlHighInterferenceInformation{&ulHighInterferenceInformation}
444
445         cellLoadInformation.RelativeNarrowbandTxPower = &entities.RelativeNarrowbandTxPower{
446                 RntpPerPrb:                       "xxx",
447                 RntpThreshold:                    entities.RntpThreshold_NEG_4,
448                 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V1_ANT_PRT,
449                 PB:                               1,
450                 PdcchInterferenceImpact:          2,
451                 EnhancedRntp: &entities.EnhancedRntp{
452                         EnhancedRntpBitmap:     "xxx",
453                         RntpHighPowerThreshold: entities.RntpThreshold_NEG_2,
454                         EnhancedRntpStartTime:  &entities.StartTime{StartSfn: 500, StartSubframeNumber: 5},
455                 },
456         }
457
458         cellLoadInformation.AbsInformation = &entities.AbsInformation{
459                 Mode:                             entities.AbsInformationMode_ABS_INFO_FDD,
460                 AbsPatternInfo:                   "xxx",
461                 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V2_ANT_PRT,
462                 MeasurementSubset:                "xxx",
463         }
464
465         cellLoadInformation.InvokeIndication = entities.InvokeIndication_ABS_INFORMATION
466
467         cellLoadInformation.ExtendedUlInterferenceOverloadInfo = &entities.ExtendedUlInterferenceOverloadInfo{
468                 AssociatedSubframes:                       "xxx",
469                 ExtendedUlInterferenceOverloadIndications: cellLoadInformation.UlInterferenceOverloadIndications,
470         }
471
472         compInformationItem := &entities.CompInformationItem{
473                 CompHypothesisSets: []*entities.CompHypothesisSet{&entities.CompHypothesisSet{CellId: "789", CompHypothesis: "xxx"}},
474                 BenefitMetric:      50,
475         }
476
477         cellLoadInformation.CompInformation = &entities.CompInformation{
478                 CompInformationItems:     []*entities.CompInformationItem{compInformationItem},
479                 CompInformationStartTime: &entities.StartTime{StartSfn: 123, StartSubframeNumber: 456},
480         }
481
482         cellLoadInformation.DynamicDlTransmissionInformation = &entities.DynamicDlTransmissionInformation{
483                 State:             entities.NaicsState_NAICS_ACTIVE,
484                 TransmissionModes: "xxx",
485                 PB:                2,
486                 PAList:            []entities.PA{entities.PA_DB_NEG_3},
487         }
488
489         return &cellLoadInformation
490 }
491
492 func generateRanLoadInformation() *entities.RanLoadInformation {
493         ranLoadInformation := entities.RanLoadInformation{}
494
495         ranLoadInformation.LoadTimestamp = uint64(time.Now().UnixNano())
496
497         cellLoadInformation := generateCellLoadInformation()
498         ranLoadInformation.CellLoadInfos = []*entities.CellLoadInformation{cellLoadInformation}
499
500         return &ranLoadInformation
501 }
502
503 func TestSaveNilEntityFailure(t *testing.T) {
504         writerPool = nil
505         initSdlInstanceMock(namespace, 1)
506         w := GetRNibWriter()
507         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
508         nbIdentity := &entities.NbIdentity{}
509         actualErr := w.SaveNodeb(nbIdentity, nil)
510         assert.Equal(t, expectedErr, actualErr)
511 }
512
513 func TestSaveUnknownTypeEntityFailure(t *testing.T) {
514         writerPool = nil
515         initSdlInstanceMock(namespace, 1)
516         w := GetRNibWriter()
517         expectedErr := common.NewValidationError(errors.New("#rNibWriter.saveNodeB - Unknown responding node type, entity: ip:\"localhost\" port:5656 "))
518         nbIdentity := &entities.NbIdentity{InventoryName: "name", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
519         nb := &entities.NodebInfo{}
520         nb.Port = 5656
521         nb.Ip = "localhost"
522         actualErr := w.SaveNodeb(nbIdentity, nb)
523         assert.Equal(t, expectedErr, actualErr)
524 }
525
526 func TestSaveEntityFailure(t *testing.T) {
527         name := "name"
528         plmnId := "02f829"
529         nbId := "4a952a0a"
530
531         writerPool = nil
532         sdlInstanceMock := initSdlInstanceMock(namespace, 1)
533         w := GetRNibWriter()
534         gnb := entities.NodebInfo{}
535         gnb.NodeType = entities.Node_GNB
536         data, err := proto.Marshal(&gnb)
537         if err != nil {
538                 t.Errorf("#rNibWriter_test.TestSaveEntityFailure - Failed to marshal NodeB entity. Error: %v", err)
539         }
540         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}}
541         setExpected := []interface{}{"RAN:" + name, data}
542         setExpected = append(setExpected, "GNB:"+plmnId+":"+nbId, data)
543         expectedErr := errors.New("expected error")
544         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
545         rNibErr := w.SaveNodeb(nbIdentity, &gnb)
546         assert.NotEmpty(t, rNibErr)
547 }
548
549 func TestGetRNibWriterPoolNotInitializedFailure(t *testing.T) {
550         writerPool = nil
551         assert.Panics(t, func() { GetRNibWriter() })
552 }
553
554 func TestGetRNibWriter(t *testing.T) {
555         writerPool = nil
556         initSdlInstanceMock(namespace, 1)
557         received := GetRNibWriter()
558         assert.NotEmpty(t, received)
559         available, created := writerPool.Stats()
560         assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
561         assert.Equal(t, 1, created, "number of created objects in the writerPool should be 1")
562         writerPool.Close()
563 }
564
565 func TestClose(t *testing.T) {
566         writerPool = nil
567         instanceMock := initSdlInstanceMock(namespace, 2)
568         w1 := GetRNibWriter()
569         w2 := GetRNibWriter()
570         writerPool.Put(w1)
571         writerPool.Put(w2)
572         available, created := writerPool.Stats()
573         assert.Equal(t, 2, available, "number of available objects in the writerPool should be 2")
574         assert.Equal(t, 2, created, "number of created objects in the writerPool should be 2")
575         var e error
576         instanceMock.On("Close").Return(e)
577         Close()
578         available, created = writerPool.Stats()
579         assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
580         assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
581 }
582
583 func TestCloseOnClosedPoolFailure(t *testing.T) {
584         writerPool = nil
585         instanceMock := initSdlInstanceMock(namespace, 1)
586         w1 := GetRNibWriter()
587         writerPool.Put(w1)
588         available, created := writerPool.Stats()
589         assert.Equal(t, 1, available, "number of available objects in the writerPool should be 1")
590         assert.Equal(t, 1, created, "number of created objects in the writerPool should be 1")
591         var e error
592         instanceMock.On("Close").Return(e)
593         Close()
594         assert.Panics(t, func() { Close() })
595 }
596
597 func TestCloseFailure(t *testing.T) {
598         writerPool = nil
599         instanceMock := initSdlInstanceMock(namespace, 2)
600         w1 := GetRNibWriter()
601         writerPool.Put(w1)
602         available, created := writerPool.Stats()
603         assert.Equal(t, 1, available, "number of available objects in the writerPool should be 1")
604         assert.Equal(t, 1, created, "number of created objects in the writerPool should be 1")
605         e := errors.New("expected error")
606         instanceMock.On("Close").Return(e)
607         Close()
608         available, created = writerPool.Stats()
609         assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
610         assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
611 }
612
613 func TestInit(t *testing.T) {
614         writerPool = nil
615         Init("", 1)
616         assert.NotNil(t, writerPool)
617         assert.NotNil(t, writerPool.New)
618         assert.NotNil(t, writerPool.Destroy)
619         available, created := writerPool.Stats()
620         assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
621         assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
622 }
623
624 //Integration tests
625 //
626 //func TestSaveEnbGnbInteg(t *testing.T){
627 //      for i := 0; i<10; i++{
628 //              Init("e2Manager", 1)
629 //              w := GetRNibWriter()
630 //              nb := entities.NodebInfo{}
631 //              nb.NodeType = entities.Node_ENB
632 //              nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED
633 //              nb.Ip = "localhost"
634 //              nb.Port = uint32(5656 + i)
635 //              enb := entities.Enb{}
636 //              cell1 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",111 + i), Pci:uint32(11 + i)}
637 //              cell2 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",222 + i), Pci:uint32(22 + i)}
638 //              cell3 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",333 + i), Pci:uint32(33 + i)}
639 //              enb.ServedCells = []*entities.ServedCellInfo{cell1, cell2, cell3}
640 //              nb.Configuration = &entities.NodebInfo_Enb{Enb:&enb}
641 //              plmnId := 0x02f828
642 //              nbId := 0x4a952a0a
643 //              nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameEnb%d" ,i), GlobalNbId:&entities.GlobalNbId{PlmnId:fmt.Sprintf("%02x", plmnId + i), NbId:fmt.Sprintf("%02x", nbId + i)}}
644 //              err := w.SaveNodeb(nbIdentity, &nb)
645 //              if err != nil{
646 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
647 //              }
648 //
649 //              nb1 := entities.NodebInfo{}
650 //              nb1.NodeType = entities.Node_GNB
651 //              nb1.ConnectionStatus = entities.ConnectionStatus_CONNECTED
652 //              nb1.Ip = "localhost"
653 //              nb1.Port =  uint32(6565 + i)
654 //              gnb := entities.Gnb{}
655 //              gCell1 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",1111 + i), NrPci:uint32(1 + i)}}
656 //              gCell2 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",2222 + i), NrPci:uint32(2 + i)}}
657 //              gCell3 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",3333 + i), NrPci:uint32(3 + i)}}
658 //              gnb.ServedNrCells = []*entities.ServedNRCell{gCell1, gCell2, gCell3,}
659 //              nb1.Configuration = &entities.NodebInfo_Gnb{Gnb:&gnb}
660 //              nbIdentity = &entities.NbIdentity{InventoryName: fmt.Sprintf("nameGnb%d" ,i), GlobalNbId:&entities.GlobalNbId{PlmnId:fmt.Sprintf("%02x", plmnId - i), NbId:fmt.Sprintf("%02x", nbId - i)}}
661 //              err = w.SaveNodeb(nbIdentity, &nb1)
662 //              if err != nil{
663 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
664 //              }
665 //      }
666 //}
667 //
668 //func TestSaveNbRanNamesInteg(t *testing.T){
669 //      for i := 0; i<10; i++{
670 //              Init("e2Manager", 1)
671 //              w := GetRNibWriter()
672 //              nb := entities.NodebInfo{}
673 //              nb.ConnectionStatus = entities.ConnectionStatus_CONNECTING
674 //              nb.Ip = "localhost"
675 //              nb.Port = uint32(5656 + i)
676 //              nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameOnly%d" ,i)}
677 //              err := w.SaveNodeb(nbIdentity, &nb)
678 //              if err != nil{
679 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
680 //              }
681 //      }
682 //}
683 //
684 //func TestSaveRanLoadInformationInteg(t *testing.T){
685 //              Init("e2Manager", 1)
686 //              w := GetRNibWriter()
687 //              ranLoadInformation := generateRanLoadInformation()
688 //              err := w.SaveRanLoadInformation("ran_integ", ranLoadInformation)
689 //              if err != nil{
690 //                      t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationInteg - Failed to save RanLoadInformation entity. Error: %v", err)
691 //              }
692 //}