blob: 5e1db6025b4e786e5ba9d96b6c0de2d65ec0a009 [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{}
Anssi Mannila92c38552021-12-29 09:59:24 +020092 restSubscription.xAppServiceName = "localhost"
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030093 restSubscription.xAppRmrEndPoint = "localhost:13560"
94 restSubscription.Meid = "RAN_NAME_1"
95 restSubscription.SubReqOngoing = true
96 restSubscription.SubDelReqOngoing = false
97 restSubscription.xAppIdToE2Id = make(map[int64]int64)
Markku Virtanen42723e22021-06-15 10:09:23 +030098 restSubscription.lastReqMd5sum = "856e9546f6f7b65b13a86956f2e16f6a"
Konstantinos Archangelof268d7152021-06-14 12:24:00 +030099 return restSubscription
100}
101
102func PrintRESTSubscriptionData(t *testing.T, restSubs *RESTSubscription) {
103 t.Log("TEST: RESTSubscription data")
Anssi Mannila92c38552021-12-29 09:59:24 +0200104 t.Logf("TEST: restSubs.xAppServiceName = %v", restSubs.xAppServiceName)
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300105 t.Logf("TEST: restSubs.xAppRmrEndPoint = %v", restSubs.xAppRmrEndPoint)
106 t.Logf("TEST: restSubs.Meid = %v", restSubs.Meid)
107 t.Logf("TEST: restSubs.InstanceIds = %v", restSubs.InstanceIds)
108 t.Logf("TEST: restSubs.xAppIdToE2Id = %v", restSubs.xAppIdToE2Id)
109 t.Logf("TEST: restSubs.SubReqOngoing = %v", restSubs.SubReqOngoing)
110 t.Logf("TEST: restSubs.SubDelReqOngoing = %v", restSubs.SubDelReqOngoing)
111}
112
113func TestWriteRESTSubscriptionToSdl(t *testing.T) {
114
115 // Write one subscription
116 restSubId := restSubsDbMock.AllocNextRestSubId()
117 restSubs := CreateRESTSubscription(t)
118 PrintRESTSubscriptionData(t, restSubs)
119 t.Logf("TEST: Writing subId = %v\n", restSubId)
120 err := mainCtrl.c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
121 if err != nil {
122 t.Errorf("TEST: %s", err.Error())
123 }
124 restSubsDbMock.AddRestSubIdsInDb(restSubId)
Anssi Mannilaafadb6d2021-11-15 15:03:12 +0200125 verifyRESTKeyCount(t, 1)
126}
127
128func verifyRESTKeyCount(t *testing.T, expectedCount int) {
129
130 count, err := mainCtrl.c.GetRESTKeyCount()
131 if err != nil {
132 t.Errorf("TEST: %s", err.Error())
133 } else {
134 assert.Equal(t, expectedCount, count)
135 }
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300136}
137
138func TestReadRESTSubscriptionFromSdl(t *testing.T) {
139
140 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
141 t.Logf("Reading restSubId = %v\n", restSubId)
142 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
143 if err != nil {
144 t.Errorf("TEST: %s", err.Error())
145 return
146 }
147 PrintRESTSubscriptionData(t, restSubs)
148 assert.Equal(t, restSubsDbMock.restSubscriptions[restSubId], restSubs)
149}
150
151func TestRemoveRESTSubscriptionFromSdl(t *testing.T) {
152
153 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
154 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
155 if err != nil {
156 t.Errorf("TEST: %s", err.Error())
157 return
158 }
159 delete(restSubsDbMock.restSubscriptions, restSubId)
160 t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
161 restSubsDbMock.DeleteRestSubIdsFromDb(restSubId)
162}
163
164func TestReadNotExistingRESTSubscriptionFromSdl(t *testing.T) {
165
166 restSubId := ""
167 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
168 if err != nil {
169 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
170 return
171 }
172 t.Errorf("TEST: REST subscription read from db. %v", restSubs)
173 PrintRESTSubscriptionData(t, restSubs)
174}
175
176func TestReadNotExistingRESTSubscriptionFromSdl2(t *testing.T) {
177
178 restSubId := "NotExistingSubsId"
179 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
180 if err != nil {
181 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
182 return
183 }
184 t.Errorf("TEST: REST subscription read from db. %v", restSubs)
185 PrintRESTSubscriptionData(t, restSubs)
186}
187
188func TestRemoveNotExistingRESTSubscriptionFromSdl(t *testing.T) {
189
190 restSubId := ""
191 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
192 if err != nil {
193 t.Logf("TEST: %s", err.Error())
194 return
195 }
196 t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
197}
198
199func TestWriteRESTSubscriptionsToSdl(t *testing.T) {
200
201 // Write 1st 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 2nd 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 // Write 3rd subscription
228 restSubId = restSubsDbMock.AllocNextRestSubId()
229 t.Logf("TEST: Writing restSubId = %v\n", restSubId)
230 restSubs = CreateRESTSubscription(t)
231 PrintRESTSubscriptionData(t, restSubs)
232 err = mainCtrl.c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
233 if err != nil {
234 t.Errorf("TEST: %s", err.Error())
235 return
236 }
237 restSubsDbMock.AddRestSubIdsInDb(restSubId)
238 t.Logf("TEST: REST subscription written in db = %v", restSubs)
239}
240
241func TestReadRESTSubscriptionsFromSdl(t *testing.T) {
242
243 for _, restSubId := range restSubsDbMock.restSubIdsInDb {
244 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
245 if err != nil {
246 t.Errorf("TEST: %s", err.Error())
247 return
248 }
249 PrintRESTSubscriptionData(t, restSubs)
250 }
251}
252
253func TestReadAllRESTSubscriptionsFromSdl(t *testing.T) {
254
255 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
256 if err != nil {
257 t.Errorf("TEST: %s", err.Error())
258 return
259 }
260
261 for _, restSubs := range register {
262 PrintRESTSubscriptionData(t, restSubs)
263 }
264
265 assert.Equal(t, len(register), 3)
266}
267
268func TestRemoveAllRESTSubscriptionsFromSdl(t *testing.T) {
269
270 err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
271 if err != nil {
272 t.Errorf("TEST: %s", err.Error())
273 return
274 }
275 t.Log("TEST: All subscription removed from db")
276 restSubsDbMock.EmptyRestSubIdsFromDb()
277}
278
279func TestReadAllRESTSubscriptionsFromSdl2(t *testing.T) {
280
281 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
282 if err != nil {
283 t.Errorf("TEST: %s", err.Error())
284 return
285 }
286 for _, restSubs := range restSubsDbMock.restSubscriptions {
287 PrintRESTSubscriptionData(t, restSubs)
288 }
289 assert.Equal(t, len(register), 0)
290}
291
292func TestWriteRESTSubscriptionToSdlFail(t *testing.T) {
293
294 // Try to write one subscription.
295 // Test db should return test error string
296 MakeNextSdlRestCallFail()
297 restsubId := restSubsDbMock.AllocNextRestSubId()
298 restSubs := CreateRESTSubscription(t)
299 PrintRESTSubscriptionData(t, restSubs)
300 t.Logf("TEST: Writing subId = %v\n", restsubId)
301 err := mainCtrl.c.WriteRESTSubscriptionToSdl(restsubId, restSubs)
302 if err != nil {
303 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
304 t.Errorf("TEST: %s", err.Error())
305 }
306 } else {
307 t.Errorf("TEST: This test case should return error")
308 }
309}
310
311func TestReadRESTSubscriptionFromSdlFail(t *testing.T) {
312
313 // Try to read one subscription.
314 // Test db should return test error string
315 MakeNextSdlRestCallFail()
316 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
317 t.Logf("Reading restSubId = %v\n", restSubId)
318 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
319 if err != nil {
320 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
321 t.Errorf("TEST: %s", err.Error())
322 }
323 return
324 } else {
325 t.Errorf("TEST: This test case should return error")
326 }
327 PrintRESTSubscriptionData(t, restSubs)
328}
329
330func TestRemoveRESTSubscriptionFromSdlFail(t *testing.T) {
331
332 // Try to remove one subscription.
333 // Test db should return test error string
334 MakeNextSdlRestCallFail()
335 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
336 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
337 if err != nil {
338 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
339 t.Errorf("TEST: %s", err.Error())
340 }
341 return
342 } else {
343 t.Errorf("TEST: This test case should return error")
344 }
345 t.Logf("TEST: subscription removed from db. subId = %v", restSubId)
346}
347
348func TestReadAllRESTSubscriptionsFromSdlFail(t *testing.T) {
349
350 // Try to read all subscriptions.
351 // Test db should return test error string
352 MakeNextSdlRestCallFail()
353 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
354 if err != nil {
355 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
356 t.Errorf("TEST: %s", err.Error())
357 }
358 return
359 } else {
360 t.Errorf("TEST: This test case should return error")
361 }
362
363 for _, restSubs := range register {
364 PrintRESTSubscriptionData(t, restSubs)
365 }
366}
367
368func TestRemoveAllRESTSubscriptionsFromSdlFail(t *testing.T) {
369
370 // Try to remove all subscriptions.
371 // Test db should return test error string
372 MakeNextSdlRestCallFail()
373 err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
374 if err != nil {
375 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
376 t.Errorf("TEST: %s", err.Error())
377 }
378 return
379 } else {
380 t.Errorf("TEST: This test case should return error")
381 }
382 t.Log("TEST: All subscription removed from db")
383}
384
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300385func (m *RestSubsDbMock) Set(ns string, pairs ...interface{}) error {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300386 var key string
387 var val string
388
389 m.marshalLock.Lock()
390 defer m.marshalLock.Unlock()
391
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300392 if ns != restSubSdlNs {
393 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
394 }
395
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300396 if sdlRestShouldReturnError == true {
397 return GetSdlRestError()
398 }
399
400 for _, v := range pairs {
401 reflectType := reflect.TypeOf(v)
402 switch reflectType.Kind() {
403 case reflect.Slice:
404 val = fmt.Sprintf("%s", v.([]uint8))
405 default:
406 switch v.(type) {
407 case string:
408 key = v.(string)
409 default:
410 return fmt.Errorf("Set() error: Unexpected type\n")
411 }
412 }
413 }
414
415 if key != "" {
416 m.restSubsDb[key] = val
417 restSubId := key
418 restSubscriptionInfo := &RESTSubscriptionInfo{}
419 err := json.Unmarshal([]byte(val), restSubscriptionInfo)
420 if err != nil {
421 return fmt.Errorf("Set() json.unmarshal error: %s\n", err.Error())
422 }
423
424 restSubs := mainCtrl.c.CreateRESTSubscription(restSubscriptionInfo, &val)
425 m.restSubscriptions[restSubId] = restSubs
426 } else {
427 return fmt.Errorf("Set() error: key == ''\n")
428 }
429 return nil
430}
431
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300432func (m *RestSubsDbMock) Get(ns string, keys []string) (map[string]interface{}, error) {
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300433 retMap := make(map[string]interface{})
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300434
435 if ns != restSubSdlNs {
436 return nil, fmt.Errorf("Unexpected namespace '%s' error\n", ns)
437 }
438
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300439 if len(keys) == 0 {
440 return nil, fmt.Errorf("Get() error: len(key) == 0\n")
441 }
442
443 if sdlRestShouldReturnError == true {
444 return nil, GetSdlRestError()
445 }
446
447 for _, key := range keys {
448 if key != "" {
449 retMap[key] = m.restSubsDb[key]
450 } else {
451 return nil, fmt.Errorf("Get() error: key == ''\n")
452 }
453 }
454 return retMap, nil
455}
456
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300457func (m *RestSubsDbMock) GetAll(ns string) ([]string, error) {
458
459 if ns != restSubSdlNs {
460 return nil, fmt.Errorf("Unexpected namespace '%s' error\n", ns)
461 }
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300462
463 if sdlRestShouldReturnError == true {
464 return nil, GetSdlRestError()
465 }
466
467 keys := []string{}
468 for key, _ := range m.restSubsDb {
469 keys = append(keys, key)
470 }
471 return keys, nil
472}
473
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300474func (m *RestSubsDbMock) Remove(ns string, keys []string) error {
475
476 if ns != restSubSdlNs {
477 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
478 }
479
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300480 if len(keys) == 0 {
481 return fmt.Errorf("Remove() error: len(key) == 0\n")
482 }
483
484 if sdlRestShouldReturnError == true {
485 return GetSdlRestError()
486 }
487
488 restSubId := keys[0]
489 delete(m.restSubsDb, restSubId)
490 delete(m.restSubscriptions, restSubId)
491 return nil
492}
493
Timo Tietavainenf0bb66c2021-10-08 09:16:20 +0300494func (m *RestSubsDbMock) RemoveAll(ns string) error {
495
496 if ns != restSubSdlNs {
497 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
498 }
Konstantinos Archangelof268d7152021-06-14 12:24:00 +0300499
500 for key := range m.restSubsDb {
501
502 restSubId := key
503 delete(m.restSubsDb, restSubId)
504 delete(m.restSubscriptions, restSubId)
505 }
506
507 if sdlRestShouldReturnError == true {
508 return GetSdlRestError()
509 }
510
511 return nil
512}
513
514func MakeNextSdlRestCallFail() {
515 sdlRestShouldReturnError = true
516}
517
518func GetSdlRestError() error {
519 sdlRestShouldReturnError = false
520 return fmt.Errorf(sdlRestTestErrorString)
521}