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 ==================================================================================
32 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
33 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststube2ap"
34 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
35 "github.com/stretchr/testify/assert"
38 var sdlShouldReturnError bool = false
40 const sdlTestErrorString string = "Test sdl returned error on purpose"
49 subsDB map[string]string // Store information as a string like real db does.
50 register map[uint32]*Subscription
52 lastAllocatedSubId uint32
53 marshalLock sync.Mutex
58 func CreateMock() *Mock {
59 fmt.Println("Test CreateMock()")
61 mock.ResetTestSettings()
65 func (m *Mock) ResetTestSettings() {
66 m.subsDB = make(map[string]string)
67 m.register = make(map[uint32]*Subscription)
69 for i = 1; i < 65535; i++ {
70 m.subIds = append(m.subIds, i)
74 func (m *Mock) AllocNextSubId() uint32 {
75 m.lastAllocatedSubId = m.subIds[0]
76 return m.lastAllocatedSubId
79 func TestWait(t *testing.T) {
80 // Wait to test settings to complete
81 <-time.After(1 * time.Second)
84 func GetSubscription(t *testing.T, e2SubId uint32, responseType int, srcEndPoint, ranName string, xId string) *Subscription {
85 t.Log("TEST: Getting subscription")
87 subs := &Subscription{}
89 // Create unpacked e2SubReqMsg
90 subReqParams := &teststube2ap.E2StubSubsReqParams{}
93 meid := xapp.RMRMeid{}
94 meid.RanName = ranName
96 params := &xapp.RMRParams{}
97 params.Src = srcEndPoint
101 // Create xApp transaction
102 trans := mainCtrl.c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqParams.Req.RequestId, params.Meid)
104 t.Errorf("TEST: %s", idstring(fmt.Errorf("transaction not created"), params))
108 // Allocate E2 instanceId/subId
109 subReqParams.Req.RequestId.InstanceId = e2SubId
112 subs.ReqId.InstanceId = subReqParams.Req.RequestId.InstanceId
114 subs.EpList.AddEndpoint(trans.GetEndpoint())
115 subs.SubReqMsg = subReqParams.Req
116 // subs.SubRFMsg contains received/cached SubscriptionResponse or SubscriptionFailure, nil in no response received
117 if responseType == subsResponse {
118 subs.SubRFMsg = GetSubsResponse(t, subReqParams.Req)
120 } else if responseType == subsFailure {
121 subs.SubRFMsg = GetSubsFailure(t, subReqParams.Req)
123 } else if responseType == noResponse {
130 func GetSubsResponse(t *testing.T, req *e2ap.E2APSubscriptionRequest) *e2ap.E2APSubscriptionResponse {
131 t.Log("TEST: Getting ricSubscriptionResponse")
133 // Create e2SubRespMsg
134 resp := &e2ap.E2APSubscriptionResponse{}
135 resp.RequestId.Id = 123
136 resp.RequestId.InstanceId = req.RequestId.InstanceId
137 resp.FunctionId = req.FunctionId
139 resp.ActionAdmittedList.Items = make([]e2ap.ActionAdmittedItem, len(req.ActionSetups))
140 for index := int(0); index < len(req.ActionSetups); index++ {
141 resp.ActionAdmittedList.Items[index].ActionId = req.ActionSetups[index].ActionId
144 for index := uint64(0); index < 1; index++ {
145 item := e2ap.ActionNotAdmittedItem{}
146 item.ActionId = index
147 item.Cause.Content = 1
149 resp.ActionNotAdmittedList.Items = append(resp.ActionNotAdmittedList.Items, item)
154 func GetSubsFailure(t *testing.T, req *e2ap.E2APSubscriptionRequest) *e2ap.E2APSubscriptionFailure {
155 t.Log("TEST: Getting ricSubscriptionFailure")
157 fail := &e2ap.E2APSubscriptionFailure{}
158 fail.RequestId.Id = req.RequestId.Id
159 fail.RequestId.InstanceId = req.RequestId.InstanceId
160 fail.FunctionId = req.FunctionId
164 func PrintSubscriptionData(t *testing.T, subs *Subscription) {
165 t.Log("TEST: subscription data")
166 t.Logf("TEST: subs.mutex = %v", subs.mutex)
167 t.Logf("TEST: subs.ReqId.InstanceId = %v", subs.ReqId.InstanceId)
168 t.Logf("TEST: subs.ReqId.Id = %v", subs.ReqId.Id)
169 t.Logf("TEST: subs.EpList = %v", subs.EpList)
170 t.Logf("TEST: subs.Meid.RanName = %v", subs.Meid.RanName)
171 t.Logf("TEST: subs.SubReqMsg = %v", subs.SubReqMsg.String())
172 t.Logf("TEST: subs.valid = %v", subs.valid)
174 if subs.SubRFMsg != nil {
175 switch typeofSubsMessage(subs.SubRFMsg) {
177 t.Logf("TEST: subs.SubRFMsg == SubResp")
178 subResp := subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse)
179 t.Logf("TEST: subResp = %+v", subResp)
181 t.Logf("TEST: subs.SubRFMsg == SubFail")
182 subFail := subs.SubRFMsg.(*e2ap.E2APSubscriptionFailure)
183 t.Logf("TEST: subFail = %+v", subFail)
186 t.Logf("TEST: subs.SubRFMsg == nil")
190 func TestWriteSubscriptionToSdl(t *testing.T) {
192 // Write one subscription
193 subId := mock.AllocNextSubId()
194 subs := GetSubscription(t, subId, subsResponse, "localhost:13560", "RAN_NAME_1", "123456")
195 PrintSubscriptionData(t, subs)
196 t.Logf("TEST: Writing subId = %v\n", subId)
197 err := mainCtrl.c.WriteSubscriptionToSdl(subId, subs)
199 t.Errorf("TEST: %s", err.Error())
203 func TestReadSubscriptionFromSdl(t *testing.T) {
205 subId := mock.lastAllocatedSubId
206 t.Logf("Reading subId = %v\n", subId)
207 subs, err := mainCtrl.c.ReadSubscriptionFromSdl(subId)
209 t.Errorf("TEST: %s", err.Error())
212 PrintSubscriptionData(t, subs)
213 assert.Equal(t, mock.register[subId].SubReqMsg, subs.SubReqMsg)
216 func TestRemoveSubscriptionFromSdl(t *testing.T) {
218 subId := mock.lastAllocatedSubId
219 err := mainCtrl.c.RemoveSubscriptionFromSdl(subId)
221 t.Errorf("TEST: %s", err.Error())
224 delete(mock.register, subId)
225 mock.subIds = append(mock.subIds, subId)
226 t.Logf("TEST: subscription removed from db. subId = %v", subId)
229 func TestReadNotExistingSubscriptionFromSdl(t *testing.T) {
232 subs, err := mainCtrl.c.ReadSubscriptionFromSdl(subId)
234 t.Logf("TEST: subscription not found from db. subId = %v", subId)
237 t.Errorf("TEST: subscription read from db. %v", subs.String())
238 PrintSubscriptionData(t, subs)
241 func TestReadNotExistingSubscriptionFromSdl2(t *testing.T) {
244 subs, err := mainCtrl.c.ReadSubscriptionFromSdl(subId)
246 t.Logf("TEST: subscription not found from db. subId = %v", subId)
249 t.Errorf("TEST: subscription read from db. %v", subs.String())
250 PrintSubscriptionData(t, subs)
253 func TestRemoveNotExistingSubscriptionFromSdl(t *testing.T) {
256 err := mainCtrl.c.RemoveSubscriptionFromSdl(subId)
258 t.Logf("TEST: %s", err.Error())
261 t.Logf("TEST: subscription removed from db. subId = %v", subId)
264 func TestWriteSubscriptionsToSdl(t *testing.T) {
266 // Write 1st subscription
267 subId := mock.AllocNextSubId()
268 t.Logf("TEST: Writing subId = %v\n", subId)
269 subs := GetSubscription(t, subId, subsResponse, "localhost:13560", "RAN_NAME_1", "123456")
270 PrintSubscriptionData(t, subs)
271 err := mainCtrl.c.WriteSubscriptionToSdl(subId, subs)
273 t.Errorf("TEST: %s", err.Error())
276 t.Logf("TEST: subscription written in db = %v", subs.String())
278 // Write 2nd subscription
279 subId = mock.AllocNextSubId()
280 t.Logf("TEST:Writing subId = %v\n", subId)
281 subs = GetSubscription(t, subId, subsFailure, "localhost:13560", "RAN_NAME_2", "123457")
282 PrintSubscriptionData(t, subs)
283 err = mainCtrl.c.WriteSubscriptionToSdl(subId, subs)
285 t.Errorf("TEST: %s", err.Error())
288 t.Logf("TEST: subscription written in db = %v", subs.String())
290 // Write 3rd subscription
291 subId = mock.AllocNextSubId()
292 t.Logf("TEST:Writing subId = %v\n", subId)
293 subs = GetSubscription(t, subId, noResponse, "localhost:13560", "RAN_NAME_3", "123458")
294 PrintSubscriptionData(t, subs)
295 err = mainCtrl.c.WriteSubscriptionToSdl(subId, subs)
297 t.Errorf("TEST: %s", err.Error())
300 t.Logf("TEST: subscription written in db = %v", subs.String())
303 func TestReadSubscriptionsFromSdl(t *testing.T) {
305 // Subscription with subId 1 was added and and removed above. Then subscriptions with subIds 2, 3 and 4 was added
306 // Db subscriptions should now contain subIDs 2, 3 and 4
308 for subId = 2; subId <= 4; subId++ {
309 subs, err := mainCtrl.c.ReadSubscriptionFromSdl(subId)
311 t.Errorf("TEST: %s", err.Error())
314 PrintSubscriptionData(t, subs)
318 func TestReadAllSubscriptionsFromSdl(t *testing.T) {
320 // This test cases simulates submgr restart. SubIds and subscriptions are restored from db
321 // after initializing mock.subIds and mock.register
323 subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
325 t.Errorf("TEST: %s", err.Error())
328 // for _, subs := range mock.register {
329 for _, subs := range register {
330 PrintSubscriptionData(t, subs)
332 // SubIds slices before and after restart can't be directly compared as original slice is not stored
333 // in the db. SubId values 1, 2, 3, 4 are already removed from the beginning of subIds slice above
334 // so far. Next free subId is 5 in the beginning of mock.subIds slice. The db contains now however only
335 // 3 subscriptions with subIds 2, 3 and 4, so only subId values 2, 3, 4 are removed from the returned
336 // subIds slice and there next free value is 1
337 assert.Equal(t, uint32(0x1), subIds[0])
340 func TestRemoveAllSubscriptionsFromSdl(t *testing.T) {
342 err := mainCtrl.c.RemoveAllSubscriptionsFromSdl()
344 t.Errorf("TEST: %s", err.Error())
347 t.Log("TEST: All subscription removed from db")
350 func TestReadAllSubscriptionsFromSdl2(t *testing.T) {
352 // This test cases simulates submgr startup. SubIds and subscriptions are restored from empty db
353 // after initializing mock.subIds and mock.register
354 subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
356 t.Errorf("TEST: %s", err.Error())
359 for _, subs := range mock.register {
360 PrintSubscriptionData(t, subs)
362 assert.Equal(t, len(subIds), 65534)
363 assert.Equal(t, len(register), 0)
366 func TestWriteSubscriptionToSdlFail(t *testing.T) {
368 // Try to write one subscription. Test db should return test error string
369 MakeNextSdlCallFail()
370 subId := mock.AllocNextSubId()
371 subs := GetSubscription(t, subId, subsResponse, "localhost:13560", "RAN_NAME_1", "123456")
372 PrintSubscriptionData(t, subs)
373 t.Logf("TEST: Writing subId = %v\n", subId)
374 err := mainCtrl.c.WriteSubscriptionToSdl(subId, subs)
376 if !strings.Contains(fmt.Sprintf("%s", err), sdlTestErrorString) {
377 t.Errorf("TEST: %s", err.Error())
380 t.Errorf("TEST: This test case should return error")
384 func TestReadSubscriptionFromSdlFail(t *testing.T) {
386 // Try to read one subscription. Test db should return test error string
387 MakeNextSdlCallFail()
388 subId := mock.lastAllocatedSubId
389 t.Logf("Reading subId = %v\n", subId)
390 subs, err := mainCtrl.c.ReadSubscriptionFromSdl(subId)
392 if !strings.Contains(fmt.Sprintf("%s", err), sdlTestErrorString) {
393 t.Errorf("TEST: %s", err.Error())
397 t.Errorf("TEST: This test case should return error")
399 PrintSubscriptionData(t, subs)
400 assert.Equal(t, mock.register[subId].SubReqMsg, subs.SubReqMsg)
403 func TestRemoveSubscriptionFromSdlFail(t *testing.T) {
405 // Try to remove one subscription. Test db should return test error string
406 MakeNextSdlCallFail()
407 subId := mock.lastAllocatedSubId
408 err := mainCtrl.c.RemoveSubscriptionFromSdl(subId)
410 if !strings.Contains(fmt.Sprintf("%s", err), sdlTestErrorString) {
411 t.Errorf("TEST: %s", err.Error())
415 t.Errorf("TEST: This test case should return error")
417 delete(mock.register, subId)
418 mock.subIds = append(mock.subIds, subId)
419 t.Logf("TEST: subscription removed from db. subId = %v", subId)
422 func TestReadAllSubscriptionsFromSdlFail(t *testing.T) {
424 // Try to read all subscriptions. Test db should return test error string
425 MakeNextSdlCallFail()
426 // This test cases simulates submgr restart. SubIds and subscriptions are restored from db
427 // after initializing mock.subIds and mock.register
429 subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
431 if !strings.Contains(fmt.Sprintf("%s", err), sdlTestErrorString) {
432 t.Errorf("TEST: %s", err.Error())
436 t.Errorf("TEST: This test case should return error")
438 // for _, subs := range mock.register {
439 for _, subs := range register {
440 PrintSubscriptionData(t, subs)
442 // SubIds slices before and after restart can't be directly compared as original slice is not stored
443 // in the db. SubId values 1, 2, 3, 4 are already removed from the beginning of subIds slice above
444 // so far. Next free subId is 5 in the beginning of mock.subIds slice. The db contains now however only
445 // 3 subscriptions with subIds 2, 3 and 4, so only subId values 2, 3, 4 are removed from the returned
446 // subIds slice and there next free value is 1
447 assert.Equal(t, uint32(0x1), subIds[0])
450 func TestRemoveAllSubscriptionsFromSdlFail(t *testing.T) {
452 // Try to remove all subscriptions. Test db should return test error string
453 MakeNextSdlCallFail()
454 err := mainCtrl.c.RemoveAllSubscriptionsFromSdl()
456 if !strings.Contains(fmt.Sprintf("%s", err), sdlTestErrorString) {
457 t.Errorf("TEST: %s", err.Error())
461 t.Errorf("TEST: This test case should return error")
463 t.Log("TEST: All subscription removed from db")
466 func (m *Mock) Set(pairs ...interface{}) error {
471 defer m.marshalLock.Unlock()
473 if sdlShouldReturnError == true {
477 for _, v := range pairs {
478 reflectType := reflect.TypeOf(v)
479 switch reflectType.Kind() {
481 val = fmt.Sprintf("%s", v.([]uint8))
487 return fmt.Errorf("Set() error: Unexpected type\n")
495 subscriptionInfo := &SubscriptionInfo{}
496 err := json.Unmarshal([]byte(val), subscriptionInfo)
498 return fmt.Errorf("Set() json.unmarshal error: %s\n", err.Error())
501 subs := mainCtrl.c.CreateSubscription(subscriptionInfo, &val)
502 m.register[subId] = subs
503 m.subIds = m.subIds[1:]
505 return fmt.Errorf("Set() error: key == ''\n")
510 func (m *Mock) Get(keys []string) (map[string]interface{}, error) {
511 retMap := make(map[string]interface{})
513 return nil, fmt.Errorf("Get() error: len(key) == 0\n")
516 if sdlShouldReturnError == true {
517 return nil, GetSdlError()
520 for _, key := range keys {
522 retMap[key] = m.subsDB[key]
524 return nil, fmt.Errorf("Get() error: key == ''\n")
530 func (m *Mock) GetAll() ([]string, error) {
532 if sdlShouldReturnError == true {
533 return nil, GetSdlError()
537 for key, _ := range m.subsDB {
538 keys = append(keys, key)
543 func (m *Mock) Remove(keys []string) error {
545 return fmt.Errorf("Remove() error: len(key) == 0\n")
547 subId64, err := strconv.ParseUint(keys[0], 10, 64)
549 return fmt.Errorf("Remove() ParseUint() error: %s\n", err.Error())
552 if sdlShouldReturnError == true {
556 subId := uint32(subId64)
557 delete(m.subsDB, keys[0])
558 delete(m.register, subId)
559 m.subIds = append(m.subIds, subId)
563 func (m *Mock) RemoveAll() error {
565 for key := range m.subsDB {
566 subId64, err := strconv.ParseUint(key, 10, 64)
568 return fmt.Errorf("RemoveAll() ParseUint() error: %s\n", err.Error())
571 subId := uint32(subId64)
572 delete(m.subsDB, key)
573 delete(m.register, subId)
574 m.subIds = append(m.subIds, subId)
577 if sdlShouldReturnError == true {
584 func MakeNextSdlCallFail() {
585 sdlShouldReturnError = true
588 func GetSdlError() error {
589 sdlShouldReturnError = false
590 return fmt.Errorf(sdlTestErrorString)