[RICPLT-2165] Add rnibDataService to support retries
[ric-plt/e2mgr.git] / E2Manager / converters / endc_x2setupFailureResponseToProtobuf_test.go
1 /*******************************************************************************
2  *
3  *   Copyright (c) 2019 AT&T Intellectual Property.
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 converters
19
20 import (
21         "e2mgr/e2pdus"
22         "e2mgr/logger"
23         "fmt"
24         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
25         "strings"
26         "testing"
27 )
28
29 /*
30 Test permutations of x2 setup response to protobuf enb
31 */
32
33 func TestUnpackEndcX2SetupFailureResponseAndExtract(t *testing.T) {
34         logger, _ := logger.InitLogger(logger.InfoLevel)
35
36         var testCases = []struct {
37                 response  string
38                 packedPdu string
39                 failure   error
40         }{
41                 {
42                         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 > > ",
43                         /*
44                                 E2AP-PDU:
45                                  unsuccessfulOutcome_t
46                                   procedureCode_t = 0x24
47                                   criticality_t = 0
48                                   ENDCX2SetupFailure
49                                    protocolIEs_t:
50                                     ProtocolIE_Container_elm
51                                      id_t = 0x5
52                                      criticality_t = 0x1
53                                      Cause:
54                                       radioNetwork_t = 0
55                                     ProtocolIE_Container_elm
56                                      id_t = 0x16
57                                      criticality_t = 0x1
58                                      TimeToWait = 0
59                                     ProtocolIE_Container_elm
60                                      id_t = 0x11
61                                      criticality_t = 0x1
62                                      CriticalityDiagnostics
63                                       procedureCode_t = 0x21
64                                       triggeringMessage_t = 0x2
65                                       procedureCriticality_t = 0x2
66                                       iEsCriticalityDiagnostics_t:
67                                        CriticalityDiagnostics_IE_List_elm
68                                         iECriticality_t = 0
69                                         iE_ID_t = 0x80
70                                         typeOfError_t = 0x1
71                         */
72                         packedPdu: "4024001a0000030005400200000016400100001140087821a00000008040"},
73
74                 /**** shares the same code with x2setup failure response to protobuf ****/
75         }
76
77         for _, tc := range testCases {
78                 t.Run(tc.packedPdu, func(t *testing.T) {
79
80                         var payload []byte
81                         _, err := fmt.Sscanf(tc.packedPdu, "%x", &payload)
82                         if err != nil {
83                                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
84                         }
85
86                         response, err := UnpackEndcX2SetupFailureResponseAndExtract(logger, e2pdus.MaxAsn1CodecAllocationBufferSize /*allocation buffer*/, len(payload), payload, e2pdus.MaxAsn1CodecMessageBufferSize /*message buffer*/)
87
88                         if err != nil {
89                                 if tc.failure == nil {
90                                         t.Errorf("want: success, got: error: %v\n", err)
91                                 } else {
92                                         if strings.Compare(err.Error(), tc.failure.Error()) != 0 {
93                                                 t.Errorf("want: %s, got: %s", tc.failure, err)
94                                         }
95                                 }
96                         }
97
98                         if response == nil {
99                                 if tc.failure == nil {
100                                         t.Errorf("want: response=%s, got: empty response", tc.response)
101                                 }
102                         } else {
103                                 nb := &entities.NodebInfo{}
104                                 nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED_SETUP_FAILED
105                                 nb.SetupFailure = response
106                                 nb.FailureType = entities.Failure_X2_SETUP_FAILURE
107                                 respStr := fmt.Sprintf("%s %s", nb.ConnectionStatus, response)
108                                 if !strings.EqualFold(respStr, tc.response) {
109                                         t.Errorf("want: response=[%s], got: [%s]", tc.response, respStr)
110                                 }
111
112                         }
113                 })
114         }
115 }