blob: b9713afdb3f36f22f22676f96a4ac1689a9b2804 [file] [log] [blame]
Mohamed Abukar5120ec12020-02-04 11:01:24 +02001/*
2==================================================================================
3 Copyright (c) 2019 Nokia
4==================================================================================
5*/
6
7package xapp
8
9import (
Mohamed Abukar8dcedb42020-03-10 13:20:56 +020010 apimodel "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel"
11 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
12 "github.com/stretchr/testify/assert"
Mohamed Abukar5120ec12020-02-04 11:01:24 +020013 "testing"
14 "time"
Mohamed Abukar5120ec12020-02-04 11:01:24 +020015)
16
17var suite *testing.T
18
Mohamed Abukar429da192020-02-26 16:46:34 +020019var meid = "gnb123456"
20var funId = int64(1)
21var clientEndpoint = "localhost:4561"
Mohamed Abukar5120ec12020-02-04 11:01:24 +020022var direction = int64(0)
23var procedureCode = int64(27)
24var typeOfMessage = int64(1)
25
26var reportParams = apimodel.ReportParams{
Mohamed Abukar429da192020-02-26 16:46:34 +020027 Meid: meid,
28 RANFunctionID: &funId,
29 ClientEndpoint: &clientEndpoint,
Mohamed Abukar5120ec12020-02-04 11:01:24 +020030 EventTriggers: apimodel.EventTriggerList{
31 &apimodel.EventTrigger{
Mohamed Abukar429da192020-02-26 16:46:34 +020032 InterfaceDirection: direction,
33 ProcedureCode: procedureCode,
34 TypeOfMessage: typeOfMessage,
Mohamed Abukar5120ec12020-02-04 11:01:24 +020035 },
36 },
37}
38
Mohamed Abukar5120ec12020-02-04 11:01:24 +020039var policyParams = apimodel.PolicyParams{
Mohamed Abukar429da192020-02-26 16:46:34 +020040 Meid: &meid,
41 RANFunctionID: &funId,
42 ClientEndpoint: &clientEndpoint,
43 EventTriggers: apimodel.EventTriggerList{
44 &apimodel.EventTrigger{
45 InterfaceDirection: direction,
46 ProcedureCode: procedureCode,
47 TypeOfMessage: typeOfMessage,
48 },
49 },
50 PolicyActionDefinitions: &apimodel.PolicyActionDefinition{},
Mohamed Abukar5120ec12020-02-04 11:01:24 +020051}
52
Mohamed Abukarb6341a52020-03-23 08:55:05 +020053func subscriptionHandler(stype models.SubscriptionType, params interface{}) (*models.SubscriptionResponse, error) {
Mohamed Abukar5120ec12020-02-04 11:01:24 +020054 switch stype {
55 case models.SubscriptionTypeReport:
56 p := params.(*models.ReportParams)
Mohamed Abukar429da192020-02-26 16:46:34 +020057 assert.Equal(suite, meid, p.Meid)
58 assert.Equal(suite, funId, *p.RANFunctionID)
59 assert.Equal(suite, clientEndpoint, *p.ClientEndpoint)
60 assert.Equal(suite, direction, p.EventTriggers[0].InterfaceDirection)
61 assert.Equal(suite, procedureCode, p.EventTriggers[0].ProcedureCode)
62 assert.Equal(suite, typeOfMessage, p.EventTriggers[0].TypeOfMessage)
Mohamed Abukar5120ec12020-02-04 11:01:24 +020063 case models.SubscriptionTypePolicy:
64 p := params.(*models.PolicyParams)
Mohamed Abukar429da192020-02-26 16:46:34 +020065 assert.Equal(suite, clientEndpoint, *p.ClientEndpoint)
Mohamed Abukar5120ec12020-02-04 11:01:24 +020066 }
67
Mohamed Abukar429da192020-02-26 16:46:34 +020068 subId := "xapp-11"
69 reqId := int64(11)
70 instanceId := int64(22)
Mohamed Abukarb6341a52020-03-23 08:55:05 +020071 return &models.SubscriptionResponse{
72 SubscriptionID: &subId,
73 SubscriptionInstances: []*models.SubscriptionInstance{
74 &models.SubscriptionInstance{RequestorID: &reqId, InstanceID: &instanceId},
75 &models.SubscriptionInstance{RequestorID: &reqId, InstanceID: &instanceId},
76 },
Mohamed Abukar429da192020-02-26 16:46:34 +020077 }, nil
Mohamed Abukar5120ec12020-02-04 11:01:24 +020078}
79
Mohamed Abukar9568a2d2020-02-18 16:50:32 +020080func queryHandler() (models.SubscriptionList, error) {
81 resp := models.SubscriptionList{
82 &models.SubscriptionData{
83 SubscriptionID: 11,
Mohamed Abukar8dcedb42020-03-10 13:20:56 +020084 Meid: "Test-Gnb",
85 Endpoint: []string{"127.0.0.1:4056"},
Mohamed Abukar9568a2d2020-02-18 16:50:32 +020086 },
87 }
88
89 return resp, nil
90}
91
Mohamed Abukar429da192020-02-26 16:46:34 +020092func deleteHandler(ep string) error {
93 assert.Equal(suite, clientEndpoint, ep)
94
95 return nil
96}
97
Mohamed Abukar5120ec12020-02-04 11:01:24 +020098func TestSetup(t *testing.T) {
99 suite = t
100
101 // Start the server to simulate SubManager
Mohamed Abukar429da192020-02-26 16:46:34 +0200102 go Subscription.Listen(subscriptionHandler, queryHandler, deleteHandler)
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200103 time.Sleep(time.Duration(2) * time.Second)
104}
105
Mohamed Abukar9568a2d2020-02-18 16:50:32 +0200106func TestSubscriptionQueryHandling(t *testing.T) {
107 resp, err := Subscription.QuerySubscriptions()
108
109 assert.Equal(t, err, nil)
110 assert.Equal(t, resp[0].SubscriptionID, int64(11))
111 assert.Equal(t, resp[0].Meid, "Test-Gnb")
112 assert.Equal(t, resp[0].Endpoint, []string{"127.0.0.1:4056"})
113}
114
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200115func TestSubscriptionReportHandling(t *testing.T) {
Mohamed Abukarb6341a52020-03-23 08:55:05 +0200116 resp, err := Subscription.SubscribeReport(&reportParams)
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200117
118 assert.Equal(t, err, nil)
Mohamed Abukarb6341a52020-03-23 08:55:05 +0200119 assert.Equal(t, len(resp.SubscriptionInstances), 2)
120 assert.Equal(t, *resp.SubscriptionInstances[0].RequestorID, int64(11))
121 assert.Equal(t, *resp.SubscriptionInstances[0].InstanceID, int64(22))
122 assert.Equal(t, *resp.SubscriptionInstances[1].RequestorID, int64(11))
123 assert.Equal(t, *resp.SubscriptionInstances[1].InstanceID, int64(22))
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200124}
125
126func TestSubscriptionPolicytHandling(t *testing.T) {
Mohamed Abukarb6341a52020-03-23 08:55:05 +0200127 resp, err := Subscription.SubscribePolicy(&policyParams)
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200128
129 assert.Equal(t, err, nil)
Mohamed Abukarb6341a52020-03-23 08:55:05 +0200130 assert.Equal(t, len(resp.SubscriptionInstances), 2)
131 assert.Equal(t, *resp.SubscriptionInstances[0].RequestorID, int64(11))
132 assert.Equal(t, *resp.SubscriptionInstances[0].InstanceID, int64(22))
Mohamed Abukar8dcedb42020-03-10 13:20:56 +0200133}
Mohamed Abukar429da192020-02-26 16:46:34 +0200134
135func TestSubscriptionDeleteHandling(t *testing.T) {
136 err := Subscription.UnSubscribe(clientEndpoint)
137
138 assert.Equal(t, err, nil)
139}