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 ==================================================================================
25 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
26 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststube2ap"
27 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
28 "github.com/stretchr/testify/assert"
42 subsDB map[string]string // Store information as a string like real db does.
43 register map[uint32]*Subscription
45 lastAllocatedSubId uint32
50 func CreateMock() *Mock {
51 fmt.Println("Test CreateMock()")
53 mock.ResetTestSettings()
57 func (m *Mock) ResetTestSettings() {
58 m.subsDB = make(map[string]string)
59 m.register = make(map[uint32]*Subscription)
61 for i = 1; i < 65535; i++ {
62 m.subIds = append(m.subIds, i)
66 func (m *Mock) AllocNextSubId() uint32 {
67 m.lastAllocatedSubId = m.subIds[0]
68 return m.lastAllocatedSubId
71 func TestWait(t *testing.T) {
72 // Wait to test settings to complete
73 <-time.After(1 * time.Second)
76 func GetSubscription(t *testing.T, e2SubId uint32, responseType int, srcEndPoint, ranName string, xId string) *Subscription {
77 t.Log("TEST: Getting subscription")
79 subs := &Subscription{}
81 // Create unpacked e2SubReqMsg
82 subReqParams := &teststube2ap.E2StubSubsReqParams{}
85 meid := xapp.RMRMeid{}
86 meid.RanName = ranName
88 params := &xapp.RMRParams{}
89 params.Src = srcEndPoint
93 // Create xApp transaction
94 trans := mainCtrl.c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqParams.Req.RequestId, params.Meid)
96 t.Errorf("TEST: %s", idstring(fmt.Errorf("transaction not created"), params))
100 // Allocate E2 instanceId/subId
101 subReqParams.Req.RequestId.InstanceId = e2SubId
104 subs.ReqId.InstanceId = subReqParams.Req.RequestId.InstanceId
106 subs.EpList.AddEndpoint(trans.GetEndpoint())
107 subs.SubReqMsg = subReqParams.Req
108 // subs.SubRFMsg contains received/cached SubscriptionResponse or SubscriptionFailure, nil in no response received
109 if responseType == subsResponse {
110 subs.SubRFMsg = GetSubsResponse(t, subReqParams.Req)
112 } else if responseType == subsFailure {
113 subs.SubRFMsg = GetSubsFailure(t, subReqParams.Req)
115 } else if responseType == noResponse {
122 func GetSubsResponse(t *testing.T, req *e2ap.E2APSubscriptionRequest) *e2ap.E2APSubscriptionResponse {
123 t.Log("TEST: Getting ricSubscriptionResponse")
125 // Create e2SubRespMsg
126 resp := &e2ap.E2APSubscriptionResponse{}
127 resp.RequestId.Id = 123
128 resp.RequestId.InstanceId = req.RequestId.InstanceId
129 resp.FunctionId = req.FunctionId
131 resp.ActionAdmittedList.Items = make([]e2ap.ActionAdmittedItem, len(req.ActionSetups))
132 for index := int(0); index < len(req.ActionSetups); index++ {
133 resp.ActionAdmittedList.Items[index].ActionId = req.ActionSetups[index].ActionId
136 for index := uint64(0); index < 1; index++ {
137 item := e2ap.ActionNotAdmittedItem{}
138 item.ActionId = index
139 item.Cause.Content = 1
141 resp.ActionNotAdmittedList.Items = append(resp.ActionNotAdmittedList.Items, item)
146 func GetSubsFailure(t *testing.T, req *e2ap.E2APSubscriptionRequest) *e2ap.E2APSubscriptionFailure {
147 t.Log("TEST: Getting ricSubscriptionFailure")
149 fail := &e2ap.E2APSubscriptionFailure{}
150 fail.RequestId.Id = req.RequestId.Id
151 fail.RequestId.InstanceId = req.RequestId.InstanceId
152 fail.FunctionId = req.FunctionId
156 func PrintSubscriptionData(t *testing.T, subs *Subscription) {
157 t.Log("TEST: subscription data")
158 t.Logf("TEST: subs.mutex = %v", subs.mutex)
159 t.Logf("TEST: subs.ReqId.InstanceId = %v", subs.ReqId.InstanceId)
160 t.Logf("TEST: subs.ReqId.Id = %v", subs.ReqId.Id)
161 t.Logf("TEST: subs.EpList = %v", subs.EpList)
162 t.Logf("TEST: subs.Meid.RanName = %v", subs.Meid.RanName)
163 t.Logf("TEST: subs.SubReqMsg = %v", subs.SubReqMsg.String())
164 t.Logf("TEST: subs.valid = %v", subs.valid)
166 if subs.SubRFMsg != nil {
167 switch typeofSubsMessage(subs.SubRFMsg) {
169 t.Logf("TEST: subs.SubRFMsg == SubResp")
170 subResp := subs.SubRFMsg.(*e2ap.E2APSubscriptionResponse)
171 t.Logf("TEST: subResp = %+v", subResp)
173 t.Logf("TEST: subs.SubRFMsg == SubFail")
174 subFail := subs.SubRFMsg.(*e2ap.E2APSubscriptionFailure)
175 t.Logf("TEST: subFail = %+v", subFail)
178 t.Logf("TEST: subs.SubRFMsg == nil")
182 func TestWriteSubscriptionToSdl(t *testing.T) {
183 t.Log("TestWriteSubscriptionToSdl")
185 // Write one subscription
186 subId := mock.AllocNextSubId()
187 subs := GetSubscription(t, subId, subsResponse, "localhost:13560", "RAN_NAME_1", "123456")
188 PrintSubscriptionData(t, subs)
189 t.Logf("TEST: Writing subId = %v\n", subId)
190 err := mainCtrl.c.WriteSubscriptionToSdl(subId, subs)
192 t.Errorf("TEST: %s", err.Error())
197 func TestReadSubscriptionFromSdl(t *testing.T) {
198 t.Log("TestReadSubscriptionFromSdl")
200 subId := mock.lastAllocatedSubId
201 t.Logf("Reading subId = %v\n", subId)
202 subs, err := mainCtrl.c.ReadSubscriptionFromSdl(subId)
204 t.Errorf("TEST: %s", err.Error())
207 PrintSubscriptionData(t, subs)
208 assert.Equal(t, mock.register[subId].SubReqMsg, subs.SubReqMsg)
211 func TestRemoveSubscriptionFromSdl(t *testing.T) {
212 t.Log("TestRemoveSubscriptionFromSdl")
214 subId := mock.lastAllocatedSubId
215 err := mainCtrl.c.RemoveSubscriptionFromSdl(subId)
217 t.Errorf("TEST: %s", err.Error())
220 delete(mock.register, subId)
221 mock.subIds = append(mock.subIds, subId)
222 t.Logf("TEST: subscription removed from db. subId = %v", subId)
225 func TestReadNotExistingSubscriptionFromSdl(t *testing.T) {
226 t.Log("TestReadNotExistingSubscriptionFromSdl")
229 subs, err := mainCtrl.c.ReadSubscriptionFromSdl(subId)
231 t.Logf("TEST: subscription not found from db. subId = %v", subId)
234 t.Errorf("TEST: subscription read from db. %v", subs.String())
235 PrintSubscriptionData(t, subs)
238 func TestReadNotExistingSubscriptionFromSdl2(t *testing.T) {
239 t.Log("TestReadNotExistingSubscriptionFromSdl")
242 subs, err := mainCtrl.c.ReadSubscriptionFromSdl(subId)
244 t.Logf("TEST: subscription not found from db. subId = %v", subId)
247 t.Errorf("TEST: subscription read from db. %v", subs.String())
248 PrintSubscriptionData(t, subs)
251 func TestRemoveNotExistingSubscriptionFromSdl(t *testing.T) {
252 t.Log("TestRemoveNotExistingSubscriptionFromSdl")
255 err := mainCtrl.c.RemoveSubscriptionFromSdl(subId)
257 t.Logf("TEST: %s", err.Error())
260 t.Logf("TEST: subscription removed from db. subId = %v", subId)
263 func TestWriteSubscriptionsToSdl(t *testing.T) {
264 t.Log("TestWriteSubscriptionsToSdl")
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) {
304 t.Log("TestReadSubscriptionsFromSdl")
306 // Subscription with subId 1 was added and and removed above. Then subscriptions with subIds 2, 3 and 4 was added
307 // Db subscriptions should now contain subIDs 2, 3 and 4
309 for subId = 2; subId <= 4; subId++ {
310 subs, err := mainCtrl.c.ReadSubscriptionFromSdl(subId)
312 t.Errorf("TEST: %s", err.Error())
315 PrintSubscriptionData(t, subs)
319 func TestReadAllSubscriptionsFromSdl(t *testing.T) {
320 t.Log("TestReadAllSubscriptionsFromSdl")
322 // This test cases simulates submgr restart. SubIds and subscriptions are restored from db
323 // after initializing mock.subIds and mock.register
325 subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
327 t.Errorf("TEST: %s", err.Error())
330 // for _, subs := range mock.register {
331 for _, subs := range register {
332 PrintSubscriptionData(t, subs)
334 // SubIds slices before and after restart can't be directly compared as original slice is not stored
335 // in the db. SubId values 1, 2, 3, 4 are already removed from the beginning of subIds slice above
336 // so far. Next free subId is 5 in the beginning of mock.subIds slice. The db contains now however only
337 // 3 subscriptions with subIds 2, 3 and 4, so only subId values 2, 3, 4 are removed from the returned
338 // subIds slice and there next free value is 1
339 assert.Equal(t, uint32(0x1), subIds[0])
342 func TestRemoveAllSubscriptionsFromSdl(t *testing.T) {
343 t.Log("TestRemoveAllSubscriptionsFromSdl")
345 err := mainCtrl.c.RemoveAllSubscriptionsFromSdl()
347 t.Errorf("TEST: %s", err.Error())
350 t.Log("TEST: All subscription removed from db")
353 func TestReadAllSubscriptionsFromSdl2(t *testing.T) {
354 t.Log("TestReadAllSubscriptionsFromSdl2")
356 // This test cases simulates submgr startup. SubIds and subscriptions are restored from empty db
357 // after initializing mock.subIds and mock.register
358 subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
360 t.Errorf("TEST: %s", err.Error())
363 for _, subs := range mock.register {
364 PrintSubscriptionData(t, subs)
366 assert.Equal(t, len(subIds), 65534)
367 assert.Equal(t, len(register), 0)
370 func (m *Mock) Set(pairs ...interface{}) error {
374 for _, v := range pairs {
375 reflectType := reflect.TypeOf(v)
376 switch reflectType.Kind() {
378 val = fmt.Sprintf("%s", v.([]uint8))
384 return fmt.Errorf("Set() error: Unexpected type\n")
392 subscriptionInfo := &SubscriptionInfo{}
393 err := json.Unmarshal([]byte(val), subscriptionInfo)
395 return fmt.Errorf("Set() json.unmarshal error: %s\n", err.Error())
398 subs := mainCtrl.c.CreateSubscription(subscriptionInfo, &val)
399 m.register[subId] = subs
400 m.subIds = m.subIds[1:]
402 return fmt.Errorf("Set() error: key == ''\n")
407 func (m *Mock) Get(keys []string) (map[string]interface{}, error) {
408 retMap := make(map[string]interface{})
410 return nil, fmt.Errorf("Get() error: len(key) == 0\n")
413 for _, key := range keys {
415 retMap[key] = m.subsDB[key]
417 return nil, fmt.Errorf("Get() error: key == ''\n")
423 func (m *Mock) GetAll() ([]string, error) {
426 for key, _ := range m.subsDB {
427 keys = append(keys, key)
432 func (m *Mock) Remove(keys []string) error {
434 return fmt.Errorf("Remove() error: len(key) == 0\n")
436 subId64, err := strconv.ParseUint(keys[0], 10, 64)
438 return fmt.Errorf("Remove() ParseUint() error: %s\n", err.Error())
440 subId := uint32(subId64)
441 delete(m.subsDB, keys[0])
442 delete(m.register, subId)
443 m.subIds = append(m.subIds, subId)
447 func (m *Mock) RemoveAll() error {
448 for key := range m.subsDB {
449 subId64, err := strconv.ParseUint(key, 10, 64)
451 return fmt.Errorf("RemoveAll() ParseUint() error: %s\n", err.Error())
453 subId := uint32(subId64)
454 delete(m.subsDB, key)
455 delete(m.register, subId)
456 m.subIds = append(m.subIds, subId)