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)
125 func TestReadRESTSubscriptionFromSdl(t *testing.T) {
127 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
128 t.Logf("Reading restSubId = %v\n", restSubId)
129 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
131 t.Errorf("TEST: %s", err.Error())
134 PrintRESTSubscriptionData(t, restSubs)
135 assert.Equal(t, restSubsDbMock.restSubscriptions[restSubId], restSubs)
138 func TestRemoveRESTSubscriptionFromSdl(t *testing.T) {
140 restSubId := restSubsDbMock.GetLastAllocatedRestSubId()
141 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
143 t.Errorf("TEST: %s", err.Error())
146 delete(restSubsDbMock.restSubscriptions, restSubId)
147 t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
148 restSubsDbMock.DeleteRestSubIdsFromDb(restSubId)
151 func TestReadNotExistingRESTSubscriptionFromSdl(t *testing.T) {
154 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
156 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
159 t.Errorf("TEST: REST subscription read from db. %v", restSubs)
160 PrintRESTSubscriptionData(t, restSubs)
163 func TestReadNotExistingRESTSubscriptionFromSdl2(t *testing.T) {
165 restSubId := "NotExistingSubsId"
166 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
168 t.Logf("TEST: REST subscription not found from db. restSubId = %v", restSubId)
171 t.Errorf("TEST: REST subscription read from db. %v", restSubs)
172 PrintRESTSubscriptionData(t, restSubs)
175 func TestRemoveNotExistingRESTSubscriptionFromSdl(t *testing.T) {
178 err := mainCtrl.c.RemoveRESTSubscriptionFromSdl(restSubId)
180 t.Logf("TEST: %s", err.Error())
183 t.Logf("TEST: REST subscription removed from db. subId = %v", restSubId)
186 func TestWriteRESTSubscriptionsToSdl(t *testing.T) {
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)
195 t.Errorf("TEST: %s", err.Error())
198 restSubsDbMock.AddRestSubIdsInDb(restSubId)
199 t.Logf("TEST: REST subscription written in db = %v", restSubs)
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)
208 t.Errorf("TEST: %s", err.Error())
211 restSubsDbMock.AddRestSubIdsInDb(restSubId)
212 t.Logf("TEST: REST subscription written in db = %v", restSubs)
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)
221 t.Errorf("TEST: %s", err.Error())
224 restSubsDbMock.AddRestSubIdsInDb(restSubId)
225 t.Logf("TEST: REST subscription written in db = %v", restSubs)
228 func TestReadRESTSubscriptionsFromSdl(t *testing.T) {
230 for _, restSubId := range restSubsDbMock.restSubIdsInDb {
231 restSubs, err := mainCtrl.c.ReadRESTSubscriptionFromSdl(restSubId)
233 t.Errorf("TEST: %s", err.Error())
236 PrintRESTSubscriptionData(t, restSubs)
240 func TestReadAllRESTSubscriptionsFromSdl(t *testing.T) {
242 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
244 t.Errorf("TEST: %s", err.Error())
248 for _, restSubs := range register {
249 PrintRESTSubscriptionData(t, restSubs)
252 assert.Equal(t, len(register), 3)
255 func TestRemoveAllRESTSubscriptionsFromSdl(t *testing.T) {
257 err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
259 t.Errorf("TEST: %s", err.Error())
262 t.Log("TEST: All subscription removed from db")
263 restSubsDbMock.EmptyRestSubIdsFromDb()
266 func TestReadAllRESTSubscriptionsFromSdl2(t *testing.T) {
268 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
270 t.Errorf("TEST: %s", err.Error())
273 for _, restSubs := range restSubsDbMock.restSubscriptions {
274 PrintRESTSubscriptionData(t, restSubs)
276 assert.Equal(t, len(register), 0)
279 func TestWriteRESTSubscriptionToSdlFail(t *testing.T) {
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)
290 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
291 t.Errorf("TEST: %s", err.Error())
294 t.Errorf("TEST: This test case should return error")
298 func TestReadRESTSubscriptionFromSdlFail(t *testing.T) {
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)
307 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
308 t.Errorf("TEST: %s", err.Error())
312 t.Errorf("TEST: This test case should return error")
314 PrintRESTSubscriptionData(t, restSubs)
317 func TestRemoveRESTSubscriptionFromSdlFail(t *testing.T) {
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)
325 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
326 t.Errorf("TEST: %s", err.Error())
330 t.Errorf("TEST: This test case should return error")
332 t.Logf("TEST: subscription removed from db. subId = %v", restSubId)
335 func TestReadAllRESTSubscriptionsFromSdlFail(t *testing.T) {
337 // Try to read all subscriptions.
338 // Test db should return test error string
339 MakeNextSdlRestCallFail()
340 register, err := mainCtrl.c.ReadAllRESTSubscriptionsFromSdl()
342 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
343 t.Errorf("TEST: %s", err.Error())
347 t.Errorf("TEST: This test case should return error")
350 for _, restSubs := range register {
351 PrintRESTSubscriptionData(t, restSubs)
355 func TestRemoveAllRESTSubscriptionsFromSdlFail(t *testing.T) {
357 // Try to remove all subscriptions.
358 // Test db should return test error string
359 MakeNextSdlRestCallFail()
360 err := mainCtrl.c.RemoveAllRESTSubscriptionsFromSdl()
362 if !strings.Contains(fmt.Sprintf("%s", err), sdlRestTestErrorString) {
363 t.Errorf("TEST: %s", err.Error())
367 t.Errorf("TEST: This test case should return error")
369 t.Log("TEST: All subscription removed from db")
372 func (m *RestSubsDbMock) Set(pairs ...interface{}) error {
377 defer m.marshalLock.Unlock()
379 if sdlRestShouldReturnError == true {
380 return GetSdlRestError()
383 for _, v := range pairs {
384 reflectType := reflect.TypeOf(v)
385 switch reflectType.Kind() {
387 val = fmt.Sprintf("%s", v.([]uint8))
393 return fmt.Errorf("Set() error: Unexpected type\n")
399 m.restSubsDb[key] = val
401 restSubscriptionInfo := &RESTSubscriptionInfo{}
402 err := json.Unmarshal([]byte(val), restSubscriptionInfo)
404 return fmt.Errorf("Set() json.unmarshal error: %s\n", err.Error())
407 restSubs := mainCtrl.c.CreateRESTSubscription(restSubscriptionInfo, &val)
408 m.restSubscriptions[restSubId] = restSubs
410 return fmt.Errorf("Set() error: key == ''\n")
415 func (m *RestSubsDbMock) Get(keys []string) (map[string]interface{}, error) {
416 retMap := make(map[string]interface{})
418 return nil, fmt.Errorf("Get() error: len(key) == 0\n")
421 if sdlRestShouldReturnError == true {
422 return nil, GetSdlRestError()
425 for _, key := range keys {
427 retMap[key] = m.restSubsDb[key]
429 return nil, fmt.Errorf("Get() error: key == ''\n")
435 func (m *RestSubsDbMock) GetAll() ([]string, error) {
437 if sdlRestShouldReturnError == true {
438 return nil, GetSdlRestError()
442 for key, _ := range m.restSubsDb {
443 keys = append(keys, key)
448 func (m *RestSubsDbMock) Remove(keys []string) error {
450 return fmt.Errorf("Remove() error: len(key) == 0\n")
453 if sdlRestShouldReturnError == true {
454 return GetSdlRestError()
458 delete(m.restSubsDb, restSubId)
459 delete(m.restSubscriptions, restSubId)
463 func (m *RestSubsDbMock) RemoveAll() error {
465 for key := range m.restSubsDb {
468 delete(m.restSubsDb, restSubId)
469 delete(m.restSubscriptions, restSubId)
472 if sdlRestShouldReturnError == true {
473 return GetSdlRestError()
479 func MakeNextSdlRestCallFail() {
480 sdlRestShouldReturnError = true
483 func GetSdlRestError() error {
484 sdlRestShouldReturnError = false
485 return fmt.Errorf(sdlRestTestErrorString)