Merge "README.md file updated - The file was completely rewritten"
[ric-plt/submgr.git] / pkg / control / sdl_restSubsDb_test.go
1 /*
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
20 package control
21
22 import (
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
34 var sdlRestShouldReturnError bool = false
35
36 const sdlRestTestErrorString string = "Test sdl REST returned error on purpose"
37
38 type 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
46 var restSubsDbMock *RestSubsDbMock
47
48 func CreateRestSubsDbMock() *RestSubsDbMock {
49         fmt.Println("Test CreateRestSubsDbMock()")
50         restSubsDbMock = new(RestSubsDbMock)
51         restSubsDbMock.ResetTestSettings()
52         restSubsDbMock.lastAllocatedRestSubId = ""
53         return restSubsDbMock
54 }
55
56 func (m *RestSubsDbMock) ResetTestSettings() {
57         m.restSubsDb = make(map[string]string)
58         m.restSubscriptions = make(map[string]*RESTSubscription)
59 }
60
61 func (m *RestSubsDbMock) AllocNextRestSubId() string {
62         m.lastAllocatedRestSubId = ksuid.New().String()
63         return m.lastAllocatedRestSubId
64 }
65
66 func (m *RestSubsDbMock) GetLastAllocatedRestSubId() string {
67         return m.lastAllocatedRestSubId
68 }
69
70 func (m *RestSubsDbMock) AddRestSubIdsInDb(restSubId string) {
71         m.restSubIdsInDb = append(m.restSubIdsInDb, restSubId)
72 }
73
74 func (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
84 func (m *RestSubsDbMock) EmptyRestSubIdsFromDb() {
85         m.restSubIdsInDb = nil
86 }
87
88 func 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)
97         restSubscription.lastReqMd5sum = "856e9546f6f7b65b13a86956f2e16f6a"
98         return restSubscription
99 }
100
101 func 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
111 func 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         verifyRESTKeyCount(t, 1)
124 }
125
126 func verifyRESTKeyCount(t *testing.T, expectedCount int) {
127
128         count, err := mainCtrl.c.GetRESTKeyCount()
129         if err != nil {
130                 t.Errorf("TEST: %s", err.Error())
131         } else {
132                 assert.Equal(t, expectedCount, count)
133         }
134 }
135
136 func TestReadRESTSubscriptionFromSdl(t *testing.T) {
137
138         restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
139         t.Logf("Reading restSubId = %v\n", restSubId)
140         restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
141         if err != nil {
142                 t.Errorf("TEST: %s", err.Error())
143                 return
144         }
145         PrintRESTSubscriptionData(t, restSubs)
146         assert.Equal(t, restSubsDbMock.restSubscriptions[restSubId], restSubs)
147 }
148
149 func TestRemoveRESTSubscriptionFromSdl(t *testing.T) {
150
151         restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
152         err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
153         if err != nil {
154                 t.Errorf("TEST: %s", err.Error())
155                 return
156         }
157         delete(restSubsDbMock.restSubscriptions, restSubId)
158         t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
159         restSubsDbMock.DeleteRestSubIdsFromDb(restSubId)
160 }
161
162 func TestReadNotExistingRESTSubscriptionFromSdl(t *testing.T) {
163
164         restSubId := ""
165         restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
166         if err != nil {
167                 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
168                 return
169         }
170         t.Errorf("TEST: REST subscription read from db. %v", restSubs)
171         PrintRESTSubscriptionData(t, restSubs)
172 }
173
174 func TestReadNotExistingRESTSubscriptionFromSdl2(t *testing.T) {
175
176         restSubId := "NotExistingSubsId"
177         restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
178         if err != nil {
179                 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
180                 return
181         }
182         t.Errorf("TEST: REST subscription read from db. %v", restSubs)
183         PrintRESTSubscriptionData(t, restSubs)
184 }
185
186 func TestRemoveNotExistingRESTSubscriptionFromSdl(t *testing.T) {
187
188         restSubId := ""
189         err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
190         if err != nil {
191                 t.Logf("TEST: %s", err.Error())
192                 return
193         }
194         t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
195 }
196
197 func TestWriteRESTSubscriptionsToSdl(t *testing.T) {
198
199         // Write 1st subscription
200         restSubId := restSubsDbMock.AllocNextRestSubId()
201         t.Logf("TEST: Writing restSubId = %v\n", restSubId)
202         restSubs := CreateRESTSubscription(t)
203         PrintRESTSubscriptionData(t, restSubs)
204         err := mainCtrl.c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
205         if err != nil {
206                 t.Errorf("TEST: %s", err.Error())
207                 return
208         }
209         restSubsDbMock.AddRestSubIdsInDb(restSubId)
210         t.Logf("TEST: REST subscription written in db = %v", restSubs)
211
212         // Write 2nd subscription
213         restSubId = restSubsDbMock.AllocNextRestSubId()
214         t.Logf("TEST:Writing restSubId = %v\n", restSubId)
215         restSubs = CreateRESTSubscription(t)
216         PrintRESTSubscriptionData(t, restSubs)
217         err = mainCtrl.c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
218         if err != nil {
219                 t.Errorf("TEST: %s", err.Error())
220                 return
221         }
222         restSubsDbMock.AddRestSubIdsInDb(restSubId)
223         t.Logf("TEST: REST subscription written in db = %v", restSubs)
224
225         // Write 3rd subscription
226         restSubId = restSubsDbMock.AllocNextRestSubId()
227         t.Logf("TEST: Writing restSubId = %v\n", restSubId)
228         restSubs = CreateRESTSubscription(t)
229         PrintRESTSubscriptionData(t, restSubs)
230         err = mainCtrl.c.WriteRESTSubscriptionToSdl(restSubId, restSubs)
231         if err != nil {
232                 t.Errorf("TEST: %s", err.Error())
233                 return
234         }
235         restSubsDbMock.AddRestSubIdsInDb(restSubId)
236         t.Logf("TEST: REST subscription written in db = %v", restSubs)
237 }
238
239 func TestReadRESTSubscriptionsFromSdl(t *testing.T) {
240
241         for _, restSubId := range restSubsDbMock.restSubIdsInDb {
242                 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
243                 if err != nil {
244                         t.Errorf("TEST: %s", err.Error())
245                         return
246                 }
247                 PrintRESTSubscriptionData(t, restSubs)
248         }
249 }
250
251 func TestReadAllRESTSubscriptionsFromSdl(t *testing.T) {
252
253         register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
254         if err != nil {
255                 t.Errorf("TEST: %s", err.Error())
256                 return
257         }
258
259         for _, restSubs := range register {
260                 PrintRESTSubscriptionData(t, restSubs)
261         }
262
263         assert.Equal(t, len(register), 3)
264 }
265
266 func TestRemoveAllRESTSubscriptionsFromSdl(t *testing.T) {
267
268         err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
269         if err != nil {
270                 t.Errorf("TEST: %s", err.Error())
271                 return
272         }
273         t.Log("TEST: All subscription removed from db")
274         restSubsDbMock.EmptyRestSubIdsFromDb()
275 }
276
277 func TestReadAllRESTSubscriptionsFromSdl2(t *testing.T) {
278
279         register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
280         if err != nil {
281                 t.Errorf("TEST: %s", err.Error())
282                 return
283         }
284         for _, restSubs := range restSubsDbMock.restSubscriptions {
285                 PrintRESTSubscriptionData(t, restSubs)
286         }
287         assert.Equal(t, len(register), 0)
288 }
289
290 func TestWriteRESTSubscriptionToSdlFail(t *testing.T) {
291
292         // Try to write one subscription.
293         // Test db should return test error string
294         MakeNextSdlRestCallFail()
295         restsubId := restSubsDbMock.AllocNextRestSubId()
296         restSubs := CreateRESTSubscription(t)
297         PrintRESTSubscriptionData(t, restSubs)
298         t.Logf("TEST: Writing subId = %v\n", restsubId)
299         err := mainCtrl.c.WriteRESTSubscriptionToSdl(restsubId, restSubs)
300         if err != nil {
301                 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
302                         t.Errorf("TEST: %s", err.Error())
303                 }
304         } else {
305                 t.Errorf("TEST: This test case should return error")
306         }
307 }
308
309 func TestReadRESTSubscriptionFromSdlFail(t *testing.T) {
310
311         // Try to read one subscription.
312         // Test db should return test error string
313         MakeNextSdlRestCallFail()
314         restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
315         t.Logf("Reading restSubId = %v\n", restSubId)
316         restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
317         if err != nil {
318                 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
319                         t.Errorf("TEST: %s", err.Error())
320                 }
321                 return
322         } else {
323                 t.Errorf("TEST: This test case should return error")
324         }
325         PrintRESTSubscriptionData(t, restSubs)
326 }
327
328 func TestRemoveRESTSubscriptionFromSdlFail(t *testing.T) {
329
330         // Try to remove one subscription.
331         // Test db should return test error string
332         MakeNextSdlRestCallFail()
333         restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
334         err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
335         if err != nil {
336                 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
337                         t.Errorf("TEST: %s", err.Error())
338                 }
339                 return
340         } else {
341                 t.Errorf("TEST: This test case should return error")
342         }
343         t.Logf("TEST: subscription removed from db. subId = %v", restSubId)
344 }
345
346 func TestReadAllRESTSubscriptionsFromSdlFail(t *testing.T) {
347
348         // Try to read all subscriptions.
349         // Test db should return test error string
350         MakeNextSdlRestCallFail()
351         register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
352         if err != nil {
353                 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
354                         t.Errorf("TEST: %s", err.Error())
355                 }
356                 return
357         } else {
358                 t.Errorf("TEST: This test case should return error")
359         }
360
361         for _, restSubs := range register {
362                 PrintRESTSubscriptionData(t, restSubs)
363         }
364 }
365
366 func TestRemoveAllRESTSubscriptionsFromSdlFail(t *testing.T) {
367
368         // Try to remove all subscriptions.
369         // Test db should return test error string
370         MakeNextSdlRestCallFail()
371         err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
372         if err != nil {
373                 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
374                         t.Errorf("TEST: %s", err.Error())
375                 }
376                 return
377         } else {
378                 t.Errorf("TEST: This test case should return error")
379         }
380         t.Log("TEST: All subscription removed from db")
381 }
382
383 func (m *RestSubsDbMock) Set(ns string, pairs ...interface{}) error {
384         var key string
385         var val string
386
387         m.marshalLock.Lock()
388         defer m.marshalLock.Unlock()
389
390         if ns != restSubSdlNs {
391                 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
392         }
393
394         if sdlRestShouldReturnError == true {
395                 return GetSdlRestError()
396         }
397
398         for _, v := range pairs {
399                 reflectType := reflect.TypeOf(v)
400                 switch reflectType.Kind() {
401                 case reflect.Slice:
402                         val = fmt.Sprintf("%s", v.([]uint8))
403                 default:
404                         switch v.(type) {
405                         case string:
406                                 key = v.(string)
407                         default:
408                                 return fmt.Errorf("Set() error: Unexpected type\n")
409                         }
410                 }
411         }
412
413         if key != "" {
414                 m.restSubsDb[key] = val
415                 restSubId := key
416                 restSubscriptionInfo := &RESTSubscriptionInfo{}
417                 err := json.Unmarshal([]byte(val), restSubscriptionInfo)
418                 if err != nil {
419                         return fmt.Errorf("Set() json.unmarshal error: %s\n", err.Error())
420                 }
421
422                 restSubs := mainCtrl.c.CreateRESTSubscription(restSubscriptionInfo, &val)
423                 m.restSubscriptions[restSubId] = restSubs
424         } else {
425                 return fmt.Errorf("Set() error: key == ''\n")
426         }
427         return nil
428 }
429
430 func (m *RestSubsDbMock) Get(ns string, keys []string) (map[string]interface{}, error) {
431         retMap := make(map[string]interface{})
432
433         if ns != restSubSdlNs {
434                 return nil, fmt.Errorf("Unexpected namespace '%s' error\n", ns)
435         }
436
437         if len(keys) == 0 {
438                 return nil, fmt.Errorf("Get() error: len(key) == 0\n")
439         }
440
441         if sdlRestShouldReturnError == true {
442                 return nil, GetSdlRestError()
443         }
444
445         for _, key := range keys {
446                 if key != "" {
447                         retMap[key] = m.restSubsDb[key]
448                 } else {
449                         return nil, fmt.Errorf("Get() error: key == ''\n")
450                 }
451         }
452         return retMap, nil
453 }
454
455 func (m *RestSubsDbMock) GetAll(ns string) ([]string, error) {
456
457         if ns != restSubSdlNs {
458                 return nil, fmt.Errorf("Unexpected namespace '%s' error\n", ns)
459         }
460
461         if sdlRestShouldReturnError == true {
462                 return nil, GetSdlRestError()
463         }
464
465         keys := []string{}
466         for key, _ := range m.restSubsDb {
467                 keys = append(keys, key)
468         }
469         return keys, nil
470 }
471
472 func (m *RestSubsDbMock) Remove(ns string, keys []string) error {
473
474         if ns != restSubSdlNs {
475                 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
476         }
477
478         if len(keys) == 0 {
479                 return fmt.Errorf("Remove() error: len(key) == 0\n")
480         }
481
482         if sdlRestShouldReturnError == true {
483                 return GetSdlRestError()
484         }
485
486         restSubId := keys[0]
487         delete(m.restSubsDb, restSubId)
488         delete(m.restSubscriptions, restSubId)
489         return nil
490 }
491
492 func (m *RestSubsDbMock) RemoveAll(ns string) error {
493
494         if ns != restSubSdlNs {
495                 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
496         }
497
498         for key := range m.restSubsDb {
499
500                 restSubId := key
501                 delete(m.restSubsDb, restSubId)
502                 delete(m.restSubscriptions, restSubId)
503         }
504
505         if sdlRestShouldReturnError == true {
506                 return GetSdlRestError()
507         }
508
509         return nil
510 }
511
512 func MakeNextSdlRestCallFail() {
513         sdlRestShouldReturnError = true
514 }
515
516 func GetSdlRestError() error {
517         sdlRestShouldReturnError = false
518         return fmt.Errorf(sdlRestTestErrorString)
519 }