2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
30 "github.com/segmentio/ksuid"
31 "github.com/stretchr/testify/assert"
34 var sdlRestShouldReturnError bool = false
36 const sdlRestTestErrorString string = "Test sdl REST returned error on purpose"
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
46 var restSubsDbMock *RestSubsDbMock
48 func CreateRestSubsDbMock() *RestSubsDbMock {
49 fmt.Println("Test CreateRestSubsDbMock()")
50 restSubsDbMock = new(RestSubsDbMock)
51 restSubsDbMock.ResetTestSettings()
52 restSubsDbMock.lastAllocatedRestSubId = ""
56 func (m *RestSubsDbMock) ResetTestSettings() {
57 m.restSubsDb = make(map[string]string)
58 m.restSubscriptions = make(map[string]*RESTSubscription)
61 func (m *RestSubsDbMock) AllocNextRestSubId() string {
62 m.lastAllocatedRestSubId = ksuid.New().String()
63 return m.lastAllocatedRestSubId
66 func (m *RestSubsDbMock) GetLastAllocatedRestSubId() string {
67 return m.lastAllocatedRestSubId
70 func (m *RestSubsDbMock) AddRestSubIdsInDb(restSubId string) {
71 m.restSubIdsInDb = append(m.restSubIdsInDb, restSubId)
74 func (m *RestSubsDbMock) DeleteRestSubIdsFromDb(restSubId string) {
75 newrestSubIdsInDb := []string{}
76 for _, i := range m.restSubIdsInDb {
78 newrestSubIdsInDb = append(newrestSubIdsInDb, i)
81 m.restSubIdsInDb = newrestSubIdsInDb
84 func (m *RestSubsDbMock) EmptyRestSubIdsFromDb() {
85 m.restSubIdsInDb = nil
88 func CreateRESTSubscription(t *testing.T) *RESTSubscription {
89 t.Log("TEST: Creating REST subscription")
91 restSubscription := &RESTSubscription{}
92 restSubscription.xAppServiceName = "localhost"
93 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)
98 restSubscription.lastReqMd5sum = "856e9546f6f7b65b13a86956f2e16f6a"
99 return restSubscription
102 func PrintRESTSubscriptionData(t *testing.T, restSubs *RESTSubscription) {
103 t.Log("TEST: RESTSubscription data")
104 t.Logf("TEST: restSubs.xAppServiceName = %v", restSubs.xAppServiceName)
105 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)
113 func TestWriteRESTSubscriptionToSdl(t *testing.T) {
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)
122 t.Errorf("TEST: %s", err.Error())
124 restSubsDbMock.AddRestSubIdsInDb(restSubId)
125 verifyRESTKeyCount(t, 1)
128 func verifyRESTKeyCount(t *testing.T, expectedCount int) {
130 count, err := mainCtrl.c.GetRESTKeyCount()
132 t.Errorf("TEST: %s", err.Error())
134 assert.Equal(t, expectedCount, count)
138 func TestReadRESTSubscriptionFromSdl(t *testing.T) {
140 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
141 t.Logf("Reading restSubId = %v\n", restSubId)
142 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
144 t.Errorf("TEST: %s", err.Error())
147 PrintRESTSubscriptionData(t, restSubs)
148 assert.Equal(t, restSubsDbMock.restSubscriptions[restSubId], restSubs)
151 func TestRemoveRESTSubscriptionFromSdl(t *testing.T) {
153 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
154 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
156 t.Errorf("TEST: %s", err.Error())
159 delete(restSubsDbMock.restSubscriptions, restSubId)
160 t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
161 restSubsDbMock.DeleteRestSubIdsFromDb(restSubId)
164 func TestReadNotExistingRESTSubscriptionFromSdl(t *testing.T) {
167 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
169 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
172 t.Errorf("TEST: REST subscription read from db. %v", restSubs)
173 PrintRESTSubscriptionData(t, restSubs)
176 func TestReadNotExistingRESTSubscriptionFromSdl2(t *testing.T) {
178 restSubId := "NotExistingSubsId"
179 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
181 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
184 t.Errorf("TEST: REST subscription read from db. %v", restSubs)
185 PrintRESTSubscriptionData(t, restSubs)
188 func TestRemoveNotExistingRESTSubscriptionFromSdl(t *testing.T) {
191 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
193 t.Logf("TEST: %s", err.Error())
196 t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
199 func TestWriteRESTSubscriptionsToSdl(t *testing.T) {
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)
208 t.Errorf("TEST: %s", err.Error())
211 restSubsDbMock.AddRestSubIdsInDb(restSubId)
212 t.Logf("TEST: REST subscription written in db = %v", restSubs)
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)
221 t.Errorf("TEST: %s", err.Error())
224 restSubsDbMock.AddRestSubIdsInDb(restSubId)
225 t.Logf("TEST: REST subscription written in db = %v", restSubs)
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)
234 t.Errorf("TEST: %s", err.Error())
237 restSubsDbMock.AddRestSubIdsInDb(restSubId)
238 t.Logf("TEST: REST subscription written in db = %v", restSubs)
241 func TestReadRESTSubscriptionsFromSdl(t *testing.T) {
243 for _, restSubId := range restSubsDbMock.restSubIdsInDb {
244 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
246 t.Errorf("TEST: %s", err.Error())
249 PrintRESTSubscriptionData(t, restSubs)
253 func TestReadAllRESTSubscriptionsFromSdl(t *testing.T) {
255 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
257 t.Errorf("TEST: %s", err.Error())
261 for _, restSubs := range register {
262 PrintRESTSubscriptionData(t, restSubs)
265 assert.Equal(t, len(register), 3)
268 func TestRemoveAllRESTSubscriptionsFromSdl(t *testing.T) {
270 err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
272 t.Errorf("TEST: %s", err.Error())
275 t.Log("TEST: All subscription removed from db")
276 restSubsDbMock.EmptyRestSubIdsFromDb()
279 func TestReadAllRESTSubscriptionsFromSdl2(t *testing.T) {
281 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
283 t.Errorf("TEST: %s", err.Error())
286 for _, restSubs := range restSubsDbMock.restSubscriptions {
287 PrintRESTSubscriptionData(t, restSubs)
289 assert.Equal(t, len(register), 0)
292 func TestWriteRESTSubscriptionToSdlFail(t *testing.T) {
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)
303 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
304 t.Errorf("TEST: %s", err.Error())
307 t.Errorf("TEST: This test case should return error")
311 func TestReadRESTSubscriptionFromSdlFail(t *testing.T) {
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)
320 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
321 t.Errorf("TEST: %s", err.Error())
325 t.Errorf("TEST: This test case should return error")
327 PrintRESTSubscriptionData(t, restSubs)
330 func TestRemoveRESTSubscriptionFromSdlFail(t *testing.T) {
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)
338 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
339 t.Errorf("TEST: %s", err.Error())
343 t.Errorf("TEST: This test case should return error")
345 t.Logf("TEST: subscription removed from db. subId = %v", restSubId)
348 func TestReadAllRESTSubscriptionsFromSdlFail(t *testing.T) {
350 // Try to read all subscriptions.
351 // Test db should return test error string
352 MakeNextSdlRestCallFail()
353 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
355 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
356 t.Errorf("TEST: %s", err.Error())
360 t.Errorf("TEST: This test case should return error")
363 for _, restSubs := range register {
364 PrintRESTSubscriptionData(t, restSubs)
368 func TestRemoveAllRESTSubscriptionsFromSdlFail(t *testing.T) {
370 // Try to remove all subscriptions.
371 // Test db should return test error string
372 MakeNextSdlRestCallFail()
373 err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
375 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
376 t.Errorf("TEST: %s", err.Error())
380 t.Errorf("TEST: This test case should return error")
382 t.Log("TEST: All subscription removed from db")
385 func (m *RestSubsDbMock) Set(ns string, pairs ...interface{}) error {
390 defer m.marshalLock.Unlock()
392 if ns != restSubSdlNs {
393 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
396 if sdlRestShouldReturnError == true {
397 return GetSdlRestError()
400 for _, v := range pairs {
401 reflectType := reflect.TypeOf(v)
402 switch reflectType.Kind() {
404 val = fmt.Sprintf("%s", v.([]uint8))
410 return fmt.Errorf("Set() error: Unexpected type\n")
416 m.restSubsDb[key] = val
418 restSubscriptionInfo := &RESTSubscriptionInfo{}
419 err := json.Unmarshal([]byte(val), restSubscriptionInfo)
421 return fmt.Errorf("Set() json.unmarshal error: %s\n", err.Error())
424 restSubs := mainCtrl.c.CreateRESTSubscription(restSubscriptionInfo, &val)
425 m.restSubscriptions[restSubId] = restSubs
427 return fmt.Errorf("Set() error: key == ''\n")
432 func (m *RestSubsDbMock) Get(ns string, keys []string) (map[string]interface{}, error) {
433 retMap := make(map[string]interface{})
435 if ns != restSubSdlNs {
436 return nil, fmt.Errorf("Unexpected namespace '%s' error\n", ns)
440 return nil, fmt.Errorf("Get() error: len(key) == 0\n")
443 if sdlRestShouldReturnError == true {
444 return nil, GetSdlRestError()
447 for _, key := range keys {
449 retMap[key] = m.restSubsDb[key]
451 return nil, fmt.Errorf("Get() error: key == ''\n")
457 func (m *RestSubsDbMock) GetAll(ns string) ([]string, error) {
459 if ns != restSubSdlNs {
460 return nil, fmt.Errorf("Unexpected namespace '%s' error\n", ns)
463 if sdlRestShouldReturnError == true {
464 return nil, GetSdlRestError()
468 for key, _ := range m.restSubsDb {
469 keys = append(keys, key)
474 func (m *RestSubsDbMock) Remove(ns string, keys []string) error {
476 if ns != restSubSdlNs {
477 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
481 return fmt.Errorf("Remove() error: len(key) == 0\n")
484 if sdlRestShouldReturnError == true {
485 return GetSdlRestError()
489 delete(m.restSubsDb, restSubId)
490 delete(m.restSubscriptions, restSubId)
494 func (m *RestSubsDbMock) RemoveAll(ns string) error {
496 if ns != restSubSdlNs {
497 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
500 for key := range m.restSubsDb {
503 delete(m.restSubsDb, restSubId)
504 delete(m.restSubscriptions, restSubId)
507 if sdlRestShouldReturnError == true {
508 return GetSdlRestError()
514 func MakeNextSdlRestCallFail() {
515 sdlRestShouldReturnError = true
518 func GetSdlRestError() error {
519 sdlRestShouldReturnError = false
520 return fmt.Errorf(sdlRestTestErrorString)