2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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.
18 This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 platform project (RICP).
21 ==================================================================================
24 Mnemonic: nngpush_test.go
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"
41 Returns an error free Socket instance
43 func createNewStubPushSocket() (NngSocket, error) {
44 socket := stub.MangosSocket{}
51 func createNewStubPushSocketError() (NngSocket, error) {
52 return nil, errors.New("stub generated Create Socket error")
56 Returns a Socket which always generates error on Close()
58 func createNewStubPushSocketCloseError() (NngSocket, error) {
59 socket := stub.MangosSocket{}
60 socket.GenerateSocketCloseError = true
65 Returns a Socket which always generates error on Send()
67 func createNewStubPushSocketSendError() (NngSocket, error) {
68 socket := stub.MangosSocket{}
69 socket.GenerateSocketSendError = true
74 Returns a Socket which always generates error on Dial()
76 func createNewStubPushSocketDialError() (NngSocket, error) {
77 socket := stub.MangosSocket{}
78 socket.GenerateSocketDialError = true
83 Resets the EndpointList according to argumnets
85 func resetTestPushDataset(instance NngPush, testdata []rtmgr.Endpoint) {
86 rtmgr.Eps = make(map[string]*rtmgr.Endpoint)
87 for _, endpoint := range testdata {
89 ep.Socket, _ = instance.NewSocket()
90 rtmgr.Eps[ep.Uuid] = &ep
95 nngpush.Initialize() method is empty, nothing to be tested
97 func TestNngPushInitialize(t *testing.T) {
98 var nngpush = NngPush{}
99 nngpush.NewSocket = createNewStubPushSocket
101 _ = nngpush.Initialize("")
105 nngpush.Terminate() method is empty, nothing to be tested
107 func TestNngPushTerminate(t *testing.T) {
108 var nngpush = NngPush{}
109 nngpush.NewSocket = createNewStubPushSocket
111 _ = nngpush.Terminate()
115 nngpush.UpdateEndpoints() is testd against stub.ValidXApps dataset
117 func TestNngPushUpdateEndpoints(t *testing.T) {
118 var nngpush = NngPush{}
119 nngpush.NewSocket = createNewStubPushSocket
120 rtmgr.Eps = make(rtmgr.Endpoints)
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")
129 nngpush.AddEndpoint() is tested for happy path case
131 func TestNngPushAddEndpoint(t *testing.T) {
133 var nngpush = NngPush{}
134 nngpush.NewSocket = createNewStubPushSocket
135 resetTestPushDataset(nngpush, stub.ValidEndpoints)
137 err = nngpush.AddEndpoint(rtmgr.Eps["10.0.0.1:0"])
139 t.Errorf("nngpush.AddEndpoint() return was incorrect, got: %v, want: %v.", err, "nil")
141 if rtmgr.Eps["10.0.0.1:0"].Socket == nil {
142 t.Errorf("nngpush.AddEndpoint() was incorrect, got: %v, want: %v.", nil, "Socket")
147 nngpush.AddEndpoint() is tested for Socket creating error case
149 func TestNngPushAddEndpointWithSocketError(t *testing.T) {
151 var nngpush = NngPush{}
152 nngpush.NewSocket = createNewStubPushSocketError
153 resetTestPushDataset(nngpush, stub.ValidEndpoints)
155 err = nngpush.AddEndpoint(rtmgr.Eps["10.0.0.1:0"])
157 t.Errorf("nngpush.AddEndpoint() was incorrect, got: %v, want: %v.", nil, "error")
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)
165 nngpush.AddEndpoint() is tested for Dialing error case
167 func TestNngPushAddEndpointWithSocketDialError(t *testing.T) {
169 var nngpush = NngPush{}
170 nngpush.NewSocket = createNewStubPushSocketDialError
171 resetTestPushDataset(nngpush, stub.ValidEndpoints)
173 err = nngpush.AddEndpoint(rtmgr.Eps["10.0.0.1:0"])
175 t.Errorf("nngpush.AddEndpoint() was incorrect, got: %v, want: %v.", nil, "error")
180 nngpush.DistributeAll() is tested for happy path case
182 func TestNngPushDistributeAll(t *testing.T) {
184 var nngpush = NngPush{}
185 nngpush.NewSocket = createNewStubPushSocket
186 resetTestPushDataset(nngpush, stub.ValidEndpoints)
188 err = nngpush.DistributeAll(stub.ValidPolicies)
190 t.Errorf("nngpush.DistributeAll(policies) was incorrect, got: %v, want: %v.", err, "nil")
195 nngpush.DistributeAll() is tested for Sending error case
197 func TestNngPushDistributeAllSocketSendError(t *testing.T) {
199 var nngpush = NngPush{}
200 nngpush.NewSocket = createNewStubPushSocketSendError
201 resetTestPushDataset(nngpush, stub.ValidEndpoints)
203 err = nngpush.DistributeAll(stub.ValidPolicies)
205 t.Errorf("nngpush.DistributeAll(policies) was incorrect, got: %v, want: %v.", err, "nil")
209 func TestNngPushDeleteEndpoint(t *testing.T) {
211 var nngpush = NngPush{}
212 nngpush.NewSocket = createNewStubPushSocket
213 resetTestPushDataset(nngpush, stub.ValidEndpoints)
215 err = nngpush.DeleteEndpoint(rtmgr.Eps["10.0.0.1:0"])
217 t.Errorf("nngpush.DeleteEndpoint() was incorrect, got: %v, want: %v.", err, "nil")
221 func TestNngPushDeleteEndpointWithSocketCloseError(t *testing.T) {
223 var nngpush = NngPush{}
224 nngpush.NewSocket = createNewStubPushSocketCloseError
225 resetTestPushDataset(nngpush, stub.ValidEndpoints)
227 err = nngpush.DeleteEndpoint(rtmgr.Eps["10.1.1.1:0"])
229 t.Errorf("nngpush.DeleteEndpoint() was incorrect, got: %v, want: %v.", nil, "error")
234 Initialize and send policies
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)
244 func TestPipeEventHandler(t *testing.T) {
245 var sock mangos.Socket
246 var event mangos.PipeEvent
250 sock, err = push.NewSocket()
251 sock.Dial("tcp://127.0.0.1:4555")
252 sock.SetPipeEventHook(pipeEventHandler)
253 pipeEventHandler(event,pipe)