2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
25 "e2mgr/e2managererrors"
32 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
33 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
34 "github.com/stretchr/testify/assert"
35 "github.com/stretchr/testify/mock"
40 func initRanSetupManagerTest(t *testing.T) (*mocks.RmrMessengerMock, *mocks.RnibWriterMock, *RanSetupManager) {
42 logger, err := logger.InitLogger(DebugLevel)
44 t.Errorf("#... - failed to initialize logger, error: %s", err)
46 config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
48 rmrMessengerMock := &mocks.RmrMessengerMock{}
49 rmrSender := initRmrSender(rmrMessengerMock, logger)
51 readerMock := &mocks.RnibReaderMock{}
53 writerMock := &mocks.RnibWriterMock{}
55 rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
56 ranSetupManager := NewRanSetupManager(logger, rmrSender, rnibDataService)
57 return rmrMessengerMock, writerMock, ranSetupManager
60 func TestExecuteSetupConnectingX2Setup(t *testing.T) {
61 rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
65 var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
66 var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
68 writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
70 payload := e2pdus.PackedX2setupRequest
71 xAction := []byte(ranName)
72 var msgSrc unsafe.Pointer
73 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
74 rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, nil)
76 if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err != nil {
77 t.Errorf("want: success, got: error: %s", err)
80 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
81 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
84 func TestExecuteSetupConnectingEndcX2Setup(t *testing.T) {
85 rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
89 var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
90 var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
92 writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
94 payload := e2pdus.PackedEndcX2setupRequest
95 xAction := []byte(ranName)
96 var msgSrc unsafe.Pointer
97 msg := rmrCgo.NewMBuf(rmrCgo.RIC_ENDC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
98 rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, nil)
100 if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err != nil {
101 t.Errorf("want: success, got: error: %s", err)
104 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
105 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
108 func TestExecuteSetupDisconnected(t *testing.T) {
109 rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
113 var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
114 var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
115 var argNodebDisconnected = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
117 writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
118 writerMock.On("UpdateNodebInfo", argNodebDisconnected).Return(rnibErr)
121 xAction := []byte(ranName)
122 var msgSrc unsafe.Pointer
123 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
124 rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, fmt.Errorf("send failure"))
126 if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err == nil {
127 t.Errorf("want: failure, got: success")
130 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
131 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
134 func TestExecuteSetupConnectingRnibError(t *testing.T) {
135 rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
139 var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
140 var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
141 var argNodebDisconnected = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
142 var rnibErr = common.NewInternalError(fmt.Errorf("DB error"))
143 writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
144 writerMock.On("UpdateNodebInfo", argNodebDisconnected).Return(rnibErr)
147 xAction := []byte(ranName)
148 var msgSrc unsafe.Pointer
149 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
150 rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, fmt.Errorf("send failure"))
152 if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err == nil {
153 t.Errorf("want: failure, got: success")
155 assert.IsType(t, e2managererrors.NewRnibDbError(), err)
158 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
159 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 0)
162 func TestExecuteSetupDisconnectedRnibError(t *testing.T) {
163 rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
167 var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
168 var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
169 var argNodebDisconnected = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
171 writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
172 writerMock.On("UpdateNodebInfo", argNodebDisconnected).Return(common.NewInternalError(fmt.Errorf("DB error")))
175 xAction := []byte(ranName)
176 var msgSrc unsafe.Pointer
177 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
178 rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, fmt.Errorf("send failure"))
180 if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err == nil {
181 t.Errorf("want: failure, got: success")
183 assert.IsType(t, e2managererrors.NewRnibDbError(), err)
186 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
187 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
190 func TestExecuteSetupUnsupportedProtocol(t *testing.T) {
191 rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
195 var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_UNKNOWN_E2_APPLICATION_PROTOCOL}
196 var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_UNKNOWN_E2_APPLICATION_PROTOCOL}
198 writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
200 payload := e2pdus.PackedX2setupRequest
201 xAction := []byte(ranName)
202 var msgSrc unsafe.Pointer
203 msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
204 rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, nil)
206 if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err == nil {
207 t.Errorf("want: error, got: success")
210 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
211 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 0)
214 func initLog(t *testing.T) *logger.Logger {
216 log, err := logger.InitLogger(InfoLevel)
218 t.Errorf("#initLog test - failed to initialize logger, error: %s", err)