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