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"
32 type Consumer struct {
35 func (m Consumer) Consume(mtype, sid, len int, payload []byte) (err error) {
36 Sdl.Store("myKey", payload)
41 func TestMain(m *testing.M) {
42 // Just run on the background (for coverage)
49 func TestGetHealthCheckRetursServiceUnavailableError(t *testing.T) {
50 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
51 response := executeRequest(req)
53 checkResponseCode(t, http.StatusServiceUnavailable, response.Code)
56 func 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)
62 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
63 response := executeRequest(req)
65 checkResponseCode(t, http.StatusOK, response.Code)
68 func TestInjectQuerySinglePath(t *testing.T) {
69 var handler = func(w http.ResponseWriter, r *http.Request) {
72 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar")
74 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar", nil)
75 response := executeRequest(req)
76 checkResponseCode(t, http.StatusOK, response.Code)
79 func TestInjectQueryMultiplePaths(t *testing.T) {
80 var handler = func(w http.ResponseWriter, r *http.Request) {
83 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
85 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil)
86 response := executeRequest(req)
87 checkResponseCode(t, http.StatusOK, response.Code)
90 func TestInjectQueryFailures(t *testing.T) {
91 var handler = func(w http.ResponseWriter, r *http.Request) {
94 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
96 req, _ := http.NewRequest("GET", "/ric/v1/user?invalid=bar&no=mykey", nil)
97 response := executeRequest(req)
98 checkResponseCode(t, http.StatusNotFound, response.Code)
101 func TestMessagesReceivedSuccessfully(t *testing.T) {
102 for i := 0; i < 100; i++ {
103 Rmr.Send(10004, 1111, 100, []byte{1, 2, 3, 4, 5, 6})
106 // Allow time to process the messages
107 time.Sleep(time.Duration(2) * time.Second)
109 stats := getMetrics(t)
110 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 100") {
111 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect")
114 if !strings.Contains(stats, "ricxapp_RMR_Received 100") {
115 t.Errorf("Error: ricxapp_RMR_Received value incorrect")
118 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
119 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
122 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
123 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
126 if !strings.Contains(stats, "ricxapp_SDL_Stored 100") {
127 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
130 if !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
131 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
135 func TestGetgNBList(t *testing.T) {
136 Rnib.Store("Kiikale", "Hello")
137 Rnib.Store("mykey", "myval")
139 v, _ := Rnib.GetgNBList()
140 if v["Kiikale"] != "Hello" || v["mykey"] != "myval" {
141 t.Errorf("Error: GetgNBList failed!")
145 func 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")
152 if err := Sdl.Subscribe(NotificationCb, "channel1"); err != nil {
153 t.Errorf("Error: Subscribe failed: %v", err)
155 time.Sleep(time.Duration(2) * time.Second)
157 if err := Sdl.StoreAndPublish("channel1", "event", "key1", "data1"); err != nil {
158 t.Errorf("Error: Publish failed: %v", err)
162 func 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)
168 name := Rmr.GetRicMessageName(12010)
169 if name != "RIC_SUB_REQ" {
170 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
174 func TestGetRicMessageFails(t *testing.T) {
175 id, ok := Rmr.GetRicMessageId("INVALID")
177 t.Errorf("Error: GetRicMessageId returned invalid value id=%d", id)
180 name := Rmr.GetRicMessageName(123456)
182 t.Errorf("Error: GetRicMessageName returned invalid value: name=%s", name)
186 func TestTeardown(t *testing.T) {
192 func 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)
201 func checkResponseCode(t *testing.T, expected, actual int) {
202 if expected != actual {
203 t.Errorf("Expected response code %d. Got %d\n", expected, actual)
207 func getMetrics(t *testing.T) string {
208 req, _ := http.NewRequest("GET", "/ric/v1/metrics", nil)
209 response := executeRequest(req)
211 return response.Body.String()