f0de7c4db72c899587f7a3a94ddc765fe38489ec
[ric-plt/e2mgr.git] / E2Manager / handlers / setup_request_handler_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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         "bytes"
22         "e2mgr/logger"
23         "e2mgr/mocks"
24         "e2mgr/models"
25         "e2mgr/rNibWriter"
26         "e2mgr/sessions"
27         "fmt"
28         "github.com/stretchr/testify/assert"
29         "strings"
30         "sync"
31         "testing"
32         "time"
33 )
34
35 func TestNewSetupRequestHandler(t *testing.T) {
36
37         rnibWriterProvider := func() rNibWriter.RNibWriter {
38                 return &mocks.RnibWriterMock{}
39         }
40
41         h := NewSetupRequestHandler(rnibWriterProvider)
42         assert.NotNil(t, h)
43 }
44
45 func TestCreateMessageSuccess(t *testing.T) {
46         log, err := logger.InitLogger(logger.InfoLevel)
47         if err != nil {
48                 t.Errorf("#setup_request_handler_test.TestCreateMessageSuccess - failed to initialize logger, error: %s", err)
49         }
50         messageChannel := make(chan *models.E2RequestMessage)
51         assert.NotPanics(t, func() { createMsg(log, messageChannel) })
52         assert.NotEmpty(t, <-messageChannel)
53 }
54
55 func TestParseRicId(t *testing.T) {
56         var testCases = []struct {
57                 ricId       string
58                 pLMNId      []byte
59                 eNBId       []byte
60                 eNBIdBitqty uint
61                 failure     error
62         }{
63                 {
64                         ricId:       "bbbccc-abcd02/18",
65                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
66                         eNBId:       []byte{0xab, 0xcd, 0x2}, /*00000010 -> 10000000*/
67                         eNBIdBitqty: shortMacro_eNB_ID,
68                 },
69                 {
70                         ricId:       "bbbccc-abcd0e/20",
71                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
72                         eNBId:       []byte{0xab, 0xcd, 0xe},
73                         eNBIdBitqty: macro_eNB_ID,
74                 },
75                 {
76                         ricId:       "bbbccc-abcd07/21",
77                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
78                         eNBId:       []byte{0xab, 0xcd, 0x7}, /*00000111 -> 00111000*/
79                         eNBIdBitqty: longMacro_eNB_ID,
80                 },
81                 {
82                         ricId:       "bbbccc-abcdef08/28",
83                         pLMNId:      []byte{0xbb, 0xbc, 0xcc},
84                         eNBId:       []byte{0xab, 0xcd, 0xef, 0x8},
85                         eNBIdBitqty: home_eNB_ID,
86                 },
87                 {
88                         ricId:   "",
89                         failure: fmt.Errorf("unable to extract the value of RIC_ID: EOF"),
90                 },
91
92                 {
93                         ricId:   "bbbccc",
94                         failure: fmt.Errorf("unable to extract the value of RIC_ID: unexpected EOF"),
95                 },
96                 {
97                         ricId:   "bbbccc-",
98                         failure: fmt.Errorf("unable to extract the value of RIC_ID: EOF"),
99                 },
100                 {
101                         ricId:   "-bbbccc",
102                         failure: fmt.Errorf("%s", "unable to extract the value of RIC_ID: no hex data for %x string"),
103                 },
104                 {
105                         ricId:   "/20",
106                         failure: fmt.Errorf("%s", "unable to extract the value of RIC_ID: no hex data for %x string"),
107                 },
108                 {
109                         ricId:   "bbbcccdd-abcdef08/28", // pLMNId too long
110                         failure: fmt.Errorf("unable to extract the value of RIC_ID: input does not match format"),
111                 },
112                 {
113                         ricId:   "bbbccc-abcdef0809/28", // eNBId too long
114                         failure: fmt.Errorf("unable to extract the value of RIC_ID: input does not match format"),
115                 },
116
117                 {
118                         ricId:   "bbbc-abcdef08/28", // pLMNId too short
119                         failure: fmt.Errorf("invalid value for RIC_ID, len(pLMNId:[187 188]) != 3"),
120                 },
121                 {
122                         ricId:   "bbbccc-abcd/28", // eNBId too short
123                         failure: fmt.Errorf("invalid value for RIC_ID, len(eNBId:[171 205]) != 3 or 4"),
124                 },
125                 {
126                         ricId:   "bbbccc-abcdef08/239", // bit quantity too long - no error, will return 23 (which is invalid)
127                         failure: fmt.Errorf("invalid value for RIC_ID, eNBIdBitqty: 23"),
128                 },
129         }
130
131         for _, tc := range testCases {
132                 t.Run(tc.ricId, func(t *testing.T) {
133
134                         err := parseRicID(tc.ricId)
135                         if err != nil {
136                                 if tc.failure == nil {
137                                         t.Errorf("want: success, got: parse failed. Error: %v\n", err)
138                                 } else {
139                                         if strings.Compare(err.Error(), tc.failure.Error()) != 0 {
140                                                 t.Errorf("want: %s, got: %s\n", err, tc.failure)
141                                         }
142                                 }
143                         } else {
144                                 if bytes.Compare(tc.pLMNId, pLMNId) != 0 {
145                                         t.Errorf("want: pLMNId = %v, got: pLMNId = %v", tc.pLMNId, pLMNId)
146                                 }
147
148                                 if bytes.Compare(tc.eNBId, eNBId) != 0 {
149                                         t.Errorf("want: eNBId = %v, got: eNBId = %v", tc.eNBId, eNBId)
150                                 }
151
152                                 if tc.eNBIdBitqty != eNBIdBitqty {
153                                         t.Errorf("want: eNBIdBitqty = %d, got: eNBIdBitqty = %d", tc.eNBIdBitqty, eNBIdBitqty)
154                                 }
155                         }
156                 })
157         }
158 }
159 func createMsg(log *logger.Logger, messageChannel chan *models.E2RequestMessage) {
160         h := SetupRequestHandler{}
161         E2Sessions := make(sessions.E2Sessions)
162         var wg sync.WaitGroup
163         var rd models.RequestDetails
164         go h.CreateMessage(log, &rd, messageChannel, E2Sessions, time.Now(), wg)
165         wg.Wait()
166 }