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.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
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)
111 func TestWriteRESTSubscriptionToSdl(t *testing.T) {
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)
120 t.Errorf("TEST: %s", err.Error())
122 restSubsDbMock.AddRestSubIdsInDb(restSubId)
123 verifyRESTKeyCount(t, 1)
126 func verifyRESTKeyCount(t *testing.T, expectedCount int) {
128 count, err := mainCtrl.c.GetRESTKeyCount()
130 t.Errorf("TEST: %s", err.Error())
132 assert.Equal(t, expectedCount, count)
136 func TestReadRESTSubscriptionFromSdl(t *testing.T) {
138 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
139 t.Logf("Reading restSubId = %v\n", restSubId)
140 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
142 t.Errorf("TEST: %s", err.Error())
145 PrintRESTSubscriptionData(t, restSubs)
146 assert.Equal(t, restSubsDbMock.restSubscriptions[restSubId], restSubs)
149 func TestRemoveRESTSubscriptionFromSdl(t *testing.T) {
151 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
152 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
154 t.Errorf("TEST: %s", err.Error())
157 delete(restSubsDbMock.restSubscriptions, restSubId)
158 t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
159 restSubsDbMock.DeleteRestSubIdsFromDb(restSubId)
162 func TestReadNotExistingRESTSubscriptionFromSdl(t *testing.T) {
165 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
167 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
170 t.Errorf("TEST: REST subscription read from db. %v", restSubs)
171 PrintRESTSubscriptionData(t, restSubs)
174 func TestReadNotExistingRESTSubscriptionFromSdl2(t *testing.T) {
176 restSubId := "NotExistingSubsId"
177 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
179 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
182 t.Errorf("TEST: REST subscription read from db. %v", restSubs)
183 PrintRESTSubscriptionData(t, restSubs)
186 func TestRemoveNotExistingRESTSubscriptionFromSdl(t *testing.T) {
189 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
191 t.Logf("TEST: %s", err.Error())
194 t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
197 func TestWriteRESTSubscriptionsToSdl(t *testing.T) {
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)
206 t.Errorf("TEST: %s", err.Error())
209 restSubsDbMock.AddRestSubIdsInDb(restSubId)
210 t.Logf("TEST: REST subscription written in db = %v", restSubs)
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)
219 t.Errorf("TEST: %s", err.Error())
222 restSubsDbMock.AddRestSubIdsInDb(restSubId)
223 t.Logf("TEST: REST subscription written in db = %v", restSubs)
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)
232 t.Errorf("TEST: %s", err.Error())
235 restSubsDbMock.AddRestSubIdsInDb(restSubId)
236 t.Logf("TEST: REST subscription written in db = %v", restSubs)
239 func TestReadRESTSubscriptionsFromSdl(t *testing.T) {
241 for _, restSubId := range restSubsDbMock.restSubIdsInDb {
242 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
244 t.Errorf("TEST: %s", err.Error())
247 PrintRESTSubscriptionData(t, restSubs)
251 func TestReadAllRESTSubscriptionsFromSdl(t *testing.T) {
253 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
255 t.Errorf("TEST: %s", err.Error())
259 for _, restSubs := range register {
260 PrintRESTSubscriptionData(t, restSubs)
263 assert.Equal(t, len(register), 3)
266 func TestRemoveAllRESTSubscriptionsFromSdl(t *testing.T) {
268 err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
270 t.Errorf("TEST: %s", err.Error())
273 t.Log("TEST: All subscription removed from db")
274 restSubsDbMock.EmptyRestSubIdsFromDb()
277 func TestReadAllRESTSubscriptionsFromSdl2(t *testing.T) {
279 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
281 t.Errorf("TEST: %s", err.Error())
284 for _, restSubs := range restSubsDbMock.restSubscriptions {
285 PrintRESTSubscriptionData(t, restSubs)
287 assert.Equal(t, len(register), 0)
290 func TestWriteRESTSubscriptionToSdlFail(t *testing.T) {
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)
301 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
302 t.Errorf("TEST: %s", err.Error())
305 t.Errorf("TEST: This test case should return error")
309 func TestReadRESTSubscriptionFromSdlFail(t *testing.T) {
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)
318 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
319 t.Errorf("TEST: %s", err.Error())
323 t.Errorf("TEST: This test case should return error")
325 PrintRESTSubscriptionData(t, restSubs)
328 func TestRemoveRESTSubscriptionFromSdlFail(t *testing.T) {
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)
336 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
337 t.Errorf("TEST: %s", err.Error())
341 t.Errorf("TEST: This test case should return error")
343 t.Logf("TEST: subscription removed from db. subId = %v", restSubId)
346 func TestReadAllRESTSubscriptionsFromSdlFail(t *testing.T) {
348 // Try to read all subscriptions.
349 // Test db should return test error string
350 MakeNextSdlRestCallFail()
351 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
353 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
354 t.Errorf("TEST: %s", err.Error())
358 t.Errorf("TEST: This test case should return error")
361 for _, restSubs := range register {
362 PrintRESTSubscriptionData(t, restSubs)
366 func TestRemoveAllRESTSubscriptionsFromSdlFail(t *testing.T) {
368 // Try to remove all subscriptions.
369 // Test db should return test error string
370 MakeNextSdlRestCallFail()
371 err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
373 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
374 t.Errorf("TEST: %s", err.Error())
378 t.Errorf("TEST: This test case should return error")
380 t.Log("TEST: All subscription removed from db")
383 func (m *RestSubsDbMock) Set(ns string, pairs ...interface{}) error {
388 defer m.marshalLock.Unlock()
390 if ns != restSubSdlNs {
391 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
394 if sdlRestShouldReturnError == true {
395 return GetSdlRestError()
398 for _, v := range pairs {
399 reflectType := reflect.TypeOf(v)
400 switch reflectType.Kind() {
402 val = fmt.Sprintf("%s", v.([]uint8))
408 return fmt.Errorf("Set() error: Unexpected type\n")
414 m.restSubsDb[key] = val
416 restSubscriptionInfo := &RESTSubscriptionInfo{}
417 err := json.Unmarshal([]byte(val), restSubscriptionInfo)
419 return fmt.Errorf("Set() json.unmarshal error: %s\n", err.Error())
422 restSubs := mainCtrl.c.CreateRESTSubscription(restSubscriptionInfo, &val)
423 m.restSubscriptions[restSubId] = restSubs
425 return fmt.Errorf("Set() error: key == ''\n")
430 func (m *RestSubsDbMock) Get(ns string, keys []string) (map[string]interface{}, error) {
431 retMap := make(map[string]interface{})
433 if ns != restSubSdlNs {
434 return nil, fmt.Errorf("Unexpected namespace '%s' error\n", ns)
438 return nil, fmt.Errorf("Get() error: len(key) == 0\n")
441 if sdlRestShouldReturnError == true {
442 return nil, GetSdlRestError()
445 for _, key := range keys {
447 retMap[key] = m.restSubsDb[key]
449 return nil, fmt.Errorf("Get() error: key == ''\n")
455 func (m *RestSubsDbMock) GetAll(ns string) ([]string, error) {
457 if ns != restSubSdlNs {
458 return nil, fmt.Errorf("Unexpected namespace '%s' error\n", ns)
461 if sdlRestShouldReturnError == true {
462 return nil, GetSdlRestError()
466 for key, _ := range m.restSubsDb {
467 keys = append(keys, key)
472 func (m *RestSubsDbMock) Remove(ns string, keys []string) error {
474 if ns != restSubSdlNs {
475 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
479 return fmt.Errorf("Remove() error: len(key) == 0\n")
482 if sdlRestShouldReturnError == true {
483 return GetSdlRestError()
487 delete(m.restSubsDb, restSubId)
488 delete(m.restSubscriptions, restSubId)
492 func (m *RestSubsDbMock) RemoveAll(ns string) error {
494 if ns != restSubSdlNs {
495 return fmt.Errorf("Unexpected namespace '%s' error\n", ns)
498 for key := range m.restSubsDb {
501 delete(m.restSubsDb, restSubId)
502 delete(m.restSubscriptions, restSubId)
505 if sdlRestShouldReturnError == true {
506 return GetSdlRestError()
512 func MakeNextSdlRestCallFail() {
513 sdlRestShouldReturnError = true
516 func GetSdlRestError() error {
517 sdlRestShouldReturnError = false
518 return fmt.Errorf(sdlRestTestErrorString)