12bf1eb449ae0ab2a1ce52c035890c895c547b5e
[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 }
124
125 func 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
138 func 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
151 func 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
163 func 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
175 func 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
186 func 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
228 func 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
240 func 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
255 func 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
266 func 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
279 func 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
298 func 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
317 func 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
335 func 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
355 func 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
372 func (m *RestSubsDbMock) Set(pairs ...interface{}) error {
373         var key string
374         var val string
375
376         m.marshalLock.Lock()
377         defer m.marshalLock.Unlock()
378
379         if sdlRestShouldReturnError == true {
380                 return GetSdlRestError()
381         }
382
383         for _, v := range pairs {
384                 reflectType := reflect.TypeOf(v)
385                 switch reflectType.Kind() {
386                 case reflect.Slice:
387                         val = fmt.Sprintf("%s", v.([]uint8))
388                 default:
389                         switch v.(type) {
390                         case string:
391                                 key = v.(string)
392                         default:
393                                 return fmt.Errorf("Set() error: Unexpected type\n")
394                         }
395                 }
396         }
397
398         if key != "" {
399                 m.restSubsDb[key] = val
400                 restSubId := key
401                 restSubscriptionInfo := &RESTSubscriptionInfo{}
402                 err := json.Unmarshal([]byte(val), restSubscriptionInfo)
403                 if err != nil {
404                         return fmt.Errorf("Set() json.unmarshal error: %s\n", err.Error())
405                 }
406
407                 restSubs := mainCtrl.c.CreateRESTSubscription(restSubscriptionInfo, &val)
408                 m.restSubscriptions[restSubId] = restSubs
409         } else {
410                 return fmt.Errorf("Set() error: key == ''\n")
411         }
412         return nil
413 }
414
415 func (m *RestSubsDbMock) Get(keys []string) (map[string]interface{}, error) {
416         retMap := make(map[string]interface{})
417         if len(keys) == 0 {
418                 return nil, fmt.Errorf("Get() error: len(key) == 0\n")
419         }
420
421         if sdlRestShouldReturnError == true {
422                 return nil, GetSdlRestError()
423         }
424
425         for _, key := range keys {
426                 if key != "" {
427                         retMap[key] = m.restSubsDb[key]
428                 } else {
429                         return nil, fmt.Errorf("Get() error: key == ''\n")
430                 }
431         }
432         return retMap, nil
433 }
434
435 func (m *RestSubsDbMock) GetAll() ([]string, error) {
436
437         if sdlRestShouldReturnError == true {
438                 return nil, GetSdlRestError()
439         }
440
441         keys := []string{}
442         for key, _ := range m.restSubsDb {
443                 keys = append(keys, key)
444         }
445         return keys, nil
446 }
447
448 func (m *RestSubsDbMock) Remove(keys []string) error {
449         if len(keys) == 0 {
450                 return fmt.Errorf("Remove() error: len(key) == 0\n")
451         }
452
453         if sdlRestShouldReturnError == true {
454                 return GetSdlRestError()
455         }
456
457         restSubId := keys[0]
458         delete(m.restSubsDb, restSubId)
459         delete(m.restSubscriptions, restSubId)
460         return nil
461 }
462
463 func (m *RestSubsDbMock) RemoveAll() error {
464
465         for key := range m.restSubsDb {
466
467                 restSubId := key
468                 delete(m.restSubsDb, restSubId)
469                 delete(m.restSubscriptions, restSubId)
470         }
471
472         if sdlRestShouldReturnError == true {
473                 return GetSdlRestError()
474         }
475
476         return nil
477 }
478
479 func MakeNextSdlRestCallFail() {
480         sdlRestShouldReturnError = true
481 }
482
483 func GetSdlRestError() error {
484         sdlRestShouldReturnError = false
485         return fmt.Errorf(sdlRestTestErrorString)
486 }