Subscription via REST interface
[ric-plt/xapp-frame.git] / pkg / xapp / subscription_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 Nokia
4 ==================================================================================
5 */
6
7 package xapp
8
9 import (
10         "testing"
11         "time"
12         "github.com/stretchr/testify/assert"
13         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
14     apimodel "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel"
15 )
16
17 var suite *testing.T
18
19 var requestorId = int64(0x4EEC)
20 var direction = int64(0)
21 var procedureCode = int64(27)
22 var typeOfMessage = int64(1)
23
24 var reportParams = apimodel.ReportParams{
25         RequestorID: &requestorId,
26         EventTriggers: apimodel.EventTriggerList{
27                 &apimodel.EventTrigger{
28                         InterfaceDirection: &direction,
29                         ProcedureCode: &procedureCode,
30                         TypeOfMessage: &typeOfMessage,
31                 },
32         },
33 }
34
35 var controlParams = apimodel.ControlParams{
36         RequestorID: requestorId,
37 }
38
39 var policyParams = apimodel.PolicyParams{
40         RequestorID: requestorId,
41 }
42
43 func subscriptionHandler(stype models.SubscriptionType, params interface{}) (models.SubscriptionResult, error) {
44         switch stype {
45         case models.SubscriptionTypeReport:
46                 p := params.(*models.ReportParams)
47                 assert.Equal(suite, requestorId, *p.RequestorID)
48                 assert.Equal(suite, direction, *p.EventTriggers[0].InterfaceDirection)
49                 assert.Equal(suite, procedureCode, *p.EventTriggers[0].ProcedureCode)
50                 assert.Equal(suite, typeOfMessage, *p.EventTriggers[0].TypeOfMessage)
51         case models.SubscriptionTypeControl:
52                 p := params.(*models.ControlParams)
53                 assert.Equal(suite, requestorId, p.RequestorID)
54         case models.SubscriptionTypePolicy:
55                 p := params.(*models.PolicyParams)
56                 assert.Equal(suite, requestorId, p.RequestorID)
57         }
58
59         return models.SubscriptionResult{11, 22, 33}, nil
60 }
61
62 func TestSetup(t *testing.T) {
63         suite = t
64
65         // Start the server to simulate SubManager
66         go Subscription.Listen(subscriptionHandler)
67         time.Sleep(time.Duration(2) * time.Second)
68 }
69
70 func TestSubscriptionReportHandling(t *testing.T) {
71         result, err := Subscription.SubscribeReport(&reportParams)
72
73         assert.Equal(t, err, nil)
74         assert.Equal(t, len(result), 3)
75         assert.Equal(t, result[0], int64(11))
76         assert.Equal(t, result[1], int64(22))
77         assert.Equal(t, result[2], int64(33))
78 }
79
80 func TestSubscriptionControltHandling(t *testing.T) {
81         result, err := Subscription.SubscribeControl(&controlParams)
82
83         assert.Equal(t, err, nil)
84         assert.Equal(t, len(result), 3)
85         assert.Equal(t, result[0], int64(11))
86 }
87
88 func TestSubscriptionPolicytHandling(t *testing.T) {
89         result, err := Subscription.SubscribePolicy(&policyParams)
90
91         assert.Equal(t, err, nil)
92         assert.Equal(t, len(result), 3)
93         assert.Equal(t, result[0], int64(11))
94 }