change tag version
[ric-plt/e2mgr.git] / E2Manager / rNibWriter / rNibWriter_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20
21 package rNibWriter
22
23 import (
24         "e2mgr/mocks"
25         "errors"
26         "fmt"
27         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
28         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
29         "github.com/golang/protobuf/proto"
30         "github.com/stretchr/testify/assert"
31         "testing"
32         "time"
33 )
34
35 func TestInitRNibWriter(t *testing.T) {
36         writerPool = nil
37         initSdlInstanceMock(namespace, 1)
38         available, created := writerPool.Stats()
39         assert.Equal(t, available, 0, "number of available objects in the writerPool should be 0")
40         assert.Equal(t, created, 0, "number of created objects in the writerPool should be 0")
41         w := GetRNibWriter()
42         assert.NotNil(t, w)
43 }
44
45 func TestInitPool(t *testing.T) {
46         writerPool = nil
47         sdlInstanceMock := new(mocks.MockSdlInstance)
48         initPool(1, func() interface{} {
49                 sdlI := common.ISdlInstance(sdlInstanceMock)
50                 return &rNibWriterInstance{sdl: &sdlI, namespace: namespace}
51         },
52                 func(obj interface{}) {
53                 },
54         )
55         assert.NotNil(t, writerPool)
56         assert.NotNil(t, writerPool.New)
57         assert.NotNil(t, writerPool.Destroy)
58         available, created := writerPool.Stats()
59         assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
60         assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
61 }
62
63 var namespace = "namespace"
64
65 func initSdlInstanceMock(namespace string, poolSize int) *mocks.MockSdlInstance {
66         sdlInstanceMock := new(mocks.MockSdlInstance)
67         initPool(poolSize, func() interface{} {
68                 sdlI := common.ISdlInstance(sdlInstanceMock)
69                 return &rNibWriterInstance{sdl: &sdlI, namespace: namespace}
70         },
71                 func(obj interface{}) {
72                 },
73         )
74         return sdlInstanceMock
75 }
76
77 func TestUpdateNodebInfoSuccess(t *testing.T) {
78         inventoryName := "name"
79         plmnId := "02f829"
80         nbId := "4a952a0a"
81         writerPool = nil
82         sdlInstanceMock := initSdlInstanceMock(namespace, 1)
83         w := GetRNibWriter()
84         nodebInfo := &entities.NodebInfo{}
85         nodebInfo.RanName = inventoryName
86         nodebInfo.GlobalNbId = &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}
87         nodebInfo.NodeType = entities.Node_ENB
88         nodebInfo.ConnectionStatus = 1
89         enb := entities.Enb{}
90         nodebInfo.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
91         data, err := proto.Marshal(nodebInfo)
92         if err != nil {
93                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
94         }
95         var e error
96         var setExpected []interface{}
97
98         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
99         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
100         setExpected = append(setExpected, nodebNameKey, data)
101         setExpected = append(setExpected, nodebIdKey, data)
102
103         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
104
105         rNibErr := w.UpdateNodebInfo(nodebInfo)
106         assert.Nil(t, rNibErr)
107 }
108
109 func TestUpdateNodebInfoMissingInventoryNameFailure(t *testing.T) {
110         inventoryName := "name"
111         plmnId := "02f829"
112         nbId := "4a952a0a"
113         writerPool = nil
114         sdlInstanceMock := initSdlInstanceMock(namespace, 1)
115         w := GetRNibWriter()
116         nodebInfo := &entities.NodebInfo{}
117         data, err := proto.Marshal(nodebInfo)
118         if err != nil {
119                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
120         }
121         var e error
122         var setExpected []interface{}
123
124         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
125         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
126         setExpected = append(setExpected, nodebNameKey, data)
127         setExpected = append(setExpected, nodebIdKey, data)
128
129         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
130
131         rNibErr := w.UpdateNodebInfo(nodebInfo)
132
133         assert.NotNil(t, rNibErr)
134         assert.IsType(t, &common.ValidationError{}, rNibErr)
135 }
136
137 func TestUpdateNodebInfoMissingGlobalNbId(t *testing.T) {
138         inventoryName := "name"
139         writerPool = nil
140         sdlInstanceMock := initSdlInstanceMock(namespace, 1)
141         w := GetRNibWriter()
142         nodebInfo := &entities.NodebInfo{}
143         nodebInfo.RanName = inventoryName
144         data, err := proto.Marshal(nodebInfo)
145         if err != nil {
146                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
147         }
148         var e error
149         var setExpected []interface{}
150
151         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
152         setExpected = append(setExpected, nodebNameKey, data)
153         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
154
155         rNibErr := w.UpdateNodebInfo(nodebInfo)
156
157         assert.Nil(t, rNibErr)
158 }
159
160 func TestSaveEnb(t *testing.T) {
161         name := "name"
162         ranName := "RAN:" + name
163         writerPool = nil
164         sdlInstanceMock := initSdlInstanceMock(namespace, 1)
165         w := GetRNibWriter()
166         nb := entities.NodebInfo{}
167         nb.NodeType = entities.Node_ENB
168         nb.ConnectionStatus = 1
169         nb.Ip = "localhost"
170         nb.Port = 5656
171         enb := entities.Enb{}
172         cell := &entities.ServedCellInfo{CellId: "aaff", Pci: 3}
173         cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
174         enb.ServedCells = []*entities.ServedCellInfo{cell}
175         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
176         data, err := proto.Marshal(&nb)
177         if err != nil {
178                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
179         }
180         var e error
181
182         cellData, err := proto.Marshal(&cellEntity)
183         if err != nil {
184                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal Cell entity. Error: %v", err)
185         }
186         var setExpected []interface{}
187         setExpected = append(setExpected, ranName, data)
188         setExpected = append(setExpected, "ENB:02f829:4a952a0a", data)
189         setExpected = append(setExpected, fmt.Sprintf("CELL:%s", cell.GetCellId()), cellData)
190         setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetPci()), cellData)
191
192         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
193
194         nbIdData, err := proto.Marshal(&entities.NbIdentity{InventoryName: name})
195         if err != nil {
196                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
197         }
198         sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
199
200         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
201         nbIdData, err = proto.Marshal(nbIdentity)
202         if err != nil {
203                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB Identity entity. Error: %v", err)
204         }
205         sdlInstanceMock.On("AddMember", "ENB", []interface{}{nbIdData}).Return(e)
206
207         rNibErr := w.SaveNodeb(nbIdentity, &nb)
208         assert.Nil(t, rNibErr)
209 }
210
211 func TestSaveEnbCellIdValidationFailure(t *testing.T) {
212         name := "name"
213         writerPool = nil
214         initSdlInstanceMock(namespace, 1)
215         w := GetRNibWriter()
216         nb := entities.NodebInfo{}
217         nb.NodeType = entities.Node_ENB
218         nb.ConnectionStatus = 1
219         nb.Ip = "localhost"
220         nb.Port = 5656
221         enb := entities.Enb{}
222         cell := &entities.ServedCellInfo{Pci: 3}
223         enb.ServedCells = []*entities.ServedCellInfo{cell}
224         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
225
226         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
227         rNibErr := w.SaveNodeb(nbIdentity, &nb)
228         assert.NotNil(t, rNibErr)
229         assert.IsType(t, &common.ValidationError{}, rNibErr)
230         assert.Equal(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", rNibErr.Error())
231 }
232
233 func TestSaveEnbInventoryNameValidationFailure(t *testing.T) {
234         writerPool = nil
235         initSdlInstanceMock(namespace, 1)
236         w := GetRNibWriter()
237         nb := entities.NodebInfo{}
238         nb.NodeType = entities.Node_ENB
239         nb.ConnectionStatus = 1
240         nb.Ip = "localhost"
241         nb.Port = 5656
242         enb := entities.Enb{}
243         cell := &entities.ServedCellInfo{CellId: "aaa", Pci: 3}
244         enb.ServedCells = []*entities.ServedCellInfo{cell}
245         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
246
247         nbIdentity := &entities.NbIdentity{InventoryName: "", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
248         rNibErr := w.SaveNodeb(nbIdentity, &nb)
249         assert.NotNil(t, rNibErr)
250         assert.IsType(t, &common.ValidationError{}, rNibErr)
251         assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error())
252 }
253
254 func TestSaveEnbOnClosedPool(t *testing.T) {
255         name := "name"
256         writerPool = nil
257         sdlInstanceMock := initSdlInstanceMock(namespace, 1)
258         w := GetRNibWriter()
259         nb := entities.NodebInfo{}
260         nb.NodeType = entities.Node_ENB
261         nb.ConnectionStatus = 1
262         nb.Ip = "localhost"
263         nb.Port = 5656
264         enb := entities.Enb{}
265         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
266         data, err := proto.Marshal(&nb)
267         if err != nil {
268                 t.Errorf("#rNibWriter_test.TestSaveEnbOnClosedPool - Failed to marshal NodeB entity. Error: %v", err)
269         }
270         setExpected := []interface{}{name, data}
271         var e error
272         sdlInstanceMock.On("Set", setExpected).Return(e)
273         writerPool.Close()
274         nbIdentity := &entities.NbIdentity{}
275         assert.Panics(t, func() { w.SaveNodeb(nbIdentity, &nb) })
276 }
277
278 func TestSaveGnbCellIdValidationFailure(t *testing.T) {
279         name := "name"
280         writerPool = nil
281         initSdlInstanceMock(namespace, 1)
282         w := GetRNibWriter()
283         nb := entities.NodebInfo{}
284         nb.NodeType = entities.Node_GNB
285         nb.ConnectionStatus = 1
286         nb.Ip = "localhost"
287         nb.Port = 5656
288         gnb := entities.Gnb{}
289         cellInfo := &entities.ServedNRCellInformation{NrPci: 2}
290         cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
291         gnb.ServedNrCells = []*entities.ServedNRCell{cell}
292         nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
293
294         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
295         rNibErr := w.SaveNodeb(nbIdentity, &nb)
296         assert.NotNil(t, rNibErr)
297         assert.IsType(t, &common.ValidationError{}, rNibErr)
298         assert.Equal(t, "#utils.ValidateAndBuildNrCellIdKey - an empty cell id received", rNibErr.Error())
299 }
300
301 func TestSaveGnb(t *testing.T) {
302         name := "name"
303         ranName := "RAN:" + name
304         writerPool = nil
305         sdlInstanceMock := initSdlInstanceMock(namespace, 1)
306         w := GetRNibWriter()
307         nb := entities.NodebInfo{}
308         nb.NodeType = entities.Node_GNB
309         nb.ConnectionStatus = 1
310         nb.Ip = "localhost"
311         nb.Port = 5656
312         gnb := entities.Gnb{}
313         cellInfo := &entities.ServedNRCellInformation{NrPci: 2, CellId: "ccdd"}
314         cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
315         cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
316         gnb.ServedNrCells = []*entities.ServedNRCell{cell}
317         nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
318         data, err := proto.Marshal(&nb)
319         if err != nil {
320                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB entity. Error: %v", err)
321         }
322         var e error
323
324         cellData, err := proto.Marshal(&cellEntity)
325         if err != nil {
326                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal Cell entity. Error: %v", err)
327         }
328         var setExpected []interface{}
329         setExpected = append(setExpected, ranName, data)
330         setExpected = append(setExpected, "GNB:02f829:4a952a0a", data)
331         setExpected = append(setExpected, fmt.Sprintf("NRCELL:%s", cell.GetServedNrCellInformation().GetCellId()), cellData)
332         setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetServedNrCellInformation().GetNrPci()), cellData)
333
334         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
335         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
336         nbIdData, err := proto.Marshal(nbIdentity)
337         if err != nil {
338                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB Identity entity. Error: %v", err)
339         }
340         sdlInstanceMock.On("AddMember", "GNB", []interface{}{nbIdData}).Return(e)
341
342         nbIdData, err = proto.Marshal(&entities.NbIdentity{InventoryName: name})
343         if err != nil {
344                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
345         }
346         sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
347
348         rNibErr := w.SaveNodeb(nbIdentity, &nb)
349         assert.Nil(t, rNibErr)
350 }
351
352 func TestSaveRanLoadInformationSuccess(t *testing.T) {
353         inventoryName := "name"
354         loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
355
356         if validationErr != nil {
357                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
358         }
359
360         writerPool = nil
361         sdlInstanceMock := initSdlInstanceMock(namespace, 1)
362         w := GetRNibWriter()
363
364         ranLoadInformation := generateRanLoadInformation()
365         data, err := proto.Marshal(ranLoadInformation)
366
367         if err != nil {
368                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
369         }
370
371         var e error
372         var setExpected []interface{}
373         setExpected = append(setExpected, loadKey, data)
374         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
375
376         rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
377         assert.Nil(t, rNibErr)
378 }
379
380 func TestSaveRanLoadInformationMarshalNilFailure(t *testing.T) {
381         inventoryName := "name2"
382         writerPool = nil
383         initSdlInstanceMock(namespace, 1)
384         w := GetRNibWriter()
385
386         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
387         err := w.SaveRanLoadInformation(inventoryName, nil)
388         assert.Equal(t, expectedErr, err)
389 }
390
391 func TestSaveRanLoadInformationEmptyInventoryNameFailure(t *testing.T) {
392         inventoryName := ""
393         writerPool = nil
394         initSdlInstanceMock(namespace, 1)
395         w := GetRNibWriter()
396
397         err := w.SaveRanLoadInformation(inventoryName, nil)
398         assert.NotNil(t, err)
399         assert.IsType(t, &common.ValidationError{}, err)
400 }
401
402 func TestSaveRanLoadInformationSdlFailure(t *testing.T) {
403         inventoryName := "name2"
404
405         loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
406
407         if validationErr != nil {
408                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
409         }
410
411         writerPool = nil
412         sdlInstanceMock := initSdlInstanceMock(namespace, 1)
413         w := GetRNibWriter()
414
415         ranLoadInformation := generateRanLoadInformation()
416         data, err := proto.Marshal(ranLoadInformation)
417
418         if err != nil {
419                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
420         }
421
422         expectedErr := errors.New("expected error")
423         var setExpected []interface{}
424         setExpected = append(setExpected, loadKey, data)
425         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
426
427         rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
428         assert.NotNil(t, rNibErr)
429         assert.IsType(t, &common.InternalError{}, rNibErr)
430 }
431
432 func generateCellLoadInformation() *entities.CellLoadInformation {
433         cellLoadInformation := entities.CellLoadInformation{}
434
435         cellLoadInformation.CellId = "123"
436
437         ulInterferenceOverloadIndication := entities.UlInterferenceOverloadIndication_HIGH_INTERFERENCE
438         cellLoadInformation.UlInterferenceOverloadIndications = []entities.UlInterferenceOverloadIndication{ulInterferenceOverloadIndication}
439
440         ulHighInterferenceInformation := entities.UlHighInterferenceInformation{
441                 TargetCellId:                 "456",
442                 UlHighInterferenceIndication: "xxx",
443         }
444
445         cellLoadInformation.UlHighInterferenceInfos = []*entities.UlHighInterferenceInformation{&ulHighInterferenceInformation}
446
447         cellLoadInformation.RelativeNarrowbandTxPower = &entities.RelativeNarrowbandTxPower{
448                 RntpPerPrb:                       "xxx",
449                 RntpThreshold:                    entities.RntpThreshold_NEG_4,
450                 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V1_ANT_PRT,
451                 PB:                               1,
452                 PdcchInterferenceImpact:          2,
453                 EnhancedRntp: &entities.EnhancedRntp{
454                         EnhancedRntpBitmap:     "xxx",
455                         RntpHighPowerThreshold: entities.RntpThreshold_NEG_2,
456                         EnhancedRntpStartTime:  &entities.StartTime{StartSfn: 500, StartSubframeNumber: 5},
457                 },
458         }
459
460         cellLoadInformation.AbsInformation = &entities.AbsInformation{
461                 Mode:                             entities.AbsInformationMode_ABS_INFO_FDD,
462                 AbsPatternInfo:                   "xxx",
463                 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V2_ANT_PRT,
464                 MeasurementSubset:                "xxx",
465         }
466
467         cellLoadInformation.InvokeIndication = entities.InvokeIndication_ABS_INFORMATION
468
469         cellLoadInformation.ExtendedUlInterferenceOverloadInfo = &entities.ExtendedUlInterferenceOverloadInfo{
470                 AssociatedSubframes:                       "xxx",
471                 ExtendedUlInterferenceOverloadIndications: cellLoadInformation.UlInterferenceOverloadIndications,
472         }
473
474         compInformationItem := &entities.CompInformationItem{
475                 CompHypothesisSets: []*entities.CompHypothesisSet{&entities.CompHypothesisSet{CellId: "789", CompHypothesis: "xxx"}},
476                 BenefitMetric:      50,
477         }
478
479         cellLoadInformation.CompInformation = &entities.CompInformation{
480                 CompInformationItems:     []*entities.CompInformationItem{compInformationItem},
481                 CompInformationStartTime: &entities.StartTime{StartSfn: 123, StartSubframeNumber: 456},
482         }
483
484         cellLoadInformation.DynamicDlTransmissionInformation = &entities.DynamicDlTransmissionInformation{
485                 State:             entities.NaicsState_NAICS_ACTIVE,
486                 TransmissionModes: "xxx",
487                 PB:                2,
488                 PAList:            []entities.PA{entities.PA_DB_NEG_3},
489         }
490
491         return &cellLoadInformation
492 }
493
494 func generateRanLoadInformation() *entities.RanLoadInformation {
495         ranLoadInformation := entities.RanLoadInformation{}
496
497         ranLoadInformation.LoadTimestamp = uint64(time.Now().UnixNano())
498
499         cellLoadInformation := generateCellLoadInformation()
500         ranLoadInformation.CellLoadInfos = []*entities.CellLoadInformation{cellLoadInformation}
501
502         return &ranLoadInformation
503 }
504
505 func TestSaveNilEntityFailure(t *testing.T) {
506         writerPool = nil
507         initSdlInstanceMock(namespace, 1)
508         w := GetRNibWriter()
509         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
510         nbIdentity := &entities.NbIdentity{}
511         actualErr := w.SaveNodeb(nbIdentity, nil)
512         assert.Equal(t, expectedErr, actualErr)
513 }
514
515 func TestSaveUnknownTypeEntityFailure(t *testing.T) {
516         writerPool = nil
517         initSdlInstanceMock(namespace, 1)
518         w := GetRNibWriter()
519         expectedErr := common.NewValidationError("#rNibWriter.saveNodeB - Unknown responding node type, entity: ip:\"localhost\" port:5656 ")
520         nbIdentity := &entities.NbIdentity{InventoryName: "name", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
521         nb := &entities.NodebInfo{}
522         nb.Port = 5656
523         nb.Ip = "localhost"
524         actualErr := w.SaveNodeb(nbIdentity, nb)
525         assert.Equal(t, expectedErr, actualErr)
526 }
527
528 func TestSaveEntityFailure(t *testing.T) {
529         name := "name"
530         plmnId := "02f829"
531         nbId := "4a952a0a"
532
533         writerPool = nil
534         sdlInstanceMock := initSdlInstanceMock(namespace, 1)
535         w := GetRNibWriter()
536         gnb := entities.NodebInfo{}
537         gnb.NodeType = entities.Node_GNB
538         data, err := proto.Marshal(&gnb)
539         if err != nil {
540                 t.Errorf("#rNibWriter_test.TestSaveEntityFailure - Failed to marshal NodeB entity. Error: %v", err)
541         }
542         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}}
543         setExpected := []interface{}{"RAN:" + name, data}
544         setExpected = append(setExpected, "GNB:"+plmnId+":"+nbId, data)
545         expectedErr := errors.New("expected error")
546         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
547         rNibErr := w.SaveNodeb(nbIdentity, &gnb)
548         assert.NotEmpty(t, rNibErr)
549 }
550
551 func TestGetRNibWriterPoolNotInitializedFailure(t *testing.T) {
552         writerPool = nil
553         assert.Panics(t, func() { GetRNibWriter().SaveNodeb(nil,nil) })
554 }
555
556 func TestGetRNibWriter(t *testing.T) {
557         writerPool = nil
558         initSdlInstanceMock(namespace, 1)
559         received := GetRNibWriter()
560         assert.Empty(t, received)
561         available, created := writerPool.Stats()
562         assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
563         assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
564         writerPool.Close()
565 }
566
567 func TestClose(t *testing.T) {
568         writerPool = nil
569         instanceMock := initSdlInstanceMock(namespace, 2)
570         w1 := GetRNibWriter()
571         w2 := GetRNibWriter()
572         writerPool.Put(w1)
573         writerPool.Put(w2)
574         available, created := writerPool.Stats()
575         assert.Equal(t, 2, available, "number of available objects in the writerPool should be 2")
576         assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
577         var e error
578         instanceMock.On("Close").Return(e)
579         Close()
580         available, created = writerPool.Stats()
581         assert.Equal(t, 0, available, "number of available objects in the writerPool should be 0")
582 }
583
584 func TestCloseOnClosedPoolFailure(t *testing.T) {
585         writerPool = nil
586         instanceMock := initSdlInstanceMock(namespace, 1)
587         w1 := GetRNibWriter()
588         writerPool.Put(w1)
589         available, created := writerPool.Stats()
590         assert.Equal(t, 1, available, "number of available objects in the writerPool should be 1")
591         assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
592         var e error
593         instanceMock.On("Close").Return(e)
594         Close()
595         assert.Panics(t, func() { Close() })
596 }
597
598 func TestCloseFailure(t *testing.T) {
599         writerPool = nil
600         instanceMock := initSdlInstanceMock(namespace, 2)
601         w1 := GetRNibWriter()
602         writerPool.Put(w1)
603         available, created := writerPool.Stats()
604         assert.Equal(t, 1, available, "number of available objects in the writerPool should be 1")
605         assert.Equal(t, 0, created, "number of created objects in the writerPool should be 0")
606         e := errors.New("expected error")
607         instanceMock.On("Close").Return(e)
608         Close()
609         available, created = writerPool.Stats()
610         assert.Equal(t, 0, available, "number of available 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 //}