RIC-547 - E2M publish SDL event - Automation
[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 package rNibWriter
21
22 import (
23         "e2mgr/mocks"
24         "encoding/json"
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 var namespace = "namespace"
36 const (
37         RanName = "test"
38 )
39
40 func initSdlInstanceMock(namespace string) (w RNibWriter, sdlInstanceMock *mocks.MockSdlInstance) {
41         sdlInstanceMock = new(mocks.MockSdlInstance)
42         w = GetRNibWriter(sdlInstanceMock)
43         return
44 }
45
46 func generateNodebInfo(inventoryName string, nodeType entities.Node_Type, plmnId string, nbId string) *entities.NodebInfo {
47         nodebInfo := &entities.NodebInfo{
48                 RanName:          inventoryName,
49                 GlobalNbId:       &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId},
50                 NodeType:         nodeType,
51                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
52         }
53
54         if nodeType == entities.Node_ENB {
55                 nodebInfo.Configuration = &entities.NodebInfo_Enb{
56                         Enb: &entities.Enb{},
57                 }
58         } else if nodeType == entities.Node_GNB {
59                 nodebInfo.Configuration = &entities.NodebInfo_Gnb{
60                         Gnb: &entities.Gnb{},
61                 }
62         }
63
64         return nodebInfo
65 }
66
67 func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell {
68
69         servedNrCells := []*entities.ServedNRCell{}
70
71         for i, v := range cellIds {
72                 servedNrCells = append(servedNrCells, &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{
73                         CellId: v,
74                         ChoiceNrMode: &entities.ServedNRCellInformation_ChoiceNRMode{
75                                 Fdd: &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{
76
77                                 },
78                         },
79                         NrMode:      entities.Nr_FDD,
80                         NrPci:       uint32(i + 1),
81                         ServedPlmns: []string{"whatever"},
82                 }})
83         }
84
85         return servedNrCells
86 }
87
88 func TestRemoveServedNrCellsSuccess(t *testing.T) {
89         w, sdlInstanceMock := initSdlInstanceMock(namespace)
90         servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2")
91         sdlInstanceMock.On("Remove", buildCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(nil)
92         err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove)
93         assert.Nil(t, err)
94 }
95
96 func TestRemoveServedNrCellsFailure(t *testing.T) {
97         w, sdlInstanceMock := initSdlInstanceMock(namespace)
98         servedNrCellsToRemove := generateServedNrCells("whatever1", "whatever2")
99         sdlInstanceMock.On("Remove", buildCellKeysToRemove(RanName, servedNrCellsToRemove)).Return(errors.New("expected error"))
100         err := w.RemoveServedNrCells(RanName, servedNrCellsToRemove)
101         assert.IsType(t, &common.InternalError{}, err)
102 }
103
104 func TestUpdateGnbCellsInvalidNodebInfoFailure(t *testing.T) {
105         w, sdlInstanceMock := initSdlInstanceMock(namespace)
106         servedNrCells := generateServedNrCells("test1", "test2")
107         nodebInfo := &entities.NodebInfo{}
108         sdlInstanceMock.AssertNotCalled(t, "Set")
109         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
110         assert.IsType(t, &common.ValidationError{}, rNibErr)
111 }
112
113 func TestUpdateGnbCellsInvalidCellFailure(t *testing.T) {
114         inventoryName := "name"
115         plmnId := "02f829"
116         nbId := "4a952a0a"
117         w, sdlInstanceMock := initSdlInstanceMock(namespace)
118         servedNrCells := []*entities.ServedNRCell{{ServedNrCellInformation: &entities.ServedNRCellInformation{}}}
119         nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
120         nodebInfo.GetGnb().ServedNrCells = servedNrCells
121         sdlInstanceMock.AssertNotCalled(t, "Set")
122         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
123         assert.IsType(t, &common.ValidationError{}, rNibErr)
124 }
125
126 func getUpdateGnbCellsSetExpected(t *testing.T, nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) []interface{} {
127
128         nodebInfoData, err := proto.Marshal(nodebInfo)
129         if err != nil {
130                 t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal NodeB entity. Error: %s", err)
131         }
132
133         nodebNameKey, _ := common.ValidateAndBuildNodeBNameKey(nodebInfo.RanName)
134         nodebIdKey, _ := common.ValidateAndBuildNodeBIdKey(nodebInfo.NodeType.String(), nodebInfo.GlobalNbId.PlmnId, nodebInfo.GlobalNbId.NbId)
135         setExpected := []interface{}{nodebNameKey, nodebInfoData, nodebIdKey, nodebInfoData}
136
137         for _, v := range servedNrCells {
138
139                 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: v}}
140                 cellData, err := proto.Marshal(&cellEntity)
141
142                 if err != nil {
143                         t.Fatalf("#rNibWriter_test.getUpdateGnbCellsSetExpected - Failed to marshal cell entity. Error: %s", err)
144                 }
145
146                 nrCellIdKey, _ := common.ValidateAndBuildNrCellIdKey(v.GetServedNrCellInformation().GetCellId())
147                 cellNamePciKey, _ := common.ValidateAndBuildCellNamePciKey(nodebInfo.RanName, v.GetServedNrCellInformation().GetNrPci())
148                 setExpected = append(setExpected, nrCellIdKey, cellData, cellNamePciKey, cellData)
149         }
150
151         return setExpected
152 }
153
154 func TestUpdateGnbCellsSdlFailure(t *testing.T) {
155         inventoryName := "name"
156         plmnId := "02f829"
157         nbId := "4a952a0a"
158         w, sdlInstanceMock := initSdlInstanceMock(namespace)
159         servedNrCells := generateServedNrCells("test1", "test2")
160         nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
161         nodebInfo.GetGnb().ServedNrCells = servedNrCells
162         setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
163         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(errors.New("expected error"))
164         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
165         assert.IsType(t, &common.InternalError{}, rNibErr)
166 }
167
168 func TestUpdateGnbCellsSuccess(t *testing.T) {
169         inventoryName := "name"
170         plmnId := "02f829"
171         nbId := "4a952a0a"
172         w, sdlInstanceMock := initSdlInstanceMock(namespace)
173         servedNrCells := generateServedNrCells("test1", "test2")
174         nodebInfo := generateNodebInfo(inventoryName, entities.Node_GNB, plmnId, nbId)
175         nodebInfo.GetGnb().ServedNrCells = servedNrCells
176         setExpected := getUpdateGnbCellsSetExpected(t, nodebInfo, servedNrCells)
177         var e error
178         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
179         rNibErr := w.UpdateGnbCells(nodebInfo, servedNrCells)
180         assert.Nil(t, rNibErr)
181 }
182
183 func TestUpdateNodebInfoSuccess(t *testing.T) {
184         inventoryName := "name"
185         plmnId := "02f829"
186         nbId := "4a952a0a"
187         w, sdlInstanceMock := initSdlInstanceMock(namespace)
188         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
189         data, err := proto.Marshal(nodebInfo)
190         if err != nil {
191                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
192         }
193         var e error
194         var setExpected []interface{}
195
196         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
197         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
198         setExpected = append(setExpected, nodebNameKey, data)
199         setExpected = append(setExpected, nodebIdKey, data)
200
201         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
202
203         rNibErr := w.UpdateNodebInfo(nodebInfo)
204         assert.Nil(t, rNibErr)
205 }
206
207 func TestUpdateNodebInfoMissingInventoryNameFailure(t *testing.T) {
208         inventoryName := "name"
209         plmnId := "02f829"
210         nbId := "4a952a0a"
211         w, sdlInstanceMock := initSdlInstanceMock(namespace)
212         nodebInfo := &entities.NodebInfo{}
213         data, err := proto.Marshal(nodebInfo)
214         if err != nil {
215                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
216         }
217         var e error
218         var setExpected []interface{}
219
220         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
221         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
222         setExpected = append(setExpected, nodebNameKey, data)
223         setExpected = append(setExpected, nodebIdKey, data)
224
225         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
226
227         rNibErr := w.UpdateNodebInfo(nodebInfo)
228
229         assert.NotNil(t, rNibErr)
230         assert.IsType(t, &common.ValidationError{}, rNibErr)
231 }
232
233 func TestUpdateNodebInfoMissingGlobalNbId(t *testing.T) {
234         inventoryName := "name"
235         w, sdlInstanceMock := initSdlInstanceMock(namespace)
236         nodebInfo := &entities.NodebInfo{}
237         nodebInfo.RanName = inventoryName
238         data, err := proto.Marshal(nodebInfo)
239         if err != nil {
240                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
241         }
242         var e error
243         var setExpected []interface{}
244
245         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
246         setExpected = append(setExpected, nodebNameKey, data)
247         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
248
249         rNibErr := w.UpdateNodebInfo(nodebInfo)
250
251         assert.Nil(t, rNibErr)
252 }
253
254 func TestSaveEnb(t *testing.T) {
255         name := "name"
256         ranName := "RAN:" + name
257         w, sdlInstanceMock := initSdlInstanceMock(namespace)
258         nb := entities.NodebInfo{}
259         nb.NodeType = entities.Node_ENB
260         nb.ConnectionStatus = 1
261         nb.Ip = "localhost"
262         nb.Port = 5656
263         enb := entities.Enb{}
264         cell := &entities.ServedCellInfo{CellId: "aaff", Pci: 3}
265         cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
266         enb.ServedCells = []*entities.ServedCellInfo{cell}
267         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
268         data, err := proto.Marshal(&nb)
269         if err != nil {
270                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB entity. Error: %v", err)
271         }
272         var e error
273
274         cellData, err := proto.Marshal(&cellEntity)
275         if err != nil {
276                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal Cell entity. Error: %v", err)
277         }
278         var setExpected []interface{}
279         setExpected = append(setExpected, ranName, data)
280         setExpected = append(setExpected, "ENB:02f829:4a952a0a", data)
281         setExpected = append(setExpected, fmt.Sprintf("CELL:%s", cell.GetCellId()), cellData)
282         setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetPci()), cellData)
283
284         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
285
286         nbIdData, err := proto.Marshal(&entities.NbIdentity{InventoryName: name})
287         if err != nil {
288                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
289         }
290         sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
291
292         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
293         nbIdData, err = proto.Marshal(nbIdentity)
294         if err != nil {
295                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal NodeB Identity entity. Error: %v", err)
296         }
297         sdlInstanceMock.On("AddMember", "ENB", []interface{}{nbIdData}).Return(e)
298
299         rNibErr := w.SaveNodeb(nbIdentity, &nb)
300         assert.Nil(t, rNibErr)
301 }
302
303 func TestSaveEnbCellIdValidationFailure(t *testing.T) {
304         name := "name"
305         w, _ := initSdlInstanceMock(namespace)
306         nb := entities.NodebInfo{}
307         nb.NodeType = entities.Node_ENB
308         nb.ConnectionStatus = 1
309         nb.Ip = "localhost"
310         nb.Port = 5656
311         enb := entities.Enb{}
312         cell := &entities.ServedCellInfo{Pci: 3}
313         enb.ServedCells = []*entities.ServedCellInfo{cell}
314         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
315
316         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
317         rNibErr := w.SaveNodeb(nbIdentity, &nb)
318         assert.NotNil(t, rNibErr)
319         assert.IsType(t, &common.ValidationError{}, rNibErr)
320         assert.Equal(t, "#utils.ValidateAndBuildCellIdKey - an empty cell id received", rNibErr.Error())
321 }
322
323 func TestSaveEnbInventoryNameValidationFailure(t *testing.T) {
324         w, _ := initSdlInstanceMock(namespace)
325         nb := entities.NodebInfo{}
326         nb.NodeType = entities.Node_ENB
327         nb.ConnectionStatus = 1
328         nb.Ip = "localhost"
329         nb.Port = 5656
330         enb := entities.Enb{}
331         cell := &entities.ServedCellInfo{CellId: "aaa", Pci: 3}
332         enb.ServedCells = []*entities.ServedCellInfo{cell}
333         nb.Configuration = &entities.NodebInfo_Enb{Enb: &enb}
334
335         nbIdentity := &entities.NbIdentity{InventoryName: "", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
336         rNibErr := w.SaveNodeb(nbIdentity, &nb)
337         assert.NotNil(t, rNibErr)
338         assert.IsType(t, &common.ValidationError{}, rNibErr)
339         assert.Equal(t, "#utils.ValidateAndBuildNodeBNameKey - an empty inventory name received", rNibErr.Error())
340 }
341
342 func TestSaveGnbCellIdValidationFailure(t *testing.T) {
343         name := "name"
344         w, _ := initSdlInstanceMock(namespace)
345         nb := entities.NodebInfo{}
346         nb.NodeType = entities.Node_GNB
347         nb.ConnectionStatus = 1
348         nb.Ip = "localhost"
349         nb.Port = 5656
350         gnb := entities.Gnb{}
351         cellInfo := &entities.ServedNRCellInformation{NrPci: 2}
352         cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
353         gnb.ServedNrCells = []*entities.ServedNRCell{cell}
354         nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
355
356         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
357         rNibErr := w.SaveNodeb(nbIdentity, &nb)
358         assert.NotNil(t, rNibErr)
359         assert.IsType(t, &common.ValidationError{}, rNibErr)
360         assert.Equal(t, "#utils.ValidateAndBuildNrCellIdKey - an empty cell id received", rNibErr.Error())
361 }
362
363 func TestSaveGnb(t *testing.T) {
364         name := "name"
365         ranName := "RAN:" + name
366         w, sdlInstanceMock := initSdlInstanceMock(namespace)
367         nb := entities.NodebInfo{}
368         nb.NodeType = entities.Node_GNB
369         nb.ConnectionStatus = 1
370         nb.Ip = "localhost"
371         nb.Port = 5656
372         gnb := entities.Gnb{}
373         cellInfo := &entities.ServedNRCellInformation{NrPci: 2, CellId: "ccdd"}
374         cell := &entities.ServedNRCell{ServedNrCellInformation: cellInfo}
375         cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
376         gnb.ServedNrCells = []*entities.ServedNRCell{cell}
377         nb.Configuration = &entities.NodebInfo_Gnb{Gnb: &gnb}
378         data, err := proto.Marshal(&nb)
379         if err != nil {
380                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB entity. Error: %v", err)
381         }
382         var e error
383
384         cellData, err := proto.Marshal(&cellEntity)
385         if err != nil {
386                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal Cell entity. Error: %v", err)
387         }
388         var setExpected []interface{}
389         setExpected = append(setExpected, ranName, data)
390         setExpected = append(setExpected, "GNB:02f829:4a952a0a", data)
391         setExpected = append(setExpected, fmt.Sprintf("NRCELL:%s", cell.GetServedNrCellInformation().GetCellId()), cellData)
392         setExpected = append(setExpected, fmt.Sprintf("PCI:%s:%02x", name, cell.GetServedNrCellInformation().GetNrPci()), cellData)
393
394         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
395         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
396         nbIdData, err := proto.Marshal(nbIdentity)
397         if err != nil {
398                 t.Errorf("#rNibWriter_test.TestSaveGnb - Failed to marshal NodeB Identity entity. Error: %v", err)
399         }
400         sdlInstanceMock.On("AddMember", "GNB", []interface{}{nbIdData}).Return(e)
401
402         nbIdData, err = proto.Marshal(&entities.NbIdentity{InventoryName: name})
403         if err != nil {
404                 t.Errorf("#rNibWriter_test.TestSaveEnb - Failed to marshal nbIdentity entity. Error: %v", err)
405         }
406         sdlInstanceMock.On("RemoveMember", entities.Node_UNKNOWN.String(), []interface{}{nbIdData}).Return(e)
407
408         rNibErr := w.SaveNodeb(nbIdentity, &nb)
409         assert.Nil(t, rNibErr)
410 }
411
412 func TestSaveRanLoadInformationSuccess(t *testing.T) {
413         inventoryName := "name"
414         loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
415
416         if validationErr != nil {
417                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
418         }
419
420         w, sdlInstanceMock := initSdlInstanceMock(namespace)
421
422         ranLoadInformation := generateRanLoadInformation()
423         data, err := proto.Marshal(ranLoadInformation)
424
425         if err != nil {
426                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
427         }
428
429         var e error
430         var setExpected []interface{}
431         setExpected = append(setExpected, loadKey, data)
432         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
433
434         rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
435         assert.Nil(t, rNibErr)
436 }
437
438 func TestSaveRanLoadInformationMarshalNilFailure(t *testing.T) {
439         inventoryName := "name2"
440         w, _ := initSdlInstanceMock(namespace)
441
442         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
443         err := w.SaveRanLoadInformation(inventoryName, nil)
444         assert.Equal(t, expectedErr, err)
445 }
446
447 func TestSaveRanLoadInformationEmptyInventoryNameFailure(t *testing.T) {
448         inventoryName := ""
449         w, _ := initSdlInstanceMock(namespace)
450
451         err := w.SaveRanLoadInformation(inventoryName, nil)
452         assert.NotNil(t, err)
453         assert.IsType(t, &common.ValidationError{}, err)
454 }
455
456 func TestSaveRanLoadInformationSdlFailure(t *testing.T) {
457         inventoryName := "name2"
458
459         loadKey, validationErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
460
461         if validationErr != nil {
462                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationSuccess - Failed to build ran load infromation key. Error: %v", validationErr)
463         }
464
465         w, sdlInstanceMock := initSdlInstanceMock(namespace)
466
467         ranLoadInformation := generateRanLoadInformation()
468         data, err := proto.Marshal(ranLoadInformation)
469
470         if err != nil {
471                 t.Errorf("#rNibWriter_test.TestSaveRanLoadInformation - Failed to marshal RanLoadInformation entity. Error: %v", err)
472         }
473
474         expectedErr := errors.New("expected error")
475         var setExpected []interface{}
476         setExpected = append(setExpected, loadKey, data)
477         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
478
479         rNibErr := w.SaveRanLoadInformation(inventoryName, ranLoadInformation)
480         assert.NotNil(t, rNibErr)
481         assert.IsType(t, &common.InternalError{}, rNibErr)
482 }
483
484 func generateCellLoadInformation() *entities.CellLoadInformation {
485         cellLoadInformation := entities.CellLoadInformation{}
486
487         cellLoadInformation.CellId = "123"
488
489         ulInterferenceOverloadIndication := entities.UlInterferenceOverloadIndication_HIGH_INTERFERENCE
490         cellLoadInformation.UlInterferenceOverloadIndications = []entities.UlInterferenceOverloadIndication{ulInterferenceOverloadIndication}
491
492         ulHighInterferenceInformation := entities.UlHighInterferenceInformation{
493                 TargetCellId:                 "456",
494                 UlHighInterferenceIndication: "xxx",
495         }
496
497         cellLoadInformation.UlHighInterferenceInfos = []*entities.UlHighInterferenceInformation{&ulHighInterferenceInformation}
498
499         cellLoadInformation.RelativeNarrowbandTxPower = &entities.RelativeNarrowbandTxPower{
500                 RntpPerPrb:                       "xxx",
501                 RntpThreshold:                    entities.RntpThreshold_NEG_4,
502                 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V1_ANT_PRT,
503                 PB:                               1,
504                 PdcchInterferenceImpact:          2,
505                 EnhancedRntp: &entities.EnhancedRntp{
506                         EnhancedRntpBitmap:     "xxx",
507                         RntpHighPowerThreshold: entities.RntpThreshold_NEG_2,
508                         EnhancedRntpStartTime:  &entities.StartTime{StartSfn: 500, StartSubframeNumber: 5},
509                 },
510         }
511
512         cellLoadInformation.AbsInformation = &entities.AbsInformation{
513                 Mode:                             entities.AbsInformationMode_ABS_INFO_FDD,
514                 AbsPatternInfo:                   "xxx",
515                 NumberOfCellSpecificAntennaPorts: entities.NumberOfCellSpecificAntennaPorts_V2_ANT_PRT,
516                 MeasurementSubset:                "xxx",
517         }
518
519         cellLoadInformation.InvokeIndication = entities.InvokeIndication_ABS_INFORMATION
520
521         cellLoadInformation.ExtendedUlInterferenceOverloadInfo = &entities.ExtendedUlInterferenceOverloadInfo{
522                 AssociatedSubframes:                       "xxx",
523                 ExtendedUlInterferenceOverloadIndications: cellLoadInformation.UlInterferenceOverloadIndications,
524         }
525
526         compInformationItem := &entities.CompInformationItem{
527                 CompHypothesisSets: []*entities.CompHypothesisSet{{CellId: "789", CompHypothesis: "xxx"}},
528                 BenefitMetric:      50,
529         }
530
531         cellLoadInformation.CompInformation = &entities.CompInformation{
532                 CompInformationItems:     []*entities.CompInformationItem{compInformationItem},
533                 CompInformationStartTime: &entities.StartTime{StartSfn: 123, StartSubframeNumber: 456},
534         }
535
536         cellLoadInformation.DynamicDlTransmissionInformation = &entities.DynamicDlTransmissionInformation{
537                 State:             entities.NaicsState_NAICS_ACTIVE,
538                 TransmissionModes: "xxx",
539                 PB:                2,
540                 PAList:            []entities.PA{entities.PA_DB_NEG_3},
541         }
542
543         return &cellLoadInformation
544 }
545
546 func generateRanLoadInformation() *entities.RanLoadInformation {
547         ranLoadInformation := entities.RanLoadInformation{}
548
549         ranLoadInformation.LoadTimestamp = uint64(time.Now().UnixNano())
550
551         cellLoadInformation := generateCellLoadInformation()
552         ranLoadInformation.CellLoadInfos = []*entities.CellLoadInformation{cellLoadInformation}
553
554         return &ranLoadInformation
555 }
556
557 func TestSaveNilEntityFailure(t *testing.T) {
558         w, _ := initSdlInstanceMock(namespace)
559         expectedErr := common.NewInternalError(errors.New("proto: Marshal called with nil"))
560         nbIdentity := &entities.NbIdentity{}
561         actualErr := w.SaveNodeb(nbIdentity, nil)
562         assert.Equal(t, expectedErr, actualErr)
563 }
564
565 func TestSaveUnknownTypeEntityFailure(t *testing.T) {
566         w, _ := initSdlInstanceMock(namespace)
567         nbIdentity := &entities.NbIdentity{InventoryName: "name", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
568         nb := &entities.NodebInfo{}
569         nb.Port = 5656
570         nb.Ip = "localhost"
571         actualErr := w.SaveNodeb(nbIdentity, nb)
572         assert.IsType(t, &common.ValidationError{}, actualErr)
573 }
574
575 func TestSaveEntityFailure(t *testing.T) {
576         name := "name"
577         plmnId := "02f829"
578         nbId := "4a952a0a"
579
580         w, sdlInstanceMock := initSdlInstanceMock(namespace)
581         gnb := entities.NodebInfo{}
582         gnb.NodeType = entities.Node_GNB
583         data, err := proto.Marshal(&gnb)
584         if err != nil {
585                 t.Errorf("#rNibWriter_test.TestSaveEntityFailure - Failed to marshal NodeB entity. Error: %v", err)
586         }
587         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}}
588         setExpected := []interface{}{"RAN:" + name, data}
589         setExpected = append(setExpected, "GNB:"+plmnId+":"+nbId, data)
590         expectedErr := errors.New("expected error")
591         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
592         rNibErr := w.SaveNodeb(nbIdentity, &gnb)
593         assert.NotEmpty(t, rNibErr)
594 }
595
596 func TestGetRNibWriter(t *testing.T) {
597         received, _ := initSdlInstanceMock(namespace)
598         assert.NotEmpty(t, received)
599 }
600
601 func TestSaveE2TInstanceSuccess(t *testing.T) {
602         address := "10.10.2.15:9800"
603         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
604
605         if validationErr != nil {
606                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to build E2T Instance key. Error: %v", validationErr)
607         }
608
609         w, sdlInstanceMock := initSdlInstanceMock(namespace)
610
611         e2tInstance := generateE2tInstance(address)
612         data, err := json.Marshal(e2tInstance)
613
614         if err != nil {
615                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to marshal E2tInstance entity. Error: %v", err)
616         }
617
618         var e error
619         var setExpected []interface{}
620         setExpected = append(setExpected, loadKey, data)
621         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
622
623         rNibErr := w.SaveE2TInstance(e2tInstance)
624         assert.Nil(t, rNibErr)
625 }
626
627 func TestSaveE2TInstanceNullE2tInstanceFailure(t *testing.T) {
628         w, _ := initSdlInstanceMock(namespace)
629         var address string
630         e2tInstance := entities.NewE2TInstance(address, "test")
631         err := w.SaveE2TInstance(e2tInstance)
632         assert.NotNil(t, err)
633         assert.IsType(t, &common.ValidationError{}, err)
634 }
635
636 func TestSaveE2TInstanceSdlFailure(t *testing.T) {
637         address := "10.10.2.15:9800"
638         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
639
640         if validationErr != nil {
641                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to build E2T Instance key. Error: %v", validationErr)
642         }
643
644         w, sdlInstanceMock := initSdlInstanceMock(namespace)
645
646         e2tInstance := generateE2tInstance(address)
647         data, err := json.Marshal(e2tInstance)
648
649         if err != nil {
650                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to marshal E2tInstance entity. Error: %v", err)
651         }
652
653         expectedErr := errors.New("expected error")
654         var setExpected []interface{}
655         setExpected = append(setExpected, loadKey, data)
656         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
657
658         rNibErr := w.SaveE2TInstance(e2tInstance)
659         assert.NotNil(t, rNibErr)
660         assert.IsType(t, &common.InternalError{}, rNibErr)
661 }
662
663 func generateE2tInstance(address string) *entities.E2TInstance {
664         e2tInstance := entities.NewE2TInstance(address, "pod test")
665
666         e2tInstance.AssociatedRanList = []string{"test1", "test2"}
667
668         return e2tInstance
669 }
670
671 func TestSaveE2TAddressesSuccess(t *testing.T) {
672         address := "10.10.2.15:9800"
673         w, sdlInstanceMock := initSdlInstanceMock(namespace)
674
675         e2tAddresses := []string{address}
676         data, err := json.Marshal(e2tAddresses)
677
678         if err != nil {
679                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSuccess - Failed to marshal E2TInfoList. Error: %v", err)
680         }
681
682         var e error
683         var setExpected []interface{}
684         setExpected = append(setExpected, E2TAddressesKey, data)
685         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
686
687         rNibErr := w.SaveE2TAddresses(e2tAddresses)
688         assert.Nil(t, rNibErr)
689 }
690
691 func TestSaveE2TAddressesSdlFailure(t *testing.T) {
692         address := "10.10.2.15:9800"
693         w, sdlInstanceMock := initSdlInstanceMock(namespace)
694
695         e2tAddresses := []string{address}
696         data, err := json.Marshal(e2tAddresses)
697
698         if err != nil {
699                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSdlFailure - Failed to marshal E2TInfoList. Error: %v", err)
700         }
701
702         expectedErr := errors.New("expected error")
703         var setExpected []interface{}
704         setExpected = append(setExpected, E2TAddressesKey, data)
705         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
706
707         rNibErr := w.SaveE2TAddresses(e2tAddresses)
708         assert.NotNil(t, rNibErr)
709         assert.IsType(t, &common.InternalError{}, rNibErr)
710 }
711
712 func TestRemoveE2TInstanceSuccess(t *testing.T) {
713         address := "10.10.2.15:9800"
714         w, sdlInstanceMock := initSdlInstanceMock(namespace)
715
716         e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
717         var e error
718         sdlInstanceMock.On("Remove", e2tAddresses).Return(e)
719
720         rNibErr := w.RemoveE2TInstance(address)
721         assert.Nil(t, rNibErr)
722         sdlInstanceMock.AssertExpectations(t)
723 }
724
725 func TestRemoveE2TInstanceSdlFailure(t *testing.T) {
726         address := "10.10.2.15:9800"
727         w, sdlInstanceMock := initSdlInstanceMock(namespace)
728
729         e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
730         expectedErr := errors.New("expected error")
731         sdlInstanceMock.On("Remove", e2tAddresses).Return(expectedErr)
732
733         rNibErr := w.RemoveE2TInstance(address)
734         assert.IsType(t, &common.InternalError{}, rNibErr)
735 }
736
737 func TestRemoveE2TInstanceEmptyAddressFailure(t *testing.T) {
738         w, sdlInstanceMock := initSdlInstanceMock(namespace)
739
740         rNibErr := w.RemoveE2TInstance("")
741         assert.IsType(t, &common.ValidationError{}, rNibErr)
742         sdlInstanceMock.AssertExpectations(t)
743 }
744
745 func TestUpdateNodebInfoOnConnectionStatusInversionSuccess(t *testing.T) {
746         inventoryName := "name"
747         plmnId := "02f829"
748         nbId := "4a952a0a"
749         channelName := "RAN_CONNECT_STATE_CHANGE"
750         eventName := inventoryName + "_" + "CONNECTED"
751         w, sdlInstanceMock := initSdlInstanceMock(namespace)
752         nodebInfo := generateNodebInfo(inventoryName, entities.Node_ENB, plmnId, nbId)
753         data, err := proto.Marshal(nodebInfo)
754         if err != nil {
755                 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionSuccess - Failed to marshal NodeB entity. Error: %v", err)
756         }
757         var e error
758         var setExpected []interface{}
759
760         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
761         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
762         setExpected = append(setExpected, nodebNameKey, data)
763         setExpected = append(setExpected, nodebIdKey, data)
764
765         sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
766
767         rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
768         assert.Nil(t, rNibErr)
769 }
770
771 func TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure(t *testing.T) {
772         inventoryName := "name"
773         plmnId := "02f829"
774         nbId := "4a952a0a"
775         channelName := "RAN_CONNECT_STATE_CHANGE"
776         eventName := inventoryName + "_" + "CONNECTED"
777         w, sdlInstanceMock := initSdlInstanceMock(namespace)
778         nodebInfo := &entities.NodebInfo{}
779         data, err := proto.Marshal(nodebInfo)
780         if err != nil {
781                 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
782         }
783         var e error
784         var setExpected []interface{}
785
786         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
787         nodebIdKey := fmt.Sprintf("ENB:%s:%s", plmnId, nbId)
788         setExpected = append(setExpected, nodebNameKey, data)
789         setExpected = append(setExpected, nodebIdKey, data)
790
791         sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
792
793         rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
794
795         assert.NotNil(t, rNibErr)
796         assert.IsType(t, &common.ValidationError{}, rNibErr)
797 }
798
799 func TestUpdateNodebInfoOnConnectionStatusInversionMissingGlobalNbId(t *testing.T) {
800         inventoryName := "name"
801         channelName := "RAN_CONNECT_STATE_CHANGE"
802         eventName := inventoryName + "_" + "CONNECTED"
803         w, sdlInstanceMock := initSdlInstanceMock(namespace)
804         nodebInfo := &entities.NodebInfo{}
805         nodebInfo.RanName = inventoryName
806         data, err := proto.Marshal(nodebInfo)
807         if err != nil {
808                 t.Errorf("#rNibWriter_test.TestUpdateNodebInfoOnConnectionStatusInversionMissingInventoryNameFailure - Failed to marshal NodeB entity. Error: %v", err)
809         }
810         var e error
811         var setExpected []interface{}
812
813         nodebNameKey := fmt.Sprintf("RAN:%s", inventoryName)
814         setExpected = append(setExpected, nodebNameKey, data)
815         sdlInstanceMock.On("SetAndPublish", []string{channelName, eventName}, []interface{}{setExpected}).Return(e)
816
817         rNibErr := w.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, channelName, eventName)
818
819         assert.Nil(t, rNibErr)
820 }
821
822 func TestSaveGeneralConfiguration(t *testing.T) {
823         w, sdlInstanceMock := initSdlInstanceMock(namespace)
824
825         key := common.BuildGeneralConfigurationKey()
826         configurationData := "{\"enableRic\":true}"
827         configuration := &entities.GeneralConfiguration{}
828         configuration.EnableRic = true
829
830         sdlInstanceMock.On("Set",[]interface{}{[]interface{}{key, []byte(configurationData)}}).Return(nil)
831         rNibErr := w.SaveGeneralConfiguration(configuration)
832
833         assert.Nil(t, rNibErr)
834         sdlInstanceMock.AssertExpectations(t)
835 }
836
837 func TestSaveGeneralConfigurationDbError(t *testing.T) {
838         w, sdlInstanceMock := initSdlInstanceMock(namespace)
839
840         key := common.BuildGeneralConfigurationKey()
841         configurationData := "{\"enableRic\":true}"
842         configuration := &entities.GeneralConfiguration{}
843         configuration.EnableRic = true
844
845         expectedErr := errors.New("expected error")
846
847         sdlInstanceMock.On("Set",[]interface{}{[]interface{}{key, []byte(configurationData)}}).Return(expectedErr)
848         rNibErr := w.SaveGeneralConfiguration(configuration)
849
850         assert.NotNil(t, rNibErr)
851 }
852
853 //Integration tests
854 //
855 //func TestSaveEnbGnbInteg(t *testing.T){
856 //      for i := 0; i<10; i++{
857 //              Init("e2Manager", 1)
858 //              w := GetRNibWriter()
859 //              nb := entities.NodebInfo{}
860 //              nb.NodeType = entities.Node_ENB
861 //              nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED
862 //              nb.Ip = "localhost"
863 //              nb.Port = uint32(5656 + i)
864 //              enb := entities.Enb{}
865 //              cell1 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",111 + i), Pci:uint32(11 + i)}
866 //              cell2 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",222 + i), Pci:uint32(22 + i)}
867 //              cell3 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",333 + i), Pci:uint32(33 + i)}
868 //              enb.ServedCells = []*entities.ServedCellInfo{cell1, cell2, cell3}
869 //              nb.Configuration = &entities.NodebInfo_Enb{Enb:&enb}
870 //              plmnId := 0x02f828
871 //              nbId := 0x4a952a0a
872 //              nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameEnb%d" ,i), GlobalNbId:&entities.GlobalNbId{RicId:fmt.Sprintf("%02x", plmnId + i), NbId:fmt.Sprintf("%02x", nbId + i)}}
873 //              err := w.SaveNodeb(nbIdentity, &nb)
874 //              if err != nil{
875 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
876 //              }
877 //
878 //              nb1 := entities.NodebInfo{}
879 //              nb1.NodeType = entities.Node_GNB
880 //              nb1.ConnectionStatus = entities.ConnectionStatus_CONNECTED
881 //              nb1.Ip = "localhost"
882 //              nb1.Port =  uint32(6565 + i)
883 //              gnb := entities.Gnb{}
884 //              gCell1 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",1111 + i), NrPci:uint32(1 + i)}}
885 //              gCell2 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",2222 + i), NrPci:uint32(2 + i)}}
886 //              gCell3 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",3333 + i), NrPci:uint32(3 + i)}}
887 //              gnb.ServedNrCells = []*entities.ServedNRCell{gCell1, gCell2, gCell3,}
888 //              nb1.Configuration = &entities.NodebInfo_Gnb{Gnb:&gnb}
889 //              nbIdentity = &entities.NbIdentity{InventoryName: fmt.Sprintf("nameGnb%d" ,i), GlobalNbId:&entities.GlobalNbId{RicId:fmt.Sprintf("%02x", plmnId - i), NbId:fmt.Sprintf("%02x", nbId - i)}}
890 //              err = w.SaveNodeb(nbIdentity, &nb1)
891 //              if err != nil{
892 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
893 //              }
894 //      }
895 //}
896 //
897 //func TestSaveNbRanNamesInteg(t *testing.T){
898 //      for i := 0; i<10; i++{
899 //              Init("e2Manager", 1)
900 //              w := GetRNibWriter()
901 //              nb := entities.NodebInfo{}
902 //              nb.ConnectionStatus = entities.ConnectionStatus_CONNECTING
903 //              nb.Ip = "localhost"
904 //              nb.Port = uint32(5656 + i)
905 //              nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameOnly%d" ,i)}
906 //              err := w.SaveNodeb(nbIdentity, &nb)
907 //              if err != nil{
908 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
909 //              }
910 //      }
911 //}
912 //
913 //func TestSaveRanLoadInformationInteg(t *testing.T){
914 //              Init("e2Manager", 1)
915 //              w := GetRNibWriter()
916 //              ranLoadInformation := generateRanLoadInformation()
917 //              err := w.SaveRanLoadInformation("ran_integ", ranLoadInformation)
918 //              if err != nil{
919 //                      t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationInteg - Failed to save RanLoadInformation entity. Error: %v", err)
920 //              }
921 //}