package rmrmsghandlers
import (
+ "bytes"
+ "e2mgr/clients"
"e2mgr/configuration"
"e2mgr/e2pdus"
"e2mgr/logger"
"fmt"
"gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
"gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
+ "github.com/pkg/errors"
"github.com/stretchr/testify/mock"
+ "io/ioutil"
+ "net/http"
"testing"
)
e2tInstancesManagerMock := &mocks.E2TInstancesManagerMock{}
ranReconnectionManager := managers.NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, ranSetupManager, e2tInstancesManagerMock)
- handler := NewE2TermInitNotificationHandler(logger, ranReconnectionManager, rnibDataService, e2tInstancesManagerMock, routingManagerClientMock)
+ handler := NewE2TermInitNotificationHandler(logger, ranReconnectionManager, e2tInstancesManagerMock, routingManagerClientMock)
return logger, handler, readerMock, writerMock, rmrMessengerMock, e2tInstancesManagerMock, routingManagerClientMock
}
-func initRanLostConnectionTestWithRealE2tInstanceManager(t *testing.T) (*logger.Logger, E2TermInitNotificationHandler, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.RmrMessengerMock) {
+func initRanLostConnectionTestWithRealE2tInstanceManager(t *testing.T) (*logger.Logger, *configuration.Configuration, E2TermInitNotificationHandler, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.RmrMessengerMock, *mocks.HttpClientMock) {
logger := initLog(t)
- config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
+ config := configuration.ParseConfiguration()
rmrMessengerMock := &mocks.RmrMessengerMock{}
rmrSender := initRmrSender(rmrMessengerMock, logger)
readerMock := &mocks.RnibReaderMock{}
writerMock := &mocks.RnibWriterMock{}
- routingManagerClientMock := &mocks.RoutingManagerClientMock{}
+ httpClientMock := &mocks.HttpClientMock{}
+ routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClientMock)
rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
ranSetupManager := managers.NewRanSetupManager(logger, rmrSender, rnibDataService)
e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, logger)
ranReconnectionManager := managers.NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, ranSetupManager, e2tInstancesManager)
- handler := NewE2TermInitNotificationHandler(logger, ranReconnectionManager, rnibDataService, e2tInstancesManager, routingManagerClientMock)
- return logger, handler, readerMock, writerMock, rmrMessengerMock
+ handler := NewE2TermInitNotificationHandler(logger, ranReconnectionManager, e2tInstancesManager, routingManagerClient)
+ return logger, config, handler, readerMock, writerMock, rmrMessengerMock, httpClientMock
}
func TestE2TermInitUnmarshalPayloadFailure(t *testing.T) {
}
func TestE2TermInitGetE2TInstanceDbFailure(t *testing.T) {
- _, handler, readerMock, writerMock, rmrMessengerMock := initRanLostConnectionTestWithRealE2tInstanceManager(t)
+ _, _, handler, readerMock, writerMock, rmrMessengerMock,_ := initRanLostConnectionTestWithRealE2tInstanceManager(t)
var e2tInstance *entities.E2TInstance
readerMock.On("GetE2TInstance", e2tInstanceAddress).Return(e2tInstance, common.NewInternalError(fmt.Errorf("internal error")))
notificationRequest := &models.NotificationRequest{RanName: RanName, Payload: []byte(e2tInitPayload)}
}
func TestE2TermInitNewE2TInstance(t *testing.T) {
- _, handler, _, _, _, e2tInstancesManagerMock, routingManagerClient := initRanLostConnectionTest(t)
+ _, config, handler, readerMock, writerMock, _, httpClientMock := initRanLostConnectionTestWithRealE2tInstanceManager(t)
var e2tInstance *entities.E2TInstance
- e2tInstancesManagerMock.On("GetE2TInstance", e2tInstanceAddress).Return(e2tInstance, common.NewResourceNotFoundError("not found"))
- e2tInstance = entities.NewE2TInstance(e2tInstanceAddress)
- e2tInstancesManagerMock.On("AddE2TInstance", e2tInstanceAddress).Return(nil)
- routingManagerClient.On("AddE2TInstance", e2tInstanceAddress).Return(nil)
+
+ readerMock.On("GetE2TInstance", e2tInstanceAddress).Return(e2tInstance, common.NewResourceNotFoundError("not found"))
+ writerMock.On("SaveE2TInstance", mock.Anything).Return(nil)
+
+ respBody := ioutil.NopCloser(bytes.NewBufferString(""))
+ url := config.RoutingManager.BaseUrl + clients.AddE2TInstanceApiSuffix
+ httpClientMock.On("Post", url, mock.Anything, mock.Anything).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
+
+ e2tAddresses := []string{}
+ readerMock.On("GetE2TAddresses").Return(e2tAddresses, common.NewResourceNotFoundError(""))
+
+ e2tAddresses = append(e2tAddresses, e2tInstanceAddress)
+ writerMock.On("SaveE2TAddresses", e2tAddresses).Return(nil)
+
notificationRequest := &models.NotificationRequest{RanName: RanName, Payload: []byte(e2tInitPayload)}
handler.Handle(notificationRequest)
- routingManagerClient.AssertCalled(t, "AddE2TInstance", e2tInstanceAddress)
- e2tInstancesManagerMock.AssertCalled(t, "AddE2TInstance", e2tInstanceAddress)
+
+ httpClientMock.AssertCalled(t, "Post", url, mock.Anything, mock.Anything)
+ writerMock.AssertCalled(t, "SaveE2TInstance", mock.Anything)
+ writerMock.AssertCalled(t, "SaveE2TAddresses", e2tAddresses)
}
func TestE2TermInitNewE2TInstance_RoutingManagerError(t *testing.T) {
- _, handler, _, _, _, e2tInstancesManagerMock, routingManagerClient := initRanLostConnectionTest(t)
+ _, config, handler, readerMock, writerMock, _, httpClientMock := initRanLostConnectionTestWithRealE2tInstanceManager(t)
var e2tInstance *entities.E2TInstance
- e2tInstancesManagerMock.On("GetE2TInstance", e2tInstanceAddress).Return(e2tInstance, common.NewResourceNotFoundError("not found"))
- e2tInstance = entities.NewE2TInstance(e2tInstanceAddress)
- e2tInstancesManagerMock.On("AddE2TInstance", e2tInstanceAddress).Return(nil)
- routingManagerClient.On("AddE2TInstance", e2tInstanceAddress).Return(fmt.Errorf("error"))
- notificationRequest := &models.NotificationRequest{RanName: RanName, Payload: []byte(e2tInitPayload)}
+ readerMock.On("GetE2TInstance", e2tInstanceAddress).Return(e2tInstance, common.NewResourceNotFoundError("not found"))
+
+ url := config.RoutingManager.BaseUrl + clients.AddE2TInstanceApiSuffix
+ httpClientMock.On("Post", url, mock.Anything, mock.Anything).Return(&http.Response{}, errors.New("error"))
+
+ notificationRequest := &models.NotificationRequest{RanName: RanName, Payload: []byte(e2tInitPayload)}
handler.Handle(notificationRequest)
- routingManagerClient.AssertCalled(t, "AddE2TInstance", e2tInstanceAddress)
- e2tInstancesManagerMock.AssertNotCalled(t, "AddE2TInstance", e2tInstanceAddress)
+ httpClientMock.AssertCalled(t, "Post", url, mock.Anything, mock.Anything)
+ writerMock.AssertNotCalled(t, "SaveE2TInstance", mock.Anything)
+ writerMock.AssertNotCalled(t, "SaveE2TAddresses", mock.Anything)
}
func TestE2TermInitExistingE2TInstanceNoAssociatedRans(t *testing.T) {
}
func TestE2TermInitHandlerSuccessOneRanShuttingdown(t *testing.T) {
- _, handler, readerMock, writerMock, rmrMessengerMock := initRanLostConnectionTestWithRealE2tInstanceManager(t)
+ _, _, handler, readerMock, writerMock, rmrMessengerMock,_ := initRanLostConnectionTestWithRealE2tInstanceManager(t)
var rnibErr error
var initialNodeb = &entities.NodebInfo{RanName: RanName, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
}
func TestE2TermInitHandlerSuccessOneRan_ToBeDeleted(t *testing.T) {
- _, handler, readerMock, writerMock, rmrMessengerMock, e2tInstancesManagerMock, _ := initRanLostConnectionTest(t)
+ _, _, handler, readerMock, writerMock, rmrMessengerMock, httpClientMock:= initRanLostConnectionTestWithRealE2tInstanceManager(t)
var rnibErr error
var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
e2tInstance.State = entities.ToBeDeleted
e2tInstance.AssociatedRanList = append(e2tInstance.AssociatedRanList, RanName)
- e2tInstancesManagerMock.On("GetE2TInstance", e2tInstanceAddress).Return(e2tInstance, nil)
+ readerMock.On("GetE2TInstance", e2tInstanceAddress).Return(e2tInstance, nil)
notificationRequest := &models.NotificationRequest{RanName: RanName, Payload: []byte(e2tInitPayload)}
handler.Handle(notificationRequest)
+ httpClientMock.AssertNotCalled(t, "Post", mock.Anything, mock.Anything, mock.Anything)
writerMock.AssertNotCalled(t, "UpdateNodebInfo")
rmrMessengerMock.AssertNotCalled(t, "SendMsg")
}
-func TestE2TermInitHandlerSuccessOneRan_RoutingManagerFailure(t *testing.T) {
- _, handler, readerMock, writerMock, rmrMessengerMock, e2tInstancesManagerMock, _ := initRanLostConnectionTest(t)
- var rnibErr error
+func TestE2TermInitHandlerSuccessTwoRans_RoutingManagerFailure(t *testing.T) {
+ _, _, handler, readerMock, writerMock, rmrMessengerMock, httpClientMock:= initRanLostConnectionTestWithRealE2tInstanceManager(t)
- var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
- readerMock.On("GetNodeb", RanName).Return(initialNodeb, rnibErr)
+ var rnibErr error
+ var initialNodeb0 = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
+ var initialNodeb1 = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
+ readerMock.On("GetNodeb", RanName).Return(initialNodeb0, rnibErr)
+ readerMock.On("GetNodeb", "test2").Return(initialNodeb1, rnibErr)
var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, ConnectionAttempts: 1}
writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
e2tInstance := entities.NewE2TInstance(e2tInstanceAddress)
e2tInstance.State = entities.RoutingManagerFailure
- e2tInstance.AssociatedRanList = append(e2tInstance.AssociatedRanList, RanName)
+ e2tInstance.AssociatedRanList = append(e2tInstance.AssociatedRanList, RanName, "test2")
- e2tInstancesManagerMock.On("GetE2TInstance", e2tInstanceAddress).Return(e2tInstance, nil)
- e2tInstancesManagerMock.On("ActivateE2TInstance", e2tInstance).Return(nil)
- writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
+ readerMock.On("GetE2TInstance", e2tInstanceAddress).Return(e2tInstance, nil)
+ writerMock.On("SaveE2TInstance", mock.Anything).Return(nil)
notificationRequest := &models.NotificationRequest{RanName: RanName, Payload: []byte(e2tInitPayload)}
handler.Handle(notificationRequest)
- writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
- rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
+ rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 2)
+ writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
+
+ httpClientMock.AssertNotCalled(t, "Post", mock.Anything, mock.Anything, mock.Anything)
+ writerMock.AssertCalled(t, "SaveE2TInstance", mock.Anything)
}
func TestE2TermInitHandlerSuccessOneRan_RoutingManagerFailure_Error(t *testing.T) {
}
func TestE2TermInitHandlerSuccessTwoRans(t *testing.T) {
- _, handler, readerMock, writerMock, rmrMessengerMock, e2tInstancesManagerMock, _ := initRanLostConnectionTest(t)
+ _, handler, readerMock, writerMock, rmrMessengerMock, e2tInstancesManagerMock, httpClientMock := initRanLostConnectionTest(t)
var rnibErr error
var initialNodeb0 = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
var initialNodeb1 = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 2)
+ httpClientMock.AssertNotCalled(t, "Post", mock.Anything, mock.Anything, mock.Anything)
}
func TestE2TermInitHandlerSuccessTwoRansSecondRanShutdown(t *testing.T) {
xaction := []byte(ids[0].InventoryName)
msg0 := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ids[0].InventoryName, &payload, &xaction)
- // Cannot use Mock because request MBuf contains pointers
- //payload =models.NewE2RequestMessage(ids[1].InventoryName /*tid*/, "", 0,ids[1].InventoryName, e2pdus.PackedX2setupRequest).GetMessageAsBytes(log)
- //xaction = []byte(ids[1].InventoryName)
- //msg1 := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ids[1].InventoryName, &payload, &xaction)
-
rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg0, fmt.Errorf("RMR Error"))
e2tInstance := entities.NewE2TInstance(e2tInstanceAddress)
xaction := []byte(ids[0].InventoryName)
msg0 := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ids[0].InventoryName, &payload, &xaction)
- // Cannot use Mock because request MBuf contains pointers
- //payload =models.NewE2RequestMessage(ids[1].InventoryName /*tid*/, "", 0,ids[1].InventoryName, e2pdus.PackedX2setupRequest).GetMessageAsBytes(log)
- //xaction = []byte(ids[1].InventoryName)
- //msg1 := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ids[1].InventoryName, &payload, &xaction)
-
rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg0, nil)
e2tInstance := entities.NewE2TInstance(e2tInstanceAddress)
xaction := []byte(ids[0].InventoryName)
msg0 := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ids[0].InventoryName, &payload, &xaction)
- // Cannot use Mock because request MBuf contains pointers
- //payload =models.NewE2RequestMessage(ids[1].InventoryName /*tid*/, "", 0,ids[1].InventoryName, e2pdus.PackedX2setupRequest).GetMessageAsBytes(log)
- //xaction = []byte(ids[1].InventoryName)
- //msg1 := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ids[1].InventoryName, &payload, &xaction)
-
rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg0, nil)
e2tInstance := entities.NewE2TInstance(e2tInstanceAddress)