[RIC-213] Add globalRicId to configuration and remove env var RIC_ID
[ric-plt/e2mgr.git] / E2Manager / e2pdus / x2_setup_requests_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 e2pdus
23
24 import (
25         "bytes"
26         "fmt"
27         "strings"
28         "testing"
29 )
30
31
32 func TestParseRicId(t *testing.T) {
33         var testCases = []struct {
34                 ricId       string
35                 pLMNId      []byte
36                 eNBId       []byte
37                 eNBIdBitqty uint
38                 failure     error
39         }{
40                 {
41                         ricId:       "bbbccc-abcd02/18",
42                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
43                         eNBId:       []byte{0xab, 0xcd, 0x2}, /*00000010 -> 10000000*/
44                         eNBIdBitqty: ShortMacro_eNB_ID,
45                 },
46                 {
47                         ricId:       "bbbccc-abcd0e/20",
48                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
49                         eNBId:       []byte{0xab, 0xcd, 0xe},
50                         eNBIdBitqty: Macro_eNB_ID,
51                 },
52                 {
53                         ricId:       "bbbccc-abcd07/21",
54                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
55                         eNBId:       []byte{0xab, 0xcd, 0x7}, /*00000111 -> 00111000*/
56                         eNBIdBitqty: LongMacro_eNB_ID,
57                 },
58                 {
59                         ricId:       "bbbccc-abcdef08/28",
60                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
61                         eNBId:       []byte{0xab, 0xcd, 0xef, 0x8},
62                         eNBIdBitqty: Home_eNB_ID,
63                 },
64                 {
65                         ricId:   "",
66                         failure: fmt.Errorf("unable to extract the value of RIC_ID: EOF"),
67                 },
68
69                 {
70                         ricId:   "bbbccc",
71                         failure: fmt.Errorf("unable to extract the value of RIC_ID: unexpected EOF"),
72                 },
73                 {
74                         ricId:   "bbbccc-",
75                         failure: fmt.Errorf("unable to extract the value of RIC_ID: EOF"),
76                 },
77                 {
78                         ricId:   "-bbbccc",
79                         failure: fmt.Errorf("%s", "unable to extract the value of RIC_ID: no hex data for %x string"),
80                 },
81                 {
82                         ricId:   "/20",
83                         failure: fmt.Errorf("%s", "unable to extract the value of RIC_ID: no hex data for %x string"),
84                 },
85                 {
86                         ricId:   "bbbcccdd-abcdef08/28", // pLMNId too long
87                         failure: fmt.Errorf("unable to extract the value of RIC_ID: input does not match format"),
88                 },
89                 {
90                         ricId:   "bbbccc-abcdef0809/28", // eNBId too long
91                         failure: fmt.Errorf("unable to extract the value of RIC_ID: input does not match format"),
92                 },
93
94                 {
95                         ricId:   "bbbc-abcdef08/28", // pLMNId too short
96                         failure: fmt.Errorf("invalid value for RIC_ID, len(pLMNId:[187 188]) != 3"),
97                 },
98                 {
99                         ricId:   "bbbccc-abcd/28", // eNBId too short
100                         failure: fmt.Errorf("invalid value for RIC_ID, len(eNBId:[171 205]) != 3 or 4"),
101                 },
102                 {
103                         ricId:   "bbbccc-abcdef08/239", // bit quantity too long - no error, will return 23 (which is invalid)
104                         failure: fmt.Errorf("invalid value for RIC_ID, eNBIdBitqty: 23"),
105                 },
106         }
107
108         for _, tc := range testCases {
109                 t.Run(tc.ricId, func(t *testing.T) {
110
111                         err := parseRicID(tc.ricId)
112                         if err != nil {
113                                 if tc.failure == nil {
114                                         t.Errorf("want: success, got: parse failed. Error: %v\n", err)
115                                 } else {
116                                         if strings.Compare(err.Error(), tc.failure.Error()) != 0 {
117                                                 t.Errorf("want: %s, got: %s\n", err, tc.failure)
118                                         }
119                                 }
120                         } else {
121                                 if bytes.Compare(tc.pLMNId, pLMNId) != 0 {
122                                         t.Errorf("want: pLMNId = %v, got: pLMNId = %v", tc.pLMNId, pLMNId)
123                                 }
124
125                                 if bytes.Compare(tc.eNBId, eNBId) != 0 {
126                                         t.Errorf("want: eNBId = %v, got: eNBId = %v", tc.eNBId, eNBId)
127                                 }
128
129                                 if tc.eNBIdBitqty != eNBIdBitqty {
130                                         t.Errorf("want: eNBIdBitqty = %d, got: eNBIdBitqty = %d", tc.eNBIdBitqty, eNBIdBitqty)
131                                 }
132                         }
133                 })
134         }
135 }