Initial version with full functionality
[ric-plt/appmgr.git] / src / subscriptions_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19
20 package main
21
22 import (
23     "net/http"
24     "testing"
25     "bytes"
26     "encoding/json"
27     "net/http/httptest"
28     "net"
29     "log"
30     "fmt"
31 )
32
33 var resp SubscriptionResp
34
35 // Test cases
36 func TestNoSubscriptionsFound(t *testing.T) {
37     req, _ := http.NewRequest("GET", "/ric/v1/subscriptions", nil)
38     response := executeRequest(req)
39
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)
43     }
44 }
45
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)
50
51     checkResponseCode(t, http.StatusCreated, response.Code)
52
53     json.NewDecoder(response.Body).Decode(&resp)
54     if resp.Version != 0 {
55         t.Errorf("Creating new subscription failed: %v", resp)
56     }
57 }
58
59 func TestGettAllSubscriptions(t *testing.T) {
60     req, _ := http.NewRequest("GET", "/ric/v1/subscriptions", nil)
61     response := executeRequest(req)
62
63     checkResponseCode(t, http.StatusOK, response.Code)
64
65     var subscriptions []SubscriptionReq
66     json.NewDecoder(response.Body).Decode(&subscriptions)
67
68     verifySubscription(t, subscriptions[0], "http://localhost:8087/xapps_handler", 3, 5, "Created")
69 }
70
71 func TestGetSingleSubscription(t *testing.T) {
72     req, _ := http.NewRequest("GET", "/ric/v1/subscriptions/" + resp.Id, nil)
73     response := executeRequest(req)
74
75     checkResponseCode(t, http.StatusOK, response.Code)
76
77     var subscription SubscriptionReq
78     json.NewDecoder(response.Body).Decode(&subscription)
79
80     verifySubscription(t, subscription, "http://localhost:8087/xapps_handler", 3, 5, "Created")
81 }
82
83 func TestUpdateSingleSubscription(t *testing.T) {
84     payload := []byte(`{"maxRetries": 11, "retryTimer": 22, "eventType":"Deleted", "targetUrl": "http://localhost:8088/xapps_handler"}`)
85
86     req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/" + resp.Id, bytes.NewBuffer(payload))
87     response := executeRequest(req)
88
89     checkResponseCode(t, http.StatusOK, response.Code)
90
91     var res SubscriptionResp
92     json.NewDecoder(response.Body).Decode(&res)
93     if res.Version != 0 {
94         t.Errorf("handler returned unexpected data: %v", resp)
95     }
96
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)
101
102     var subscription SubscriptionReq
103     json.NewDecoder(response.Body).Decode(&subscription)
104
105     verifySubscription(t, subscription, "http://localhost:8088/xapps_handler", 11, 22, "Deleted")
106 }
107
108 func TestDeleteSingleSubscription(t *testing.T) {
109     req, _ := http.NewRequest("DELETE", "/ric/v1/subscriptions/" + resp.Id, nil)
110     response := executeRequest(req)
111
112     checkResponseCode(t, http.StatusNoContent, response.Code)
113
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)
118 }
119
120 func TestDeleteSingleSubscriptionFails(t *testing.T) {
121     req, _ := http.NewRequest("DELETE", "/ric/v1/subscriptions/invalidSubscriptionId" , nil)
122     response := executeRequest(req)
123
124     checkResponseCode(t, http.StatusNotFound, response.Code)
125 }
126
127 func TestAddSingleSubscriptionFailsBodyEmpty(t *testing.T) {
128     req, _ := http.NewRequest("POST", "/ric/v1/subscriptions/" + resp.Id , nil)
129     response := executeRequest(req)
130
131     checkResponseCode(t, http.StatusMethodNotAllowed, response.Code)
132 }
133
134 func TestUpdateeSingleSubscriptionFailsBodyEmpty(t *testing.T) {
135     req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/" + resp.Id , nil)
136     response := executeRequest(req)
137
138     checkResponseCode(t, http.StatusMethodNotAllowed, response.Code)
139 }
140
141 func TestUpdateeSingleSubscriptionFailsInvalidId(t *testing.T) {
142     payload := []byte(`{"maxRetries": 11, "retryTimer": 22, "eventType":"Deleted", "targetUrl": "http://localhost:8088/xapps_handler"}`)
143
144     req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/invalidSubscriptionId" + resp.Id, bytes.NewBuffer(payload))
145     response := executeRequest(req)
146
147     checkResponseCode(t, http.StatusNotFound, response.Code)
148 }
149
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)
154
155     checkResponseCode(t, http.StatusCreated, response.Code)
156
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!")
160     }))
161
162     l, err := net.Listen("tcp", "127.0.0.1:8888")
163     if err != nil {
164         log.Fatal(err)
165     }
166     ts.Listener.Close()
167     ts.Listener = l
168     ts.Start()
169
170     defer ts.Close()
171
172     x.sd.Publish(xapp, EventType("created"))
173 }
174
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)
178     }
179
180     if subscription.MaxRetries != retries {
181         t.Errorf("Unexpected retries: got=%d expected=%d", subscription.MaxRetries, retries)
182     }
183
184     if subscription.RetryTimer != timer {
185         t.Errorf("Unexpected timer: got=%d expected=%d", subscription.RetryTimer, timer)
186     }
187
188     if subscription.EventType != event {
189         t.Errorf("Unexpected event type: got=%s expected=%s", subscription.EventType, event)
190     }
191 }