[RICPLT-1898] - Replace http router to gorilla-mux + UT + Automation code
[ric-plt/e2mgr.git] / E2Manager / managers / ran_reconnection_manager_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
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
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 package managers
19
20 import (
21         "e2mgr/configuration"
22         "e2mgr/logger"
23         "e2mgr/mocks"
24         "e2mgr/models"
25         "e2mgr/rNibWriter"
26         "e2mgr/rmrCgo"
27         "e2mgr/services"
28         "e2mgr/sessions"
29         "e2mgr/tests"
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"
35         "testing"
36 )
37
38 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
39         logger, err := logger.InitLogger(logger.DebugLevel)
40         if err != nil {
41                 t.Errorf("#... - failed to initialize logger, error: %s", err)
42         }
43
44         rmrService := getRmrService(&mocks.RmrMessengerMock{}, logger)
45
46         readerMock := &mocks.RnibReaderMock{}
47         rnibReaderProvider := func() reader.RNibReader {
48                 return readerMock
49         }
50         writerMock := &mocks.RnibWriterMock{}
51         rnibWriterProvider := func() rNibWriter.RNibWriter {
52                 return writerMock
53         }
54
55         ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibReaderProvider, rnibWriterProvider, rmrService)
56         return logger, readerMock, writerMock, ranReconnectionManager
57 }
58
59 func TestRanReconnectionGetNodebFailure(t *testing.T) {
60         _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
61         ranName := "test"
62         var nodebInfo *entities.NodebInfo
63         readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
64         err := ranReconnectionManager.ReconnectRan(ranName)
65         assert.NotNil(t, err)
66         readerMock.AssertCalled(t, "GetNodeb", ranName)
67         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
68 }
69
70 func TestShutdownRanReconnection(t *testing.T) {
71         _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
72         ranName := "test"
73         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
74         var rnibErr common.IRNibError
75         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
76         err := ranReconnectionManager.ReconnectRan(ranName)
77         assert.Nil(t, err)
78         readerMock.AssertCalled(t, "GetNodeb", ranName)
79         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
80 }
81
82 func TestShuttingdownRanReconnection(t *testing.T) {
83         _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
84         ranName := "test"
85         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
86         var rnibErr common.IRNibError
87         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
88         updatedNodebInfo := *origNodebInfo
89         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
90         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
91         err := ranReconnectionManager.ReconnectRan(ranName)
92         assert.Nil(t, err)
93         readerMock.AssertCalled(t, "GetNodeb", ranName)
94         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
95 }
96
97 func TestConnectingRanWithMaxAttemptsReconnection(t *testing.T) {
98         _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
99         ranName := "test"
100         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20}
101         var rnibErr common.IRNibError
102         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
103         updatedNodebInfo := *origNodebInfo
104         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
105         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
106         err := ranReconnectionManager.ReconnectRan(ranName)
107         assert.Nil(t, err)
108         readerMock.AssertCalled(t, "GetNodeb", ranName)
109         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
110 }
111
112 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
113         _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
114         ranName := "test"
115         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
116         var rnibErr common.IRNibError
117         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
118         updatedNodebInfo := *origNodebInfo
119         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
120         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
121         err := ranReconnectionManager.ReconnectRan(ranName)
122         assert.NotNil(t, err)
123         readerMock.AssertCalled(t, "GetNodeb", ranName)
124         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
125 }
126
127 // TODO: should extract to test_utils
128 func getRmrService(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *services.RmrService {
129         rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
130         messageChannel := make(chan *models.NotificationResponse)
131         rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
132         return services.NewRmrService(services.NewRmrConfig(tests.Port, tests.MaxMsgSize, tests.Flags, log), rmrMessenger, make(sessions.E2Sessions), messageChannel)
133 }