257bb85d95f841844496373f706ffabb3489dc8f
[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         expectedErr := common.NewValidationError("#rNibWriter.saveNodeB - Unknown responding node type, entity: ip:\"localhost\" port:5656 ")
568         nbIdentity := &entities.NbIdentity{InventoryName: "name", GlobalNbId: &entities.GlobalNbId{PlmnId: "02f829", NbId: "4a952a0a"}}
569         nb := &entities.NodebInfo{}
570         nb.Port = 5656
571         nb.Ip = "localhost"
572         actualErr := w.SaveNodeb(nbIdentity, nb)
573         assert.Equal(t, expectedErr, actualErr)
574 }
575
576 func TestSaveEntityFailure(t *testing.T) {
577         name := "name"
578         plmnId := "02f829"
579         nbId := "4a952a0a"
580
581         w, sdlInstanceMock := initSdlInstanceMock(namespace)
582         gnb := entities.NodebInfo{}
583         gnb.NodeType = entities.Node_GNB
584         data, err := proto.Marshal(&gnb)
585         if err != nil {
586                 t.Errorf("#rNibWriter_test.TestSaveEntityFailure - Failed to marshal NodeB entity. Error: %v", err)
587         }
588         nbIdentity := &entities.NbIdentity{InventoryName: name, GlobalNbId: &entities.GlobalNbId{PlmnId: plmnId, NbId: nbId}}
589         setExpected := []interface{}{"RAN:" + name, data}
590         setExpected = append(setExpected, "GNB:"+plmnId+":"+nbId, data)
591         expectedErr := errors.New("expected error")
592         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
593         rNibErr := w.SaveNodeb(nbIdentity, &gnb)
594         assert.NotEmpty(t, rNibErr)
595 }
596
597 func TestGetRNibWriter(t *testing.T) {
598         received, _ := initSdlInstanceMock(namespace)
599         assert.NotEmpty(t, received)
600 }
601
602 func TestSaveE2TInstanceSuccess(t *testing.T) {
603         address := "10.10.2.15:9800"
604         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
605
606         if validationErr != nil {
607                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to build E2T Instance key. Error: %v", validationErr)
608         }
609
610         w, sdlInstanceMock := initSdlInstanceMock(namespace)
611
612         e2tInstance := generateE2tInstance(address)
613         data, err := json.Marshal(e2tInstance)
614
615         if err != nil {
616                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSuccess - Failed to marshal E2tInstance entity. Error: %v", err)
617         }
618
619         var e error
620         var setExpected []interface{}
621         setExpected = append(setExpected, loadKey, data)
622         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
623
624         rNibErr := w.SaveE2TInstance(e2tInstance)
625         assert.Nil(t, rNibErr)
626 }
627
628 func TestSaveE2TInstanceNullE2tInstanceFailure(t *testing.T) {
629         w, _ := initSdlInstanceMock(namespace)
630         var address string
631         e2tInstance := entities.NewE2TInstance(address, "test")
632         err := w.SaveE2TInstance(e2tInstance)
633         assert.NotNil(t, err)
634         assert.IsType(t, &common.ValidationError{}, err)
635 }
636
637 func TestSaveE2TInstanceSdlFailure(t *testing.T) {
638         address := "10.10.2.15:9800"
639         loadKey, validationErr := common.ValidateAndBuildE2TInstanceKey(address)
640
641         if validationErr != nil {
642                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to build E2T Instance key. Error: %v", validationErr)
643         }
644
645         w, sdlInstanceMock := initSdlInstanceMock(namespace)
646
647         e2tInstance := generateE2tInstance(address)
648         data, err := json.Marshal(e2tInstance)
649
650         if err != nil {
651                 t.Errorf("#rNibWriter_test.TestSaveE2TInstanceSdlFailure - Failed to marshal E2tInstance entity. Error: %v", err)
652         }
653
654         expectedErr := errors.New("expected error")
655         var setExpected []interface{}
656         setExpected = append(setExpected, loadKey, data)
657         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
658
659         rNibErr := w.SaveE2TInstance(e2tInstance)
660         assert.NotNil(t, rNibErr)
661         assert.IsType(t, &common.InternalError{}, rNibErr)
662 }
663
664 func generateE2tInstance(address string) *entities.E2TInstance {
665         e2tInstance := entities.NewE2TInstance(address, "pod test")
666
667         e2tInstance.AssociatedRanList = []string{"test1", "test2"}
668
669         return e2tInstance
670 }
671
672 func TestSaveE2TAddressesSuccess(t *testing.T) {
673         address := "10.10.2.15:9800"
674         w, sdlInstanceMock := initSdlInstanceMock(namespace)
675
676         e2tAddresses := []string{address}
677         data, err := json.Marshal(e2tAddresses)
678
679         if err != nil {
680                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSuccess - Failed to marshal E2TInfoList. Error: %v", err)
681         }
682
683         var e error
684         var setExpected []interface{}
685         setExpected = append(setExpected, E2TAddressesKey, data)
686         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(e)
687
688         rNibErr := w.SaveE2TAddresses(e2tAddresses)
689         assert.Nil(t, rNibErr)
690 }
691
692 func TestSaveE2TAddressesSdlFailure(t *testing.T) {
693         address := "10.10.2.15:9800"
694         w, sdlInstanceMock := initSdlInstanceMock(namespace)
695
696         e2tAddresses := []string{address}
697         data, err := json.Marshal(e2tAddresses)
698
699         if err != nil {
700                 t.Errorf("#rNibWriter_test.TestSaveE2TInfoListSdlFailure - Failed to marshal E2TInfoList. Error: %v", err)
701         }
702
703         expectedErr := errors.New("expected error")
704         var setExpected []interface{}
705         setExpected = append(setExpected, E2TAddressesKey, data)
706         sdlInstanceMock.On("Set", []interface{}{setExpected}).Return(expectedErr)
707
708         rNibErr := w.SaveE2TAddresses(e2tAddresses)
709         assert.NotNil(t, rNibErr)
710         assert.IsType(t, &common.InternalError{}, rNibErr)
711 }
712
713 func TestRemoveE2TInstanceSuccess(t *testing.T) {
714         address := "10.10.2.15:9800"
715         w, sdlInstanceMock := initSdlInstanceMock(namespace)
716
717         e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
718         var e error
719         sdlInstanceMock.On("Remove", e2tAddresses).Return(e)
720
721         rNibErr := w.RemoveE2TInstance(address)
722         assert.Nil(t, rNibErr)
723         sdlInstanceMock.AssertExpectations(t)
724 }
725
726 func TestRemoveE2TInstanceSdlFailure(t *testing.T) {
727         address := "10.10.2.15:9800"
728         w, sdlInstanceMock := initSdlInstanceMock(namespace)
729
730         e2tAddresses := []string{fmt.Sprintf("E2TInstance:%s", address)}
731         expectedErr := errors.New("expected error")
732         sdlInstanceMock.On("Remove", e2tAddresses).Return(expectedErr)
733
734         rNibErr := w.RemoveE2TInstance(address)
735         assert.IsType(t, &common.InternalError{}, rNibErr)
736 }
737
738 func TestRemoveE2TInstanceEmptyAddressFailure(t *testing.T) {
739         w, sdlInstanceMock := initSdlInstanceMock(namespace)
740
741         rNibErr := w.RemoveE2TInstance("")
742         assert.IsType(t, &common.ValidationError{}, rNibErr)
743         sdlInstanceMock.AssertExpectations(t)
744 }
745
746 //Integration tests
747 //
748 //func TestSaveEnbGnbInteg(t *testing.T){
749 //      for i := 0; i<10; i++{
750 //              Init("e2Manager", 1)
751 //              w := GetRNibWriter()
752 //              nb := entities.NodebInfo{}
753 //              nb.NodeType = entities.Node_ENB
754 //              nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED
755 //              nb.Ip = "localhost"
756 //              nb.Port = uint32(5656 + i)
757 //              enb := entities.Enb{}
758 //              cell1 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",111 + i), Pci:uint32(11 + i)}
759 //              cell2 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",222 + i), Pci:uint32(22 + i)}
760 //              cell3 := &entities.ServedCellInfo{CellId:fmt.Sprintf("%02x",333 + i), Pci:uint32(33 + i)}
761 //              enb.ServedCells = []*entities.ServedCellInfo{cell1, cell2, cell3}
762 //              nb.Configuration = &entities.NodebInfo_Enb{Enb:&enb}
763 //              plmnId := 0x02f828
764 //              nbId := 0x4a952a0a
765 //              nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameEnb%d" ,i), GlobalNbId:&entities.GlobalNbId{PlmnId:fmt.Sprintf("%02x", plmnId + i), NbId:fmt.Sprintf("%02x", nbId + i)}}
766 //              err := w.SaveNodeb(nbIdentity, &nb)
767 //              if err != nil{
768 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
769 //              }
770 //
771 //              nb1 := entities.NodebInfo{}
772 //              nb1.NodeType = entities.Node_GNB
773 //              nb1.ConnectionStatus = entities.ConnectionStatus_CONNECTED
774 //              nb1.Ip = "localhost"
775 //              nb1.Port =  uint32(6565 + i)
776 //              gnb := entities.Gnb{}
777 //              gCell1 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",1111 + i), NrPci:uint32(1 + i)}}
778 //              gCell2 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",2222 + i), NrPci:uint32(2 + i)}}
779 //              gCell3 := &entities.ServedNRCell{ServedNrCellInformation:&entities.ServedNRCellInformation{CellId:fmt.Sprintf("%02x",3333 + i), NrPci:uint32(3 + i)}}
780 //              gnb.ServedNrCells = []*entities.ServedNRCell{gCell1, gCell2, gCell3,}
781 //              nb1.Configuration = &entities.NodebInfo_Gnb{Gnb:&gnb}
782 //              nbIdentity = &entities.NbIdentity{InventoryName: fmt.Sprintf("nameGnb%d" ,i), GlobalNbId:&entities.GlobalNbId{PlmnId:fmt.Sprintf("%02x", plmnId - i), NbId:fmt.Sprintf("%02x", nbId - i)}}
783 //              err = w.SaveNodeb(nbIdentity, &nb1)
784 //              if err != nil{
785 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
786 //              }
787 //      }
788 //}
789 //
790 //func TestSaveNbRanNamesInteg(t *testing.T){
791 //      for i := 0; i<10; i++{
792 //              Init("e2Manager", 1)
793 //              w := GetRNibWriter()
794 //              nb := entities.NodebInfo{}
795 //              nb.ConnectionStatus = entities.ConnectionStatus_CONNECTING
796 //              nb.Ip = "localhost"
797 //              nb.Port = uint32(5656 + i)
798 //              nbIdentity := &entities.NbIdentity{InventoryName: fmt.Sprintf("nameOnly%d" ,i)}
799 //              err := w.SaveNodeb(nbIdentity, &nb)
800 //              if err != nil{
801 //                      t.Errorf("#rNibWriter_test.TestSaveEnbInteg - Failed to save NodeB entity. Error: %v", err)
802 //              }
803 //      }
804 //}
805 //
806 //func TestSaveRanLoadInformationInteg(t *testing.T){
807 //              Init("e2Manager", 1)
808 //              w := GetRNibWriter()
809 //              ranLoadInformation := generateRanLoadInformation()
810 //              err := w.SaveRanLoadInformation("ran_integ", ranLoadInformation)
811 //              if err != nil{
812 //                      t.Errorf("#rNibWriter_test.TestSaveRanLoadInformationInteg - Failed to save RanLoadInformation entity. Error: %v", err)
813 //              }
814 //}