2 * Copyright 2018 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
29 "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
31 pkgerrors "github.com/pkg/errors"
34 //Creating an embedded interface via anonymous variable
35 //This allows us to make mockDB satisfy the DatabaseConnection
36 //interface even if we are not implementing all the methods in it
37 type mockRBDefinition struct {
39 // Items and err will be used to customize each test
40 // via a localized instantiation of mockRBDefinition
45 func (m *mockRBDefinition) Create(inp rb.Definition) (rb.Definition, error) {
47 return rb.Definition{}, m.Err
50 return m.Items[0], nil
53 func (m *mockRBDefinition) List(name string) ([]rb.Definition, error) {
55 return []rb.Definition{}, m.Err
61 func (m *mockRBDefinition) Get(name, version string) (rb.Definition, error) {
63 return rb.Definition{}, m.Err
66 return m.Items[0], nil
69 func (m *mockRBDefinition) Delete(name, version string) error {
73 func (m *mockRBDefinition) Upload(name, version string, inp []byte) error {
77 func TestRBDefCreateHandler(t *testing.T) {
78 testCases := []struct {
81 expected rb.Definition
83 rbDefClient *mockRBDefinition
86 label: "Missing Body Failure",
87 expectedCode: http.StatusBadRequest,
88 rbDefClient: &mockRBDefinition{},
91 label: "Create Definition",
92 expectedCode: http.StatusCreated,
93 reader: bytes.NewBuffer([]byte(`{
94 "rb-name":"testresourcebundle",
96 "chart-name":"testchart",
97 "description":"test description"
99 expected: rb.Definition{
100 RBName: "testresourcebundle",
102 ChartName: "testchart",
103 Description: "test description",
105 rbDefClient: &mockRBDefinition{
106 //Items that will be returned by the mocked Client
107 Items: []rb.Definition{
109 RBName: "testresourcebundle",
111 ChartName: "testchart",
112 Description: "test description",
118 label: "Missing Name in Request Body",
119 reader: bytes.NewBuffer([]byte(`{
121 "chart-name":"testchart",
122 "description":"test description"
124 expectedCode: http.StatusBadRequest,
125 rbDefClient: &mockRBDefinition{},
128 label: "Missing Version in Request Body",
129 reader: bytes.NewBuffer([]byte(`{
130 "rb-name":"testresourcebundle",
131 "chart-name":"testchart",
132 "description":"test description"
134 expectedCode: http.StatusBadRequest,
135 rbDefClient: &mockRBDefinition{},
139 for _, testCase := range testCases {
140 t.Run(testCase.label, func(t *testing.T) {
141 request := httptest.NewRequest("POST", "/v1/rb/definition", testCase.reader)
142 resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil))
144 //Check returned code
145 if resp.StatusCode != testCase.expectedCode {
146 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
149 //Check returned body only if statusCreated
150 if resp.StatusCode == http.StatusCreated {
151 got := rb.Definition{}
152 json.NewDecoder(resp.Body).Decode(&got)
154 if reflect.DeepEqual(testCase.expected, got) == false {
155 t.Errorf("createHandler returned unexpected body: got %v;"+
156 " expected %v", got, testCase.expected)
163 func TestRBDefListVersionsHandler(t *testing.T) {
165 testCases := []struct {
167 expected []rb.Definition
169 rbDefClient *mockRBDefinition
172 label: "List Bundle Definitions",
173 expectedCode: http.StatusOK,
174 expected: []rb.Definition{
176 RBName: "testresourcebundle",
178 ChartName: "testchart",
179 Description: "test description",
182 RBName: "testresourcebundle",
184 ChartName: "testchart",
185 Description: "test description",
188 rbDefClient: &mockRBDefinition{
189 // list of definitions that will be returned by the mockclient
190 Items: []rb.Definition{
192 RBName: "testresourcebundle",
194 ChartName: "testchart",
195 Description: "test description",
198 RBName: "testresourcebundle",
200 ChartName: "testchart",
201 Description: "test description",
208 for _, testCase := range testCases {
209 t.Run(testCase.label, func(t *testing.T) {
210 request := httptest.NewRequest("GET", "/v1/rb/definition/testresourcebundle", nil)
211 resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil))
213 //Check returned code
214 if resp.StatusCode != testCase.expectedCode {
215 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
218 //Check returned body only if statusOK
219 if resp.StatusCode == http.StatusOK {
220 got := []rb.Definition{}
221 json.NewDecoder(resp.Body).Decode(&got)
223 // Since the order of returned slice is not guaranteed
224 // Check both and return error if both don't match
225 sort.Slice(got, func(i, j int) bool {
226 return got[i].RBVersion < got[j].RBVersion
228 // Sort both as it is not expected that testCase.expected
230 sort.Slice(testCase.expected, func(i, j int) bool {
231 return testCase.expected[i].RBVersion < testCase.expected[j].RBVersion
234 if reflect.DeepEqual(testCase.expected, got) == false {
235 t.Errorf("listHandler returned unexpected body: got %v;"+
236 " expected %v", got, testCase.expected)
243 func TestRBDefListAllHandler(t *testing.T) {
245 testCases := []struct {
247 expected []rb.Definition
249 rbDefClient *mockRBDefinition
252 label: "List Bundle Definitions",
253 expectedCode: http.StatusOK,
254 expected: []rb.Definition{
256 RBName: "resourcebundle1",
258 ChartName: "barchart",
259 Description: "test description for one",
262 RBName: "resourcebundle2",
263 RBVersion: "version2",
264 ChartName: "foochart",
265 Description: "test description for two",
268 rbDefClient: &mockRBDefinition{
269 // list of definitions that will be returned by the mockclient
270 Items: []rb.Definition{
272 RBName: "resourcebundle1",
274 ChartName: "barchart",
275 Description: "test description for one",
278 RBName: "resourcebundle2",
279 RBVersion: "version2",
280 ChartName: "foochart",
281 Description: "test description for two",
288 for _, testCase := range testCases {
289 t.Run(testCase.label, func(t *testing.T) {
290 request := httptest.NewRequest("GET", "/v1/rb/definition", nil)
291 resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil))
293 //Check returned code
294 if resp.StatusCode != testCase.expectedCode {
295 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
298 //Check returned body only if statusOK
299 if resp.StatusCode == http.StatusOK {
300 got := []rb.Definition{}
301 json.NewDecoder(resp.Body).Decode(&got)
303 // Since the order of returned slice is not guaranteed
304 // Check both and return error if both don't match
305 sort.Slice(got, func(i, j int) bool {
306 return got[i].RBVersion < got[j].RBVersion
308 // Sort both as it is not expected that testCase.expected
310 sort.Slice(testCase.expected, func(i, j int) bool {
311 return testCase.expected[i].RBVersion < testCase.expected[j].RBVersion
314 if reflect.DeepEqual(testCase.expected, got) == false {
315 t.Errorf("listHandler returned unexpected body: got %v;"+
316 " expected %v", got, testCase.expected)
323 func TestRBDefGetHandler(t *testing.T) {
325 testCases := []struct {
327 expected rb.Definition
330 rbDefClient *mockRBDefinition
333 label: "Get Bundle Definition",
334 expectedCode: http.StatusOK,
335 expected: rb.Definition{
336 RBName: "testresourcebundle",
338 ChartName: "testchart",
339 Description: "test description",
341 name: "testresourcebundle",
343 rbDefClient: &mockRBDefinition{
344 // list of definitions that will be returned by the mockclient
345 Items: []rb.Definition{
347 RBName: "testresourcebundle",
349 ChartName: "testchart",
350 Description: "test description",
356 label: "Get Non-Exiting Bundle Definition",
357 expectedCode: http.StatusInternalServerError,
358 name: "nonexistingbundle",
360 rbDefClient: &mockRBDefinition{
361 // list of definitions that will be returned by the mockclient
362 Items: []rb.Definition{},
363 Err: pkgerrors.New("Internal Error"),
368 for _, testCase := range testCases {
369 t.Run(testCase.label, func(t *testing.T) {
370 request := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.name+"/"+testCase.version, nil)
371 resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil))
373 //Check returned code
374 if resp.StatusCode != testCase.expectedCode {
375 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
378 //Check returned body only if statusOK
379 if resp.StatusCode == http.StatusOK {
380 got := rb.Definition{}
381 json.NewDecoder(resp.Body).Decode(&got)
383 if reflect.DeepEqual(testCase.expected, got) == false {
384 t.Errorf("listHandler returned unexpected body: got %v;"+
385 " expected %v", got, testCase.expected)
392 func TestRBDefDeleteHandler(t *testing.T) {
394 testCases := []struct {
399 rbDefClient *mockRBDefinition
402 label: "Delete Bundle Definition",
403 expectedCode: http.StatusNoContent,
406 rbDefClient: &mockRBDefinition{},
409 label: "Delete Non-Exiting Bundle Definition",
410 expectedCode: http.StatusInternalServerError,
413 rbDefClient: &mockRBDefinition{
414 Err: pkgerrors.New("Internal Error"),
419 for _, testCase := range testCases {
420 t.Run(testCase.label, func(t *testing.T) {
421 request := httptest.NewRequest("DELETE", "/v1/rb/definition/"+testCase.name+"/"+testCase.version, nil)
422 resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil))
424 //Check returned code
425 if resp.StatusCode != testCase.expectedCode {
426 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
432 func TestRBDefUploadHandler(t *testing.T) {
434 testCases := []struct {
440 rbDefClient *mockRBDefinition
443 label: "Upload Bundle Definition Content",
444 expectedCode: http.StatusOK,
447 body: bytes.NewBuffer([]byte{
448 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
449 0x00, 0xff, 0xf2, 0x48, 0xcd,
451 rbDefClient: &mockRBDefinition{},
454 label: "Upload Invalid Bundle Definition Content",
455 expectedCode: http.StatusInternalServerError,
458 body: bytes.NewBuffer([]byte{
459 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
460 0x00, 0xff, 0xf2, 0x48, 0xcd,
462 rbDefClient: &mockRBDefinition{
463 Err: pkgerrors.New("Internal Error"),
467 label: "Upload Empty Body Content",
468 expectedCode: http.StatusBadRequest,
471 rbDefClient: &mockRBDefinition{},
475 for _, testCase := range testCases {
476 t.Run(testCase.label, func(t *testing.T) {
477 request := httptest.NewRequest("POST",
478 "/v1/rb/definition/"+testCase.name+"/"+testCase.version+"/content", testCase.body)
479 resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil, nil, nil))
481 //Check returned code
482 if resp.StatusCode != testCase.expectedCode {
483 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)