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 ==================================================================================
26 sdl "gerrit.oran-osc.org/r/ric-plt/sdlgo"
27 "github.com/spf13/viper"
35 var resp SubscriptionResp
38 func TestNoSubscriptionsFound(t *testing.T) {
39 req, _ := http.NewRequest("GET", "/ric/v1/subscriptions", nil)
40 response := executeRequest(req)
42 checkResponseCode(t, http.StatusOK, response.Code)
43 if body := response.Body.String(); body != "[]" {
44 t.Errorf("handler returned unexpected body: got %v want []", body)
48 func TestAddNewSubscription(t *testing.T) {
49 payload := []byte(`{"maxRetries": 3, "retryTimer": 5, "eventType":"Created", "targetUrl": "http://localhost:8087/xapps_handler"}`)
50 req, _ := http.NewRequest("POST", "/ric/v1/subscriptions", bytes.NewBuffer(payload))
51 response := executeRequest(req)
53 checkResponseCode(t, http.StatusCreated, response.Code)
55 json.NewDecoder(response.Body).Decode(&resp)
56 if resp.Version != 0 {
57 t.Errorf("Creating new subscription failed: %v", resp)
61 func TestGettAllSubscriptions(t *testing.T) {
62 req, _ := http.NewRequest("GET", "/ric/v1/subscriptions", nil)
63 response := executeRequest(req)
65 checkResponseCode(t, http.StatusOK, response.Code)
67 var subscriptions []SubscriptionReq
68 json.NewDecoder(response.Body).Decode(&subscriptions)
70 verifySubscription(t, subscriptions[0], "http://localhost:8087/xapps_handler", 3, 5, "Created")
73 func TestGetSingleSubscription(t *testing.T) {
74 req, _ := http.NewRequest("GET", "/ric/v1/subscriptions/"+resp.Id, nil)
75 response := executeRequest(req)
77 checkResponseCode(t, http.StatusOK, response.Code)
79 var subscription SubscriptionReq
80 json.NewDecoder(response.Body).Decode(&subscription)
82 verifySubscription(t, subscription, "http://localhost:8087/xapps_handler", 3, 5, "Created")
85 func TestUpdateSingleSubscription(t *testing.T) {
86 payload := []byte(`{"maxRetries": 11, "retryTimer": 22, "eventType":"Deleted", "targetUrl": "http://localhost:8088/xapps_handler"}`)
88 req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/"+resp.Id, bytes.NewBuffer(payload))
89 response := executeRequest(req)
91 checkResponseCode(t, http.StatusOK, response.Code)
93 var res SubscriptionResp
94 json.NewDecoder(response.Body).Decode(&res)
96 t.Errorf("handler returned unexpected data: %v", resp)
99 // Check that the subscription is updated properly
100 req, _ = http.NewRequest("GET", "/ric/v1/subscriptions/"+resp.Id, nil)
101 response = executeRequest(req)
102 checkResponseCode(t, http.StatusOK, response.Code)
104 var subscription SubscriptionReq
105 json.NewDecoder(response.Body).Decode(&subscription)
107 verifySubscription(t, subscription, "http://localhost:8088/xapps_handler", 11, 22, "Deleted")
110 func TestDeleteSingleSubscription(t *testing.T) {
111 req, _ := http.NewRequest("DELETE", "/ric/v1/subscriptions/"+resp.Id, nil)
112 response := executeRequest(req)
114 checkResponseCode(t, http.StatusNoContent, response.Code)
116 // Check that the subscription is removed properly
117 req, _ = http.NewRequest("GET", "/ric/v1/subscriptions/"+resp.Id, nil)
118 response = executeRequest(req)
119 checkResponseCode(t, http.StatusNotFound, response.Code)
122 func TestDeleteSingleSubscriptionFails(t *testing.T) {
123 req, _ := http.NewRequest("DELETE", "/ric/v1/subscriptions/invalidSubscriptionId", nil)
124 response := executeRequest(req)
126 checkResponseCode(t, http.StatusNotFound, response.Code)
129 func TestAddSingleSubscriptionFailsBodyEmpty(t *testing.T) {
130 req, _ := http.NewRequest("POST", "/ric/v1/subscriptions/"+resp.Id, nil)
131 response := executeRequest(req)
133 checkResponseCode(t, http.StatusMethodNotAllowed, response.Code)
136 func TestUpdateeSingleSubscriptionFailsBodyEmpty(t *testing.T) {
137 req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/"+resp.Id, nil)
138 response := executeRequest(req)
140 checkResponseCode(t, http.StatusMethodNotAllowed, response.Code)
143 func TestUpdateeSingleSubscriptionFailsInvalidId(t *testing.T) {
144 payload := []byte(`{"maxRetries": 11, "retryTimer": 22, "eventType":"Deleted", "targetUrl": "http://localhost:8088/xapps_handler"}`)
146 req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/invalidSubscriptionId"+resp.Id, bytes.NewBuffer(payload))
147 response := executeRequest(req)
149 checkResponseCode(t, http.StatusNotFound, response.Code)
152 func TestPublishXappAction(t *testing.T) {
153 payload := []byte(`{"maxRetries": 3, "retryTimer": 5, "eventType":"Created", "targetUrl": "http://127.0.0.1:8888"}`)
154 req, _ := http.NewRequest("POST", "/ric/v1/subscriptions", bytes.NewBuffer(payload))
155 response := executeRequest(req)
157 checkResponseCode(t, http.StatusCreated, response.Code)
159 // Create a RestApi server (simulating RM)
160 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
161 fmt.Fprintln(w, "Hello, XM!")
164 l, err := net.Listen("tcp", "127.0.0.1:8888")
174 x.sd.Publish(xapp, EventType("created"))
177 func TestTeardown(t *testing.T) {
178 db := sdl.NewSdlInstance(viper.GetString("db.sessionNamespace"), sdl.NewDatabase())
182 func verifySubscription(t *testing.T, subscription SubscriptionReq, url string, retries int, timer int, event string) {
183 if subscription.TargetUrl != url {
184 t.Errorf("Unexpected url: got=%s expected=%s", subscription.TargetUrl, url)
187 if subscription.MaxRetries != retries {
188 t.Errorf("Unexpected retries: got=%d expected=%d", subscription.MaxRetries, retries)
191 if subscription.RetryTimer != timer {
192 t.Errorf("Unexpected timer: got=%d expected=%d", subscription.RetryTimer, timer)
195 if subscription.EventType != event {
196 t.Errorf("Unexpected event type: got=%s expected=%s", subscription.EventType, event)