Unit test case correction
[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 import (
31         //"errors"
32         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
33         "os"
34         "routing-manager/pkg/rtmgr"
35         "routing-manager/pkg/stub"
36         "testing"
37         "time"
38 )
39
40 type Consumer struct{}
41
42 func (m Consumer) Consume(params *xapp.RMRParams) (err error) {
43         xapp.Sdl.Store("myKey", params.Payload)
44         return nil
45 }
46
47 // Test cases
48 func TestMain(m *testing.M) {
49         go xapp.RunWithParams(Consumer{}, false)
50         time.Sleep(time.Duration(5) * time.Second)
51         code := m.Run()
52         os.Exit(code)
53 }
54
55 /*
56 Resets the EndpointList according to argumnets
57 */
58 func resetTestPushDataset(instance RmrPush, testdata []rtmgr.Endpoint) {
59         rtmgr.Eps = make(map[string]*rtmgr.Endpoint)
60         for _, endpoint := range testdata {
61                 ep := endpoint
62                 //ep.Socket, _ = instance.NewSocket()
63                 rtmgr.Eps[ep.Uuid] = &ep
64         }
65 }
66
67 /*
68 rmrpush.Initialize() method is empty, nothing to be tested
69 */
70 func TestRmrPushInitialize(t *testing.T) {
71         var rmrpush = RmrPush{}
72
73         _ = rmrpush.Initialize("")
74 }
75
76 /*
77 rmrpush.Terminate() method is empty, nothing to be tested
78 */
79 func TestRmrPushTerminate(t *testing.T) {
80         var rmrpush = RmrPush{}
81
82         _ = rmrpush.Terminate()
83 }
84
85 /*
86 rmrpush.UpdateEndpoints() is testd against stub.ValidXApps dataset
87 */
88 func TestRmrPushUpdateEndpoints(t *testing.T) {
89         var rmrpush = RmrPush{}
90         resetTestPushDataset(rmrpush, stub.ValidEndpoints)
91
92         rmrpush.UpdateEndpoints(&stub.ValidRicComponents)
93         if rtmgr.Eps == nil {
94                 t.Errorf("rmrpush.UpdateEndpoints() result was incorrect, got: %v, want: %v.", nil, "rtmgr.Endpoints")
95         }
96 }
97
98 /*
99 rmrpush.AddEndpoint() is tested for happy path case
100 */
101 func TestRmrPushAddEndpoint(t *testing.T) {
102         //      var err error
103         var rmrpush = RmrPush{}
104         resetTestPushDataset(rmrpush, stub.ValidEndpoints)
105         _ = rmrpush.AddEndpoint(rtmgr.Eps["localhost"])
106         /*      if err != nil {
107                 t.Errorf("rmrpush.AddEndpoint() return was incorrect, got: %v, want: %v.", err, "nil")
108         }*/
109 }
110
111 /*
112 rmrpush.DistributeAll() is tested for happy path case
113 */
114 func TestRmrPushDistributeAll(t *testing.T) {
115         var err error
116         var rmrpush = RmrPush{}
117         resetTestPushDataset(rmrpush, stub.ValidEndpoints)
118
119         rmrcallid = 200
120         err = rmrpush.DistributeAll(stub.ValidPolicies)
121         t.Log(err)
122         /*if err != nil {
123                 t.Errorf("rmrpush.DistributeAll(policies) was incorrect, got: %v, want: %v.", err, "nil")
124         }*/
125 }
126
127 /*
128 rmrpush.DistributeToEp() is tested for Sending case
129 */
130 func TestDistributeToEp(t *testing.T) {
131         var err error
132         var rmrpush = RmrPush{}
133         resetTestPushDataset(rmrpush, stub.ValidEndpoints)
134
135         rmrdynamiccallid = 255
136         err = rmrpush.DistributeToEp(stub.ValidPolicies, "localhost:4561", 100)
137         if err != nil {
138                 t.Errorf("rmrpush.DistributetoEp(policies) was incorrect, got: %v, want: %v.", err, "nil")
139         }
140 }
141
142 func TestDeleteEndpoint(t *testing.T) {
143         var err error
144         var rmrpush = RmrPush{}
145         resetTestPushDataset(rmrpush, stub.ValidEndpoints)
146
147         err = rmrpush.DeleteEndpoint(rtmgr.Eps["localhost"])
148         if err != nil {
149                 t.Errorf("rmrpush.DeleteEndpoint() was incorrect, got: %v, want: %v.", err, "nil")
150         }
151 }
152
153 func TestCheckEndpoint(t *testing.T) {
154         var rmrpush = RmrPush{}
155         resetTestPushDataset(rmrpush, stub.ValidEndpoints1)
156         rmrpush.CheckEndpoint("192.168.0.1:0")
157         rmrpush.CheckEndpoint("10.2.2.1:0")
158         rmrpush.CheckEndpoint("localhost:0")
159 }
160
161 func TestCreateEndpoint(t *testing.T) {
162         var rmrpush = RmrPush{}
163         resetTestPushDataset(rmrpush, stub.ValidEndpoints1)
164         rmrpush.CreateEndpoint("Src=127.0.0.1:4561 hello")
165 }
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 }