Add header missing license header
[ric-plt/resource-status-manager.git] / RSM / services / resource_status_service_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 //  platform project (RICP).
20
21 package services
22
23 import (
24         "fmt"
25         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
26         "github.com/stretchr/testify/assert"
27         "rsm/enums"
28         "rsm/logger"
29         "rsm/mocks"
30         "rsm/models"
31         "rsm/rmrcgo"
32         "rsm/rsmerrors"
33         "rsm/services/rmrsender"
34         "rsm/tests"
35         "testing"
36 )
37
38 const RanName = "test"
39 const NodebOneCellPackedExample = "0009003c00000800270003000000001c00010000260004fe000000001d400d00001f40080002f8290007ab00001e4001000040400100006d4001400091400120"
40 const NodebTwoCellsPackedExample = "0009004800000800270003000000001c00010000260004fe000000001d401901001f40080002f8290007ab00001f40080002f8290007ab50001e4001000040400100006d4001400091400120"
41 const StopPackedExample = "0009004f0000090027000300000000280003000001001c00014000260004fe000000001d401901001f40080002f8290007ab00001f40080002f8290007ab50001e4001000040400100006d4001400091400120"
42
43 func initResourceStatusServiceTest(t *testing.T) (*mocks.RmrMessengerMock, *models.RsmGeneralConfiguration, *ResourceStatusService) {
44         logger, err := logger.InitLogger(logger.DebugLevel)
45         if err != nil {
46                 t.Errorf("#... - failed to initialize logger, error: %s", err)
47         }
48
49         rmrMessengerMock := &mocks.RmrMessengerMock{}
50         rmrSender := InitRmrSender(rmrMessengerMock, logger)
51         resourceStatusService := NewResourceStatusService(logger, rmrSender)
52
53         rsmGeneralConfiguration := models.RsmGeneralConfiguration{
54                 EnableResourceStatus:         true,
55                 PartialSuccessAllowed:        true,
56                 PrbPeriodic:                  true,
57                 TnlLoadIndPeriodic:           true,
58                 HwLoadIndPeriodic:            true,
59                 AbsStatusPeriodic:            true,
60                 RsrpMeasurementPeriodic:      true,
61                 CsiPeriodic:                  true,
62                 PeriodicityMs:                enums.ReportingPeriodicity_one_thousand_ms,
63                 PeriodicityRsrpMeasurementMs: enums.ReportingPeriodicityRSRPMR_four_hundred_80_ms,
64                 PeriodicityCsiMs:             enums.ReportingPeriodicityCSIR_ms20,
65         }
66
67         return rmrMessengerMock, &rsmGeneralConfiguration, resourceStatusService
68 }
69
70 func TestOneCellSuccess(t *testing.T) {
71         cellId := "02f829:0007ab00"
72         rmrMessengerMock, rsmGeneralConfiguration, resourceStatusService := initResourceStatusServiceTest(t)
73
74         xaction := []byte(RanName)
75         nodebInfo := &entities.NodebInfo{
76                 RanName:          RanName,
77                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
78                 Configuration: &entities.NodebInfo_Enb{
79                         Enb: &entities.Enb{
80                                 ServedCells: []*entities.ServedCellInfo{{CellId: cellId}},
81                         },
82                 },
83         }
84
85         var expectedPayload []byte
86         _, _ = fmt.Sscanf(NodebOneCellPackedExample, "%x", &expectedPayload)
87         var err error
88         expectedMbuf := rmrcgo.NewMBuf(rmrcgo.RicResStatusReq, len(expectedPayload), RanName, &expectedPayload, &xaction)
89         rmrMessengerMock.On("SendMsg", expectedMbuf).Return(&rmrcgo.MBuf{}, err)
90         err = resourceStatusService.BuildAndSendInitiateRequest(nodebInfo, rsmGeneralConfiguration, enums.Enb1MeasurementId)
91         assert.Nil(t, err)
92         rmrMessengerMock.AssertCalled(t, "SendMsg", expectedMbuf)
93 }
94
95 func TestTwoCellsSuccess(t *testing.T) {
96         cellId1 := "02f829:0007ab00"
97         cellId2 := "02f829:0007ab50"
98         rmrMessengerMock, rsmGeneralConfiguration, resourceStatusService := initResourceStatusServiceTest(t)
99         xaction := []byte(RanName)
100         nodebInfo := &entities.NodebInfo{
101                 RanName:          RanName,
102                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
103                 Configuration: &entities.NodebInfo_Enb{
104                         Enb: &entities.Enb{
105                                 ServedCells: []*entities.ServedCellInfo{{CellId: cellId1}, {CellId: cellId2}},
106                         },
107                 },
108         }
109
110         var expectedPayload []byte
111         _, _ = fmt.Sscanf(NodebTwoCellsPackedExample, "%x", &expectedPayload)
112         expectedMbuf := rmrcgo.NewMBuf(rmrcgo.RicResStatusReq, len(expectedPayload), RanName, &expectedPayload, &xaction)
113         var err error
114         rmrMessengerMock.On("SendMsg", expectedMbuf).Return(&rmrcgo.MBuf{}, err)
115         err = resourceStatusService.BuildAndSendInitiateRequest(nodebInfo, rsmGeneralConfiguration, enums.Enb1MeasurementId)
116         assert.Nil(t, err)
117         rmrMessengerMock.AssertCalled(t, "SendMsg", expectedMbuf)
118 }
119
120 func TestOneCellSendFailure(t *testing.T) {
121         cellId := "02f829:0007ab00"
122         rmrMessengerMock, rsmGeneralConfiguration, resourceStatusService := initResourceStatusServiceTest(t)
123
124         xaction := []byte(RanName)
125         var err error
126         nodebInfo := &entities.NodebInfo{
127                 RanName:          RanName,
128                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
129                 Configuration: &entities.NodebInfo_Enb{
130                         Enb: &entities.Enb{
131                                 ServedCells: []*entities.ServedCellInfo{{CellId: cellId}},
132                         },
133                 },
134         }
135
136         var expectedPayload []byte
137         _, _ = fmt.Sscanf(NodebOneCellPackedExample, "%x", &expectedPayload)
138         expectedMbuf := rmrcgo.NewMBuf(rmrcgo.RicResStatusReq, len(expectedPayload), RanName, &expectedPayload, &xaction)
139         rmrMessengerMock.On("SendMsg", expectedMbuf).Return(&rmrcgo.MBuf{}, rsmerrors.NewRmrError())
140         err = resourceStatusService.BuildAndSendInitiateRequest(nodebInfo, rsmGeneralConfiguration, 1)
141         assert.NotNil(t, err)
142         rmrMessengerMock.AssertCalled(t, "SendMsg", expectedMbuf)
143 }
144
145
146
147 func TestNodebConfigurationFailure(t *testing.T) {
148         rmrMessengerMock, rsmGeneralConfiguration, resourceStatusService := initResourceStatusServiceTest(t)
149         nodebInfo := &entities.NodebInfo{
150                 RanName:          RanName,
151                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
152         }
153
154         err := resourceStatusService.BuildAndSendInitiateRequest(nodebInfo, rsmGeneralConfiguration, enums.Enb1MeasurementId)
155         assert.NotNil(t, err)
156         rmrMessengerMock.AssertNotCalled(t, "SendMsg")
157 }
158
159 func TestNodebEmptyCellList(t *testing.T) {
160         rmrMessengerMock, rsmGeneralConfiguration, resourceStatusService := initResourceStatusServiceTest(t)
161         nodebInfo := &entities.NodebInfo{
162                 RanName:          RanName,
163                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
164                 Configuration: &entities.NodebInfo_Enb{
165                         Enb: &entities.Enb{
166                                 ServedCells: []*entities.ServedCellInfo{},
167                         },
168                 },
169         }
170
171         err := resourceStatusService.BuildAndSendInitiateRequest(nodebInfo, rsmGeneralConfiguration, enums.Enb1MeasurementId)
172         assert.NotNil(t, err)
173         rmrMessengerMock.AssertNotCalled(t, "SendMsg")
174 }
175
176 func TestPackFailure(t *testing.T) {
177         rmrMessengerMock, rsmGeneralConfiguration, resourceStatusService := initResourceStatusServiceTest(t)
178         nodebInfo := &entities.NodebInfo{
179                 RanName:          RanName,
180                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
181                 Configuration: &entities.NodebInfo_Enb{
182                         Enb: &entities.Enb{
183                                 ServedCells: []*entities.ServedCellInfo{{CellId: ""}},
184                         },
185                 },
186         }
187
188         err := resourceStatusService.BuildAndSendInitiateRequest(nodebInfo, rsmGeneralConfiguration, enums.Enb1MeasurementId)
189         assert.NotNil(t, err)
190         rmrMessengerMock.AssertNotCalled(t, "SendMsg")
191 }
192
193 func TestBuildAndSendStopRequestSuccess(t *testing.T) {
194         rmrMessengerMock, rsmGeneralConfiguration, resourceStatusService := initResourceStatusServiceTest(t)
195
196         cellId1 := "02f829:0007ab00"
197         cellId2 := "02f829:0007ab50"
198         nodebInfo := &entities.NodebInfo{
199                 RanName:          RanName,
200                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
201                 Configuration: &entities.NodebInfo_Enb{
202                         Enb: &entities.Enb{
203                                 ServedCells: []*entities.ServedCellInfo{{CellId: cellId1}, {CellId: cellId2}},
204                         },
205                 },
206         }
207         xaction := []byte(RanName)
208         var expectedPayload []byte
209         _, _ = fmt.Sscanf(StopPackedExample, "%x", &expectedPayload)
210         expectedMbuf := rmrcgo.NewMBuf(rmrcgo.RicResStatusReq, len(expectedPayload), RanName, &expectedPayload, &xaction)
211         var err error
212         rmrMessengerMock.On("SendMsg", expectedMbuf).Return(&rmrcgo.MBuf{}, err)
213         err = resourceStatusService.BuildAndSendStopRequest(nodebInfo, rsmGeneralConfiguration, enums.Enb1MeasurementId, 2)
214         assert.Nil(t, err)
215         rmrMessengerMock.AssertCalled(t, "SendMsg", expectedMbuf)
216 }
217
218 func TestBuildAndSendStopRequestSendFailure(t *testing.T) {
219         rmrMessengerMock, rsmGeneralConfiguration, resourceStatusService := initResourceStatusServiceTest(t)
220
221         xaction := []byte(RanName)
222         cellId1 := "02f829:0007ab00"
223         cellId2 := "02f829:0007ab50"
224         nodebInfo := &entities.NodebInfo{
225                 RanName:          RanName,
226                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
227                 Configuration: &entities.NodebInfo_Enb{
228                         Enb: &entities.Enb{
229                                 ServedCells: []*entities.ServedCellInfo{{CellId: cellId1}, {CellId: cellId2}},
230                         },
231                 },
232         }
233
234         var err error
235         var expectedPayload []byte
236         _, _ = fmt.Sscanf(StopPackedExample, "%x", &expectedPayload)
237         expectedMbuf := rmrcgo.NewMBuf(rmrcgo.RicResStatusReq, len(expectedPayload), RanName, &expectedPayload, &xaction)
238         rmrMessengerMock.On("SendMsg", expectedMbuf).Return(&rmrcgo.MBuf{}, rsmerrors.NewRmrError())
239         err = resourceStatusService.BuildAndSendStopRequest(nodebInfo, rsmGeneralConfiguration, enums.Enb1MeasurementId, 2)
240
241         assert.NotNil(t, err)
242         rmrMessengerMock.AssertCalled(t, "SendMsg", expectedMbuf)
243 }
244
245 func InitRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
246         rmrMessenger := rmrcgo.RmrMessenger(rmrMessengerMock)
247         rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
248         return rmrsender.NewRmrSender(log, rmrMessenger)
249 }