836c02a0298f27f0bd1f23ee4b9868f85d25ea33
[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         ranReconnectionManagerMock := &mocks.RanReconnectionManagerMock{}
46         ranReconnectionManagerMock.On("ReconnectRan", ranName).Return(nil)
47         handler := NewRanLostConnectionHandler(logger, ranReconnectionManagerMock)
48         handler.Handle(&notificationRequest)
49         ranReconnectionManagerMock.AssertCalled(t, "ReconnectRan", ranName)
50 }
51
52 func TestLostConnectionHandlerFailure(t *testing.T) {
53         logger, _ := logger.InitLogger(logger.InfoLevel)
54
55         notificationRequest := models.NotificationRequest{RanName: ranName}
56         ranReconnectionManagerMock := &mocks.RanReconnectionManagerMock{}
57         ranReconnectionManagerMock.On("ReconnectRan", ranName).Return(errors.New("error"))
58         handler := NewRanLostConnectionHandler(logger, ranReconnectionManagerMock)
59         handler.Handle(&notificationRequest)
60         ranReconnectionManagerMock.AssertCalled(t, "ReconnectRan", ranName)
61 }
62
63 func setupLostConnectionHandlerTestWithRealReconnectionManager(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         rmrMessengerMock := &mocks.RmrMessengerMock{}
68         rmrSender := initRmrSender(rmrMessengerMock, logger)
69         readerMock := &mocks.RnibReaderMock{}
70         writerMock := &mocks.RnibWriterMock{}
71         rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
72         e2tInstancesManager := managers.NewE2TInstancesManager(rnibDataService, logger)
73         ranSetupManager := managers.NewRanSetupManager(logger, rmrSender, rnibDataService)
74         httpClientMock := &mocks.HttpClientMock{}
75         routingManagerClient := clients.NewRoutingManagerClient(logger, config, httpClientMock)
76         e2tAssociationManager := managers.NewE2TAssociationManager(logger, rnibDataService, e2tInstancesManager, routingManagerClient)
77         ranReconnectionManager := managers.NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, ranSetupManager, e2tAssociationManager)
78         handler := NewRanLostConnectionHandler(logger, ranReconnectionManager)
79
80         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20, AssociatedE2TInstanceAddress: e2tAddress}
81         var rnibErr error
82         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
83         updatedNodebInfo1 := *origNodebInfo
84         updatedNodebInfo1.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
85         writerMock.On("UpdateNodebInfo", &updatedNodebInfo1).Return(rnibErr)
86         updatedNodebInfo2 := *origNodebInfo
87         updatedNodebInfo2.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
88         updatedNodebInfo2.AssociatedE2TInstanceAddress = ""
89         writerMock.On("UpdateNodebInfo", &updatedNodebInfo2).Return(rnibErr)
90         e2tInstance := &entities.E2TInstance{Address: e2tAddress, AssociatedRanList:[]string{ranName}}
91         readerMock.On("GetE2TInstance", e2tAddress).Return(e2tInstance, nil)
92         e2tInstanceToSave := *e2tInstance
93         e2tInstanceToSave .AssociatedRanList = []string{}
94         writerMock.On("SaveE2TInstance", &e2tInstanceToSave).Return(nil)
95         mockHttpClient(httpClientMock, isSuccessfulHttpPost)
96
97         return handler, readerMock, writerMock, httpClientMock
98 }
99
100 func mockHttpClient(httpClientMock *mocks.HttpClientMock, isSuccessful bool) {
101         data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(e2tAddress, RanName)}
102         marshaled, _ := json.Marshal(data)
103         body := bytes.NewBuffer(marshaled)
104         respBody := ioutil.NopCloser(bytes.NewBufferString(""))
105         var respStatusCode int
106         if isSuccessful {
107                 respStatusCode = http.StatusCreated
108         } else {
109                 respStatusCode = http.StatusBadRequest
110         }
111         httpClientMock.On("Post", clients.DissociateRanE2TInstanceApiSuffix, "application/json", body).Return(&http.Response{StatusCode: respStatusCode, Body: respBody}, nil)
112 }
113
114 func TestLostConnectionHandlerFailureWithRealReconnectionManager(t *testing.T) {
115         handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTestWithRealReconnectionManager(t, false)
116
117         notificationRequest := models.NotificationRequest{RanName: ranName}
118         handler.Handle(&notificationRequest)
119
120         readerMock.AssertExpectations(t)
121         writerMock.AssertExpectations(t)
122         httpClientMock.AssertExpectations(t)
123         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
124 }
125
126 func TestLostConnectionHandlerSuccessWithRealReconnectionManager(t *testing.T) {
127         handler, readerMock, writerMock, httpClientMock := setupLostConnectionHandlerTestWithRealReconnectionManager(t, true)
128
129         notificationRequest := models.NotificationRequest{RanName: ranName}
130         handler.Handle(&notificationRequest)
131
132         readerMock.AssertExpectations(t)
133         writerMock.AssertExpectations(t)
134         httpClientMock.AssertExpectations(t)
135         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
136 }