blob: 273a15e1fb5053b70e6ddd346cb31662b8213893 [file] [log] [blame]
Konstantinos Archangelof268d7152021-06-14 12:24:00 +03001/*
2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
18*/
19
20package control
21
22import (
23 "encoding/json"
24 "fmt"
25 "reflect"
26 "strings"
27 "sync"
28 "testing"
29
30 "github.com/segmentio/ksuid"
31 "github.com/stretchr/testify/assert"
32)
33
34var sdlRestShouldReturnError bool = false
35
36const sdlRestTestErrorString string = "Test sdl REST returned error on purpose"
37
38type RestSubsDbMock struct {
39 restSubsDb map[string]string // Store information as a string like real db does.
40 restSubscriptions map[string]*RESTSubscription
41 lastAllocatedRestSubId string
42 restSubIdsInDb []string
43 marshalLock sync.Mutex
44}
45
46var restSubsDbMock *RestSubsDbMock
47
48func CreateRestSubsDbMock() *RestSubsDbMock {
49 fmt.Println("Test CreateRestSubsDbMock()")
50 restSubsDbMock = new(RestSubsDbMock)
51 restSubsDbMock.ResetTestSettings()
52 restSubsDbMock.lastAllocatedRestSubId = ""
53 return restSubsDbMock
54}
55
56func (m *RestSubsDbMock) ResetTestSettings() {
57 m.restSubsDb = make(map[string]string)
58 m.restSubscriptions = make(map[string]*RESTSubscription)
59}
60
61func (m *RestSubsDbMock) AllocNextRestSubId() string {
62 m.lastAllocatedRestSubId = ksuid.New().String()
63 return m.lastAllocatedRestSubId
64}
65
66func (m *RestSubsDbMock) GetLastAllocatedRestSubId() string {
67 return m.lastAllocatedRestSubId
68}
69
70func (m *RestSubsDbMock) AddRestSubIdsInDb(restSubId string) {
71 m.restSubIdsInDb = append(m.restSubIdsInDb, restSubId)
72}
73
74func (m *RestSubsDbMock) DeleteRestSubIdsFromDb(restSubId string) {
75 newrestSubIdsInDb := []string{}
76 for _, i := range m.restSubIdsInDb {
77 if i != restSubId {
78 newrestSubIdsInDb = append(newrestSubIdsInDb, i)
79 }
80 }
81 m.restSubIdsInDb = newrestSubIdsInDb
82}
83
84func (m *RestSubsDbMock) EmptyRestSubIdsFromDb() {
85 m.restSubIdsInDb = nil
86}
87
88func CreateRESTSubscription(t *testing.T) *RESTSubscription {
89 t.Log("TEST: Creating REST subscription")
90
91 restSubscription := &RESTSubscription{}
92 restSubscription.xAppRmrEndPoint = "localhost:13560"
93 restSubscription.Meid = "RAN_NAME_1"
94 restSubscription.SubReqOngoing = true
95 restSubscription.SubDelReqOngoing = false
96 restSubscription.xAppIdToE2Id = make(map[int64]int64)
Markku Virtanen42723e22021-06-15 10:09:23 +030097 restSubscription.lastReqMd5sum = "856e9546f6f7b65b13a86956f2e16f6a"
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030098 return restSubscription
99}
100
101func PrintRESTSubscriptionData(t *testing.T, restSubs *RESTSubscription) {
102 t.Log("TEST: RESTSubscription data")
103 t.Logf("TEST: restSubs.xAppRmrEndPoint = %v", restSubs.xAppRmrEndPoint)
104 t.Logf("TEST: restSubs.Meid = %v", restSubs.Meid)
105 t.Logf("TEST: restSubs.InstanceIds = %v", restSubs.InstanceIds)
106 t.Logf("TEST: restSubs.xAppIdToE2Id = %v", restSubs.xAppIdToE2Id)
107 t.Logf("TEST: restSubs.SubReqOngoing = %v", restSubs.SubReqOngoing)
108 t.Logf("TEST: restSubs.SubDelReqOngoing = %v", restSubs.SubDelReqOngoing)
109}
110
111func TestWriteRESTSubscriptionToSdl(t *testing.T) {
112
113 // Write one subscription
114 restSubId := restSubsDbMock.AllocNextRestSubId()
115 restSubs := CreateRESTSubscription(t)
116 PrintRESTSubscriptionData(t, restSubs)
117 t.Logf("TEST: Writing subId = %v\n", restSubId)
118 err := mainCtrl.c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
119 if err != nil {
120 t.Errorf("TEST: %s", err.Error())
121 }
122 restSubsDbMock.AddRestSubIdsInDb(restSubId)
123}
124
125func TestReadRESTSubscriptionFromSdl(t *testing.T) {
126
127 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
128 t.Logf("Reading restSubId = %v\n", restSubId)
129 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
130 if err != nil {
131 t.Errorf("TEST: %s", err.Error())
132 return
133 }
134 PrintRESTSubscriptionData(t, restSubs)
135 assert.Equal(t, restSubsDbMock.restSubscriptions[restSubId], restSubs)
136}
137
138func TestRemoveRESTSubscriptionFromSdl(t *testing.T) {
139
140 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
141 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
142 if err != nil {
143 t.Errorf("TEST: %s", err.Error())
144 return
145 }
146 delete(restSubsDbMock.restSubscriptions, restSubId)
147 t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
148 restSubsDbMock.DeleteRestSubIdsFromDb(restSubId)
149}
150
151func TestReadNotExistingRESTSubscriptionFromSdl(t *testing.T) {
152
153 restSubId := ""
154 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
155 if err != nil {
156 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
157 return
158 }
159 t.Errorf("TEST: REST subscription read from db. %v", restSubs)
160 PrintRESTSubscriptionData(t, restSubs)
161}
162
163func TestReadNotExistingRESTSubscriptionFromSdl2(t *testing.T) {
164
165 restSubId := "NotExistingSubsId"
166 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
167 if err != nil {
168 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
169 return
170 }
171 t.Errorf("TEST: REST subscription read from db. %v", restSubs)
172 PrintRESTSubscriptionData(t, restSubs)
173}
174
175func TestRemoveNotExistingRESTSubscriptionFromSdl(t *testing.T) {
176
177 restSubId := ""
178 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
179 if err != nil {
180 t.Logf("TEST: %s", err.Error())
181 return
182 }
183 t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
184}
185
186func TestWriteRESTSubscriptionsToSdl(t *testing.T) {
187
188 // Write 1st subscription
189 restSubId := restSubsDbMock.AllocNextRestSubId()
190 t.Logf("TEST: Writing restSubId = %v\n", restSubId)
191 restSubs := CreateRESTSubscription(t)
192 PrintRESTSubscriptionData(t, restSubs)
193 err := mainCtrl.c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
194 if err != nil {
195 t.Errorf("TEST: %s", err.Error())
196 return
197 }
198 restSubsDbMock.AddRestSubIdsInDb(restSubId)
199 t.Logf("TEST: REST subscription written in db = %v", restSubs)
200
201 // Write 2nd subscription
202 restSubId = restSubsDbMock.AllocNextRestSubId()
203 t.Logf("TEST:Writing restSubId = %v\n", restSubId)
204 restSubs = CreateRESTSubscription(t)
205 PrintRESTSubscriptionData(t, restSubs)
206 err = mainCtrl.c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
207 if err != nil {
208 t.Errorf("TEST: %s", err.Error())
209 return
210 }
211 restSubsDbMock.AddRestSubIdsInDb(restSubId)
212 t.Logf("TEST: REST subscription written in db = %v", restSubs)
213
214 // Write 3rd subscription
215 restSubId = restSubsDbMock.AllocNextRestSubId()
216 t.Logf("TEST: Writing restSubId = %v\n", restSubId)
217 restSubs = CreateRESTSubscription(t)
218 PrintRESTSubscriptionData(t, restSubs)
219 err = mainCtrl.c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
220 if err != nil {
221 t.Errorf("TEST: %s", err.Error())
222 return
223 }
224 restSubsDbMock.AddRestSubIdsInDb(restSubId)
225 t.Logf("TEST: REST subscription written in db = %v", restSubs)
226}
227
228func TestReadRESTSubscriptionsFromSdl(t *testing.T) {
229
230 for _, restSubId := range restSubsDbMock.restSubIdsInDb {
231 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
232 if err != nil {
233 t.Errorf("TEST: %s", err.Error())
234 return
235 }
236 PrintRESTSubscriptionData(t, restSubs)
237 }
238}
239
240func TestReadAllRESTSubscriptionsFromSdl(t *testing.T) {
241
242 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
243 if err != nil {
244 t.Errorf("TEST: %s", err.Error())
245 return
246 }
247
248 for _, restSubs := range register {
249 PrintRESTSubscriptionData(t, restSubs)
250 }
251
252 assert.Equal(t, len(register), 3)
253}
254
255func TestRemoveAllRESTSubscriptionsFromSdl(t *testing.T) {
256
257 err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
258 if err != nil {
259 t.Errorf("TEST: %s", err.Error())
260 return
261 }
262 t.Log("TEST: All subscription removed from db")
263 restSubsDbMock.EmptyRestSubIdsFromDb()
264}
265
266func TestReadAllRESTSubscriptionsFromSdl2(t *testing.T) {
267
268 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
269 if err != nil {
270 t.Errorf("TEST: %s", err.Error())
271 return
272 }
273 for _, restSubs := range restSubsDbMock.restSubscriptions {
274 PrintRESTSubscriptionData(t, restSubs)
275 }
276 assert.Equal(t, len(register), 0)
277}
278
279func TestWriteRESTSubscriptionToSdlFail(t *testing.T) {
280
281 // Try to write one subscription.
282 // Test db should return test error string
283 MakeNextSdlRestCallFail()
284 restsubId := restSubsDbMock.AllocNextRestSubId()
285 restSubs := CreateRESTSubscription(t)
286 PrintRESTSubscriptionData(t, restSubs)
287 t.Logf("TEST: Writing subId = %v\n", restsubId)
288 err := mainCtrl.c.WriteRESTSubscriptionToSdl(restsubId, restSubs)
289 if err != nil {
290 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
291 t.Errorf("TEST: %s", err.Error())
292 }
293 } else {
294 t.Errorf("TEST: This test case should return error")
295 }
296}
297
298func TestReadRESTSubscriptionFromSdlFail(t *testing.T) {
299
300 // Try to read one subscription.
301 // Test db should return test error string
302 MakeNextSdlRestCallFail()
303 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
304 t.Logf("Reading restSubId = %v\n", restSubId)
305 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
306 if err != nil {
307 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
308 t.Errorf("TEST: %s", err.Error())
309 }
310 return
311 } else {
312 t.Errorf("TEST: This test case should return error")
313 }
314 PrintRESTSubscriptionData(t, restSubs)
315}
316
317func TestRemoveRESTSubscriptionFromSdlFail(t *testing.T) {
318
319 // Try to remove one subscription.
320 // Test db should return test error string
321 MakeNextSdlRestCallFail()
322 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
323 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
324 if err != nil {
325 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
326 t.Errorf("TEST: %s", err.Error())
327 }
328 return
329 } else {
330 t.Errorf("TEST: This test case should return error")
331 }
332 t.Logf("TEST: subscription removed from db. subId = %v", restSubId)
333}
334
335func TestReadAllRESTSubscriptionsFromSdlFail(t *testing.T) {
336
337 // Try to read all subscriptions.
338 // Test db should return test error string
339 MakeNextSdlRestCallFail()
340 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
341 if err != nil {
342 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
343 t.Errorf("TEST: %s", err.Error())
344 }
345 return
346 } else {
347 t.Errorf("TEST: This test case should return error")
348 }
349
350 for _, restSubs := range register {
351 PrintRESTSubscriptionData(t, restSubs)
352 }
353}
354
355func TestRemoveAllRESTSubscriptionsFromSdlFail(t *testing.T) {
356
357 // Try to remove all subscriptions.
358 // Test db should return test error string
359 MakeNextSdlRestCallFail()
360 err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
361 if err != nil {
362 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
363 t.Errorf("TEST: %s", err.Error())
364 }
365 return
366 } else {
367 t.Errorf("TEST: This test case should return error")
368 }
369 t.Log("TEST: All subscription removed from db")
370}
371
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300372func (m *RestSubsDbMock) Set(ns string, pairs ...interface{}) error {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300373 var key string
374 var val string
375
376 m.marshalLock.Lock()
377 defer m.marshalLock.Unlock()
378
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300379 if ns != restSubSdlNs {
380 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
381 }
382
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300383 if sdlRestShouldReturnError == true {
384 return GetSdlRestError()
385 }
386
387 for _, v := range pairs {
388 reflectType := reflect.TypeOf(v)
389 switch reflectType.Kind() {
390 case reflect.Slice:
391 val = fmt.Sprintf("%s", v.([]uint8))
392 default:
393 switch v.(type) {
394 case string:
395 key = v.(string)
396 default:
397 return fmt.Errorf("Set() error: Unexpected type\n")
398 }
399 }
400 }
401
402 if key != "" {
403 m.restSubsDb[key] = val
404 restSubId := key
405 restSubscriptionInfo := &RESTSubscriptionInfo{}
406 err := json.Unmarshal([]byte(val), restSubscriptionInfo)
407 if err != nil {
408 return fmt.Errorf("Set() json.unmarshal error: %s\n", err.Error())
409 }
410
411 restSubs := mainCtrl.c.CreateRESTSubscription(restSubscriptionInfo, &val)
412 m.restSubscriptions[restSubId] = restSubs
413 } else {
414 return fmt.Errorf("Set() error: key == ''\n")
415 }
416 return nil
417}
418
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300419func (m *RestSubsDbMock) Get(ns string, keys []string) (map[string]interface{}, error) {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300420 retMap := make(map[string]interface{})
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300421
422 if ns != restSubSdlNs {
423 return nil, fmt.Errorf("Unexpected namespace '%s' error\n", ns)
424 }
425
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300426 if len(keys) == 0 {
427 return nil, fmt.Errorf("Get() error: len(key) == 0\n")
428 }
429
430 if sdlRestShouldReturnError == true {
431 return nil, GetSdlRestError()
432 }
433
434 for _, key := range keys {
435 if key != "" {
436 retMap[key] = m.restSubsDb[key]
437 } else {
438 return nil, fmt.Errorf("Get() error: key == ''\n")
439 }
440 }
441 return retMap, nil
442}
443
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300444func (m *RestSubsDbMock) GetAll(ns string) ([]string, error) {
445
446 if ns != restSubSdlNs {
447 return nil, fmt.Errorf("Unexpected namespace '%s' error\n", ns)
448 }
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300449
450 if sdlRestShouldReturnError == true {
451 return nil, GetSdlRestError()
452 }
453
454 keys := []string{}
455 for key, _ := range m.restSubsDb {
456 keys = append(keys, key)
457 }
458 return keys, nil
459}
460
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300461func (m *RestSubsDbMock) Remove(ns string, keys []string) error {
462
463 if ns != restSubSdlNs {
464 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
465 }
466
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300467 if len(keys) == 0 {
468 return fmt.Errorf("Remove() error: len(key) == 0\n")
469 }
470
471 if sdlRestShouldReturnError == true {
472 return GetSdlRestError()
473 }
474
475 restSubId := keys[0]
476 delete(m.restSubsDb, restSubId)
477 delete(m.restSubscriptions, restSubId)
478 return nil
479}
480
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300481func (m *RestSubsDbMock) RemoveAll(ns string) error {
482
483 if ns != restSubSdlNs {
484 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
485 }
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300486
487 for key := range m.restSubsDb {
488
489 restSubId := key
490 delete(m.restSubsDb, restSubId)
491 delete(m.restSubscriptions, restSubId)
492 }
493
494 if sdlRestShouldReturnError == true {
495 return GetSdlRestError()
496 }
497
498 return nil
499}
500
501func MakeNextSdlRestCallFail() {
502 sdlRestShouldReturnError = true
503}
504
505func GetSdlRestError() error {
506 sdlRestShouldReturnError = false
507 return fmt.Errorf(sdlRestTestErrorString)
508}