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.
18 package rmrmsghandlers
29 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
30 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
31 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
32 "github.com/pkg/errors"
33 "github.com/stretchr/testify/assert"
34 "github.com/stretchr/testify/mock"
40 X2SetupResponsePackedPdu = "2006002a000002001500080002f82900007a8000140017000000630002f8290007ab50102002f829000001000133"
41 EndcSetupResponsePackedPdu = "202400808e00000100f600808640000200fc00090002f829504a952a0a00fd007200010c0005001e3f271f2e3d4ff03d44d34e4f003e4e5e4400010000150400000a000211e148033e4e5e4c0005001e3f271f2e3d4ff03d44d34e4f003e4e5e4400010000150400000a00021a0044033e4e5e000000002c001e3f271f2e3d4ff0031e3f274400010000150400000a00020000"
42 X2SetupFailureResponsePackedPdu = "4006001a0000030005400200000016400100001140087821a00000008040"
43 EndcSetupFailureResponsePackedPdu = "4024001a0000030005400200000016400100001140087821a00000008040"
46 type setupResponseTestContext struct {
48 readerMock *mocks.RnibReaderMock
49 writerMock *mocks.RnibWriterMock
50 rnibDataService services.RNibDataService
51 setupResponseManager managers.ISetupResponseManager
54 func NewSetupResponseTestContext(manager managers.ISetupResponseManager) *setupResponseTestContext {
55 logger, _ := logger.InitLogger(logger.InfoLevel)
56 config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
57 readerMock := &mocks.RnibReaderMock{}
58 writerMock := &mocks.RnibWriterMock{}
59 rnibReaderProvider := func() reader.RNibReader {
62 rnibWriterProvider := func() rNibWriter.RNibWriter {
65 rnibDataService := services.NewRnibDataService(logger, config, rnibReaderProvider, rnibWriterProvider)
67 return &setupResponseTestContext{
69 readerMock: readerMock,
70 writerMock: writerMock,
71 rnibDataService: rnibDataService,
72 setupResponseManager: manager,
76 func TestSetupResponseGetNodebFailure(t *testing.T) {
77 notificationRequest := models.NotificationRequest{RanName: RanName}
78 testContext := NewSetupResponseTestContext(nil)
79 handler := NewSetupResponseNotificationHandler(testContext.rnibDataService, &managers.X2SetupResponseManager{}, "X2 Setup Response")
80 testContext.readerMock.On("GetNodeb", RanName).Return(&entities.NodebInfo{}, common.NewInternalError(errors.New("Error")))
81 handler.Handle(testContext.logger, ¬ificationRequest, nil)
82 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
83 testContext.writerMock.AssertNotCalled(t, "SaveNodeb")
86 func TestSetupResponseInvalidConnectionStatus(t *testing.T) {
88 notificationRequest := models.NotificationRequest{RanName: ranName}
89 testContext := NewSetupResponseTestContext(nil)
90 handler := NewSetupResponseNotificationHandler(testContext.rnibDataService, &managers.X2SetupResponseManager{}, "X2 Setup Response")
92 testContext.readerMock.On("GetNodeb", ranName).Return(&entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}, rnibErr)
93 handler.Handle(testContext.logger, ¬ificationRequest, nil)
94 testContext.readerMock.AssertCalled(t, "GetNodeb", ranName)
95 testContext.writerMock.AssertNotCalled(t, "SaveNodeb")
98 func executeHandleSuccessSetupResponse(t *testing.T, packedPdu string, setupResponseManager managers.ISetupResponseManager, notificationType string, saveNodebMockReturnValue error) (*setupResponseTestContext, *entities.NodebInfo) {
100 _, err := fmt.Sscanf(packedPdu, "%x", &payload)
102 t.Fatalf("Failed converting packed pdu. Error: %v\n", err)
105 notificationRequest := models.NotificationRequest{RanName: RanName, Payload: payload}
106 testContext := NewSetupResponseTestContext(setupResponseManager)
108 handler := NewSetupResponseNotificationHandler(testContext.rnibDataService, testContext.setupResponseManager, notificationType)
112 nodebInfo := &entities.NodebInfo{
113 ConnectionStatus: entities.ConnectionStatus_CONNECTING,
114 ConnectionAttempts: 1,
120 testContext.readerMock.On("GetNodeb", RanName).Return(nodebInfo, rnibErr)
121 testContext.writerMock.On("SaveNodeb", mock.Anything, mock.Anything).Return(saveNodebMockReturnValue)
122 handler.Handle(testContext.logger, ¬ificationRequest, nil)
124 return testContext, nodebInfo
127 func TestX2SetupResponse(t *testing.T) {
129 testContext, nodebInfo := executeHandleSuccessSetupResponse(t, X2SetupResponsePackedPdu, &managers.X2SetupResponseManager{}, "X2 Setup Response", rnibErr)
130 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
131 testContext.writerMock.AssertCalled(t, "SaveNodeb", mock.Anything, nodebInfo)
132 assert.EqualValues(t, entities.ConnectionStatus_CONNECTED, nodebInfo.ConnectionStatus)
133 assert.EqualValues(t, 0, nodebInfo.ConnectionAttempts)
134 assert.EqualValues(t, entities.Node_ENB, nodebInfo.NodeType)
136 assert.IsType(t, &entities.NodebInfo_Enb{}, nodebInfo.Configuration)
137 i, _ := nodebInfo.Configuration.(*entities.NodebInfo_Enb)
138 assert.NotNil(t, i.Enb)
141 func TestX2SetupFailureResponse(t *testing.T) {
143 testContext, nodebInfo := executeHandleSuccessSetupResponse(t, X2SetupFailureResponsePackedPdu, &managers.X2SetupFailureResponseManager{}, "X2 Setup Failure Response", rnibErr)
144 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
145 testContext.writerMock.AssertCalled(t, "SaveNodeb", mock.Anything, nodebInfo)
146 assert.EqualValues(t, entities.ConnectionStatus_CONNECTED_SETUP_FAILED, nodebInfo.ConnectionStatus)
147 assert.EqualValues(t, 0, nodebInfo.ConnectionAttempts)
148 assert.EqualValues(t, entities.Failure_X2_SETUP_FAILURE, nodebInfo.FailureType)
149 assert.NotNil(t, nodebInfo.SetupFailure)
152 func TestEndcSetupResponse(t *testing.T) {
154 testContext, nodebInfo := executeHandleSuccessSetupResponse(t, EndcSetupResponsePackedPdu, &managers.EndcSetupResponseManager{}, "ENDC Setup Response", rnibErr)
155 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
156 testContext.writerMock.AssertCalled(t, "SaveNodeb", mock.Anything, nodebInfo)
157 assert.EqualValues(t, entities.ConnectionStatus_CONNECTED, nodebInfo.ConnectionStatus)
158 assert.EqualValues(t, 0, nodebInfo.ConnectionAttempts)
159 assert.EqualValues(t, entities.Node_GNB, nodebInfo.NodeType)
160 assert.IsType(t, &entities.NodebInfo_Gnb{}, nodebInfo.Configuration)
162 i, _ := nodebInfo.Configuration.(*entities.NodebInfo_Gnb)
163 assert.NotNil(t, i.Gnb)
165 func TestEndcSetupFailureResponse(t *testing.T) {
167 testContext, nodebInfo := executeHandleSuccessSetupResponse(t, EndcSetupFailureResponsePackedPdu, &managers.EndcSetupFailureResponseManager{}, "ENDC Setup Failure Response", rnibErr)
168 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
169 testContext.writerMock.AssertCalled(t, "SaveNodeb", mock.Anything, nodebInfo)
170 assert.EqualValues(t, entities.ConnectionStatus_CONNECTED_SETUP_FAILED, nodebInfo.ConnectionStatus)
171 assert.EqualValues(t, 0, nodebInfo.ConnectionAttempts)
172 assert.EqualValues(t, entities.Failure_ENDC_X2_SETUP_FAILURE, nodebInfo.FailureType)
173 assert.NotNil(t, nodebInfo.SetupFailure)
176 func TestSetupResponseInvalidPayload(t *testing.T) {
178 notificationRequest := models.NotificationRequest{RanName: ranName, Payload: []byte("123")}
179 testContext := NewSetupResponseTestContext(nil)
180 handler := NewSetupResponseNotificationHandler(testContext.rnibDataService, &managers.X2SetupResponseManager{}, "X2 Setup Response")
182 testContext.readerMock.On("GetNodeb", ranName).Return(&entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 1}, rnibErr)
183 handler.Handle(testContext.logger, ¬ificationRequest, nil)
184 testContext.readerMock.AssertCalled(t, "GetNodeb", ranName)
185 testContext.writerMock.AssertNotCalled(t, "SaveNodeb")
188 func TestSetupResponseSaveNodebFailure(t *testing.T) {
189 rnibErr := common.NewInternalError(errors.New("Error"))
190 testContext, nodebInfo := executeHandleSuccessSetupResponse(t, X2SetupResponsePackedPdu, &managers.X2SetupResponseManager{}, "X2 Setup Response", rnibErr)
191 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
192 testContext.writerMock.AssertCalled(t, "SaveNodeb", mock.Anything, nodebInfo)