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