[RIC-475] [RIC-507] Inject RanStatusChangeManager | Enhance E2 Setup flow | Remove...
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / ran_lost_connection_handler_test.go
1                 //// Copyright 2019 AT&T Intellectual Property
2 //// Copyright 2019 Nokia
3 ////
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
7 ////
8 ////      http://www.apache.org/licenses/LICENSE-2.0
9 ////
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.
15
16 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 //  platform project (RICP).
18
19 package rmrmsghandlers
20
21 import (
22         "bytes"
23         "e2mgr/clients"
24         "e2mgr/configuration"
25         "e2mgr/logger"
26         "e2mgr/managers"
27         "e2mgr/mocks"
28         "e2mgr/models"
29         "e2mgr/services"
30         "encoding/json"
31         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32         "github.com/pkg/errors"
33         "io/ioutil"
34         "net/http"
35         "testing"
36 )
37
38 const ranName = "test"
39 const e2tAddress = "10.10.2.15:9800"
40
41 func TestLostConnectionHandlerSuccess(t *testing.T) {
42         logger, _ := logger.InitLogger(logger.InfoLevel)
43
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(&notificationRequest)
49         ranDisconnectionManagerMock.AssertCalled(t, "DisconnectRan", ranName)
50 }
51
52 func TestLostConnectionHandlerFailure(t *testing.T) {
53         logger, _ := logger.InitLogger(logger.InfoLevel)
54
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(&notificationRequest)
60         ranDisconnectionManagerMock.AssertCalled(t, "DisconnectRan", ranName)
61 }
62
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}
66
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         ranListManager := managers.NewRanListManager(logger)
74         ranAlarmService := services.NewRanAlarmService(logger, config)
75         ranConnectStatusChangeManager := managers.NewRanConnectStatusChangeManager(logger, rnibDataService,ranListManager, ranAlarmService)
76
77         e2tAssociationManager := managers.NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient, ranConnectStatusChangeManager)
78         ranDisconnectionManager := managers.NewRanDisconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, e2tAssociationManager)
79         handler := NewRanLostConnectionHandler(logger, ranDisconnectionManager)
80
81         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, AssociatedE2TInstanceAddress: e2tAddress}
82         var rnibErr error
83         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
84         updatedNodebInfo1 := *origNodebInfo
85         updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
86         writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
87         updatedNodebInfo2 := *origNodebInfo
88         updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
89         updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
90         writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
91         e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList: []string{ranName}}
92         readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
93         e2tInstanceToSave := *e2tInstance
94         e2tInstanceToSave.AssociatedRanList = []string{}
95         writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
96         mockHttpClient(httpClientMock, isSuccessfulHttpPost)
97
98         return handler, readerMock, writerMock, httpClientMock
99 }
100
101 func mockHttpClient(httpClientMock *mocks.HttpClientMock, isSuccessful bool) {
102         data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(e2tAddress, RanName)}
103         marshaled, _ := json.Marshal(data)
104         body := bytes.NewBuffer(marshaled)
105         respBody := ioutil.NopCloser(bytes.NewBufferString(""))
106         var respStatusCode int
107         if isSuccessful {
108                 respStatusCode = http.StatusCreated
109         } else {
110                 respStatusCode = http.StatusBadRequest
111         }
112         httpClientMock.On("Post", clients.DissociateRanE2TInstanceApiSuffix, "application/json", body).Return(&http.Response{StatusCode: respStatusCode, Body: respBody}, nil)
113 }
114
115 func TestLostConnectionHandlerFailureWithRealDisconnectionManager(t *testing.T) {
116         handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTestWithRealDisconnectionManager(t, false)
117
118         notificationRequest := models.NotificationRequest{RanName: ranName}
119         handler.Handle(&notificationRequest)
120
121         readerMock.AssertExpectations(t)
122         writerMock.AssertExpectations(t)
123         httpClientMock.AssertExpectations(t)
124         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
125 }
126
127 func TestLostConnectionHandlerSuccessWithRealDisconnectionManager(t *testing.T) {
128         handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTestWithRealDisconnectionManager(t, true)
129
130         notificationRequest := models.NotificationRequest{RanName: ranName}
131         handler.Handle(&notificationRequest)
132
133         readerMock.AssertExpectations(t)
134         writerMock.AssertExpectations(t)
135         httpClientMock.AssertExpectations(t)
136         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
137 }