1 //// Copyright 2019 AT&T Intellectual Property
2 //// Copyright 2019 Nokia
4 //// Licensed under the Apache License, Version 2.0 (the "License");
5 //// you may not use this file except in compliance with the License.
6 //// You may obtain a copy of the License at
8 //// http://www.apache.org/licenses/LICENSE-2.0
10 //// Unless required by applicable law or agreed to in writing, software
11 //// distributed under the License is distributed on an "AS IS" BASIS,
12 //// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //// See the License for the specific language governing permissions and
14 //// limitations under the License.
16 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 // platform project (RICP).
19 package rmrmsghandlers
31 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32 "github.com/pkg/errors"
38 const ranName = "test"
39 const e2tAddress = "10.10.2.15:9800"
41 func TestLostConnectionHandlerSuccess(t *testing.T) {
42 logger, _ := logger.InitLogger(logger.InfoLevel)
44 notificationRequest := models.NotificationRequest{RanName: ranName}
45 ranDisconnectionManagerMock := &mocks.RanDisconnectionManagerMock{}
46 ranDisconnectionManagerMock.On("DisconnectRan", ranName).Return(nil)
47 handler := NewRanLostConnectionHandler(logger, ranDisconnectionManagerMock)
48 handler.Handle(¬ificationRequest)
49 ranDisconnectionManagerMock.AssertCalled(t, "DisconnectRan", ranName)
52 func TestLostConnectionHandlerFailure(t *testing.T) {
53 logger, _ := logger.InitLogger(logger.InfoLevel)
55 notificationRequest := models.NotificationRequest{RanName: ranName}
56 ranDisconnectionManagerMock := &mocks.RanDisconnectionManagerMock{}
57 ranDisconnectionManagerMock.On("DisconnectRan", ranName).Return(errors.New("error"))
58 handler := NewRanLostConnectionHandler(logger, ranDisconnectionManagerMock)
59 handler.Handle(¬ificationRequest)
60 ranDisconnectionManagerMock.AssertCalled(t, "DisconnectRan", ranName)
63 func setupLostConnectionHandlerTestWithRealDisconnectionManager(t *testing.T, isSuccessfulHttpPost bool) (RanLostConnectionHandler, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.HttpClientMock) {
64 logger, _ := logger.InitLogger(logger.InfoLevel)
65 config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
67 readerMock := &mocks.RnibReaderMock{}
68 writerMock := &mocks.RnibWriterMock{}
69 rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
70 e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, logger)
71 httpClientMock := &mocks.HttpClientMock{}
72 routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClientMock)
73 e2tAssociationManager := managers.NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient)
74 ranDisconnectionManager := managers.NewRanDisconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, e2tAssociationManager)
75 handler := NewRanLostConnectionHandler(logger, ranDisconnectionManager)
77 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
79 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
80 updatedNodebInfo1 := *origNodebInfo
81 updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
82 writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
83 updatedNodebInfo2 := *origNodebInfo
84 updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
85 updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
86 writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
87 e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
88 readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
89 e2tInstanceToSave := *e2tInstance
90 e2tInstanceToSave.AssociatedRanList = []string{}
91 writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
92 mockHttpClient(httpClientMock, isSuccessfulHttpPost)
94 return handler, readerMock, writerMock, httpClientMock
97 func mockHttpClient(httpClientMock *mocks.HttpClientMock, isSuccessful bool) {
98 data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(e2tAddress, RanName)}
99 marshaled, _ := json.Marshal(data)
100 body := bytes.NewBuffer(marshaled)
101 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
102 var respStatusCode int
104 respStatusCode = http.StatusCreated
106 respStatusCode = http.StatusBadRequest
108 httpClientMock.On("Post", clients.DissociateRanE2TInstanceApiSuffix, "application/json", body).Return(&http.Response{StatusCode: respStatusCode, Body: respBody}, nil)
111 func TestLostConnectionHandlerFailureWithRealDisconnectionManager(t *testing.T) {
112 handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTestWithRealDisconnectionManager(t, false)
114 notificationRequest := models.NotificationRequest{RanName: ranName}
115 handler.Handle(¬ificationRequest)
117 readerMock.AssertExpectations(t)
118 writerMock.AssertExpectations(t)
119 httpClientMock.AssertExpectations(t)
120 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
123 func TestLostConnectionHandlerSuccessWithRealDisconnectionManager(t *testing.T) {
124 handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTestWithRealDisconnectionManager(t, true)
126 notificationRequest := models.NotificationRequest{RanName: ranName}
127 handler.Handle(¬ificationRequest)
129 readerMock.AssertExpectations(t)
130 writerMock.AssertExpectations(t)
131 httpClientMock.AssertExpectations(t)
132 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)