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 ==================================================================================
33 var resp SubscriptionResp
36 func TestNoSubscriptionsFound(t *testing.T) {
37 req, _ := http.NewRequest("GET", "/ric/v1/subscriptions", nil)
38 response := executeRequest(req)
40 checkResponseCode(t, http.StatusOK, response.Code)
41 if body := response.Body.String(); body != "[]" {
42 t.Errorf("handler returned unexpected body: got %v want []", body)
46 func TestAddNewSubscription(t *testing.T) {
47 payload := []byte(`{"maxRetries": 3, "retryTimer": 5, "eventType":"Created", "targetUrl": "http://localhost:8087/xapps_handler"}`)
48 req, _ := http.NewRequest("POST", "/ric/v1/subscriptions", bytes.NewBuffer(payload))
49 response := executeRequest(req)
51 checkResponseCode(t, http.StatusCreated, response.Code)
53 json.NewDecoder(response.Body).Decode(&resp)
54 if resp.Version != 0 {
55 t.Errorf("Creating new subscription failed: %v", resp)
59 func TestGettAllSubscriptions(t *testing.T) {
60 req, _ := http.NewRequest("GET", "/ric/v1/subscriptions", nil)
61 response := executeRequest(req)
63 checkResponseCode(t, http.StatusOK, response.Code)
65 var subscriptions []SubscriptionReq
66 json.NewDecoder(response.Body).Decode(&subscriptions)
68 verifySubscription(t, subscriptions[0], "http://localhost:8087/xapps_handler", 3, 5, "Created")
71 func TestGetSingleSubscription(t *testing.T) {
72 req, _ := http.NewRequest("GET", "/ric/v1/subscriptions/" + resp.Id, nil)
73 response := executeRequest(req)
75 checkResponseCode(t, http.StatusOK, response.Code)
77 var subscription SubscriptionReq
78 json.NewDecoder(response.Body).Decode(&subscription)
80 verifySubscription(t, subscription, "http://localhost:8087/xapps_handler", 3, 5, "Created")
83 func TestUpdateSingleSubscription(t *testing.T) {
84 payload := []byte(`{"maxRetries": 11, "retryTimer": 22, "eventType":"Deleted", "targetUrl": "http://localhost:8088/xapps_handler"}`)
86 req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/" + resp.Id, bytes.NewBuffer(payload))
87 response := executeRequest(req)
89 checkResponseCode(t, http.StatusOK, response.Code)
91 var res SubscriptionResp
92 json.NewDecoder(response.Body).Decode(&res)
94 t.Errorf("handler returned unexpected data: %v", resp)
97 // Check that the subscription is updated properly
98 req, _ = http.NewRequest("GET", "/ric/v1/subscriptions/" + resp.Id, nil)
99 response = executeRequest(req)
100 checkResponseCode(t, http.StatusOK, response.Code)
102 var subscription SubscriptionReq
103 json.NewDecoder(response.Body).Decode(&subscription)
105 verifySubscription(t, subscription, "http://localhost:8088/xapps_handler", 11, 22, "Deleted")
108 func TestDeleteSingleSubscription(t *testing.T) {
109 req, _ := http.NewRequest("DELETE", "/ric/v1/subscriptions/" + resp.Id, nil)
110 response := executeRequest(req)
112 checkResponseCode(t, http.StatusNoContent, response.Code)
114 // Check that the subscription is removed properly
115 req, _ = http.NewRequest("GET", "/ric/v1/subscriptions/" + resp.Id, nil)
116 response = executeRequest(req)
117 checkResponseCode(t, http.StatusNotFound, response.Code)
120 func TestDeleteSingleSubscriptionFails(t *testing.T) {
121 req, _ := http.NewRequest("DELETE", "/ric/v1/subscriptions/invalidSubscriptionId" , nil)
122 response := executeRequest(req)
124 checkResponseCode(t, http.StatusNotFound, response.Code)
127 func TestAddSingleSubscriptionFailsBodyEmpty(t *testing.T) {
128 req, _ := http.NewRequest("POST", "/ric/v1/subscriptions/" + resp.Id , nil)
129 response := executeRequest(req)
131 checkResponseCode(t, http.StatusMethodNotAllowed, response.Code)
134 func TestUpdateeSingleSubscriptionFailsBodyEmpty(t *testing.T) {
135 req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/" + resp.Id , nil)
136 response := executeRequest(req)
138 checkResponseCode(t, http.StatusMethodNotAllowed, response.Code)
141 func TestUpdateeSingleSubscriptionFailsInvalidId(t *testing.T) {
142 payload := []byte(`{"maxRetries": 11, "retryTimer": 22, "eventType":"Deleted", "targetUrl": "http://localhost:8088/xapps_handler"}`)
144 req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/invalidSubscriptionId" + resp.Id, bytes.NewBuffer(payload))
145 response := executeRequest(req)
147 checkResponseCode(t, http.StatusNotFound, response.Code)
150 func TestPublishXappAction(t *testing.T) {
151 payload := []byte(`{"maxRetries": 3, "retryTimer": 5, "eventType":"Created", "targetUrl": "http://127.0.0.1:8888"}`)
152 req, _ := http.NewRequest("POST", "/ric/v1/subscriptions", bytes.NewBuffer(payload))
153 response := executeRequest(req)
155 checkResponseCode(t, http.StatusCreated, response.Code)
157 // Create a RestApi server (simulating RM)
158 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
159 fmt.Fprintln(w, "Hello, XM!")
162 l, err := net.Listen("tcp", "127.0.0.1:8888")
172 x.sd.Publish(xapp, EventType("created"))
175 func verifySubscription(t *testing.T, subscription SubscriptionReq, url string, retries int, timer int, event string) {
176 if subscription.TargetUrl != url {
177 t.Errorf("Unexpected url: got=%s expected=%s", subscription.TargetUrl, url)
180 if subscription.MaxRetries != retries {
181 t.Errorf("Unexpected retries: got=%d expected=%d", subscription.MaxRetries, retries)
184 if subscription.RetryTimer != timer {
185 t.Errorf("Unexpected timer: got=%d expected=%d", subscription.RetryTimer, timer)
188 if subscription.EventType != event {
189 t.Errorf("Unexpected event type: got=%s expected=%s", subscription.EventType, event)