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 Logger.Info("CASE: TestGetHealthCheckRetursServiceUnavailableError")
55 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
56 /*response :=*/ executeRequest(req)
58 //checkResponseCode(t, http.StatusServiceUnavailable, response.Code)
61 func TestGetHealthCheckReturnsSuccess(t *testing.T) {
62 Logger.Info("CASE: TestGetHealthCheckReturnsSuccess")
63 for Rmr.IsReady() == false {
64 time.Sleep(time.Duration(2) * time.Second)
67 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
68 response := executeRequest(req)
70 checkResponseCode(t, http.StatusOK, response.Code)
73 func TestInjectQuerySinglePath(t *testing.T) {
74 Logger.Info("CASE: TestInjectQuerySinglePath")
75 var handler = func(w http.ResponseWriter, r *http.Request) {
78 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar")
80 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar", nil)
81 response := executeRequest(req)
82 checkResponseCode(t, http.StatusOK, response.Code)
85 func TestInjectQueryMultiplePaths(t *testing.T) {
86 Logger.Info("CASE: TestInjectQueryMultiplePaths")
87 var handler = func(w http.ResponseWriter, r *http.Request) {
90 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
92 req, _ := http.NewRequest("GET", "/ric/v1/user?foo=bar&id=mykey", nil)
93 response := executeRequest(req)
94 checkResponseCode(t, http.StatusOK, response.Code)
97 func TestInjectQueryFailures(t *testing.T) {
98 Logger.Info("CASE: TestInjectQueryFailures")
99 var handler = func(w http.ResponseWriter, r *http.Request) {
102 Resource.InjectQueryRoute("/ric/v1/user", handler, "GET", "foo", "bar", "id", "mykey")
104 req, _ := http.NewRequest("GET", "/ric/v1/user?invalid=bar&no=mykey", nil)
105 response := executeRequest(req)
106 checkResponseCode(t, http.StatusNotFound, response.Code)
109 func TestMessagesReceivedSuccessfully(t *testing.T) {
110 Logger.Info("CASE: TestMessagesReceivedSuccessfully")
111 time.Sleep(time.Duration(5) * time.Second)
112 for i := 0; i < 100; i++ {
113 params := &RMRParams{}
116 params.Payload = []byte{1, 2, 3, 4, 5, 6}
117 params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
118 params.Xid = "TestXID"
122 // Allow time to process the messages
123 time.Sleep(time.Duration(5) * time.Second)
125 waitForSdl := viper.GetBool("db.waitForSdl")
126 stats := getMetrics(t)
127 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 100") {
128 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
131 if !strings.Contains(stats, "ricxapp_RMR_Received 100") {
132 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
135 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
136 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
139 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
140 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
143 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 100") {
144 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
147 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
148 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
152 func TestMessagesReceivedSuccessfullyUsingWh(t *testing.T) {
153 Logger.Info("CASE: TestMessagesReceivedSuccessfullyUsingWh")
154 time.Sleep(time.Duration(5) * time.Second)
155 whid := Rmr.Openwh("localhost:4560")
156 time.Sleep(time.Duration(1) * time.Second)
157 for i := 0; i < 100; i++ {
158 params := &RMRParams{}
161 params.Payload = []byte{1, 2, 3, 4, 5, 6}
162 params.Meid = &RMRMeid{PlmnID: "1234", EnbID: "7788", RanName: "RanName-1234"}
163 params.Xid = "TestXID"
164 params.Whid = int(whid)
168 // Allow time to process the messages
169 time.Sleep(time.Duration(5) * time.Second)
171 waitForSdl := viper.GetBool("db.waitForSdl")
172 stats := getMetrics(t)
173 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") {
174 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
177 if !strings.Contains(stats, "ricxapp_RMR_Received 200") {
178 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
181 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 0") {
182 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
185 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
186 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
189 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 200") {
190 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
193 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
194 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
196 Rmr.Closewh(int(whid))
199 func TestMessagesReceivedSuccessfullyUsingWhCall(t *testing.T) {
200 Logger.Info("CASE: TestMessagesReceivedSuccessfullyUsingWhCall")
201 time.Sleep(time.Duration(5) * time.Second)
202 whid := Rmr.Openwh("localhost:4560")
203 params := &RMRParams{}
204 params.Payload = []byte("newrt|start\nnewrt|end\n")
205 params.Whid = int(whid)
207 params.Timeout = 1000
208 Rmr.SendCallMsg(params)
210 // Allow time to process the messages
211 time.Sleep(time.Duration(2) * time.Second)
213 waitForSdl := viper.GetBool("db.waitForSdl")
214 stats := getMetrics(t)
215 if !strings.Contains(stats, "ricxapp_RMR_Transmitted 200") {
216 t.Errorf("Error: ricxapp_RMR_Transmitted value incorrect: %v", stats)
219 if !strings.Contains(stats, "ricxapp_RMR_Received 201") {
220 t.Errorf("Error: ricxapp_RMR_Received value incorrect: %v", stats)
223 if !strings.Contains(stats, "ricxapp_RMR_TransmitError 1") {
224 t.Errorf("Error: ricxapp_RMR_TransmitError value incorrect")
227 if !strings.Contains(stats, "ricxapp_RMR_ReceiveError 0") {
228 t.Errorf("Error: ricxapp_RMR_ReceiveError value incorrect")
231 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_Stored 201") {
232 t.Errorf("Error: ricxapp_SDL_Stored value incorrect")
235 if waitForSdl && !strings.Contains(stats, "ricxapp_SDL_StoreError 0") {
236 t.Errorf("Error: ricxapp_SDL_StoreError value incorrect")
238 Rmr.Closewh(int(whid))
241 func TestSubscribeChannels(t *testing.T) {
242 Logger.Info("CASE: TestSubscribeChannels")
243 if !viper.GetBool("db.waitForSdl") {
247 var NotificationCb = func(ch string, events ...string) {
248 if ch != "channel1" {
249 t.Errorf("Error: Callback function called with incorrect params")
253 if err := Sdl.Subscribe(NotificationCb, "channel1"); err != nil {
254 t.Errorf("Error: Subscribe failed: %v", err)
256 time.Sleep(time.Duration(2) * time.Second)
258 if err := Sdl.StoreAndPublish("channel1", "event", "key1", "data1"); err != nil {
259 t.Errorf("Error: Publish failed: %v", err)
263 func TestGetRicMessageSuccess(t *testing.T) {
264 Logger.Info("CASE: TestGetRicMessageSuccess")
265 id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
266 if !ok || id != 12010 {
267 t.Errorf("Error: GetRicMessageId failed: id=%d", id)
270 name := Rmr.GetRicMessageName(12010)
271 if name != "RIC_SUB_REQ" {
272 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
276 func TestGetRicMessageFails(t *testing.T) {
277 Logger.Info("CASE: TestGetRicMessageFails")
278 ok := Rmr.IsRetryError(&RMRParams{status: 0})
280 t.Errorf("Error: IsRetryError returned wrong value")
283 ok = Rmr.IsRetryError(&RMRParams{status: 10})
285 t.Errorf("Error: IsRetryError returned wrong value")
288 ok = Rmr.IsNoEndPointError(&RMRParams{status: 5})
290 t.Errorf("Error: IsNoEndPointError returned wrong value")
293 ok = Rmr.IsNoEndPointError(&RMRParams{status: 2})
295 t.Errorf("Error: IsNoEndPointError returned wrong value")
299 func TestIsErrorFunctions(t *testing.T) {
300 Logger.Info("CASE: TestIsErrorFunctions")
301 id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
302 if !ok || id != 12010 {
303 t.Errorf("Error: GetRicMessageId failed: id=%d", id)
306 name := Rmr.GetRicMessageName(12010)
307 if name != "RIC_SUB_REQ" {
308 t.Errorf("Error: GetRicMessageName failed: name=%s", name)
312 func TestTeardown(t *testing.T) {
313 Logger.Info("CASE: TestTeardown")
318 func executeRequest(req *http.Request) *httptest.ResponseRecorder {
319 rr := httptest.NewRecorder()
320 vars := map[string]string{"id": "1"}
321 req = mux.SetURLVars(req, vars)
322 Resource.router.ServeHTTP(rr, req)
327 func checkResponseCode(t *testing.T, expected, actual int) {
328 if expected != actual {
329 t.Errorf("Expected response code %d. Got %d\n", expected, actual)
333 func getMetrics(t *testing.T) string {
334 req, _ := http.NewRequest("GET", "/ric/v1/metrics", nil)
335 response := executeRequest(req)
337 return response.Body.String()