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