b17b2ca534fb720cd9ff693e97bcacc0139952d3
[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:       nngpush_test.go
25         Abstract:
26         Date:           3 May 2019
27 */
28 package sbi
29
30 import (
31         "errors"
32         "routing-manager/pkg/rtmgr"
33         "routing-manager/pkg/stub"
34         //"nanomsg.org/go/mangos/v2"
35         //_ "nanomsg.org/go/mangos/v2/transport/all"
36         //"nanomsg.org/go/mangos/v2/protocol/push"
37         "testing"
38 )
39
40 /*
41 Returns an error free Socket instance
42 */
43 func createNewStubPushSocket() (NngSocket, error) {
44         socket := stub.MangosSocket{}
45         return socket, nil
46 }
47
48 /*
49 Returns a SocketError
50 */
51 func createNewStubPushSocketError() (NngSocket, error) {
52         return nil, errors.New("stub generated Create Socket error")
53 }
54
55 /*
56 Returns a Socket which always generates error on Close()
57 */
58 func createNewStubPushSocketCloseError() (NngSocket, error) {
59         socket := stub.MangosSocket{}
60         socket.GenerateSocketCloseError = true
61         return socket, nil
62 }
63
64 /*
65 Returns a Socket which always generates error on Send()
66 */
67 func createNewStubPushSocketSendError() (NngSocket, error) {
68         socket := stub.MangosSocket{}
69         socket.GenerateSocketSendError = true
70         return socket, nil
71 }
72
73 /*
74 Returns a Socket which always generates error on Dial()
75 */
76 func createNewStubPushSocketDialError() (NngSocket, error) {
77         socket := stub.MangosSocket{}
78         socket.GenerateSocketDialError = true
79         return socket, nil
80 }
81
82 /*
83 Resets the EndpointList according to argumnets
84 */
85 func resetTestPushDataset(instance NngPush, testdata []rtmgr.Endpoint) {
86         rtmgr.Eps = make(map[string]*rtmgr.Endpoint)
87         for _, endpoint := range testdata {
88                 ep := endpoint
89                 ep.Socket, _ = instance.NewSocket()
90                 rtmgr.Eps[ep.Uuid] = &ep
91         }
92 }
93
94 /*
95 nngpush.Initialize() method is empty, nothing to be tested
96 */
97 func TestNngPushInitialize(t *testing.T) {
98         var nngpush = NngPush{}
99         nngpush.NewSocket = createNewStubPushSocket
100
101         _ = nngpush.Initialize("")
102 }
103
104 /*
105 nngpush.Terminate() method is empty, nothing to be tested
106 */
107 func TestNngPushTerminate(t *testing.T) {
108         var nngpush = NngPush{}
109         nngpush.NewSocket = createNewStubPushSocket
110
111         _ = nngpush.Terminate()
112 }
113
114 /*
115 nngpush.UpdateEndpoints() is testd against stub.ValidXApps dataset
116 */
117 func TestNngPushUpdateEndpoints(t *testing.T) {
118         var nngpush = NngPush{}
119         nngpush.NewSocket = createNewStubPushSocket
120         rtmgr.Eps = make(rtmgr.Endpoints)
121
122         nngpush.UpdateEndpoints(&stub.ValidRicComponents)
123         if rtmgr.Eps == nil {
124                 t.Errorf("nngpush.UpdateEndpoints() result was incorrect, got: %v, want: %v.", nil, "rtmgr.Endpoints")
125         }
126 }
127
128 /*
129 nngpush.AddEndpoint() is tested for happy path case
130 */
131 func TestNngPushAddEndpoint(t *testing.T) {
132         var err error
133         var nngpush = NngPush{}
134         nngpush.NewSocket = createNewStubPushSocket
135         resetTestPushDataset(nngpush, stub.ValidEndpoints)
136
137         err = nngpush.AddEndpoint(rtmgr.Eps["10.0.0.1:0"])
138         if err != nil {
139                 t.Errorf("nngpush.AddEndpoint() return was incorrect, got: %v, want: %v.", err, "nil")
140         }
141         if rtmgr.Eps["10.0.0.1:0"].Socket == nil {
142                 t.Errorf("nngpush.AddEndpoint() was incorrect, got: %v, want: %v.", nil, "Socket")
143         }
144 }
145
146 /*
147 nngpush.AddEndpoint() is tested for Socket creating error case
148 */
149 func TestNngPushAddEndpointWithSocketError(t *testing.T) {
150         var err error
151         var nngpush = NngPush{}
152         nngpush.NewSocket = createNewStubPushSocketError
153         resetTestPushDataset(nngpush, stub.ValidEndpoints)
154
155         err = nngpush.AddEndpoint(rtmgr.Eps["10.0.0.1:0"])
156         if err == nil {
157                 t.Errorf("nngpush.AddEndpoint() was incorrect, got: %v, want: %v.", nil, "error")
158         }
159         if rtmgr.Eps["10.0.0.1:0"].Socket != nil {
160                 t.Errorf("nngpush.AddEndpoint() was incorrect, got: %v, want: %v.", rtmgr.Eps["10.0.0.1:0"].Socket, nil)
161         }
162 }
163
164 /*
165 nngpush.AddEndpoint() is tested for Dialing error case
166 */
167 func TestNngPushAddEndpointWithSocketDialError(t *testing.T) {
168         var err error
169         var nngpush = NngPush{}
170         nngpush.NewSocket = createNewStubPushSocketDialError
171         resetTestPushDataset(nngpush, stub.ValidEndpoints)
172
173         err = nngpush.AddEndpoint(rtmgr.Eps["10.0.0.1:0"])
174         if err == nil {
175                 t.Errorf("nngpush.AddEndpoint() was incorrect, got: %v, want: %v.", nil, "error")
176         }
177 }
178
179 /*
180 nngpush.DistributeAll() is tested for happy path case
181 */
182 func TestNngPushDistributeAll(t *testing.T) {
183         var err error
184         var nngpush = NngPush{}
185         nngpush.NewSocket = createNewStubPushSocket
186         resetTestPushDataset(nngpush, stub.ValidEndpoints)
187
188         err = nngpush.DistributeAll(stub.ValidPolicies)
189         if err != nil {
190                 t.Errorf("nngpush.DistributeAll(policies) was incorrect, got: %v, want: %v.", err, "nil")
191         }
192 }
193
194 /*
195 nngpush.DistributeAll() is tested for Sending error case
196 */
197 func TestNngPushDistributeAllSocketSendError(t *testing.T) {
198         var err error
199         var nngpush = NngPush{}
200         nngpush.NewSocket = createNewStubPushSocketSendError
201         resetTestPushDataset(nngpush, stub.ValidEndpoints)
202
203         err = nngpush.DistributeAll(stub.ValidPolicies)
204         if err != nil {
205                 t.Errorf("nngpush.DistributeAll(policies) was incorrect, got: %v, want: %v.", err, "nil")
206         }
207 }
208
209 func TestNngPushDeleteEndpoint(t *testing.T) {
210         var err error
211         var nngpush = NngPush{}
212         nngpush.NewSocket = createNewStubPushSocket
213         resetTestPushDataset(nngpush, stub.ValidEndpoints)
214
215         err = nngpush.DeleteEndpoint(rtmgr.Eps["10.0.0.1:0"])
216         if err != nil {
217                 t.Errorf("nngpush.DeleteEndpoint() was incorrect, got: %v, want: %v.", err, "nil")
218         }
219 }
220
221 func TestNngPushDeleteEndpointWithSocketCloseError(t *testing.T) {
222         var err error
223         var nngpush = NngPush{}
224         nngpush.NewSocket = createNewStubPushSocketCloseError
225         resetTestPushDataset(nngpush, stub.ValidEndpoints)
226
227         err = nngpush.DeleteEndpoint(rtmgr.Eps["10.1.1.1:0"])
228         if err == nil {
229                 t.Errorf("nngpush.DeleteEndpoint() was incorrect, got: %v, want: %v.", nil, "error")
230         }
231 }
232
233 /*
234 Initialize and send policies
235 */
236 func TestNngPushInitializeandsendPolicies(t *testing.T) {
237         var nngpush = NngPush{}
238         _,_ = createNewPushSocket()
239         policies := []string{"hello","welcome"}
240         nngpush.send(rtmgr.Eps["10.1.1.1:0"],&policies)
241 }
242
243 /*
244 func TestPipeEventHandler(t *testing.T) {
245         var sock mangos.Socket
246         var event mangos.PipeEvent
247         var pipe mangos.Pipe
248
249         var err error
250         sock, err = push.NewSocket()
251         sock.Dial("tcp://127.0.0.1:4555")
252         sock.SetPipeEventHook(pipeEventHandler)
253         pipeEventHandler(event,pipe)
254       t.Log(err)
255 }
256 */