Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2019 Nokia |
| 4 | ================================================================================== |
| 5 | */ |
| 6 | |
| 7 | package xapp |
| 8 | |
| 9 | import ( |
Mohamed Abukar | 8dcedb4 | 2020-03-10 13:20:56 +0200 | [diff] [blame] | 10 | 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 Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 13 | "testing" |
| 14 | "time" |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | var suite *testing.T |
| 18 | |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 19 | var meid = "gnb123456" |
| 20 | var funId = int64(1) |
| 21 | var clientEndpoint = "localhost:4561" |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 22 | var direction = int64(0) |
| 23 | var procedureCode = int64(27) |
| 24 | var typeOfMessage = int64(1) |
| 25 | |
| 26 | var reportParams = apimodel.ReportParams{ |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 27 | Meid: meid, |
| 28 | RANFunctionID: &funId, |
| 29 | ClientEndpoint: &clientEndpoint, |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 30 | EventTriggers: apimodel.EventTriggerList{ |
| 31 | &apimodel.EventTrigger{ |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 32 | InterfaceDirection: direction, |
| 33 | ProcedureCode: procedureCode, |
| 34 | TypeOfMessage: typeOfMessage, |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 35 | }, |
| 36 | }, |
| 37 | } |
| 38 | |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 39 | var policyParams = apimodel.PolicyParams{ |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 40 | 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 Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 53 | func subscriptionHandler(stype models.SubscriptionType, params interface{}) (models.SubscriptionResponse, error) { |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 54 | switch stype { |
| 55 | case models.SubscriptionTypeReport: |
| 56 | p := params.(*models.ReportParams) |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 57 | 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 Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 63 | case models.SubscriptionTypePolicy: |
| 64 | p := params.(*models.PolicyParams) |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 65 | assert.Equal(suite, clientEndpoint, *p.ClientEndpoint) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 68 | subId := "xapp-11" |
| 69 | reqId := int64(11) |
| 70 | instanceId := int64(22) |
| 71 | return models.SubscriptionResponse{ |
| 72 | &models.SubscriptionResponseItem{SubscriptionID: &subId, RequestorID: &reqId, InstanceID: &instanceId}, |
| 73 | &models.SubscriptionResponseItem{SubscriptionID: &subId, RequestorID: &reqId, InstanceID: &instanceId}, |
| 74 | }, nil |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 77 | func queryHandler() (models.SubscriptionList, error) { |
| 78 | resp := models.SubscriptionList{ |
| 79 | &models.SubscriptionData{ |
| 80 | SubscriptionID: 11, |
Mohamed Abukar | 8dcedb4 | 2020-03-10 13:20:56 +0200 | [diff] [blame] | 81 | Meid: "Test-Gnb", |
| 82 | Endpoint: []string{"127.0.0.1:4056"}, |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 83 | }, |
| 84 | } |
| 85 | |
| 86 | return resp, nil |
| 87 | } |
| 88 | |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 89 | func deleteHandler(ep string) error { |
| 90 | assert.Equal(suite, clientEndpoint, ep) |
| 91 | |
| 92 | return nil |
| 93 | } |
| 94 | |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 95 | func TestSetup(t *testing.T) { |
| 96 | suite = t |
| 97 | |
| 98 | // Start the server to simulate SubManager |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 99 | go Subscription.Listen(subscriptionHandler, queryHandler, deleteHandler) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 100 | time.Sleep(time.Duration(2) * time.Second) |
| 101 | } |
| 102 | |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 103 | func TestSubscriptionQueryHandling(t *testing.T) { |
| 104 | resp, err := Subscription.QuerySubscriptions() |
| 105 | |
| 106 | assert.Equal(t, err, nil) |
| 107 | assert.Equal(t, resp[0].SubscriptionID, int64(11)) |
| 108 | assert.Equal(t, resp[0].Meid, "Test-Gnb") |
| 109 | assert.Equal(t, resp[0].Endpoint, []string{"127.0.0.1:4056"}) |
| 110 | } |
| 111 | |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 112 | func TestSubscriptionReportHandling(t *testing.T) { |
| 113 | result, err := Subscription.SubscribeReport(&reportParams) |
| 114 | |
| 115 | assert.Equal(t, err, nil) |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 116 | assert.Equal(t, len(result), 2) |
| 117 | assert.Equal(t, *result[0].RequestorID, int64(11)) |
| 118 | assert.Equal(t, *result[0].InstanceID, int64(22)) |
| 119 | assert.Equal(t, *result[1].RequestorID, int64(11)) |
| 120 | assert.Equal(t, *result[1].InstanceID, int64(22)) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | func TestSubscriptionPolicytHandling(t *testing.T) { |
| 124 | result, err := Subscription.SubscribePolicy(&policyParams) |
| 125 | |
| 126 | assert.Equal(t, err, nil) |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 127 | assert.Equal(t, len(result), 2) |
| 128 | assert.Equal(t, *result[0].RequestorID, int64(11)) |
| 129 | assert.Equal(t, *result[0].InstanceID, int64(22)) |
Mohamed Abukar | 8dcedb4 | 2020-03-10 13:20:56 +0200 | [diff] [blame] | 130 | } |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame^] | 131 | |
| 132 | func TestSubscriptionDeleteHandling(t *testing.T) { |
| 133 | err := Subscription.UnSubscribe(clientEndpoint) |
| 134 | |
| 135 | assert.Equal(t, err, nil) |
| 136 | } |