Moving to ubuntu 18 and fixing UT's
[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         "time"
35         "os"
36         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
37         //"nanomsg.org/go/mangos/v2"
38         //_ "nanomsg.org/go/mangos/v2/transport/all"
39         //"nanomsg.org/go/mangos/v2/protocol/push"
40         "testing"
41 )
42
43 type Consumer struct{}
44
45 func (m Consumer) Consume(params *xapp.RMRParams) (err error) {
46         xapp.Sdl.Store("myKey", params.Payload)
47         return nil
48 }
49
50 // Test cases
51 func TestMain(m *testing.M) {
52         go xapp.RunWithParams(Consumer{}, false)
53         time.Sleep(time.Duration(5) * time.Second)
54         code := m.Run()
55         os.Exit(code)
56 }
57
58 /*
59 Resets the EndpointList according to argumnets
60 */
61 func resetTestPushDataset(instance NngPush, testdata []rtmgr.Endpoint) {
62         rtmgr.Eps = make(map[string]*rtmgr.Endpoint)
63         for _, endpoint := range testdata {
64                 ep := endpoint
65                 //ep.Socket, _ = instance.NewSocket()
66                 rtmgr.Eps[ep.Uuid] = &ep
67         }
68 }
69
70 /*
71 nngpush.Initialize() method is empty, nothing to be tested
72 */
73 func TestNngPushInitialize(t *testing.T) {
74         var nngpush = NngPush{}
75
76         _ = nngpush.Initialize("")
77 }
78
79 /*
80 nngpush.Terminate() method is empty, nothing to be tested
81 */
82 func TestNngPushTerminate(t *testing.T) {
83         var nngpush = NngPush{}
84
85         _ = nngpush.Terminate()
86 }
87
88 /*
89 nngpush.UpdateEndpoints() is testd against stub.ValidXApps dataset
90 */
91 func TestNngPushUpdateEndpoints(t *testing.T) {
92         var nngpush = NngPush{}
93         resetTestPushDataset(nngpush, stub.ValidEndpoints)
94
95         nngpush.UpdateEndpoints(&stub.ValidRicComponents)
96         if rtmgr.Eps == nil {
97                 t.Errorf("nngpush.UpdateEndpoints() result was incorrect, got: %v, want: %v.", nil, "rtmgr.Endpoints")
98         }
99 }
100
101 /*
102 nngpush.AddEndpoint() is tested for happy path case
103 */
104 func TestNngPushAddEndpoint(t *testing.T) {
105         var err error
106         var nngpush = NngPush{}
107         resetTestPushDataset(nngpush, stub.ValidEndpoints)
108         err = nngpush.AddEndpoint(rtmgr.Eps["localhost"])
109         if err != nil {
110                 t.Errorf("nngpush.AddEndpoint() return was incorrect, got: %v, want: %v.", err, "nil")
111         }
112 }
113
114
115 /*
116 nngpush.DistributeAll() is tested for happy path case
117 */
118 func TestNngPushDistributeAll(t *testing.T) {
119         var err error
120         var nngpush = NngPush{}
121         resetTestPushDataset(nngpush, stub.ValidEndpoints)
122
123         err = nngpush.DistributeAll(stub.ValidPolicies)
124         if err != nil {
125                 t.Errorf("nngpush.DistributeAll(policies) was incorrect, got: %v, want: %v.", err, "nil")
126         }
127 }
128
129 /*
130 nngpush.DistributeToEp() is tested for Sending case
131 */
132 func TestDistributeToEp(t *testing.T) {
133         var err error
134         var nngpush = NngPush{}
135         resetTestPushDataset(nngpush, stub.ValidEndpoints)
136
137         err = nngpush.DistributeToEp(stub.ValidPolicies,rtmgr.Eps["localhost"])
138         if err != nil {
139                 t.Errorf("nngpush.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 nngpush = NngPush{}
146         resetTestPushDataset(nngpush, stub.ValidEndpoints)
147
148         err = nngpush.DeleteEndpoint(rtmgr.Eps["localhost"])
149         if err != nil {
150                 t.Errorf("nngpush.DeleteEndpoint() was incorrect, got: %v, want: %v.", err, "nil")
151         }
152 }
153
154 func TestCreateEndpoint(t *testing.T) {
155         var nngpush = NngPush{}
156         resetTestPushDataset(nngpush, stub.ValidEndpoints1)
157          nngpush.CreateEndpoint("192.168.0.1:0")
158          nngpush.CreateEndpoint("localhost:4560")
159 }
160 /*
161 Initialize and send policies
162 */
163 func TestNngPushInitializeandsendPolicies(t *testing.T) {
164         var nngpush = NngPush{}
165         resetTestPushDataset(nngpush, stub.ValidEndpoints)
166         policies := []string{"hello","welcome"}
167         nngpush.send(rtmgr.Eps["localhost"],&policies)
168 }