f6a3900b301f6e296c661d5fa74697c05aec9bd6
[ric-plt/resource-status-manager.git] / RSM / handlers / rmrmsghandlers / resource_status_initiate_notification_handler_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 package rmrmsghandlers
21
22 import (
23         "github.com/stretchr/testify/assert"
24         "github.com/stretchr/testify/mock"
25         "rsm/configuration"
26         "rsm/e2pdus"
27         "rsm/logger"
28         "rsm/mocks"
29         "rsm/models"
30         "testing"
31         "time"
32 )
33
34 func initRanConnectedNotificationHandlerTest(t *testing.T, requestName string) (ResourceStatusInitiateNotificationHandler, *mocks.ResourceStatusInitiateManagerMock, *configuration.Configuration) {
35         log, err := logger.InitLogger(logger.DebugLevel)
36         if err != nil {
37                 t.Errorf("#... - failed to initialize logger, error: %s", err)
38         }
39         config, err := configuration.ParseConfiguration()
40         if err != nil {
41                 t.Errorf("#... - failed to parse configuration error: %s", err)
42         }
43         managerMock := &mocks.ResourceStatusInitiateManagerMock{}
44         h := NewResourceStatusInitiateNotificationHandler(log, config, managerMock, requestName)
45         return h, managerMock, config
46 }
47
48 func TestHandlerInit(t *testing.T) {
49         h, _, _ := initRanConnectedNotificationHandlerTest(t, "RanConnected")
50         assert.NotNil(t, h)
51 }
52
53 func TestHandleSuccess(t *testing.T) {
54         h, managerMock, config := initRanConnectedNotificationHandlerTest(t, "RanConnected")
55
56         payloadStr := "{\"nodeType\":1, \"messageDirection\":1}"
57         payload := []byte(payloadStr)
58         rmrReq := &models.RmrRequest{RanName:"RAN1", Payload:payload, Len:len(payload), StartTime:time.Now()}
59         managerMock.On("Execute", rmrReq.RanName, mock.AnythingOfType("*e2pdus.ResourceStatusRequestData")).Return(nil)
60
61         resourceStatusInitiateRequestParams := &e2pdus.ResourceStatusRequestData{}
62         populateResourceStatusInitiateRequestParams(resourceStatusInitiateRequestParams, config)
63
64         h.Handle(rmrReq)
65         managerMock.AssertCalled(t, "Execute", rmrReq.RanName, resourceStatusInitiateRequestParams)
66 }
67
68 func TestHandleResourceStatusNotEnabled(t *testing.T) {
69         h, managerMock, config := initRanConnectedNotificationHandlerTest(t, "RanConnected")
70         config.ResourceStatusParams.EnableResourceStatus = false
71
72         payloadStr := "{\"nodeType\":1, \"messageDirection\":1}"
73         payload := []byte(payloadStr)
74         rmrReq := &models.RmrRequest{RanName:"RAN1", Payload:payload, Len:len(payload), StartTime:time.Now()}
75         managerMock.On("Execute", rmrReq.RanName, mock.AnythingOfType("*e2pdus.ResourceStatusRequestData")).Return(nil)
76
77         h.Handle(rmrReq)
78         managerMock.AssertNumberOfCalls(t, "Execute", 0)
79 }
80
81 func TestHandleUnknownJson(t *testing.T) {
82         h, managerMock, _ := initRanConnectedNotificationHandlerTest(t, "RanConnected")
83
84         payloadStr := "blablabla"
85         payload := []byte(payloadStr)
86         rmrReq := &models.RmrRequest{RanName:"RAN1", Payload:payload, Len:len(payload), StartTime:time.Now()}
87         managerMock.On("Execute", rmrReq.RanName, mock.AnythingOfType("*e2pdus.ResourceStatusRequestData")).Return(nil)
88
89         h.Handle(rmrReq)
90         managerMock.AssertNumberOfCalls(t, "Execute", 0)
91 }
92
93 func TestHandleGnbNode(t *testing.T) {
94         h, managerMock, _ := initRanConnectedNotificationHandlerTest(t, "RanConnected")
95
96         payloadStr := "{\"nodeType\":2, \"messageDirection\":1}"
97         payload := []byte(payloadStr)
98         rmrReq := &models.RmrRequest{RanName:"RAN1", Payload:payload, Len:len(payload), StartTime:time.Now()}
99         managerMock.On("Execute", rmrReq.RanName, mock.AnythingOfType("*e2pdus.ResourceStatusRequestData")).Return(nil)
100
101         h.Handle(rmrReq)
102         managerMock.AssertNumberOfCalls(t, "Execute", 0)
103 }