Publish Route only once to the endpoint that requests it. Periodic/Event based distri...
[ric-plt/rtmgr.git] / pkg / sbi / nngpush_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    This source code is part of the near-RT RIC (RAN Intelligent Controller)
19    platform project (RICP).
20
21 ==================================================================================
22 */
23 /*
24         Mnemonic:       rmrpush_test.go
25         Abstract:
26         Date:           3 May 2019
27 */
28 package sbi
29
30
31 import (
32         //"errors"
33         "routing-manager/pkg/rtmgr"
34         "routing-manager/pkg/stub"
35         "time"
36         "os"
37         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
38         "testing"
39 )
40
41 type Consumer struct{}
42
43 func (m Consumer) Consume(params *xapp.RMRParams) (err error) {
44         xapp.Sdl.Store("myKey", params.Payload)
45         return nil
46 }
47
48 // Test cases
49 func TestMain(m *testing.M) {
50         go xapp.RunWithParams(Consumer{}, false)
51         time.Sleep(time.Duration(5) * time.Second)
52         code := m.Run()
53         os.Exit(code)
54 }
55
56 /*
57 Resets the EndpointList according to argumnets
58 */
59 func resetTestPushDataset(instance RmrPush, testdata []rtmgr.Endpoint) {
60         rtmgr.Eps = make(map[string]*rtmgr.Endpoint)
61         for _, endpoint := range testdata {
62                 ep := endpoint
63                 //ep.Socket, _ = instance.NewSocket()
64                 rtmgr.Eps[ep.Uuid] = &ep
65         }
66 }
67
68 /*
69 rmrpush.Initialize() method is empty, nothing to be tested
70 */
71 func TestRmrPushInitialize(t *testing.T) {
72         var rmrpush = RmrPush{}
73
74         _ = rmrpush.Initialize("")
75 }
76
77 /*
78 rmrpush.Terminate() method is empty, nothing to be tested
79 */
80 func TestRmrPushTerminate(t *testing.T) {
81         var rmrpush = RmrPush{}
82
83         _ = rmrpush.Terminate()
84 }
85
86 /*
87 rmrpush.UpdateEndpoints() is testd against stub.ValidXApps dataset
88 */
89 func TestRmrPushUpdateEndpoints(t *testing.T) {
90         var rmrpush = RmrPush{}
91         resetTestPushDataset(rmrpush, stub.ValidEndpoints)
92
93         rmrpush.UpdateEndpoints(&stub.ValidRicComponents)
94         if rtmgr.Eps == nil {
95                 t.Errorf("rmrpush.UpdateEndpoints() result was incorrect, got: %v, want: %v.", nil, "rtmgr.Endpoints")
96         }
97 }
98
99 /*
100 rmrpush.AddEndpoint() is tested for happy path case
101 */
102 func TestRmrPushAddEndpoint(t *testing.T) {
103 //      var err error
104         var rmrpush = RmrPush{}
105         resetTestPushDataset(rmrpush, stub.ValidEndpoints)
106         _ = rmrpush.AddEndpoint(rtmgr.Eps["localhost"])
107 /*      if err != nil {
108                 t.Errorf("rmrpush.AddEndpoint() return was incorrect, got: %v, want: %v.", err, "nil")
109         }*/
110 }
111
112
113 /*
114 rmrpush.DistributeAll() is tested for happy path case
115 */
116 func TestRmrPushDistributeAll(t *testing.T) {
117         var err error
118         var rmrpush = RmrPush{}
119         resetTestPushDataset(rmrpush, stub.ValidEndpoints)
120
121         rmrcallid = 200
122         err = rmrpush.DistributeAll(stub.ValidPolicies)
123         if err != nil {
124                 t.Errorf("rmrpush.DistributeAll(policies) was incorrect, got: %v, want: %v.", err, "nil")
125         }
126 }
127
128 /*
129 rmrpush.DistributeToEp() is tested for Sending case
130 */
131 func TestDistributeToEp(t *testing.T) {
132         var err error
133         var rmrpush = RmrPush{}
134         resetTestPushDataset(rmrpush, stub.ValidEndpoints)
135
136         rmrdynamiccallid = 255
137         err = rmrpush.DistributeToEp(stub.ValidPolicies,"localhost:4561",100)
138         if err != nil {
139                 t.Errorf("rmrpush.DistributetoEp(policies) was incorrect, got: %v, want: %v.", err, "nil")
140         }
141 }
142
143 func TestDeleteEndpoint(t *testing.T) {
144         var err error
145         var rmrpush = RmrPush{}
146         resetTestPushDataset(rmrpush, stub.ValidEndpoints)
147
148         err = rmrpush.DeleteEndpoint(rtmgr.Eps["localhost"])
149         if err != nil {
150                 t.Errorf("rmrpush.DeleteEndpoint() was incorrect, got: %v, want: %v.", err, "nil")
151         }
152 }
153
154 func TestCheckEndpoint(t *testing.T) {
155         var rmrpush = RmrPush{}
156         resetTestPushDataset(rmrpush, stub.ValidEndpoints1)
157         rmrpush.CheckEndpoint("192.168.0.1:0")
158         rmrpush.CheckEndpoint("10.2.2.1:0")
159         rmrpush.CheckEndpoint("localhost:0")
160 }
161
162 func TestCreateEndpoint(t *testing.T) {
163         var rmrpush = RmrPush{}
164         resetTestPushDataset(rmrpush, stub.ValidEndpoints1)
165         rmrpush.CreateEndpoint("Src=127.0.0.1:4561 hello")
166 }
167 /*
168 Initialize and send policies
169 */
170 func TestRmrPushInitializeandsendPolicies(t *testing.T) {
171         var rmrpush = RmrPush{}
172         resetTestPushDataset(rmrpush, stub.ValidEndpoints)
173         policies := []string{"hello","welcome"}
174         rmrpush.send_data(rtmgr.Eps["localhost"],&policies,1)
175 }
176
177 func TestString( t *testing.T) {
178         var params xapp.RMRParams
179         params.Payload = []byte("abcdefgh")
180         params.Meid = &xapp.RMRMeid{}
181         msg := RMRParams{&params}
182         msg.String()
183
184 }
185
186 func TestSenddata(t *testing.T) {
187         var rmrpush = RmrPush{}
188         ep := rtmgr.Endpoint{Whid:-1, Ip:"1.1.1.1"}
189         policies := []string{"mse|12345|-1|local.com"}
190         rmrpush.send_data(&ep, &policies,300)
191 }
192
193 func TestSendDynamicdata(t *testing.T) {
194         var rmrpush = RmrPush{}
195         ep := "1.1.1.1"
196         policies := []string{"mse|12345|-1|local.com"}
197         rmrpush.sendDynamicRoutes(ep,1, &policies,300)
198 }