Merge "MDCLOG Implementation with Dynamic log-level changes"
[ric-plt/e2mgr.git] / E2Manager / converters / endc_setup_failure_response_converter_test.go
1 /*
2  *   Copyright (c) 2019 AT&T Intellectual Property.
3  *
4  *   Licensed under the Apache License, Version 2.0 (the "License");
5  *   you may not use this file except in compliance with the License.
6  *   You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *   Unless required by applicable law or agreed to in writing, software
11  *   distributed under the License is distributed on an "AS IS" BASIS,
12  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *   See the License for the specific language governing permissions and
14  *   limitations under the License.
15  */
16
17 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20  */
21
22 package converters
23
24 import (
25         "e2mgr/logger"
26         "fmt"
27         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
28         "regexp"
29         "strings"
30         "testing"
31 )
32
33 /*
34 Test permutations of x2 setup response to protobuf enb
35 */
36
37 func TestUnpackEndcX2SetupFailureResponseAndExtract(t *testing.T) {
38         InfoLevel := int8(3)
39         logger, _ := logger.InitLogger(InfoLevel)
40
41         var testCases = []struct {
42                 response  string
43                 packedPdu string
44                 failure   error
45         }{
46                 {
47                         response: "CONNECTED_SETUP_FAILED network_layer_cause:HANDOVER_DESIRABLE_FOR_RADIO_REASONS  time_to_wait:V1S  criticality_diagnostics:{procedure_code:33  triggering_message:UNSUCCESSFUL_OUTCOME  procedure_criticality:NOTIFY  information_element_criticality_diagnostics:{ie_criticality:REJECT  ie_id:128  type_of_error:MISSING}}",
48                         /*
49                                 E2AP-PDU:
50                                  unsuccessfulOutcome_t
51                                   procedureCode_t = 0x24
52                                   criticality_t = 0
53                                   ENDCX2SetupFailure
54                                    protocolIEs_t:
55                                     ProtocolIE_Container_elm
56                                      id_t = 0x5
57                                      criticality_t = 0x1
58                                      Cause:
59                                       radioNetwork_t = 0
60                                     ProtocolIE_Container_elm
61                                      id_t = 0x16
62                                      criticality_t = 0x1
63                                      TimeToWait = 0
64                                     ProtocolIE_Container_elm
65                                      id_t = 0x11
66                                      criticality_t = 0x1
67                                      CriticalityDiagnostics
68                                       procedureCode_t = 0x21
69                                       triggeringMessage_t = 0x2
70                                       procedureCriticality_t = 0x2
71                                       iEsCriticalityDiagnostics_t:
72                                        CriticalityDiagnostics_IE_List_elm
73                                         iECriticality_t = 0
74                                         iE_ID_t = 0x80
75                                         typeOfError_t = 0x1
76                         */
77                         packedPdu: "4024001a0000030005400200000016400100001140087821a00000008040"},
78
79                 /**** shares the same code with x2setup failure response to protobuf ****/
80         }
81
82         converter := NewEndcSetupFailureResponseConverter(logger)
83
84         for _, tc := range testCases {
85                 t.Run(tc.packedPdu, func(t *testing.T) {
86
87                         var payload []byte
88                         _, err := fmt.Sscanf(tc.packedPdu, "%x", &payload)
89                         if err != nil {
90                                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
91                         }
92
93                         response, err := converter.UnpackEndcSetupFailureResponseAndExtract(payload)
94
95                         if err != nil {
96                                 if tc.failure == nil {
97                                         t.Errorf("want: success, got: error: %v\n", err)
98                                 } else {
99                                         if strings.Compare(err.Error(), tc.failure.Error()) != 0 {
100                                                 t.Errorf("want: %s, got: %s", tc.failure, err)
101                                         }
102                                 }
103                         }
104
105                         if response == nil {
106                                 if tc.failure == nil {
107                                         t.Errorf("want: response=%s, got: empty response", tc.response)
108                                 }
109                         } else {
110                                 nb := &entities.NodebInfo{}
111                                 nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED_SETUP_FAILED
112                                 nb.SetupFailure = response
113                                 nb.FailureType = entities.Failure_X2_SETUP_FAILURE
114                                 respStr := fmt.Sprintf("%s %s", nb.ConnectionStatus, response)
115
116                                 space := regexp.MustCompile(`\s+`)
117                                 s1 := space.ReplaceAllString(respStr, " ")
118                                 s2 := space.ReplaceAllString(tc.response," ")
119
120                                 if !strings.EqualFold(s1, s2) {
121                                         t.Errorf("want: [%s], got: [%s]", tc.response, respStr)
122                                 }
123                         }
124                 })
125         }
126 }