Squash-merging e2ap-v2.0 branch
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / e2_node_config_update_notification_handler_test.go
1 //
2 // Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
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         "e2mgr/configuration"
23         "e2mgr/mocks"
24         "e2mgr/models"
25         "e2mgr/rmrCgo"
26         "e2mgr/services"
27         "e2mgr/tests"
28         "e2mgr/utils"
29         "testing"
30
31         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32         "github.com/stretchr/testify/assert"
33         "github.com/stretchr/testify/mock"
34 )
35
36 const (
37         E2nodeConfigUpdateXmlPath                      = "../../tests/resources/configurationUpdate/e2NodeConfigurationUpdate.xml"
38         E2nodeConfigUpdateOnlyAdditionXmlPath          = "../../tests/resources/configurationUpdate/e2NodeConfigurationUpdateOnlyAddition.xml"
39         E2nodeConfigUpdateOnlyAdditionAndUpdateXmlPath = "../../tests/resources/configurationUpdate/e2NodeConfigurationUpdateAdditionAndUpdateOnly.xml"
40 )
41
42 func initE2nodeConfigMocks(t *testing.T) (*E2nodeConfigUpdateNotificationHandler, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.RmrMessengerMock) {
43         logger := tests.InitLog(t)
44         config := &configuration.Configuration{
45                 RnibRetryIntervalMs:       10,
46                 MaxRnibConnectionAttempts: 3,
47                 RnibWriter: configuration.RnibWriterConfig{
48                         StateChangeMessageChannel: StateChangeMessageChannel,
49                 },
50                 GlobalRicId: struct {
51                         RicId string
52                         Mcc   string
53                         Mnc   string
54                 }{Mcc: "327", Mnc: "94", RicId: "AACCE"}}
55         readerMock := &mocks.RnibReaderMock{}
56         writerMock := &mocks.RnibWriterMock{}
57         rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
58         rmrMessengerMock := &mocks.RmrMessengerMock{}
59         rmrSender := tests.InitRmrSender(rmrMessengerMock, logger)
60         handler := NewE2nodeConfigUpdateNotificationHandler(logger, rnibDataService, rmrSender)
61         return handler, readerMock, writerMock, rmrMessengerMock
62 }
63
64 func TestE2nodeConfigUpdatetNotificationHandler(t *testing.T) {
65         e2NodeConfigUpdateXml := utils.ReadXmlFile(t, E2nodeConfigUpdateOnlyAdditionXmlPath)
66         handler, readerMock, writerMock, rmrMessengerMock := initE2nodeConfigMocks(t)
67         var nodebInfo = &entities.NodebInfo{
68                 RanName:                      gnbNodebRanName,
69                 AssociatedE2TInstanceAddress: e2tInstanceFullAddress,
70                 ConnectionStatus:             entities.ConnectionStatus_DISCONNECTED,
71                 NodeType:                     entities.Node_GNB,
72                 Configuration: &entities.NodebInfo_Gnb{
73                         Gnb: &entities.Gnb{},
74                 },
75         }
76         readerMock.On("GetNodeb", gnbNodebRanName).Return(nodebInfo, nil)
77         writerMock.On("UpdateNodebInfoAndPublish", mock.Anything).Return(nil)
78         var errEmpty error
79         rmrMessengerMock.On("SendMsg", mock.Anything, mock.Anything).Return(&rmrCgo.MBuf{}, errEmpty)
80         notificationRequest := &models.NotificationRequest{RanName: gnbNodebRanName, Payload: append([]byte(""), e2NodeConfigUpdateXml...)}
81         handler.Handle(notificationRequest)
82         readerMock.AssertExpectations(t)
83         writerMock.AssertExpectations(t)
84 }
85
86 func TestE2nodeConfigUpdatetParseFail(t *testing.T) {
87         handler, _, _, _ := initE2nodeConfigMocks(t)
88         badxml := []byte("abc")
89         e2nodeConfig, err := handler.parseE2NodeConfigurationUpdate(badxml)
90
91         var expected *models.E2nodeConfigurationUpdateMessage
92         assert.Equal(t, expected, e2nodeConfig)
93         assert.NotNil(t, err)
94 }
95
96 func TestHandleAddAndUpdateConfig(t *testing.T) {
97         e2NodeConfigUpdateXml := utils.ReadXmlFile(t, E2nodeConfigUpdateOnlyAdditionAndUpdateXmlPath)
98
99         handler, readerMock, writerMock, rmrMessengerMock := initE2nodeConfigMocks(t)
100         var nodebInfo = &entities.NodebInfo{
101                 RanName:                      gnbNodebRanName,
102                 AssociatedE2TInstanceAddress: e2tInstanceFullAddress,
103                 ConnectionStatus:             entities.ConnectionStatus_DISCONNECTED,
104                 NodeType:                     entities.Node_GNB,
105                 Configuration: &entities.NodebInfo_Gnb{
106                         Gnb: &entities.Gnb{},
107                 },
108         }
109         readerMock.On("GetNodeb", gnbNodebRanName).Return(nodebInfo, nil)
110         writerMock.On("UpdateNodebInfoAndPublish", mock.Anything).Return(nil)
111         var errEmpty error
112         rmrMessengerMock.On("SendMsg", mock.Anything, mock.Anything).Return(&rmrCgo.MBuf{}, errEmpty)
113
114         notificationRequest := &models.NotificationRequest{RanName: gnbNodebRanName, Payload: append([]byte(""), e2NodeConfigUpdateXml...)}
115
116         handler.Handle(notificationRequest)
117
118         t.Logf("len of addtionList : %d", len(nodebInfo.GetGnb().NodeConfigs))
119
120         assert.Equal(t, 5, len(nodebInfo.GetGnb().NodeConfigs))
121         assert.Equal(t, "nginterf", nodebInfo.GetGnb().NodeConfigs[0].GetE2NodeComponentInterfaceTypeNG().AmfName)
122         assert.Equal(t, "72 65 71 70 61 72 73", string(nodebInfo.GetGnb().NodeConfigs[0].E2NodeComponentRequestPart))
123         assert.Equal(t, "72 65 73 70 61 72 73", string(nodebInfo.GetGnb().NodeConfigs[0].E2NodeComponentResponsePart))
124
125         writerMock.AssertExpectations(t)
126         readerMock.AssertExpectations(t)
127 }
128
129 func TestHandleAddandDeleteConfig(t *testing.T) {
130         e2NodeConfigUpdateXml := utils.ReadXmlFile(t, E2nodeConfigUpdateXmlPath)
131
132         handler, readerMock, writerMock, rmrMessengerMock := initE2nodeConfigMocks(t)
133         var nodebInfo = &entities.NodebInfo{
134                 RanName:                      gnbNodebRanName,
135                 AssociatedE2TInstanceAddress: e2tInstanceFullAddress,
136                 ConnectionStatus:             entities.ConnectionStatus_DISCONNECTED,
137                 NodeType:                     entities.Node_GNB,
138                 Configuration: &entities.NodebInfo_Gnb{
139                         Gnb: &entities.Gnb{},
140                 },
141         }
142         readerMock.On("GetNodeb", gnbNodebRanName).Return(nodebInfo, nil)
143         writerMock.On("UpdateNodebInfoAndPublish", mock.Anything).Return(nil)
144         var errEmpty error
145         rmrMessengerMock.On("SendMsg", mock.Anything, mock.Anything).Return(&rmrCgo.MBuf{}, errEmpty)
146
147         notificationRequest := &models.NotificationRequest{RanName: gnbNodebRanName, Payload: append([]byte(""), e2NodeConfigUpdateXml...)}
148
149         handler.Handle(notificationRequest)
150
151         t.Logf("len of nodeconfig : %d", len(nodebInfo.GetGnb().NodeConfigs))
152
153         assert.Equal(t, 0, len(nodebInfo.GetGnb().NodeConfigs))
154
155         writerMock.AssertExpectations(t)
156         readerMock.AssertExpectations(t)
157 }