Copy latest code to master
[ric-plt/resource-status-manager.git] / RSM / providers / rmrmsghandlerprovider / message_handler_provider_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 rmrmsghandlerprovider
22
23 import (
24         "fmt"
25         "rsm/configuration"
26         "rsm/converters"
27         "rsm/e2pdus"
28         "rsm/handlers/rmrmsghandlers"
29         "rsm/services"
30         "rsm/tests/testhelper"
31         "strings"
32         "testing"
33
34         "rsm/rmrcgo"
35 )
36
37 /*
38  * Verify support for known providers.
39  */
40 func TestGetNotificationHandlerSuccess(t *testing.T) {
41
42         rnibDataService, rmrSender, logger := testhelper.InitTestCase(t)
43         config, err := configuration.ParseConfiguration()
44         if err != nil {
45                 t.Errorf("#... - failed to parse configuration error: %s", err)
46         }
47         resourceStatusService := services.NewResourceStatusService(logger, rmrSender)
48         unpacker := converters.NewX2apPduUnpacker(logger, e2pdus.MaxAsn1CodecMessageBufferSize)
49         var testCases = []struct {
50                 msgType int
51                 handler rmrmsghandlers.RmrMessageHandler
52         }{
53                 {rmrcgo.RanConnected, rmrmsghandlers.NewResourceStatusInitiateNotificationHandler(logger, rnibDataService, resourceStatusService, RanConnected)},
54                 {rmrcgo.RanRestarted, rmrmsghandlers.NewResourceStatusInitiateNotificationHandler(logger, rnibDataService, resourceStatusService, RanRestarted)},
55         }
56
57         for _, tc := range testCases {
58                 provider := NewMessageHandlerProvider(logger, config, rnibDataService, rmrSender, resourceStatusService, converters.NewResourceStatusResponseConverter(unpacker), converters.NewResourceStatusFailureConverter(unpacker))
59                 t.Run(fmt.Sprintf("%d", tc.msgType), func(t *testing.T) {
60                         handler, err := provider.GetMessageHandler(tc.msgType)
61                         if err != nil {
62                                 t.Errorf("want: handler for message type %d, got: error %s", tc.msgType, err)
63                         }
64                         if strings.Compare(fmt.Sprintf("%T", handler), fmt.Sprintf("%T", tc.handler)) != 0 {
65                                 t.Errorf("want: handler %T for message type %d, got: %T", tc.handler, tc.msgType, handler)
66                         }
67                 })
68         }
69 }
70
71 /*
72  * Verify handling of a request for an unsupported message.
73  */
74
75 func TestGetNotificationHandlerFailure(t *testing.T) {
76
77         rnibDataService, rmrSender, logger := testhelper.InitTestCase(t)
78         config, err := configuration.ParseConfiguration()
79         if err != nil {
80                 t.Errorf("#... - failed to parse configuration error: %s", err)
81         }
82         resourceStatusService := services.NewResourceStatusService(logger, rmrSender)
83         unpacker := converters.NewX2apPduUnpacker(logger, e2pdus.MaxAsn1CodecMessageBufferSize)
84
85         var testCases = []struct {
86                 msgType   int
87                 errorText string
88         }{
89                 {9999 /*unknown*/, "notification handler not found"},
90         }
91         for _, tc := range testCases {
92                 provider := NewMessageHandlerProvider(logger, config, rnibDataService, rmrSender, resourceStatusService, converters.NewResourceStatusResponseConverter(unpacker), converters.NewResourceStatusFailureConverter(unpacker))
93                 t.Run(fmt.Sprintf("%d", tc.msgType), func(t *testing.T) {
94                         _, err := provider.GetMessageHandler(tc.msgType)
95                         if err == nil {
96                                 t.Errorf("want: no handler for message type %d, got: success", tc.msgType)
97                         }
98                         if !strings.Contains(fmt.Sprintf("%s", err), tc.errorText) {
99                                 t.Errorf("want: error [%s] for message type %d, got: %s", tc.errorText, tc.msgType, err)
100                         }
101                 })
102         }
103 }