[RICPLT-2157] Restructure handlers and converters.......
[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         "e2mgr/rNibWriter"
24         "fmt"
25         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
26         "strings"
27         "testing"
28 )
29
30 /*
31 Test permutations of x2 setup response to protobuf enb
32 */
33
34 func TestUnpackEndcX2SetupFailureResponseAndExtract(t *testing.T) {
35         logger, _ := logger.InitLogger(logger.InfoLevel)
36
37         var testCases = []struct {
38                 saveToRNib bool
39                 response   string
40                 packedPdu  string
41                 failure    error
42         }{
43                 {
44                         saveToRNib: false, //TODO: use MOCK?
45                         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 > > ",
46                         /*
47                                 E2AP-PDU:
48                                  unsuccessfulOutcome_t
49                                   procedureCode_t = 0x24
50                                   criticality_t = 0
51                                   ENDCX2SetupFailure
52                                    protocolIEs_t:
53                                     ProtocolIE_Container_elm
54                                      id_t = 0x5
55                                      criticality_t = 0x1
56                                      Cause:
57                                       radioNetwork_t = 0
58                                     ProtocolIE_Container_elm
59                                      id_t = 0x16
60                                      criticality_t = 0x1
61                                      TimeToWait = 0
62                                     ProtocolIE_Container_elm
63                                      id_t = 0x11
64                                      criticality_t = 0x1
65                                      CriticalityDiagnostics
66                                       procedureCode_t = 0x21
67                                       triggeringMessage_t = 0x2
68                                       procedureCriticality_t = 0x2
69                                       iEsCriticalityDiagnostics_t:
70                                        CriticalityDiagnostics_IE_List_elm
71                                         iECriticality_t = 0
72                                         iE_ID_t = 0x80
73                                         typeOfError_t = 0x1
74                         */
75                         packedPdu: "4024001a0000030005400200000016400100001140087821a00000008040"},
76
77                 /**** shares the same code with x2setup failure response to protobuf ****/
78         }
79
80         initDb_f := true
81         for _, tc := range testCases {
82                 t.Run(tc.packedPdu, func(t *testing.T) {
83
84                         var payload []byte
85                         _, err := fmt.Sscanf(tc.packedPdu, "%x", &payload)
86                         if err != nil {
87                                 t.Errorf("convert inputPayloadAsStr to payloadAsByte. Error: %v\n", err)
88                         }
89
90                         response, err := UnpackEndcX2SetupFailureResponseAndExtract(logger, e2pdus.MaxAsn1CodecAllocationBufferSize /*allocation buffer*/, len(payload), payload, e2pdus.MaxAsn1CodecMessageBufferSize /*message buffer*/)
91
92                         if err != nil {
93                                 if tc.failure == nil {
94                                         t.Errorf("want: success, got: error: %v\n", err)
95                                 } else {
96                                         if strings.Compare(err.Error(), tc.failure.Error()) != 0 {
97                                                 t.Errorf("want: %s, got: %s", tc.failure, err)
98                                         }
99                                 }
100                         }
101
102                         if response == nil {
103                                 if tc.failure == nil {
104                                         t.Errorf("want: response=%s, got: empty response", tc.response)
105                                 }
106                         } else {
107                                 nb := &entities.NodebInfo{}
108                                 nb.ConnectionStatus = entities.ConnectionStatus_CONNECTED_SETUP_FAILED
109                                 nb.SetupFailure = response
110                                 nb.FailureType = entities.Failure_X2_SETUP_FAILURE
111                                 respStr := fmt.Sprintf("%s %s", nb.ConnectionStatus, response)
112                                 if !strings.EqualFold(respStr, tc.response) {
113                                         t.Errorf("want: response=[%s], got: [%s]", tc.response, respStr)
114                                 }
115
116                                 // Save to rNib
117                                 if tc.saveToRNib {
118                                         if initDb_f {
119                                                 rNibWriter.Init("e2Manager", 1)
120                                                 initDb_f = false
121                                         }
122                                         nbIdentity := &entities.NbIdentity{InventoryName: "RanName"}
123                                         if rNibErr := rNibWriter.GetRNibWriter().SaveNodeb(nbIdentity, nb); rNibErr != nil {
124                                                 if tc.failure == nil {
125                                                         t.Errorf("rNibWriter failed to save ENB. Error: %s\n", rNibErr.Error())
126                                                 } else {
127                                                         if strings.Compare(rNibErr.Error(), tc.failure.Error()) != 0 {
128                                                                 t.Errorf("want: %s, got: %s", tc.failure, rNibErr.Error())
129                                                         }
130                                                 }
131                                         }
132                                 }
133                         }
134                 })
135         }
136 }