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.
30 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
31 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
33 "github.com/pkg/errors"
34 "github.com/stretchr/testify/assert"
38 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
39 logger, err := logger.InitLogger(logger.DebugLevel)
41 t.Errorf("#... - failed to initialize logger, error: %s", err)
44 rmrService := getRmrService(&mocks.RmrMessengerMock{}, logger)
46 readerMock := &mocks.RnibReaderMock{}
47 rnibReaderProvider := func() reader.RNibReader {
50 writerMock := &mocks.RnibWriterMock{}
51 rnibWriterProvider := func() rNibWriter.RNibWriter {
55 ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibReaderProvider, rnibWriterProvider, rmrService)
56 return logger, readerMock, writerMock, ranReconnectionManager
59 func TestLostConnectionFetchingNodebFailure(t *testing.T) {
60 _, readerMock, _, ranReconnectionManager := initRanLostConnectionTest(t)
62 var nodebInfo *entities.NodebInfo
63 readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
64 err := ranReconnectionManager.ReconnectRan(ranName)
68 func TestLostConnectionUpdatingNodebForUnconnectableRanFailure(t *testing.T) {
69 _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
71 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
72 var rnibErr common.IRNibError
73 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
74 updatedNodebInfo := *origNodebInfo
75 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
76 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
77 err := ranReconnectionManager.ReconnectRan(ranName)
81 func TestLostConnectionOfConnectedRanWithMaxAttempts(t *testing.T) {
82 _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
84 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, ConnectionAttempts: 20}
85 var rnibErr common.IRNibError
86 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
87 updatedNodebInfo := *origNodebInfo
88 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
89 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
90 err := ranReconnectionManager.ReconnectRan(ranName)
94 func TestLostConnectionOfShutdownRan(t *testing.T) {
95 _, readerMock, _, ranReconnectionManager := initRanLostConnectionTest(t)
97 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
98 var rnibErr common.IRNibError
99 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
100 err := ranReconnectionManager.ReconnectRan(ranName)
104 func TestLostConnectionOfShuttingdownRan(t *testing.T) {
105 _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
107 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
108 var rnibErr common.IRNibError
109 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
110 updatedNodebInfo := *origNodebInfo
111 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
112 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
113 err := ranReconnectionManager.ReconnectRan(ranName)
117 // TODO: should extract to test_utils
118 func getRmrService(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *services.RmrService {
119 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
120 messageChannel := make(chan *models.NotificationResponse)
121 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
122 return services.NewRmrService(services.NewRmrConfig(tests.Port, tests.MaxMsgSize, tests.Flags, log), rmrMessenger, make(sessions.E2Sessions), messageChannel)