Merge "Adding testcase for notifying for Connected status"
[ric-plt/e2mgr.git] / E2Manager / managers / ran_setup_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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20
21 package managers
22
23 import (
24         "e2mgr/configuration"
25         "e2mgr/e2managererrors"
26         "e2mgr/e2pdus"
27         "e2mgr/logger"
28         "e2mgr/mocks"
29         "e2mgr/rmrCgo"
30         "e2mgr/services"
31         "fmt"
32         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
33         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
34         "github.com/stretchr/testify/assert"
35         "github.com/stretchr/testify/mock"
36         "testing"
37         "unsafe"
38 )
39
40 func initRanSetupManagerTest(t *testing.T) (*mocks.RmrMessengerMock, *mocks.RnibWriterMock, *RanSetupManager) {
41         DebugLevel := int8(4)
42         logger, err := logger.InitLogger(DebugLevel)
43         if err != nil {
44                 t.Errorf("#... - failed to initialize logger, error: %s", err)
45         }
46         config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
47
48         rmrMessengerMock := &mocks.RmrMessengerMock{}
49         rmrSender := initRmrSender(rmrMessengerMock, logger)
50
51         readerMock := &mocks.RnibReaderMock{}
52
53         writerMock := &mocks.RnibWriterMock{}
54
55         rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
56         ranSetupManager := NewRanSetupManager(logger, rmrSender, rnibDataService)
57         return rmrMessengerMock, writerMock, ranSetupManager
58 }
59
60 func TestExecuteSetupConnectingX2Setup(t *testing.T) {
61         rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
62
63         ranName := "test1"
64
65         var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
66         var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
67         var rnibErr error
68         writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
69
70         payload := e2pdus.PackedX2setupRequest
71         xAction := []byte(ranName)
72         var msgSrc unsafe.Pointer
73         msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
74         rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, nil)
75
76         if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err != nil {
77                 t.Errorf("want: success, got: error: %s", err)
78         }
79
80         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
81         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
82 }
83
84 func TestExecuteSetupConnectingEndcX2Setup(t *testing.T) {
85         rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
86
87         ranName := "test1"
88
89                 var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
90         var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
91         var rnibErr error
92         writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
93
94         payload := e2pdus.PackedEndcX2setupRequest
95         xAction := []byte(ranName)
96         var msgSrc unsafe.Pointer
97         msg := rmrCgo.NewMBuf(rmrCgo.RIC_ENDC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
98         rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, nil)
99
100         if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err != nil {
101                 t.Errorf("want: success, got: error: %s", err)
102         }
103
104         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
105         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
106 }
107
108 func TestExecuteSetupDisconnected(t *testing.T) {
109         rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
110
111         ranName := "test1"
112
113         var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
114         var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
115         var argNodebDisconnected = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
116         var rnibErr error
117         writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
118         writerMock.On("UpdateNodebInfo", argNodebDisconnected).Return(rnibErr)
119
120         payload := []byte{0}
121         xAction := []byte(ranName)
122         var msgSrc unsafe.Pointer
123         msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
124         rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, fmt.Errorf("send failure"))
125
126         if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err == nil {
127                 t.Errorf("want: failure, got: success")
128         }
129
130         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
131         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
132 }
133
134 func TestExecuteSetupConnectingRnibError(t *testing.T) {
135         rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
136
137         ranName := "test1"
138
139         var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
140         var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
141         var argNodebDisconnected = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
142         var rnibErr = common.NewInternalError(fmt.Errorf("DB error"))
143         writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
144         writerMock.On("UpdateNodebInfo", argNodebDisconnected).Return(rnibErr)
145
146         payload := []byte{0}
147         xAction := []byte(ranName)
148         var msgSrc unsafe.Pointer
149         msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
150         rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, fmt.Errorf("send failure"))
151
152         if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err == nil {
153                 t.Errorf("want: failure, got: success")
154         } else {
155                 assert.IsType(t, e2managererrors.NewRnibDbError(), err)
156         }
157
158         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
159         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 0)
160 }
161
162 func TestExecuteSetupDisconnectedRnibError(t *testing.T) {
163         rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
164
165         ranName := "test1"
166
167         var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
168         var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
169         var argNodebDisconnected = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
170         var rnibErr error
171         writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
172         writerMock.On("UpdateNodebInfo", argNodebDisconnected).Return(common.NewInternalError(fmt.Errorf("DB error")))
173
174         payload := []byte{0}
175         xAction := []byte(ranName)
176         var msgSrc unsafe.Pointer
177         msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
178         rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, fmt.Errorf("send failure"))
179
180         if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err == nil {
181                 t.Errorf("want: failure, got: success")
182         } else {
183                 assert.IsType(t, e2managererrors.NewRnibDbError(), err)
184         }
185
186         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 2)
187         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
188 }
189
190 func TestExecuteSetupUnsupportedProtocol(t *testing.T) {
191         rmrMessengerMock, writerMock, mgr := initRanSetupManagerTest(t)
192
193         ranName := "test1"
194
195         var initialNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_UNKNOWN_E2_APPLICATION_PROTOCOL}
196         var argNodeb = &entities.NodebInfo{ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_UNKNOWN_E2_APPLICATION_PROTOCOL}
197         var rnibErr error
198         writerMock.On("UpdateNodebInfo", argNodeb).Return(rnibErr)
199
200         payload := e2pdus.PackedX2setupRequest
201         xAction := []byte(ranName)
202         var msgSrc unsafe.Pointer
203         msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
204         rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, nil)
205
206         if err := mgr.ExecuteSetup(initialNodeb, entities.ConnectionStatus_CONNECTING); err == nil {
207                 t.Errorf("want: error, got: success")
208         }
209
210         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
211         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 0)
212 }
213
214 func initLog(t *testing.T) *logger.Logger {
215         InfoLevel := int8(3)
216         log, err := logger.InitLogger(InfoLevel)
217         if err != nil {
218                 t.Errorf("#initLog test - failed to initialize logger, error: %s", err)
219         }
220         return log
221 }