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