Add R5 content to master
[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         logger, _ := logger.InitLogger(logger.InfoLevel)
39
40         var testCases = []struct {
41                 response  string
42                 packedPdu string
43                 failure   error
44         }{
45                 {
46                         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}}",
47                         /*
48                                 E2AP-PDU:
49                                  unsuccessfulOutcome_t
50                                   procedureCode_t = 0x24
51                                   criticality_t = 0
52                                   ENDCX2SetupFailure
53                                    protocolIEs_t:
54                                     ProtocolIE_Container_elm
55                                      id_t = 0x5
56                                      criticality_t = 0x1
57                                      Cause:
58                                       radioNetwork_t = 0
59                                     ProtocolIE_Container_elm
60                                      id_t = 0x16
61                                      criticality_t = 0x1
62                                      TimeToWait = 0
63                                     ProtocolIE_Container_elm
64                                      id_t = 0x11
65                                      criticality_t = 0x1
66                                      CriticalityDiagnostics
67                                       procedureCode_t = 0x21
68                                       triggeringMessage_t = 0x2
69                                       procedureCriticality_t = 0x2
70                                       iEsCriticalityDiagnostics_t:
71                                        CriticalityDiagnostics_IE_List_elm
72                                         iECriticality_t = 0
73                                         iE_ID_t = 0x80
74                                         typeOfError_t = 0x1
75                         */
76                         packedPdu: "4024001a0000030005400200000016400100001140087821a00000008040"},
77
78                 /**** shares the same code with x2setup failure response to protobuf ****/
79         }
80
81         converter := NewEndcSetupFailureResponseConverter(logger)
82
83         for _, tc := range testCases {
84                 t.Run(tc.packedPdu, func(t *testing.T) {
85
86                         var payload []byte
87                         _, err := fmt.Sscanf(tc.packedPdu, "%x", &payload)
88                         if err != nil {
89                                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
90                         }
91
92                         response, err := converter.UnpackEndcSetupFailureResponseAndExtract(payload)
93
94                         if err != nil {
95                                 if tc.failure == nil {
96                                         t.Errorf("want: success, got: error: %v\n", err)
97                                 } else {
98                                         if strings.Compare(err.Error(), tc.failure.Error()) != 0 {
99                                                 t.Errorf("want: %s, got: %s", tc.failure, err)
100                                         }
101                                 }
102                         }
103
104                         if response == nil {
105                                 if tc.failure == nil {
106                                         t.Errorf("want: response=%s, got: empty response", tc.response)
107                                 }
108                         } else {
109                                 nb := &entities.NodebInfo{}
110                                 nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED_SETUP_FAILED
111                                 nb.SetupFailure = response
112                                 nb.FailureType = entities.Failure_X2_SETUP_FAILURE
113                                 respStr := fmt.Sprintf("%s %s", nb.ConnectionStatus, response)
114
115                                 space := regexp.MustCompile(`\s+`)
116                                 s1 := space.ReplaceAllString(respStr, " ")
117                                 s2 := space.ReplaceAllString(tc.response," ")
118
119                                 if !strings.EqualFold(s1, s2) {
120                                         t.Errorf("want: [%s], got: [%s]", tc.response, respStr)
121                                 }
122                         }
123                 })
124         }
125 }