blob: f0e30701535949bbd011c0d494ba416b4f5fdc7e [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"
Juha Hyttinen3cfee962020-02-13 13:51:00 +020024 "github.com/spf13/viper"
Mohamed Abukar2e78e422019-06-02 11:45:52 +030025 "net/http"
26 "net/http/httptest"
27 "os"
28 "strings"
29 "testing"
30 "time"
31)
32
Juha Hyttinen3cfee962020-02-13 13:51:00 +020033//var _ = func() bool {
34// testing.Init()
35// return true
36//}()
Mohamed Abukar5120ec12020-02-04 11:01:24 +020037
Juha Hyttinen3cfee962020-02-13 13:51:00 +020038type Consumer struct{}
Mohamed Abukar2e78e422019-06-02 11:45:52 +030039
Mohamed Abukarf11ab7a2019-08-14 16:55:01 +030040func (m Consumer) Consume(params *RMRParams) (err error) {
Mohamed Abukarf11ab7a2019-08-14 16:55:01 +030041 Sdl.Store("myKey", params.Payload)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030042 return nil
43}
44
45// Test cases
46func TestMain(m *testing.M) {
Mohamed Abukar791a77f2020-02-12 19:08:42 +020047 go RunWithParams(Consumer{}, viper.GetBool("db.waitForSdl"))
Mohamed Abukar5120ec12020-02-04 11:01:24 +020048 time.Sleep(time.Duration(5) * time.Second)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030049 code := m.Run()
50 os.Exit(code)
51}
52
53func TestGetHealthCheckRetursServiceUnavailableError(t *testing.T) {
54 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
Mohamed Abukar5120ec12020-02-04 11:01:24 +020055 /*response :=*/ executeRequest(req)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030056
Mohamed Abukar5120ec12020-02-04 11:01:24 +020057 //checkResponseCode(t, http.StatusServiceUnavailable, response.Code)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030058}
59
60func TestGetHealthCheckReturnsSuccess(t *testing.T) {
Mohamed Abukar2e78e422019-06-02 11:45:52 +030061 for Rmr.IsReady() == false {
62 time.Sleep(time.Duration(2) * time.Second)
63 }
64
65 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
66 response := executeRequest(req)
67
68 checkResponseCode(t, http.StatusOK, response.Code)
69}
70
71func TestInjectQuerySinglePath(t *testing.T) {
72 var handler = func(w http.ResponseWriter, r *http.Request) {
73 }
74
75 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar")
76
77 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar", nil)
78 response := executeRequest(req)
79 checkResponseCode(t, http.StatusOK, response.Code)
80}
81
82func TestInjectQueryMultiplePaths(t *testing.T) {
83 var handler = func(w http.ResponseWriter, r *http.Request) {
84 }
85
86 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
87
88 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil)
89 response := executeRequest(req)
90 checkResponseCode(t, http.StatusOK, response.Code)
91}
92
93func TestInjectQueryFailures(t *testing.T) {
94 var handler = func(w http.ResponseWriter, r *http.Request) {
95 }
96
97 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
98
99 req, _ := http.NewRequest("GET", "/ric/v1/user?invalid=bar&no=mykey", nil)
100 response := executeRequest(req)
101 checkResponseCode(t, http.StatusNotFound, response.Code)
102}
103
104func TestMessagesReceivedSuccessfully(t *testing.T) {
Mohamed Abukar9568a2d2020-02-18 16:50:32 +0200105 time.Sleep(time.Duration(5) * time.Second)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300106 for i := 0; i < 100; i++ {
Mohamed Abukarf11ab7a2019-08-14 16:55:01 +0300107 params := &RMRParams{}
108 params.Mtype = 10004
109 params.SubId = -1
110 params.Payload = []byte{1, 2, 3, 4, 5, 6}
Mohamed Abukar19461e12019-08-23 08:46:11 +0300111 params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
Mohamed Abukarf11ab7a2019-08-14 16:55:01 +0300112 params.Xid = "TestXID"
113 Rmr.SendMsg(params)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300114 }
115
116 // Allow time to process the messages
117 time.Sleep(time.Duration(2) * time.Second)
118
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200119 waitForSdl := viper.GetBool("db.waitForSdl")
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300120 stats := getMetrics(t)
121 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 100") {
Mohamed Abukar9568a2d2020-02-18 16:50:32 +0200122 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300123 }
124
125 if !strings.Contains(stats, "ricxapp_RMR_Received 100") {
Mohamed Abukar9568a2d2020-02-18 16:50:32 +0200126 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300127 }
128
129 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
130 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
131 }
132
133 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
134 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
135 }
136
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200137 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 100") {
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300138 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
139 }
140
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200141 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300142 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
143 }
144}
145
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300146func TestSubscribeChannels(t *testing.T) {
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200147 if !viper.GetBool("db.waitForSdl") {
148 return
149 }
150
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300151 var NotificationCb = func(ch string, events ...string) {
152 if ch != "channel1" {
153 t.Errorf("Error: Callback function called with incorrect params")
154 }
155 }
156
157 if err := Sdl.Subscribe(NotificationCb, "channel1"); err != nil {
158 t.Errorf("Error: Subscribe failed: %v", err)
159 }
160 time.Sleep(time.Duration(2) * time.Second)
161
162 if err := Sdl.StoreAndPublish("channel1", "event", "key1", "data1"); err != nil {
163 t.Errorf("Error: Publish failed: %v", err)
164 }
165}
166
Mohamed Abukar775722c2019-06-10 16:41:57 +0300167func TestGetRicMessageSuccess(t *testing.T) {
168 id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
169 if !ok || id != 12010 {
170 t.Errorf("Error: GetRicMessageId failed: id=%d", id)
171 }
172
173 name := Rmr.GetRicMessageName(12010)
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300174 if name != "RIC_SUB_REQ" {
Mohamed Abukar775722c2019-06-10 16:41:57 +0300175 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
176 }
177}
178
179func TestGetRicMessageFails(t *testing.T) {
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200180 ok := Rmr.IsRetryError(&RMRParams{status: 0})
Mohamed Abukar775722c2019-06-10 16:41:57 +0300181 if ok {
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200182 t.Errorf("Error: IsRetryError returned wrong value")
Mohamed Abukar775722c2019-06-10 16:41:57 +0300183 }
184
Mohamed Abukar791a77f2020-02-12 19:08:42 +0200185 ok = Rmr.IsRetryError(&RMRParams{status: 10})
186 if !ok {
187 t.Errorf("Error: IsRetryError returned wrong value")
188 }
189
190 ok = Rmr.IsNoEndPointError(&RMRParams{status: 5})
191 if ok {
192 t.Errorf("Error: IsNoEndPointError returned wrong value")
193 }
194
195 ok = Rmr.IsNoEndPointError(&RMRParams{status: 2})
196 if !ok {
197 t.Errorf("Error: IsNoEndPointError returned wrong value")
198 }
199}
200
201func TestIsErrorFunctions(t *testing.T) {
202 id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
203 if !ok || id != 12010 {
204 t.Errorf("Error: GetRicMessageId failed: id=%d", id)
205 }
206
207 name := Rmr.GetRicMessageName(12010)
208 if name != "RIC_SUB_REQ" {
209 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
Mohamed Abukar775722c2019-06-10 16:41:57 +0300210 }
211}
212
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300213func TestTeardown(t *testing.T) {
214 Sdl.Clear()
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300215}
216
217// Helper functions
218func executeRequest(req *http.Request) *httptest.ResponseRecorder {
219 rr := httptest.NewRecorder()
220 vars := map[string]string{"id": "1"}
221 req = mux.SetURLVars(req, vars)
222 Resource.router.ServeHTTP(rr, req)
223
224 return rr
225}
226
227func checkResponseCode(t *testing.T, expected, actual int) {
228 if expected != actual {
229 t.Errorf("Expected response code %d. Got %d\n", expected, actual)
230 }
231}
232
233func getMetrics(t *testing.T) string {
234 req, _ := http.NewRequest("GET", "/ric/v1/metrics", nil)
235 response := executeRequest(req)
236
237 return response.Body.String()
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300238}