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
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 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
30 "github.com/pkg/errors"
31 "github.com/stretchr/testify/assert"
32 "github.com/stretchr/testify/mock"
38 X2SetupResponsePackedPdu = "2006002a000002001500080002f82900007a8000140017000000630002f8290007ab50102002f829000001000133"
39 EndcSetupResponsePackedPdu = "202400808e00000100f600808640000200fc00090002f829504a952a0a00fd007200010c0005001e3f271f2e3d4ff03d44d34e4f003e4e5e4400010000150400000a000211e148033e4e5e4c0005001e3f271f2e3d4ff03d44d34e4f003e4e5e4400010000150400000a00021a0044033e4e5e000000002c001e3f271f2e3d4ff0031e3f274400010000150400000a00020000"
40 X2SetupFailureResponsePackedPdu = "4006001a0000030005400200000016400100001140087821a00000008040"
41 EndcSetupFailureResponsePackedPdu = "4024001a0000030005400200000016400100001140087821a00000008040"
44 type setupResponseTestContext struct {
46 readerMock *mocks.RnibReaderMock
47 writerMock *mocks.RnibWriterMock
48 rnibReaderProvider func() reader.RNibReader
49 rnibWriterProvider func() rNibWriter.RNibWriter
50 setupResponseManager managers.ISetupResponseManager
53 func NewSetupResponseTestContext(manager managers.ISetupResponseManager) *setupResponseTestContext {
54 logger, _ := logger.InitLogger(logger.InfoLevel)
55 readerMock := &mocks.RnibReaderMock{}
56 writerMock := &mocks.RnibWriterMock{}
58 return &setupResponseTestContext{
60 readerMock: readerMock,
61 writerMock: writerMock,
62 rnibReaderProvider: func() reader.RNibReader {
65 rnibWriterProvider: func() rNibWriter.RNibWriter {
68 setupResponseManager: manager,
72 func TestSetupResponseGetNodebFailure(t *testing.T) {
73 notificationRequest := models.NotificationRequest{RanName: RanName}
74 testContext := NewSetupResponseTestContext(nil)
75 handler := NewSetupResponseNotificationHandler(testContext.rnibReaderProvider, testContext.rnibWriterProvider, &managers.X2SetupResponseManager{}, "X2 Setup Response")
76 testContext.readerMock.On("GetNodeb", RanName).Return(&entities.NodebInfo{}, common.NewInternalError(errors.New("Error")))
77 handler.Handle(testContext.logger, ¬ificationRequest, nil)
78 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
79 testContext.writerMock.AssertNotCalled(t, "SaveNodeb")
82 func TestSetupResponseInvalidConnectionStatus(t *testing.T) {
84 notificationRequest := models.NotificationRequest{RanName: ranName}
85 testContext := NewSetupResponseTestContext(nil)
86 handler := NewSetupResponseNotificationHandler(testContext.rnibReaderProvider, testContext.rnibWriterProvider, &managers.X2SetupResponseManager{}, "X2 Setup Response")
88 testContext.readerMock.On("GetNodeb", ranName).Return(&entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}, rnibErr)
89 handler.Handle(testContext.logger, ¬ificationRequest, nil)
90 testContext.readerMock.AssertCalled(t, "GetNodeb", ranName)
91 testContext.writerMock.AssertNotCalled(t, "SaveNodeb")
94 func executeHandleSuccessSetupResponse(t *testing.T, packedPdu string, setupResponseManager managers.ISetupResponseManager, notificationType string, saveNodebMockReturnValue error) (*setupResponseTestContext, *entities.NodebInfo) {
96 _, err := fmt.Sscanf(packedPdu, "%x", &payload)
98 t.Fatalf("Failed converting packed pdu. Error: %v\n", err)
101 notificationRequest := models.NotificationRequest{RanName: RanName, Payload: payload}
102 testContext := NewSetupResponseTestContext(setupResponseManager)
104 handler := NewSetupResponseNotificationHandler(testContext.rnibReaderProvider, testContext.rnibWriterProvider, testContext.setupResponseManager, notificationType)
108 nodebInfo := &entities.NodebInfo{
109 ConnectionStatus: entities.ConnectionStatus_CONNECTING,
110 ConnectionAttempts: 1,
116 testContext.readerMock.On("GetNodeb", RanName).Return(nodebInfo, rnibErr)
117 testContext.writerMock.On("SaveNodeb", mock.Anything, mock.Anything).Return(saveNodebMockReturnValue)
118 handler.Handle(testContext.logger, ¬ificationRequest, nil)
120 return testContext, nodebInfo
123 func TestX2SetupResponse(t *testing.T) {
125 testContext, nodebInfo := executeHandleSuccessSetupResponse(t, X2SetupResponsePackedPdu, &managers.X2SetupResponseManager{}, "X2 Setup Response", rnibErr)
126 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
127 testContext.writerMock.AssertCalled(t, "SaveNodeb", mock.Anything, nodebInfo)
128 assert.EqualValues(t, entities.ConnectionStatus_CONNECTED, nodebInfo.ConnectionStatus)
129 assert.EqualValues(t, 0, nodebInfo.ConnectionAttempts)
130 assert.EqualValues(t, entities.Node_ENB, nodebInfo.NodeType)
132 assert.IsType(t, &entities.NodebInfo_Enb{}, nodebInfo.Configuration)
133 i, _ := nodebInfo.Configuration.(*entities.NodebInfo_Enb)
134 assert.NotNil(t, i.Enb)
137 func TestX2SetupFailureResponse(t *testing.T) {
139 testContext, nodebInfo := executeHandleSuccessSetupResponse(t, X2SetupFailureResponsePackedPdu, &managers.X2SetupFailureResponseManager{}, "X2 Setup Failure Response", rnibErr)
140 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
141 testContext.writerMock.AssertCalled(t, "SaveNodeb", mock.Anything, nodebInfo)
142 assert.EqualValues(t, entities.ConnectionStatus_CONNECTED_SETUP_FAILED, nodebInfo.ConnectionStatus)
143 assert.EqualValues(t, 0, nodebInfo.ConnectionAttempts)
144 assert.EqualValues(t, entities.Failure_X2_SETUP_FAILURE, nodebInfo.FailureType)
145 assert.NotNil(t, nodebInfo.SetupFailure)
148 func TestEndcSetupResponse(t *testing.T) {
150 testContext, nodebInfo := executeHandleSuccessSetupResponse(t, EndcSetupResponsePackedPdu, &managers.EndcSetupResponseManager{}, "ENDC Setup Response", rnibErr)
151 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
152 testContext.writerMock.AssertCalled(t, "SaveNodeb", mock.Anything, nodebInfo)
153 assert.EqualValues(t, entities.ConnectionStatus_CONNECTED, nodebInfo.ConnectionStatus)
154 assert.EqualValues(t, 0, nodebInfo.ConnectionAttempts)
155 assert.EqualValues(t, entities.Node_GNB, nodebInfo.NodeType)
156 assert.IsType(t, &entities.NodebInfo_Gnb{}, nodebInfo.Configuration)
158 i, _ := nodebInfo.Configuration.(*entities.NodebInfo_Gnb)
159 assert.NotNil(t, i.Gnb)
161 func TestEndcSetupFailureResponse(t *testing.T) {
163 testContext, nodebInfo := executeHandleSuccessSetupResponse(t, EndcSetupFailureResponsePackedPdu, &managers.EndcSetupFailureResponseManager{}, "ENDC Setup Failure Response", rnibErr)
164 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
165 testContext.writerMock.AssertCalled(t, "SaveNodeb", mock.Anything, nodebInfo)
166 assert.EqualValues(t, entities.ConnectionStatus_CONNECTED_SETUP_FAILED, nodebInfo.ConnectionStatus)
167 assert.EqualValues(t, 0, nodebInfo.ConnectionAttempts)
168 assert.EqualValues(t, entities.Failure_ENDC_X2_SETUP_FAILURE, nodebInfo.FailureType)
169 assert.NotNil(t, nodebInfo.SetupFailure)
172 func TestSetupResponseInvalidPayload(t *testing.T) {
174 notificationRequest := models.NotificationRequest{RanName: ranName, Payload: []byte("123")}
175 testContext := NewSetupResponseTestContext(nil)
176 handler := NewSetupResponseNotificationHandler(testContext.rnibReaderProvider, testContext.rnibWriterProvider, &managers.X2SetupResponseManager{}, "X2 Setup Response")
178 testContext.readerMock.On("GetNodeb", ranName).Return(&entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 1}, rnibErr)
179 handler.Handle(testContext.logger, ¬ificationRequest, nil)
180 testContext.readerMock.AssertCalled(t, "GetNodeb", ranName)
181 testContext.writerMock.AssertNotCalled(t, "SaveNodeb")
184 func TestSetupResponseSaveNodebFailure(t *testing.T) {
185 rnibErr := common.NewInternalError(errors.New("Error"))
186 testContext, nodebInfo := executeHandleSuccessSetupResponse(t, X2SetupResponsePackedPdu, &managers.X2SetupResponseManager{}, "X2 Setup Response", rnibErr)
187 testContext.readerMock.AssertCalled(t, "GetNodeb", RanName)
188 testContext.writerMock.AssertCalled(t, "SaveNodeb", mock.Anything, nodebInfo)