RIC-851: Updated submgr to trigger Delete Subscription Procedure
[ric-plt/submgr.git] / pkg / control / ut_ric_subs_del_reqd_test.go
1 package control
2
3 import (
4         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
5         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
6         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
7         "github.com/stretchr/testify/assert"
8         "testing"
9         "time"
10 )
11
12 func initMock() *Control {
13
14         var payload = []byte{0, 8, 0, 50, 0, 0, 3, 0, 29, 0, 5, 0, 0, 3, 0, 159, 0, 5, 0, 2, 0, 2, 0, 30, 0, 28, 0, 7, 8, 0, 0, 5, 0, 0, 0, 0, 0, 19, 64, 14, 64, 122, 32, 10, 0, 1, 4, 64, 0, 0, 0, 0, 0, 23}
15         var payload1 = []byte{0, 8, 0, 50, 0, 0, 3, 0, 29, 0, 5, 0, 0, 3, 0, 160, 0, 5, 0, 2, 0, 2, 0, 30, 0, 28, 0, 7, 8, 0, 0, 5, 0, 0, 0, 0, 0, 19, 64, 14, 64, 122, 32, 10, 0, 2, 4, 64, 0, 0, 0, 0, 0, 23}
16         meid := &xapp.RMRMeid{}
17         meid.RanName = "RAN_NAME_20"
18         var params = &xapp.RMRParams{
19                 Mtype:      12023,
20                 Payload:    payload,
21                 PayloadLen: len(payload),
22                 Meid:       meid,
23                 Xid:        "457945551669",
24                 SubId:      -1,
25                 Src:        "service-ricplt-e2term-rmr-alpha.ricplt:38000",
26                 Mbuf:       nil,
27                 Whid:       0,
28                 Callid:     0,
29                 Timeout:    0,
30         }
31         C1 := &Control{
32                 RMRClient:         mainCtrl.c.RMRClient,
33                 e2ap:              mainCtrl.c.e2ap,
34                 registry:          mainCtrl.c.registry,
35                 tracker:           mainCtrl.c.tracker,
36                 restDuplicateCtrl: mainCtrl.c.restDuplicateCtrl,
37                 e2IfState:         mainCtrl.c.e2IfState,
38                 e2IfStateDb:       mainCtrl.c.e2IfStateDb,
39                 e2SubsDb:          mainCtrl.c.e2SubsDb,
40                 restSubsDb:        mainCtrl.c.restSubsDb,
41                 CntRecvMsg:        mainCtrl.c.CntRecvMsg,
42                 ResetTestFlag:     mainCtrl.c.ResetTestFlag,
43                 Counters:          mainCtrl.c.Counters,
44                 LoggerLevel:       mainCtrl.c.LoggerLevel,
45                 UTTesting:         mainCtrl.c.UTTesting,
46         }
47
48         subReqMsg, _ := C1.e2ap.UnpackSubscriptionRequest(params.Payload)
49         subReqMsg1, _ := C1.e2ap.UnpackSubscriptionRequest(payload1)
50
51         trans := C1.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg.RequestId, params.Meid)
52         trans1 := C1.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subReqMsg1.RequestId, params.Meid)
53
54         for _, acts := range subReqMsg.ActionSetups {
55                 acts.ActionType = e2ap.E2AP_ActionTypeInsert
56         }
57         for _, acts := range subReqMsg1.ActionSetups {
58                 acts.ActionType = e2ap.E2AP_ActionTypeInsert
59         }
60         _, _, _ = C1.registry.AssignToSubscription(trans, subReqMsg, C1.ResetTestFlag, C1, true)
61         _, _, _ = C1.registry.AssignToSubscription(trans1, subReqMsg1, C1.ResetTestFlag, C1, true)
62
63         controlObj := testingSubmgrControl{
64                 RmrControl: teststub.RmrControl{},
65                 c:          C1,
66         }
67         handler := controlObj.c
68         return handler
69 }
70
71 func TestControl_handleE2TSubscriptionDeleteRequired(t *testing.T) {
72         handler := initMock()
73         var payload = []byte{0, 12, 64, 20, 0, 0, 1, 0, 50, 64, 13, 1, 0, 51, 64, 8, 0, 0, 123, 0, 1, 0, 2, 86}
74         meid := &xapp.RMRMeid{}
75         meid.RanName = "RAN_NAME_20"
76         var params = &xapp.RMRParams{
77                 Mtype:      12023,
78                 Payload:    payload,
79                 PayloadLen: len(payload),
80                 Meid:       meid,
81                 Xid:        "457945551669",
82                 SubId:      -1,
83                 Src:        "service-ricplt-e2term-rmr-alpha.ricplt:38000",
84                 Mbuf:       nil,
85                 Whid:       0,
86                 Callid:     0,
87                 Timeout:    0,
88         }
89         type fields struct {
90                 RMRClient         *xapp.RMRClient
91                 e2ap              *E2ap
92                 registry          *Registry
93                 tracker           *Tracker
94                 restDuplicateCtrl *DuplicateCtrl
95                 e2IfState         *E2IfState
96                 e2IfStateDb       XappRnibInterface
97                 e2SubsDb          Sdlnterface
98                 restSubsDb        Sdlnterface
99                 CntRecvMsg        uint64
100                 ResetTestFlag     bool
101                 Counters          map[string]xapp.Counter
102                 LoggerLevel       int
103                 UTTesting         bool
104         }
105         type args struct {
106                 params *xapp.RMRParams
107         }
108         tests := []struct {
109                 name   string
110                 fields fields
111                 args   args
112         }{
113                 {
114                         name: "abc",
115                         fields: fields{
116                                 RMRClient:         handler.RMRClient,
117                                 e2ap:              handler.e2ap,
118                                 registry:          handler.registry,
119                                 tracker:           handler.tracker,
120                                 restDuplicateCtrl: handler.restDuplicateCtrl,
121                                 e2IfState:         handler.e2IfState,
122                                 e2IfStateDb:       handler.e2IfStateDb,
123                                 e2SubsDb:          handler.e2SubsDb,
124                                 restSubsDb:        handler.restSubsDb,
125                                 CntRecvMsg:        handler.CntRecvMsg,
126                                 ResetTestFlag:     handler.ResetTestFlag,
127                                 Counters:          handler.Counters,
128                                 LoggerLevel:       handler.LoggerLevel,
129                                 UTTesting:         handler.UTTesting,
130                         },
131
132                         args: args{params: params},
133                 },
134         }
135         for _, tt := range tests {
136                 t.Run(tt.name, func(t *testing.T) {
137                         c := &Control{
138                                 RMRClient:         tt.fields.RMRClient,
139                                 e2ap:              tt.fields.e2ap,
140                                 registry:          tt.fields.registry,
141                                 tracker:           tt.fields.tracker,
142                                 restDuplicateCtrl: tt.fields.restDuplicateCtrl,
143                                 e2IfState:         tt.fields.e2IfState,
144                                 e2IfStateDb:       tt.fields.e2IfStateDb,
145                                 e2SubsDb:          tt.fields.e2SubsDb,
146                                 restSubsDb:        tt.fields.restSubsDb,
147                                 CntRecvMsg:        tt.fields.CntRecvMsg,
148                                 ResetTestFlag:     tt.fields.ResetTestFlag,
149                                 Counters:          tt.fields.Counters,
150                                 LoggerLevel:       tt.fields.LoggerLevel,
151                                 UTTesting:         tt.fields.UTTesting,
152                         }
153                         c.e2IfState.NbIdMap[params.Meid.RanName] = "_CONNECTED"
154                         c.handleE2TSubscriptionDeleteRequired(tt.args.params)
155                         subs, _ := c.registry.GetSubscriptionFirstMatch([]uint32{uint32(1)})
156                         subs1, _ := c.registry.GetSubscriptionFirstMatch([]uint32{uint32(2)})
157                         assert.Nil(t, subs)
158                         assert.NotNil(t, subs1)
159                         time.Sleep(1 * time.Second)
160                 })
161         }
162 }
163
164 func TestE2ap_UnpackSubscriptionDeleteRequired(t *testing.T) {
165         var payload = []byte{0, 12, 64, 20, 0, 0, 1, 0, 50, 64, 13, 1, 0, 51, 64, 8, 0, 0, 123, 0, 1, 0, 2, 86}
166         var e2ap1 = E2ap{}
167         list, _ := e2ap1.UnpackSubscriptionDeleteRequired(payload)
168         expectedList := &e2ap.SubscriptionDeleteRequiredList{E2APSubscriptionDeleteRequiredRequests: []e2ap.E2APSubscriptionDeleteRequired{{
169                 RequestId: e2ap.RequestId{
170                         Id:         123,
171                         InstanceId: 1,
172                 },
173                 FunctionId: 2,
174                 Cause: e2ap.Cause{
175                         Content: 6,
176                         Value:   3,
177                 },
178         }}}
179         assert.Equal(t, expectedList, list)
180 }
181 func TestE2ap_UnpackSubscriptionDeleteRequiredForWrongPayload(t *testing.T) {
182         var payload = []byte{12, 64, 20, 0, 0, 1, 0, 50, 64, 13, 1, 0, 51, 64, 8, 0, 0, 123, 0, 1, 0, 2, 86}
183         var e2ap1 = E2ap{}
184         _, err := e2ap1.UnpackSubscriptionDeleteRequired(payload)
185         assert.NotNil(t, err)
186
187 }
188
189 func TestE2ap_PackSubscriptionDeleteRequired(t *testing.T) {
190         list := &e2ap.SubscriptionDeleteRequiredList{E2APSubscriptionDeleteRequiredRequests: []e2ap.E2APSubscriptionDeleteRequired{{
191                 RequestId: e2ap.RequestId{
192                         Id:         123,
193                         InstanceId: 1,
194                 },
195                 FunctionId: 2,
196                 Cause: e2ap.Cause{
197                         Content: 6,
198                         Value:   3,
199                 },
200         }}}
201         e2ap1 := E2ap{}
202         payload1 := []byte{0, 12, 64, 20, 0, 0, 1, 0, 50, 64, 13, 1, 0, 51, 64, 8, 0, 0, 123, 0, 1, 0, 2, 86}
203         payload := &e2ap.PackedData{Buf: payload1}
204         _, packedata, _ := e2ap1.PackSubscriptionDeleteRequired(list)
205         assert.Equal(t, payload, packedata)
206 }