blob: 06345afba010a1bdd75b7690a6f3daca3aa43cb5 [file] [log] [blame]
Mohamed Abukar2e78e422019-06-02 11:45:52 +03001/*
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*/
19
20package xapp
21
22import (
23 "github.com/gorilla/mux"
24 "net/http"
25 "net/http/httptest"
26 "os"
27 "strings"
28 "testing"
29 "time"
30)
31
32type Consumer struct {
33}
34
35func (m Consumer) Consume(mtype, sid, len int, payload []byte) (err error) {
36 Sdl.Store("myKey", payload)
37 return nil
38}
39
40// Test cases
41func TestMain(m *testing.M) {
42 // Just run on the background (for coverage)
43 go Run(Consumer{})
44
45 code := m.Run()
46 os.Exit(code)
47}
48
49func TestGetHealthCheckRetursServiceUnavailableError(t *testing.T) {
50 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
51 response := executeRequest(req)
52
53 checkResponseCode(t, http.StatusServiceUnavailable, response.Code)
54}
55
56func TestGetHealthCheckReturnsSuccess(t *testing.T) {
57 // Wait until RMR is up-and-running
58 for Rmr.IsReady() == false {
59 time.Sleep(time.Duration(2) * time.Second)
60 }
61
62 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
63 response := executeRequest(req)
64
65 checkResponseCode(t, http.StatusOK, response.Code)
66}
67
68func TestInjectQuerySinglePath(t *testing.T) {
69 var handler = func(w http.ResponseWriter, r *http.Request) {
70 }
71
72 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar")
73
74 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar", nil)
75 response := executeRequest(req)
76 checkResponseCode(t, http.StatusOK, response.Code)
77}
78
79func TestInjectQueryMultiplePaths(t *testing.T) {
80 var handler = func(w http.ResponseWriter, r *http.Request) {
81 }
82
83 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
84
85 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil)
86 response := executeRequest(req)
87 checkResponseCode(t, http.StatusOK, response.Code)
88}
89
90func TestInjectQueryFailures(t *testing.T) {
91 var handler = func(w http.ResponseWriter, r *http.Request) {
92 }
93
94 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
95
96 req, _ := http.NewRequest("GET", "/ric/v1/user?invalid=bar&no=mykey", nil)
97 response := executeRequest(req)
98 checkResponseCode(t, http.StatusNotFound, response.Code)
99}
100
101func TestMessagesReceivedSuccessfully(t *testing.T) {
102 for i := 0; i < 100; i++ {
103 Rmr.Send(10004, 1111, 100, []byte{1, 2, 3, 4, 5, 6})
104 }
105
106 // Allow time to process the messages
107 time.Sleep(time.Duration(2) * time.Second)
108
109 stats := getMetrics(t)
110 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 100") {
111 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect")
112 }
113
114 if !strings.Contains(stats, "ricxapp_RMR_Received 100") {
115 t.Errorf("Error: ricxapp_RMR_Received value incorrect")
116 }
117
118 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
119 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
120 }
121
122 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
123 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
124 }
125
126 if !strings.Contains(stats, "ricxapp_SDL_Stored 100") {
127 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
128 }
129
130 if !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
131 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
132 }
133}
134
135func TestGetgNBList(t *testing.T) {
136 Rnib.Store("Kiikale", "Hello")
137 Rnib.Store("mykey", "myval")
138
139 v, _ := Rnib.GetgNBList()
140 if v["Kiikale"] != "Hello" || v["mykey"] != "myval" {
141 t.Errorf("Error: GetgNBList failed!")
142 }
143}
144
145func TestSubscribeChannels(t *testing.T) {
146 var NotificationCb = func(ch string, events ...string) {
147 if ch != "channel1" {
148 t.Errorf("Error: Callback function called with incorrect params")
149 }
150 }
151
152 if err := Sdl.Subscribe(NotificationCb, "channel1"); err != nil {
153 t.Errorf("Error: Subscribe failed: %v", err)
154 }
155 time.Sleep(time.Duration(2) * time.Second)
156
157 if err := Sdl.StoreAndPublish("channel1", "event", "key1", "data1"); err != nil {
158 t.Errorf("Error: Publish failed: %v", err)
159 }
160}
161
Mohamed Abukar775722c2019-06-10 16:41:57 +0300162func TestGetRicMessageSuccess(t *testing.T) {
163 id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
164 if !ok || id != 12010 {
165 t.Errorf("Error: GetRicMessageId failed: id=%d", id)
166 }
167
168 name := Rmr.GetRicMessageName(12010)
169 if name != "RIC_SUB_REQ" {
170 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
171 }
172}
173
174func TestGetRicMessageFails(t *testing.T) {
175 id, ok := Rmr.GetRicMessageId("INVALID")
176 if ok {
177 t.Errorf("Error: GetRicMessageId returned invalid value id=%d", id)
178 }
179
180 name := Rmr.GetRicMessageName(123456)
181 if name != "" {
182 t.Errorf("Error: GetRicMessageName returned invalid value: name=%s", name)
183 }
184}
185
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300186func TestTeardown(t *testing.T) {
187 Sdl.Clear()
188 Rnib.Clear()
189}
190
191// Helper functions
192func executeRequest(req *http.Request) *httptest.ResponseRecorder {
193 rr := httptest.NewRecorder()
194 vars := map[string]string{"id": "1"}
195 req = mux.SetURLVars(req, vars)
196 Resource.router.ServeHTTP(rr, req)
197
198 return rr
199}
200
201func checkResponseCode(t *testing.T, expected, actual int) {
202 if expected != actual {
203 t.Errorf("Expected response code %d. Got %d\n", expected, actual)
204 }
205}
206
207func getMetrics(t *testing.T) string {
208 req, _ := http.NewRequest("GET", "/ric/v1/metrics", nil)
209 response := executeRequest(req)
210
211 return response.Body.String()
212}