5d79d007520c5e01ea643693238344d222dd73d7
[ric-plt/appmgr.git] / cmd / appmgr / 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         "bytes"
24         "encoding/json"
25         "fmt"
26         sdl "gerrit.oran-osc.org/r/ric-plt/sdlgo"
27         "github.com/spf13/viper"
28         "log"
29         "net"
30         "net/http"
31         "net/http/httptest"
32         "testing"
33 )
34
35 var resp SubscriptionResp
36
37 // Test cases
38 func TestNoSubscriptionsFound(t *testing.T) {
39         req, _ := http.NewRequest("GET", "/ric/v1/subscriptions", nil)
40         response := executeRequest(req)
41
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)
45         }
46 }
47
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)
52
53         checkResponseCode(t, http.StatusCreated, response.Code)
54
55         json.NewDecoder(response.Body).Decode(&resp)
56         if resp.Version != 0 {
57                 t.Errorf("Creating new subscription failed: %v", resp)
58         }
59 }
60
61 func TestGettAllSubscriptions(t *testing.T) {
62         req, _ := http.NewRequest("GET", "/ric/v1/subscriptions", nil)
63         response := executeRequest(req)
64
65         checkResponseCode(t, http.StatusOK, response.Code)
66
67         var subscriptions []SubscriptionReq
68         json.NewDecoder(response.Body).Decode(&subscriptions)
69
70         verifySubscription(t, subscriptions[0], "http://localhost:8087/xapps_handler", 3, 5, "Created")
71 }
72
73 func TestGetSingleSubscription(t *testing.T) {
74         req, _ := http.NewRequest("GET", "/ric/v1/subscriptions/"+resp.Id, nil)
75         response := executeRequest(req)
76
77         checkResponseCode(t, http.StatusOK, response.Code)
78
79         var subscription SubscriptionReq
80         json.NewDecoder(response.Body).Decode(&subscription)
81
82         verifySubscription(t, subscription, "http://localhost:8087/xapps_handler", 3, 5, "Created")
83 }
84
85 func TestUpdateSingleSubscription(t *testing.T) {
86         payload := []byte(`{"maxRetries": 11, "retryTimer": 22, "eventType":"Deleted", "targetUrl": "http://localhost:8088/xapps_handler"}`)
87
88         req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/"+resp.Id, bytes.NewBuffer(payload))
89         response := executeRequest(req)
90
91         checkResponseCode(t, http.StatusOK, response.Code)
92
93         var res SubscriptionResp
94         json.NewDecoder(response.Body).Decode(&res)
95         if res.Version != 0 {
96                 t.Errorf("handler returned unexpected data: %v", resp)
97         }
98
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)
103
104         var subscription SubscriptionReq
105         json.NewDecoder(response.Body).Decode(&subscription)
106
107         verifySubscription(t, subscription, "http://localhost:8088/xapps_handler", 11, 22, "Deleted")
108 }
109
110 func TestDeleteSingleSubscription(t *testing.T) {
111         req, _ := http.NewRequest("DELETE", "/ric/v1/subscriptions/"+resp.Id, nil)
112         response := executeRequest(req)
113
114         checkResponseCode(t, http.StatusNoContent, response.Code)
115
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)
120 }
121
122 func TestDeleteSingleSubscriptionFails(t *testing.T) {
123         req, _ := http.NewRequest("DELETE", "/ric/v1/subscriptions/invalidSubscriptionId", nil)
124         response := executeRequest(req)
125
126         checkResponseCode(t, http.StatusNotFound, response.Code)
127 }
128
129 func TestAddSingleSubscriptionFailsBodyEmpty(t *testing.T) {
130         req, _ := http.NewRequest("POST", "/ric/v1/subscriptions/"+resp.Id, nil)
131         response := executeRequest(req)
132
133         checkResponseCode(t, http.StatusMethodNotAllowed, response.Code)
134 }
135
136 func TestUpdateeSingleSubscriptionFailsBodyEmpty(t *testing.T) {
137         req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/"+resp.Id, nil)
138         response := executeRequest(req)
139
140         checkResponseCode(t, http.StatusMethodNotAllowed, response.Code)
141 }
142
143 func TestUpdateeSingleSubscriptionFailsInvalidId(t *testing.T) {
144         payload := []byte(`{"maxRetries": 11, "retryTimer": 22, "eventType":"Deleted", "targetUrl": "http://localhost:8088/xapps_handler"}`)
145
146         req, _ := http.NewRequest("PUT", "/ric/v1/subscriptions/invalidSubscriptionId"+resp.Id, bytes.NewBuffer(payload))
147         response := executeRequest(req)
148
149         checkResponseCode(t, http.StatusNotFound, response.Code)
150 }
151
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)
156
157         checkResponseCode(t, http.StatusCreated, response.Code)
158
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!")
162         }))
163
164         l, err := net.Listen("tcp", "127.0.0.1:8888")
165         if err != nil {
166                 log.Fatal(err)
167         }
168         ts.Listener.Close()
169         ts.Listener = l
170         ts.Start()
171
172         defer ts.Close()
173
174         x.sd.Publish(xapp, EventType("created"))
175 }
176
177 func TestTeardown(t *testing.T) {
178         db := sdl.NewSdlInstance(viper.GetString("db.sessionNamespace"), sdl.NewDatabase())
179         db.RemoveAll()
180 }
181
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)
185         }
186
187         if subscription.MaxRetries != retries {
188                 t.Errorf("Unexpected retries: got=%d expected=%d", subscription.MaxRetries, retries)
189         }
190
191         if subscription.RetryTimer != timer {
192                 t.Errorf("Unexpected timer: got=%d expected=%d", subscription.RetryTimer, timer)
193         }
194
195         if subscription.EventType != event {
196                 t.Errorf("Unexpected event type: got=%s expected=%s", subscription.EventType, event)
197         }
198 }