Supporting of reading subscriptions from subscription manager while restarting rtmgr
[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         "testing"
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 NngPush, 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 nngpush.Initialize() method is empty, nothing to be tested
69 */
70 func TestNngPushInitialize(t *testing.T) {
71         var nngpush = NngPush{}
72
73         _ = nngpush.Initialize("")
74 }
75
76 /*
77 nngpush.Terminate() method is empty, nothing to be tested
78 */
79 func TestNngPushTerminate(t *testing.T) {
80         var nngpush = NngPush{}
81
82         _ = nngpush.Terminate()
83 }
84
85 /*
86 nngpush.UpdateEndpoints() is testd against stub.ValidXApps dataset
87 */
88 func TestNngPushUpdateEndpoints(t *testing.T) {
89         var nngpush = NngPush{}
90         resetTestPushDataset(nngpush, stub.ValidEndpoints)
91
92         nngpush.UpdateEndpoints(&stub.ValidRicComponents)
93         if rtmgr.Eps == nil {
94                 t.Errorf("nngpush.UpdateEndpoints() result was incorrect, got: %v, want: %v.", nil, "rtmgr.Endpoints")
95         }
96 }
97
98 /*
99 nngpush.AddEndpoint() is tested for happy path case
100 */
101 func TestNngPushAddEndpoint(t *testing.T) {
102 //      var err error
103         var nngpush = NngPush{}
104         resetTestPushDataset(nngpush, stub.ValidEndpoints)
105         _ = nngpush.AddEndpoint(rtmgr.Eps["localhost"])
106 /*      if err != nil {
107                 t.Errorf("nngpush.AddEndpoint() return was incorrect, got: %v, want: %v.", err, "nil")
108         }*/
109 }
110
111
112 /*
113 nngpush.DistributeAll() is tested for happy path case
114 */
115 func TestNngPushDistributeAll(t *testing.T) {
116         var err error
117         var nngpush = NngPush{}
118         resetTestPushDataset(nngpush, stub.ValidEndpoints)
119
120         err = nngpush.DistributeAll(stub.ValidPolicies)
121         if err != nil {
122                 t.Errorf("nngpush.DistributeAll(policies) was incorrect, got: %v, want: %v.", err, "nil")
123         }
124 }
125
126 /*
127 nngpush.DistributeToEp() is tested for Sending case
128 */
129 func TestDistributeToEp(t *testing.T) {
130         var err error
131         var nngpush = NngPush{}
132         resetTestPushDataset(nngpush, stub.ValidEndpoints)
133
134         err = nngpush.DistributeToEp(stub.ValidPolicies,rtmgr.Eps["localhost"])
135         if err != nil {
136                 t.Errorf("nngpush.DistributetoEp(policies) was incorrect, got: %v, want: %v.", err, "nil")
137         }
138 }
139
140 func TestDeleteEndpoint(t *testing.T) {
141         var err error
142         var nngpush = NngPush{}
143         resetTestPushDataset(nngpush, stub.ValidEndpoints)
144
145         err = nngpush.DeleteEndpoint(rtmgr.Eps["localhost"])
146         if err != nil {
147                 t.Errorf("nngpush.DeleteEndpoint() was incorrect, got: %v, want: %v.", err, "nil")
148         }
149 }
150
151 func TestCreateEndpoint(t *testing.T) {
152         var nngpush = NngPush{}
153         resetTestPushDataset(nngpush, stub.ValidEndpoints1)
154          nngpush.CreateEndpoint("192.168.0.1:0")
155          nngpush.CreateEndpoint("localhost:4560")
156 }
157 /*
158 Initialize and send policies
159 */
160 func TestNngPushInitializeandsendPolicies(t *testing.T) {
161         var nngpush = NngPush{}
162         resetTestPushDataset(nngpush, stub.ValidEndpoints)
163         policies := []string{"hello","welcome"}
164         nngpush.send(rtmgr.Eps["localhost"],&policies)
165 }