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.
17 ==================================================================================
23 "github.com/gorilla/mux"
24 "github.com/spf13/viper"
33 //var _ = func() bool {
38 type Consumer struct{}
40 func (m Consumer) Consume(params *RMRParams) (err error) {
41 Sdl.Store("myKey", params.Payload)
46 func TestMain(m *testing.M) {
47 go RunWithParams(Consumer{}, viper.GetBool("db.waitForSdl"))
48 time.Sleep(time.Duration(5) * time.Second)
53 func TestGetHealthCheckRetursServiceUnavailableError(t *testing.T) {
54 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
55 /*response :=*/ executeRequest(req)
57 //checkResponseCode(t, http.StatusServiceUnavailable, response.Code)
60 func TestGetHealthCheckReturnsSuccess(t *testing.T) {
61 for Rmr.IsReady() == false {
62 time.Sleep(time.Duration(2) * time.Second)
65 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
66 response := executeRequest(req)
68 checkResponseCode(t, http.StatusOK, response.Code)
71 func TestInjectQuerySinglePath(t *testing.T) {
72 var handler = func(w http.ResponseWriter, r *http.Request) {
75 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar")
77 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar", nil)
78 response := executeRequest(req)
79 checkResponseCode(t, http.StatusOK, response.Code)
82 func TestInjectQueryMultiplePaths(t *testing.T) {
83 var handler = func(w http.ResponseWriter, r *http.Request) {
86 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
88 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil)
89 response := executeRequest(req)
90 checkResponseCode(t, http.StatusOK, response.Code)
93 func TestInjectQueryFailures(t *testing.T) {
94 var handler = func(w http.ResponseWriter, r *http.Request) {
97 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
99 req, _ := http.NewRequest("GET", "/ric/v1/user?invalid=bar&no=mykey", nil)
100 response := executeRequest(req)
101 checkResponseCode(t, http.StatusNotFound, response.Code)
104 func TestMessagesReceivedSuccessfully(t *testing.T) {
105 time.Sleep(time.Duration(5) * time.Second)
106 for i := 0; i < 100; i++ {
107 params := &RMRParams{}
110 params.Payload = []byte{1, 2, 3, 4, 5, 6}
111 params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
112 params.Xid = "TestXID"
116 // Allow time to process the messages
117 time.Sleep(time.Duration(2) * time.Second)
119 waitForSdl := viper.GetBool("db.waitForSdl")
120 stats := getMetrics(t)
121 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 100") {
122 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
125 if !strings.Contains(stats, "ricxapp_RMR_Received 100") {
126 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
129 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
130 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
133 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
134 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
137 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 100") {
138 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
141 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
142 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
146 func TestMessagesReceivedSuccessfullyUsingWh(t *testing.T) {
147 time.Sleep(time.Duration(5) * time.Second)
148 whid := Rmr.Openwh("localhost:4560")
149 time.Sleep(time.Duration(1) * time.Second)
150 for i := 0; i < 100; i++ {
151 params := &RMRParams{}
154 params.Payload = []byte{1, 2, 3, 4, 5, 6}
155 params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
156 params.Xid = "TestXID"
157 params.Whid = int(whid)
161 // Allow time to process the messages
162 time.Sleep(time.Duration(2) * time.Second)
164 waitForSdl := viper.GetBool("db.waitForSdl")
165 stats := getMetrics(t)
166 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") {
167 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
170 if !strings.Contains(stats, "ricxapp_RMR_Received 200") {
171 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
174 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
175 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
178 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
179 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
182 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 200") {
183 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
186 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
187 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
189 Rmr.Closewh(int(whid))
192 func TestMessagesReceivedSuccessfullyUsingWhCall(t *testing.T) {
193 time.Sleep(time.Duration(5) * time.Second)
194 whid := Rmr.Openwh("localhost:4560")
195 params := &RMRParams{}
196 params.Payload = []byte("newrt|start\nnewrt|end\n")
197 params.Whid = int(whid)
199 params.Timeout = 1000
200 Rmr.SendCallMsg(params)
202 // Allow time to process the messages
203 time.Sleep(time.Duration(2) * time.Second)
205 waitForSdl := viper.GetBool("db.waitForSdl")
206 stats := getMetrics(t)
207 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") {
208 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
211 if !strings.Contains(stats, "ricxapp_RMR_Received 201") {
212 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
215 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 1") {
216 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
219 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
220 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
223 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 201") {
224 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
227 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
228 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
230 Rmr.Closewh(int(whid))
233 func TestSubscribeChannels(t *testing.T) {
234 if !viper.GetBool("db.waitForSdl") {
238 var NotificationCb = func(ch string, events ...string) {
239 if ch != "channel1" {
240 t.Errorf("Error: Callback function called with incorrect params")
244 if err := Sdl.Subscribe(NotificationCb, "channel1"); err != nil {
245 t.Errorf("Error: Subscribe failed: %v", err)
247 time.Sleep(time.Duration(2) * time.Second)
249 if err := Sdl.StoreAndPublish("channel1", "event", "key1", "data1"); err != nil {
250 t.Errorf("Error: Publish failed: %v", err)
254 func TestGetRicMessageSuccess(t *testing.T) {
255 id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
256 if !ok || id != 12010 {
257 t.Errorf("Error: GetRicMessageId failed: id=%d", id)
260 name := Rmr.GetRicMessageName(12010)
261 if name != "RIC_SUB_REQ" {
262 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
266 func TestGetRicMessageFails(t *testing.T) {
267 ok := Rmr.IsRetryError(&RMRParams{status: 0})
269 t.Errorf("Error: IsRetryError returned wrong value")
272 ok = Rmr.IsRetryError(&RMRParams{status: 10})
274 t.Errorf("Error: IsRetryError returned wrong value")
277 ok = Rmr.IsNoEndPointError(&RMRParams{status: 5})
279 t.Errorf("Error: IsNoEndPointError returned wrong value")
282 ok = Rmr.IsNoEndPointError(&RMRParams{status: 2})
284 t.Errorf("Error: IsNoEndPointError returned wrong value")
288 func TestIsErrorFunctions(t *testing.T) {
289 id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
290 if !ok || id != 12010 {
291 t.Errorf("Error: GetRicMessageId failed: id=%d", id)
294 name := Rmr.GetRicMessageName(12010)
295 if name != "RIC_SUB_REQ" {
296 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
300 func TestTeardown(t *testing.T) {
305 func executeRequest(req *http.Request) *httptest.ResponseRecorder {
306 rr := httptest.NewRecorder()
307 vars := map[string]string{"id": "1"}
308 req = mux.SetURLVars(req, vars)
309 Resource.router.ServeHTTP(rr, req)
314 func checkResponseCode(t *testing.T, expected, actual int) {
315 if expected != actual {
316 t.Errorf("Expected response code %d. Got %d\n", expected, actual)
320 func getMetrics(t *testing.T) string {
321 req, _ := http.NewRequest("GET", "/ric/v1/metrics", nil)
322 response := executeRequest(req)
324 return response.Body.String()