974de025b811944a7398987ded2c230dfc9aa24d
[ric-plt/e2mgr.git] / E2Manager / providers / notification_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
18 package providers
19
20 import (
21         "e2mgr/mocks"
22         "e2mgr/rNibWriter"
23         "fmt"
24         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
25         "strings"
26         "testing"
27
28         "e2mgr/handlers"
29         "e2mgr/rmrCgo"
30 )
31
32 /*
33  * Verify support for known providers.
34  */
35
36 func TestGetNotificationHandlerSuccess(t *testing.T) {
37         readerMock := &mocks.RnibReaderMock{}
38         rnibReaderProvider := func() reader.RNibReader {
39                 return readerMock
40         }
41         writerMock := &mocks.RnibWriterMock{}
42         rnibWriterProvider := func() rNibWriter.RNibWriter {
43                 return writerMock
44         }
45         var testCases = []struct {
46                 msgType int
47                 handler handlers.NotificationHandler
48         }{
49                 {rmrCgo.RIC_X2_SETUP_RESP /*successful x2 setup response*/, handlers.X2SetupResponseNotificationHandler{}},
50                 {rmrCgo.RIC_X2_SETUP_FAILURE /*unsuccessful x2 setup response*/, handlers.X2SetupFailureResponseNotificationHandler{}},
51                 {rmrCgo.RIC_ENDC_X2_SETUP_RESP /*successful en-dc x2 setup response*/, handlers.EndcX2SetupResponseNotificationHandler{}},
52                 {rmrCgo.RIC_ENDC_X2_SETUP_FAILURE /*unsuccessful en-dc x2 setup response*/, handlers.EndcX2SetupFailureResponseNotificationHandler{}},
53                 {rmrCgo.RIC_SCTP_CONNECTION_FAILURE /*sctp errors*/, handlers.NewRanLostConnectionHandler(rnibReaderProvider, rnibWriterProvider)},
54                 {rmrCgo.RIC_ENB_LOAD_INFORMATION, handlers.NewEnbLoadInformationNotificationHandler(rnibWriterProvider)},
55                 {rmrCgo.RIC_ENB_CONF_UPDATE, handlers.X2EnbConfigurationUpdateHandler{}},
56                 {rmrCgo.RIC_ENDC_CONF_UPDATE, handlers.EndcConfigurationUpdateHandler{}},
57         }
58         for _, tc := range testCases {
59
60                 provider := NewNotificationHandlerProvider(rnibReaderProvider, rnibWriterProvider)
61                 t.Run(fmt.Sprintf("%d", tc.msgType), func(t *testing.T) {
62                         handler, err := provider.GetNotificationHandler(tc.msgType)
63                         if err != nil {
64                                 t.Errorf("want: handler for message type %d, got: error %s", tc.msgType, err)
65                         }
66                         //Note struct is empty, so it will match any other empty struct.
67                         // https://golang.org/ref/spec#Comparison_operators: Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.
68                         if /*handler != tc.handler &&*/ strings.Compare(fmt.Sprintf("%T", handler), fmt.Sprintf("%T", tc.handler)) != 0 {
69                                 t.Errorf("want: handler %T for message type %d, got: %T", tc.handler, tc.msgType, handler)
70                         }
71                 })
72         }
73 }
74
75 /*
76  * Verify handling of a request for an unsupported message.
77  */
78
79 func TestGetNotificationHandlerFailure(t *testing.T) {
80         var testCases = []struct {
81                 msgType   int
82                 errorText string
83         }{
84                 {9999 /*unknown*/, "notification handler not found"},
85         }
86         for _, tc := range testCases {
87                 readerMock := &mocks.RnibReaderMock{}
88                 rnibReaderProvider := func() reader.RNibReader {
89                         return readerMock
90                 }
91                 writerMock := &mocks.RnibWriterMock{}
92                 rnibWriterProvider := func() rNibWriter.RNibWriter {
93                         return writerMock
94                 }
95                 provider := NewNotificationHandlerProvider(rnibReaderProvider, rnibWriterProvider)
96                 t.Run(fmt.Sprintf("%d", tc.msgType), func(t *testing.T) {
97                         _, err := provider.GetNotificationHandler(tc.msgType)
98                         if err == nil {
99                                 t.Errorf("want: no handler for message type %d, got: success", tc.msgType)
100                         }
101                         if !strings.Contains(fmt.Sprintf("%s", err), tc.errorText) {
102                                 t.Errorf("want: error [%s] for message type %d, got: %s", tc.errorText, tc.msgType, err)
103                         }
104                 })
105         }
106 }