2 * Copyright 2020 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.
28 moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
30 pkgerrors "github.com/pkg/errors"
33 //Creating an embedded interface via anonymous variable
34 //This allows us to make mockDB satisfy the DatabaseConnection
35 //interface even if we are not implementing all the methods in it
36 type mockControllerManager struct {
37 // Items and err will be used to customize each test
38 // via a localized instantiation of mockControllerManager
39 Items []moduleLib.Controller
43 func (m *mockControllerManager) CreateController(inp moduleLib.Controller) (moduleLib.Controller, error) {
45 return moduleLib.Controller{}, m.Err
48 return m.Items[0], nil
51 func (m *mockControllerManager) GetController(name string) (moduleLib.Controller, error) {
53 return moduleLib.Controller{}, m.Err
56 return m.Items[0], nil
59 func (m *mockControllerManager) DeleteController(name string) error {
63 func TestControllerCreateHandler(t *testing.T) {
64 testCases := []struct {
67 expected moduleLib.Controller
69 controllerClient *mockControllerManager
72 label: "Missing Body Failure",
73 expectedCode: http.StatusBadRequest,
74 controllerClient: &mockControllerManager{},
77 label: "Create Controller",
78 expectedCode: http.StatusCreated,
79 reader: bytes.NewBuffer([]byte(`{
80 "name":"testController",
81 "ip-address":"10.188.234.1",
84 expected: moduleLib.Controller{
85 Name: "testController",
89 controllerClient: &mockControllerManager{
90 //Items that will be returned by the mocked Client
91 Items: []moduleLib.Controller{
93 Name: "testController",
101 label: "Missing Controller Name in Request Body",
102 reader: bytes.NewBuffer([]byte(`{
103 "description":"test description"
105 expectedCode: http.StatusBadRequest,
106 controllerClient: &mockControllerManager{},
110 for _, testCase := range testCases {
111 t.Run(testCase.label, func(t *testing.T) {
112 request := httptest.NewRequest("POST", "/v2/controllers", testCase.reader)
113 resp := executeRequest(request, NewRouter(nil, nil, testCase.controllerClient, nil))
115 //Check returned code
116 if resp.StatusCode != testCase.expectedCode {
117 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
120 //Check returned body only if statusCreated
121 if resp.StatusCode == http.StatusCreated {
122 got := moduleLib.Controller{}
123 json.NewDecoder(resp.Body).Decode(&got)
125 if reflect.DeepEqual(testCase.expected, got) == false {
126 t.Errorf("createHandler returned unexpected body: got %v;"+
127 " expected %v", got, testCase.expected)
134 func TestControllerGetHandler(t *testing.T) {
136 testCases := []struct {
138 expected moduleLib.Controller
141 controllerClient *mockControllerManager
144 label: "Get Controller",
145 expectedCode: http.StatusOK,
146 expected: moduleLib.Controller{
147 Name: "testController",
148 Host: "10.188.234.1",
151 name: "testController",
152 controllerClient: &mockControllerManager{
153 Items: []moduleLib.Controller{
155 Name: "testController",
156 Host: "10.188.234.1",
163 label: "Get Non-Existing Controller",
164 expectedCode: http.StatusInternalServerError,
165 name: "nonexistingController",
166 controllerClient: &mockControllerManager{
167 Items: []moduleLib.Controller{},
168 Err: pkgerrors.New("Internal Error"),
173 for _, testCase := range testCases {
174 t.Run(testCase.label, func(t *testing.T) {
175 request := httptest.NewRequest("GET", "/v2/controllers/"+testCase.name, nil)
176 resp := executeRequest(request, NewRouter(nil, nil, testCase.controllerClient, nil))
178 //Check returned code
179 if resp.StatusCode != testCase.expectedCode {
180 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
183 //Check returned body only if statusOK
184 if resp.StatusCode == http.StatusOK {
185 got := moduleLib.Controller{}
186 json.NewDecoder(resp.Body).Decode(&got)
188 if reflect.DeepEqual(testCase.expected, got) == false {
189 t.Errorf("listHandler returned unexpected body: got %v;"+
190 " expected %v", got, testCase.expected)
197 func TestControllerDeleteHandler(t *testing.T) {
199 testCases := []struct {
204 controllerClient *mockControllerManager
207 label: "Delete Controller",
208 expectedCode: http.StatusNoContent,
209 name: "testController",
210 controllerClient: &mockControllerManager{},
213 label: "Delete Non-Existing Controller",
214 expectedCode: http.StatusInternalServerError,
215 name: "testController",
216 controllerClient: &mockControllerManager{
217 Err: pkgerrors.New("Internal Error"),
222 for _, testCase := range testCases {
223 t.Run(testCase.label, func(t *testing.T) {
224 request := httptest.NewRequest("DELETE", "/v2/controllers/"+testCase.name, nil)
225 resp := executeRequest(request, NewRouter(nil, nil, testCase.controllerClient, nil))
227 //Check returned code
228 if resp.StatusCode != testCase.expectedCode {
229 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)