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 "github.com/gorilla/mux"
41 type MockedHelmer struct {
44 func (sd *MockedHelmer) Initialize() {
47 func (h *MockedHelmer) Status(name string) (Xapp, error) {
48 return xapp, helmError
51 func (h *MockedHelmer) StatusAll() ([]Xapp, error) {
52 return xapps, helmError
55 func (h *MockedHelmer) List() (names []string, err error) {
56 return names, helmError
59 func (h *MockedHelmer) Install(name string) (Xapp, error) {
60 return xapp, helmError
63 func (h *MockedHelmer) Delete(name string) (Xapp, error) {
64 return xapp, helmError
68 func TestMain(m *testing.M) {
78 // Just run on the background (for coverage)
82 time.Sleep(time.Duration(2 * time.Second))
88 func TestGetHealthCheck(t *testing.T) {
89 req, _ := http.NewRequest("GET", "/ric/v1/health/ready", nil)
90 response := executeRequest(req)
92 checkResponseCode(t, http.StatusOK, response.Code)
95 func TestGetAppsReturnsEmpty(t *testing.T) {
96 req, _ := http.NewRequest("GET", "/ric/v1/xapps", nil)
97 response := executeRequest(req)
99 checkResponseCode(t, http.StatusOK, response.Code)
100 if body := response.Body.String(); body != "[]" {
101 t.Errorf("handler returned unexpected body: got %v want []", body)
105 func TestCreateXApp(t *testing.T) {
106 xapp = generateXapp("dummy-xapp", "started", "1.0", "dummy-xapp-1234-5678", "running", "127.0.0.1", "9999")
108 payload := []byte(`{"name":"dummy-xapp"}`)
109 req, _ := http.NewRequest("POST", "/ric/v1/xapps", bytes.NewBuffer(payload))
110 response := executeRequest(req)
112 checkResponseData(t, response, http.StatusCreated, false)
115 func TestGetAppsReturnsListOfXapps(t *testing.T) {
116 xapps = append(xapps, xapp)
117 req, _ := http.NewRequest("GET", "/ric/v1/xapps", nil)
118 response := executeRequest(req)
120 checkResponseData(t, response, http.StatusOK, true)
123 func TestGetAppByIdReturnsGivenXapp(t *testing.T) {
124 req, _ := http.NewRequest("GET", "/ric/v1/xapps/"+xapp.Name, nil)
125 response := executeRequest(req)
127 checkResponseData(t, response, http.StatusOK, false)
130 func TestGetAppInstanceByIdReturnsGivenXapp(t *testing.T) {
131 req, _ := http.NewRequest("GET", "/ric/v1/xapps/"+xapp.Name+"/instances/dummy-xapp-1234-5678", nil)
132 response := executeRequest(req)
135 checkResponseCode(t, http.StatusOK, response.Code)
136 json.NewDecoder(response.Body).Decode(&ins)
138 if !reflect.DeepEqual(ins, xapp.Instances[0]) {
139 t.Errorf("handler returned unexpected body: got: %v, expected: %v", ins, xapp.Instances[0])
143 func TestDeleteAppRemovesGivenXapp(t *testing.T) {
144 req, _ := http.NewRequest("DELETE", "/ric/v1/xapps/"+xapp.Name, nil)
145 response := executeRequest(req)
147 checkResponseData(t, response, http.StatusNoContent, false)
149 // Xapp not found from the Redis DB
150 helmError = errors.New("Not found")
152 req, _ = http.NewRequest("GET", "/ric/v1/xapps/"+xapp.Name, nil)
153 response = executeRequest(req)
154 checkResponseCode(t, http.StatusNotFound, response.Code)
158 func TestGetXappReturnsError(t *testing.T) {
159 helmError = errors.New("Not found")
161 req, _ := http.NewRequest("GET", "/ric/v1/xapps/invalidXappName", nil)
162 response := executeRequest(req)
163 checkResponseCode(t, http.StatusNotFound, response.Code)
166 func TestGetXappInstanceReturnsError(t *testing.T) {
167 helmError = errors.New("Some error")
169 req, _ := http.NewRequest("GET", "/ric/v1/xapps/"+xapp.Name+"/instances/invalidXappName", nil)
170 response := executeRequest(req)
171 checkResponseCode(t, http.StatusNotFound, response.Code)
174 func TestGetXappListReturnsError(t *testing.T) {
175 helmError = errors.New("Internal error")
177 req, _ := http.NewRequest("GET", "/ric/v1/xapps", nil)
178 response := executeRequest(req)
179 checkResponseCode(t, http.StatusInternalServerError, response.Code)
182 func TestCreateXAppWithoutXappData(t *testing.T) {
183 req, _ := http.NewRequest("POST", "/ric/v1/xapps", nil)
184 response := executeRequest(req)
185 checkResponseData(t, response, http.StatusMethodNotAllowed, false)
188 func TestCreateXAppWithInvalidXappData(t *testing.T) {
189 body := []byte("Invalid JSON data ...")
191 req, _ := http.NewRequest("POST", "/ric/v1/xapps", bytes.NewBuffer(body))
192 response := executeRequest(req)
193 checkResponseData(t, response, http.StatusMethodNotAllowed, false)
196 func TestCreateXAppReturnsError(t *testing.T) {
197 helmError = errors.New("Not found")
199 payload := []byte(`{"name":"dummy-xapp"}`)
200 req, _ := http.NewRequest("POST", "/ric/v1/xapps", bytes.NewBuffer(payload))
201 response := executeRequest(req)
203 checkResponseData(t, response, http.StatusInternalServerError, false)
206 func TestDeleteXappListReturnsError(t *testing.T) {
207 helmError = errors.New("Internal error")
209 req, _ := http.NewRequest("DELETE", "/ric/v1/xapps/invalidXappName", nil)
210 response := executeRequest(req)
211 checkResponseCode(t, http.StatusInternalServerError, response.Code)
215 type fn func(w http.ResponseWriter, r *http.Request)
217 func executeRequest(req *http.Request) *httptest.ResponseRecorder {
218 rr := httptest.NewRecorder()
220 vars := map[string]string{
223 req = mux.SetURLVars(req, vars)
225 x.router.ServeHTTP(rr, req)
230 func checkResponseCode(t *testing.T, expected, actual int) {
231 if expected != actual {
232 t.Errorf("Expected response code %d. Got %d\n", expected, actual)
236 func checkResponseData(t *testing.T, response *httptest.ResponseRecorder, expectedHttpStatus int, isList bool) {
239 checkResponseCode(t, expectedHttpStatus, response.Code)
242 json.NewDecoder(response.Body).Decode(&jsonResp)
244 if !reflect.DeepEqual(jsonResp[0], expectedData) {
245 t.Errorf("handler returned unexpected body: %v", jsonResp)
248 json.NewDecoder(response.Body).Decode(&xapp)
250 if !reflect.DeepEqual(xapp, expectedData) {
251 t.Errorf("handler returned unexpected body: got: %v, expected: %v", xapp, expectedData)
256 func generateXapp(name, status, ver, iname, istatus, ip, port string) (x Xapp) {
260 p, _ := strconv.Atoi(port)
261 instance := XappInstance{
266 TxMessages: []string{"RIC_E2_TERMINATION_HC_REQUEST", "RIC_E2_MANAGER_HC_REQUEST"},
267 RxMessages: []string{"RIC_E2_TERMINATION_HC_RESPONSE", "RIC_E2_MANAGER_HC_RESPONSE"},
269 x.Instances = append(x.Instances, instance)