1de62935c388bdbf85df8b0189d5601ea3dadbfa
[ric-plt/rtmgr.git] / pkg / nbi / httprestful_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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
18
19    This source code is part of the near-RT RIC (RAN Intelligent Controller)
20    platform project (RICP).
21
22 ==================================================================================
23 */
24 /*
25   Mnemonic:     httprestful_test.go
26   Abstract:     HTTPRestful unit tests
27   Date:         15 May 2019
28 */
29
30 package nbi
31
32 import (
33         "encoding/json"
34         "fmt"
35         xfmodel "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
36         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
37         "github.com/go-openapi/swag"
38         "github.com/spf13/viper"
39         "io/ioutil"
40         "net"
41         "net/http"
42         "net/http/httptest"
43         "os"
44         "routing-manager/pkg/models"
45         "routing-manager/pkg/rpe"
46         "routing-manager/pkg/rtmgr"
47         "routing-manager/pkg/sbi"
48         "routing-manager/pkg/sdl"
49         "routing-manager/pkg/stub"
50         "sync"
51         "testing"
52         "time"
53 )
54
55 var BasicXAppLists = []byte(`[
56  {
57  "name":"xapp-01","status":"unknown","version":"1.2.3",
58     "instances":[
59         {"name":"xapp-01-instance-01","status":"pending","ip":"172.16.1.103","port":4555,
60             "txMessages":["ControlIndication"],
61             "rxMessages":["LoadIndication","Reset"]
62         },
63         {"name":"xapp-01-instance-02","status":"pending","ip":"10.244.1.12","port":4561,
64             "txMessages":["ControlIndication","SNStatusTransfer"],
65             "rxMessages":["LoadIndication","HandoverPreparation"]
66         }
67     ]
68 }
69 ]`)
70
71 var SubscriptionResp = []byte(`{"ID":"deadbeef1234567890", "Version":0, "EventType":"all"}`)
72
73 var E2TListResp = []byte(`[{"e2tAddress":"127.0.0.1:0","ranNames":["RanM0","RanN0"]},{"e2tAddress":"127.0.0.1:1","ranNames":["RanM1","RanN1"]},{"e2tAddress":"127.0.0.1:2","ranNames":["RanM2","RanN2"]},{"e2tAddress":"127.0.0.1:3","ranNames":["RanM3","RanN3"]}]`)
74
75 var SubscriptionList = []byte(`[{"SubscriptionId":11,"Meid":"Test-Gnb","Endpoint":["127.0.0.1:4056"]}]`)
76
77 var InvalidSubResp = []byte(`{"Version":0, "EventType":all}`)
78
79 type Consumer struct{}
80
81 func (m Consumer) Consume(params *xapp.RMRParams) (err error) {
82         xapp.Sdl.Store("myKey", params.Payload)
83         return nil
84 }
85
86 // Test cases
87 func TestMain(m *testing.M) {
88         go xapp.RunWithParams(Consumer{}, viper.GetBool("db.waitForSdl"))
89         time.Sleep(time.Duration(5) * time.Second)
90         code := m.Run()
91         os.Exit(code)
92 }
93
94 func TestValidateXappCallbackData_1(t *testing.T) {
95         data := models.XappCallbackData{
96                 XApps:   *swag.String("[]"),
97                 Version: *swag.Int64(1),
98                 Event:   *swag.String("someevent"),
99                 ID:      *swag.String("123456")}
100
101         err := validateXappCallbackData(&data)
102         if err != nil {
103                 t.Error("Invalid XApp callback data: " + err.Error())
104         }
105 }
106
107 func TestValidateXappCallbackDataInvalid(t *testing.T) {
108         data := models.XappCallbackData{}
109         err := validateXappCallbackData(&data)
110         t.Log(err)
111 }
112
113 func TestValidateXappSubscriptionsData(t *testing.T) {
114
115         rtmgr.RMRConnStatus = make(map[string]bool)
116         ep := make(map[string]*rtmgr.Endpoint)
117         ep["dummy"] = &rtmgr.Endpoint{Uuid: "10.0.0.1:0", Name: "E2TERM", XAppType: "app1", Ip: "", Port: 0, TxMessages: []string{"", ""}, RxMessages: []string{"", ""}, Socket: nil, IsReady: true, Keepalive: true}
118         p := uint16(1234)
119         data := models.XappSubscriptionData{
120                 Address:        swag.String("10.1.1.1"),
121                 Port:           &p,
122                 SubscriptionID: swag.Int32(123456)}
123
124         var err error
125         err = validateXappSubscriptionData(&data)
126         t.Log(err)
127
128         rtmgr.Eps = ep
129         p = uint16(0)
130         data1 := models.XappSubscriptionData{
131                 Address:        swag.String(""),
132                 Port:           &p,
133                 SubscriptionID: swag.Int32(123456)}
134         err = validateXappSubscriptionData(&data1)
135
136         //Validate E2tData
137         data2 := models.E2tData{
138                 E2TAddress: swag.String(""),
139         }
140         /*err = validateE2tData(&data2)*/
141
142         //e2tchannel := make(chan *models.E2tData, 10)
143         _ = CreateNewE2tHandleHandlerImpl(&data2)
144         //defer close(e2tchannel)
145
146         //test case for provideXappSubscriptionHandleImp
147         //datachannel := make(chan *models.XappSubscriptionData, 10)
148         sdlEngine, _ = sdl.GetSdl("file")
149         sbiEngine, _ = sbi.GetSbi("rmrpush")
150         rpeEngine, _ = rpe.GetRpe("rmrpush")
151         _ = ProvideXappSubscriptionHandleImpl(&data1)
152         //defer close(datachannel)
153
154         //test case for deleteXappSubscriptionHandleImpl
155         _ = DeleteXappSubscriptionHandleImpl(&data1)
156
157         data3 := models.XappSubscriptionData{
158                 Address:        swag.String("10.55.55.5"),
159                 Port:           &p,
160                 SubscriptionID: swag.Int32(123456)}
161         //test case for deleteXappSubscriptionHandleImpl
162         _ = DeleteXappSubscriptionHandleImpl(&data3)
163         data4 := models.XappSubscriptionData{
164                 Address:        swag.String("1.5.5.5"),
165                 Port:           &p,
166                 SubscriptionID: swag.Int32(1236)}
167         _ = DeleteXappSubscriptionHandleImpl(&data4)
168
169 }
170
171 func TestValidateE2tDataEmpty(t *testing.T) {
172         data := models.E2tData{
173                 E2TAddress: swag.String(""),
174         }
175         err, _ := validateE2tData(&data)
176         t.Log(err)
177 }
178
179 func TestValidateE2tDataDNSLookUPfails(t *testing.T) {
180         rtmgr.RMRConnStatus = make(map[string]bool)
181         data := models.E2tData{
182                 E2TAddress: swag.String("e2t.1com:1234"),
183         }
184         err, _ := validateE2tData(&data)
185         t.Log(err)
186 }
187
188 func TestValidateE2tDataInvalid(t *testing.T) {
189         rtmgr.RMRConnStatus = make(map[string]bool)
190         data := models.E2tData{
191                 E2TAddress: swag.String("10.101.01.1"),
192         }
193         err, _ := validateE2tData(&data)
194         t.Log(err)
195 }
196
197 func TestValidateE2tDatavalid(t *testing.T) {
198         rtmgr.RMRConnStatus = make(map[string]bool)
199         data := models.E2tData{
200                 E2TAddress: swag.String("10.101.01.1:8098"),
201         }
202
203         err, _ := validateE2tData(&data)
204         t.Log(err)
205
206         _ = CreateNewE2tHandleHandlerImpl(&data)
207
208 }
209
210 func TestValidateE2tDatavalidEndpointPresent(t *testing.T) {
211         rtmgr.RMRConnStatus = make(map[string]bool)
212         data := models.E2tData{
213                 E2TAddress: swag.String("10.101.01.1:8098"),
214         }
215
216         // insert endpoint for testing purpose
217         uuid := "10.101.01.1:8098"
218         ep := &rtmgr.Endpoint{
219                 Uuid: uuid,
220         }
221         rtmgr.Eps[uuid] = ep
222
223         err, _ := validateE2tData(&data)
224         t.Log(err)
225
226         // delete endpoint for at end of test case
227         delete(rtmgr.Eps, uuid)
228
229 }
230
231 func TestValidateDeleteE2tData(t *testing.T) {
232
233         rtmgr.RMRConnStatus = make(map[string]bool)
234         // test-1
235         data := models.E2tDeleteData{
236                 E2TAddress: swag.String(""),
237         }
238
239         err := validateDeleteE2tData(&data)
240         if err.Error() != "E2TAddress is empty!!!" {
241                 t.Log(err)
242         }
243
244         // test-2
245         data = models.E2tDeleteData{
246                 E2TAddress: swag.String("10.101.01.1:8098"),
247         }
248
249         err = validateDeleteE2tData(&data)
250         if err != nil {
251                 t.Log(err)
252         }
253
254         // test-3
255         //################ Create End Point dummy entry
256         uuid := "10.101.01.1:8098"
257         ep := &rtmgr.Endpoint{
258                 Uuid: uuid,
259         }
260         rtmgr.Eps[uuid] = ep
261         //#####################
262
263         data = models.E2tDeleteData{
264                 E2TAddress: swag.String("10.101.01.1:8098"),
265                 RanAssocList: models.RanE2tMap{
266                         {E2TAddress: swag.String("10.101.01.1:8098")},
267                 },
268         }
269
270         err = validateDeleteE2tData(&data)
271         if err != nil {
272                 t.Log(err)
273         }
274
275         // delete endpoint for at end of test case
276         //################ Delete End Point dummy entry
277         delete(rtmgr.Eps, uuid)
278         //#####################
279
280         // test-4
281
282         //################ Create End Point dummy entry
283         uuid = "10.101.01.1:9991"
284         ep = &rtmgr.Endpoint{
285                 Uuid: uuid,
286         }
287         rtmgr.Eps[uuid] = ep
288
289         uuid = "10.101.01.1:9992"
290         ep = &rtmgr.Endpoint{
291                 Uuid: uuid,
292         }
293         rtmgr.Eps[uuid] = ep
294         //#####################
295
296         data = models.E2tDeleteData{
297                 E2TAddress: swag.String("10.101.01:8098"),
298                 RanAssocList: models.RanE2tMap{
299                         {E2TAddress: swag.String("10.101.01.1:9991")},
300                         {E2TAddress: swag.String("10.101.01.1:9992")},
301                 },
302         }
303
304         err = validateDeleteE2tData(&data)
305         if err != nil {
306                 t.Log(err)
307         }
308         //################ Delete End Point dummy entry
309         delete(rtmgr.Eps, "10.101.01.1:9991")
310         delete(rtmgr.Eps, "10.101.01.1:9992")
311         //#####################
312
313         // test-5
314
315         data = models.E2tDeleteData{
316                 E2TAddress: swag.String("10.101.01:8098"),
317                 RanAssocList: models.RanE2tMap{
318                         {E2TAddress: swag.String("10.101.01.19991")},
319                 },
320         }
321
322         err = validateDeleteE2tData(&data)
323         if err.Error() != "E2T Delete - RanAssocList E2TAddress is not a proper format like ip:port, 10.101.01.19991" {
324                 t.Log(err)
325         }
326 }
327
328 func TestValidateE2TAddressRANListData(t *testing.T) {
329
330         data := models.RanE2tMap{
331                 {
332                         E2TAddress: swag.String(""),
333                 },
334         }
335         err := validateE2TAddressRANListData(data)
336         if err != nil {
337                 t.Log(err)
338         }
339
340         data = models.RanE2tMap{
341                 {
342                         E2TAddress: swag.String("10.101.01.1:8098"),
343                 },
344         }
345         err = validateE2TAddressRANListData(data)
346         if err != nil {
347                 t.Log(err)
348         }
349
350 }
351
352 func TestAssociateRanToE2THandlerImpl(t *testing.T) {
353
354         data := models.RanE2tMap{
355                 {
356                         E2TAddress: swag.String("10.101.01.1:8098"),
357                 },
358         }
359         err := AssociateRanToE2THandlerImpl(data)
360         if err != nil {
361                 t.Log(err)
362         }
363
364         //################ Create End Point dummy entry
365         uuid := "10.101.01.1:8098"
366         ep := &rtmgr.Endpoint{
367                 Uuid: uuid,
368         }
369         rtmgr.Eps[uuid] = ep
370         //#####################
371
372         data = models.RanE2tMap{
373                 {
374                         E2TAddress: swag.String("10.101.01.1:8098"),
375                 },
376         }
377         err = AssociateRanToE2THandlerImpl(data)
378         if err != nil {
379                 t.Log(err)
380         }
381
382         //################ Delete End Point dummy entry
383         delete(rtmgr.Eps, uuid)
384         //#####################
385 }
386
387 func TestDisassociateRanToE2THandlerImpl(t *testing.T) {
388
389         data := models.RanE2tMap{
390                 {
391                         E2TAddress: swag.String("10.101.01.1:8098"),
392                 },
393         }
394         err := DisassociateRanToE2THandlerImpl(data)
395         if err != nil {
396                 t.Log(err)
397         }
398         //################ Create End Point dummy entry
399         uuid := "10.101.01.1:8098"
400         ep := &rtmgr.Endpoint{
401                 Uuid: uuid,
402         }
403         rtmgr.Eps[uuid] = ep
404         //#####################
405
406         data = models.RanE2tMap{
407                 {
408                         E2TAddress: swag.String("10.101.01.1:8098"),
409                 },
410         }
411         err = DisassociateRanToE2THandlerImpl(data)
412         if err != nil {
413                 t.Log(err)
414         }
415
416         //################ Delete End Point dummy entry
417         delete(rtmgr.Eps, uuid)
418         //#####################
419 }
420
421 func TestDeleteE2tHandleHandlerImpl(t *testing.T) {
422
423         data := models.E2tDeleteData{
424                 E2TAddress: swag.String(""),
425         }
426         err := DeleteE2tHandleHandlerImpl(&data)
427         if err != nil {
428                 t.Log(err)
429         }
430
431         //################ Create End Point dummy entry
432         uuid := "10.101.01.1:8098"
433         ep := &rtmgr.Endpoint{
434                 Uuid: uuid,
435         }
436         rtmgr.Eps[uuid] = ep
437         //#####################
438
439         data = models.E2tDeleteData{
440                 E2TAddress: swag.String("10.101.01.1:8098"),
441         }
442         err = DeleteE2tHandleHandlerImpl(&data)
443         if err != nil {
444                 t.Log(err)
445         }
446         //################ Delete End Point dummy entry
447         delete(rtmgr.Eps, uuid)
448         //#####################
449 }
450
451 func TestSubscriptionExists(t *testing.T) {
452         p := uint16(0)
453         data := models.XappSubscriptionData{
454                 Address:        swag.String("10.0.0.0"),
455                 Port:           &p,
456                 SubscriptionID: swag.Int32(1234)}
457
458         rtmgr.Subs = *stub.ValidSubscriptions
459
460         yes_no := subscriptionExists(&data)
461         yes_no = addSubscription(&rtmgr.Subs, &data)
462         yes_no = addSubscription(&rtmgr.Subs, &data)
463         yes_no = delSubscription(&rtmgr.Subs, &data)
464         yes_no = delSubscription(&rtmgr.Subs, &data)
465         t.Log(yes_no)
466 }
467
468 func TestAddSubscriptions(t *testing.T) {
469         p := uint16(1)
470         subdata := models.XappSubscriptionData{
471                 Address:        swag.String("10.0.0.0"),
472                 Port:           &p,
473                 SubscriptionID: swag.Int32(1234)}
474
475         rtmgr.Subs = *stub.ValidSubscriptions
476         yes_no := addSubscription(&rtmgr.Subs, &subdata)
477         t.Log(yes_no)
478 }
479
480 func TestHttpInstance(t *testing.T) {
481         sdlEngine, _ := sdl.GetSdl("file")
482         rpeEngine, _ := rpe.GetRpe("rmrpush")
483         sbiEngine, _ := sbi.GetSbi("rmrpush")
484         httpinstance := NewHttpRestful()
485         err := httpinstance.Terminate()
486         t.Log(err)
487         fmt.Printf("sbiEngine = %v", sbiEngine)
488
489         createMockPlatformComponents()
490         //ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, nil)
491         //ts.Start()
492         //defer ts.Close()
493         var m sync.Mutex
494         err = httpinstance.Initialize(XMURL, "httpgetter", "rt.json", "config.json", E2MURL, sdlEngine, rpeEngine, &m)
495 }
496
497 func TestXappCallbackWithData(t *testing.T) {
498         data := models.XappCallbackData{
499                 XApps:   *swag.String("[]"),
500                 Version: *swag.Int64(1),
501                 Event:   *swag.String("someevent"),
502                 ID:      *swag.String("123456")}
503         _, _ = recvXappCallbackData(&data)
504 }
505
506 func TestXappCallbackNodata(t *testing.T) {
507         //data := *models.XappCallbackData
508         _, _ = recvXappCallbackData(nil)
509 }
510
511 func TestE2TwithData(t *testing.T) {
512         data2 := models.E2tData{
513                 E2TAddress:  swag.String("1.2.3.4"),
514                 RanNamelist: []string{"ran1", "ran2"},
515         }
516         _, _, _ = recvNewE2Tdata(&data2)
517 }
518
519 func TestE2TwithNoData(t *testing.T) {
520         _, _, _ = recvNewE2Tdata(nil)
521 }
522
523 func TestProvideXappSubscriptionHandleImpl(t *testing.T) {
524         p := uint16(0)
525         data := models.XappSubscriptionData{
526                 Address:        swag.String("10.0.0.0"),
527                 Port:           &p,
528                 SubscriptionID: swag.Int32(1234)}
529         _ = ProvideXappSubscriptionHandleImpl(&data)
530 }
531
532 func createMockAppmgrWithData(url string, g []byte, p []byte, t []byte) *httptest.Server {
533         l, err := net.Listen("tcp", url)
534         if err != nil {
535                 fmt.Println("Failed to create listener: " + err.Error())
536         }
537         ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
538                 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
539                         w.Header().Add("Content-Type", "application/json")
540                         w.WriteHeader(http.StatusOK)
541                         w.Write(g)
542                 }
543                 if r.Method == "POST" && r.URL.String() == "/ric/v1/subscriptions" {
544                         w.Header().Add("Content-Type", "application/json")
545                         w.WriteHeader(http.StatusCreated)
546                         w.Write(p)
547                 }
548                 if r.Method == "GET" && r.URL.String() == "/ric/v1/e2t/list" {
549                         w.Header().Add("Content-Type", "application/json")
550                         w.WriteHeader(http.StatusOK)
551                         w.Write(t)
552                 }
553
554         }))
555         ts.Listener.Close()
556         ts.Listener = l
557         return ts
558 }
559
560 func createMockSubmgrWithData(url string, t []byte) *httptest.Server {
561         l, err := net.Listen("tcp", url)
562         if err != nil {
563                 fmt.Println("Failed to create listener: " + err.Error())
564         }
565         ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
566
567                 if r.Method == "GET" && r.URL.String() == "//ric/v1/subscriptions" {
568                         w.Header().Add("Content-Type", "application/json")
569                         w.WriteHeader(http.StatusOK)
570                         w.Write(t)
571                 }
572
573         }))
574         ts.Listener.Close()
575         ts.Listener = l
576         return ts
577 }
578
579
580 func createMockPlatformComponents() {
581         var filename = "config.json"
582         file, _ := json.MarshalIndent(stub.ValidPlatformComponents, "", "")
583         filestr := string(file)
584         filestr = "{\"PlatformComponents\":" + filestr + "}"
585         file = []byte(filestr)
586         _ = ioutil.WriteFile(filename, file, 644)
587 }
588
589 func TestProvideXappHandleHandlerImpl(t *testing.T) {
590         data := models.XappCallbackData{
591                 XApps:   *swag.String("[]"),
592                 Version: *swag.Int64(1),
593                 Event:   *swag.String("someevent"),
594                 ID:      *swag.String("123456")}
595         err := ProvideXappHandleHandlerImpl(&data)
596
597         //Empty XappCallbackdata
598         data1 := models.XappCallbackData{}
599         err = ProvideXappHandleHandlerImpl(&data1)
600         t.Log(err)
601 }
602
603 func TestValidateXappCallbackData(t *testing.T) {
604         data := models.XappCallbackData{
605                 XApps:   *swag.String("[]"),
606                 Version: *swag.Int64(1),
607                 Event:   *swag.String("someevent"),
608                 ID:      *swag.String("123456")}
609
610         err := validateXappCallbackData(&data)
611         if err != nil {
612                 t.Error("Invalid XApp callback data: " + err.Error())
613         }
614 }
615
616 func TestValidateXappCallbackDataWithInvalidData(t *testing.T) {
617         data := models.XappCallbackData{
618                 XApps:   *swag.String("{}"),
619                 Version: *swag.Int64(1),
620                 Event:   *swag.String("someevent"),
621                 ID:      *swag.String("123456")}
622
623         err := validateXappCallbackData(&data)
624         if err == nil {
625                 t.Error("Invalid XApp callback data: " + err.Error())
626         }
627 }
628
629 func TestHttpGetXAppsInvalidData(t *testing.T) {
630         _, err := httpGetXApps(XMURL)
631         if err == nil {
632                 t.Error("No XApp data received: " + err.Error())
633         }
634 }
635
636 func TestHttpGetXAppsWithValidData(t *testing.T) {
637         var expected = 1
638         ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, nil, nil)
639
640         ts.Start()
641         defer ts.Close()
642         xapplist, err := httpGetXApps(XMURL)
643         if err != nil {
644                 t.Error("Error occured: " + err.Error())
645         } else {
646                 if len(*xapplist) != expected {
647                         t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
648                 }
649         }
650 }
651
652 func TestRetrieveStartupDataTimeout(t *testing.T) {
653         sdlEngine, _ := sdl.GetSdl("file")
654         createMockPlatformComponents()
655         err := retrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", E2MURL, sdlEngine)
656         if err == nil {
657                 t.Error("Cannot retrieve startup data: " + err.Error())
658         }
659         os.Remove("rt.json")
660         os.Remove("config.json")
661 }
662
663 func TestRetrieveStartupData(t *testing.T) {
664         ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, SubscriptionResp, nil)
665         ts.Start()
666         defer ts.Close()
667
668         ts1 := createMockAppmgrWithData("127.0.0.1:8085", nil, nil, E2TListResp)
669         ts1.Start()
670         defer ts1.Close()
671
672         ts2 := createMockSubmgrWithData("127.0.0.1:8089", SubscriptionList)
673         ts2.Start()
674         defer ts2.Close()
675
676         sdlEngine, _ := sdl.GetSdl("file")
677         var httpRestful, _ = GetNbi("httpRESTful")
678         createMockPlatformComponents()
679         httpRestful.(*HttpRestful).RetrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", E2MURL, sdlEngine)
680         //err := retrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine)
681         /*if err != nil {
682                 t.Error("Cannot retrieve startup data: " + err.Error())
683         }*/
684         os.Remove("rt.json")
685         os.Remove("config.json")
686 }
687
688 func TestRetrieveStartupDatawithInvalidE2MUrl(t *testing.T) {
689         ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, SubscriptionResp, nil)
690         ts.Start()
691         defer ts.Close()
692
693         ts1 := createMockAppmgrWithData("127.0.0.1:8085", nil, nil, E2TListResp)
694         ts1.Start()
695         defer ts1.Close()
696
697         ts2 := createMockSubmgrWithData("127.0.0.1:8089", SubscriptionList)
698         ts2.Start()
699         defer ts2.Close()
700
701
702         sdlEngine, _ := sdl.GetSdl("file")
703         var httpRestful, _ = GetNbi("httpRESTful")
704         createMockPlatformComponents()
705
706         E2MURL1 := "http://127.0.0.1:8080/ric/v1/e2t/list"
707         httpRestful.(*HttpRestful).RetrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", E2MURL1, sdlEngine)
708         os.Remove("rt.json")
709         os.Remove("config.json")
710 }
711 func TestRetrieveStartupDataWithInvalidSubResp(t *testing.T) {
712         ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, InvalidSubResp, nil)
713         ts.Start()
714         defer ts.Close()
715         sdlEngine, _ := sdl.GetSdl("file")
716         var httpRestful, _ = GetNbi("httpRESTful")
717         createMockPlatformComponents()
718         err := httpRestful.(*HttpRestful).RetrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", E2MURL, sdlEngine)
719         if err == nil {
720                 t.Error("Cannot retrieve startup data: " + err.Error())
721         }
722         os.Remove("rt.json")
723         os.Remove("config.json")
724 }
725
726 func TestInvalidarguments(t *testing.T) {
727         _ = PostSubReq("\n", "nbifinterface")
728         _ = PostSubReq("xmurl", "\n")
729 }
730
731 func TestInitEngine(t *testing.T) {
732         initRtmgr()
733 }
734
735 func TestUpdateXappSubscription(t *testing.T) {
736         ep := make(map[string]*rtmgr.Endpoint)
737         ep["dummy"] = &rtmgr.Endpoint{Uuid: "10.0.0.1:0", Name: "E2TERM", XAppType: "app1", Ip: "10.1.1.1", Port: 1234, TxMessages: []string{"", ""}, RxMessages: []string{"", ""}, Socket: nil, IsReady: true, Keepalive: true}
738
739         rtmgr.Eps = ep
740
741         p := uint16(1234)
742         xapp := models.XappElement{
743                 Address: swag.String("10.1.1.1"),
744                 Port:    &p,
745         }
746
747         var b models.XappList
748         b = append(b, &xapp)
749         _ = UpdateXappSubscriptionHandleImpl(&b, 10)
750
751         //Test case when subscriptions already exist
752         data := models.XappSubscriptionData{
753                 Address:        swag.String("10.0.0.0"),
754                 Port:           &p,
755                 SubscriptionID: swag.Int32(12345)}
756
757         rtmgr.Subs = *stub.ValidSubscriptions
758
759         subscriptionExists(&data)
760         addSubscription(&rtmgr.Subs, &data)
761         _ = UpdateXappSubscriptionHandleImpl(&b, 10)
762
763 }
764
765 func TestDumpDebugdata(t *testing.T) {
766         _, _ = DumpDebugData()
767 }
768
769 func TestDumpDebugdata1(t *testing.T) {
770         _, _ = DumpDebugData()
771 }
772
773 func TestManagerRequest(t *testing.T) {
774         var params xapp.RMRParams
775         var rmrmeid xapp.RMRMeid
776         sdlEngine, _ = sdl.GetSdl("file")
777         sbiEngine, _ = sbi.GetSbi("rmrpush")
778         rpeEngine, _ = rpe.GetRpe("rmrpush")
779         rmrmeid.RanName = "gnb1"
780         c := Control{make(chan *xapp.RMRParams)}
781         params.Payload = []byte{1, 2, 3, 4}
782         params.Mtype = 1234
783         params.SubId = -1
784         params.Meid = &rmrmeid
785         params.Src = "sender"
786         params.PayloadLen = 4
787
788         c.handleUpdateToRoutingManagerRequest(&params)
789 }
790
791 func TestRecievermr(t *testing.T) {
792         var params xapp.RMRParams
793         var rmrmeid xapp.RMRMeid
794         sdlEngine, _ = sdl.GetSdl("file")
795         sbiEngine, _ = sbi.GetSbi("rmrpush")
796         rpeEngine, _ = rpe.GetRpe("rmrpush")
797         rmrmeid.RanName = "gnb1"
798
799         params.Payload = []byte{1, 2, 3, 4}
800         params.SubId = -1
801         params.Meid = &rmrmeid
802         params.Src = "sender"
803         params.PayloadLen = 4
804
805         c := Control{make(chan *xapp.RMRParams)}
806         params.Mtype = xapp.RICMessageTypes["RMRRM_REQ_TABLE"]
807         c.recievermr(&params)
808         params.Mtype = xapp.RICMessageTypes["RMRRM_TABLE_STATE"]
809         c.recievermr(&params)
810         params.Mtype = 1234
811         c.recievermr(&params)
812
813         rtmgr.Rtmgr_ready = true
814         params.Mtype = xapp.RICMessageTypes["RMRRM_REQ_TABLE"]
815         c.recievermr(&params)
816         time.Sleep(time.Duration(5) * time.Second)
817 }
818
819 func TestAddDelRmr(t *testing.T) {
820         sdlEngine, _ = sdl.GetSdl("file")
821         sbiEngine, _ = sbi.GetSbi("rmrpush")
822         rpeEngine, _ = rpe.GetRpe("rmrpush")
823         var routelist models.Routelist
824         mtype := uint32(1234)
825         tendpoint := "goofle.com"
826         listofroutes := models.AddRmrRoute{SubscriptionID: 0, SenderEndPoint: "nokia.com", MessageType: &mtype, TargetEndPoint: &tendpoint}
827         listofroutes2 := models.AddRmrRoute{SubscriptionID: 1, SenderEndPoint: "", MessageType: &mtype, TargetEndPoint: &tendpoint}
828         listofroutes3 := models.AddRmrRoute{MessageType: &mtype, TargetEndPoint: &tendpoint}
829         Adddelrmrroute(routelist, false)
830         routelist = append(routelist, &listofroutes)
831         routelist = append(routelist, &listofroutes2)
832         routelist = append(routelist, &listofroutes3)
833         routelist = append(routelist, &listofroutes3)
834         Adddelrmrroute(routelist, true)
835
836         Adddelrmrroute(routelist, false)
837 }
838
839 func TestPopulateSubscription(t *testing.T) {
840         var sublist xfmodel.SubscriptionList
841
842         subdata := xfmodel.SubscriptionData{ClientEndpoint: []string{"xapp1.com:3800"}, SubscriptionID: -1, Meid: ""}
843         subdata2 := xfmodel.SubscriptionData{ClientEndpoint: []string{"xapp2.com:3800"}, SubscriptionID: 11, Meid: ""}
844         subdata3 := xfmodel.SubscriptionData{ClientEndpoint: []string{"xapp3.com:3800"}, SubscriptionID: 221, Meid: ""}
845         sublist = append(sublist, &subdata)
846         sublist = append(sublist, &subdata2)
847         sublist = append(sublist, &subdata3)
848
849         PopulateSubscription(sublist)
850
851 }