Add v2 cluster registration api to k8s orchestrator
[multicloud/k8s.git] / src / orchestrator / api / controllerhandler_test.go
1 /*
2  * Copyright 2020 Intel Corporation, Inc
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package api
18
19 import (
20         "bytes"
21         "encoding/json"
22         "io"
23         "net/http"
24         "net/http/httptest"
25         "reflect"
26         "testing"
27
28         moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
29
30         pkgerrors "github.com/pkg/errors"
31 )
32
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
40         Err   error
41 }
42
43 func (m *mockControllerManager) CreateController(inp moduleLib.Controller) (moduleLib.Controller, error) {
44         if m.Err != nil {
45                 return moduleLib.Controller{}, m.Err
46         }
47
48         return m.Items[0], nil
49 }
50
51 func (m *mockControllerManager) GetController(name string) (moduleLib.Controller, error) {
52         if m.Err != nil {
53                 return moduleLib.Controller{}, m.Err
54         }
55
56         return m.Items[0], nil
57 }
58
59 func (m *mockControllerManager) DeleteController(name string) error {
60         return m.Err
61 }
62
63 func TestControllerCreateHandler(t *testing.T) {
64         testCases := []struct {
65                 label            string
66                 reader           io.Reader
67                 expected         moduleLib.Controller
68                 expectedCode     int
69                 controllerClient *mockControllerManager
70         }{
71                 {
72                         label:            "Missing Body Failure",
73                         expectedCode:     http.StatusBadRequest,
74                         controllerClient: &mockControllerManager{},
75                 },
76                 {
77                         label:        "Create Controller",
78                         expectedCode: http.StatusCreated,
79                         reader: bytes.NewBuffer([]byte(`{
80                                 "name":"testController",
81                                 "ip-address":"10.188.234.1",
82                                 "port":8080
83                                 }`)),
84                         expected: moduleLib.Controller{
85                                 Name: "testController",
86                                 Host: "10.188.234.1",
87                                 Port: 8080,
88                         },
89                         controllerClient: &mockControllerManager{
90                                 //Items that will be returned by the mocked Client
91                                 Items: []moduleLib.Controller{
92                                         {
93                                                 Name: "testController",
94                                                 Host: "10.188.234.1",
95                                                 Port: 8080,
96                                         },
97                                 },
98                         },
99                 },
100                 {
101                         label: "Missing Controller Name in Request Body",
102                         reader: bytes.NewBuffer([]byte(`{
103                                 "description":"test description"
104                                 }`)),
105                         expectedCode:     http.StatusBadRequest,
106                         controllerClient: &mockControllerManager{},
107                 },
108         }
109
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))
114
115                         //Check returned code
116                         if resp.StatusCode != testCase.expectedCode {
117                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
118                         }
119
120                         //Check returned body only if statusCreated
121                         if resp.StatusCode == http.StatusCreated {
122                                 got := moduleLib.Controller{}
123                                 json.NewDecoder(resp.Body).Decode(&got)
124
125                                 if reflect.DeepEqual(testCase.expected, got) == false {
126                                         t.Errorf("createHandler returned unexpected body: got %v;"+
127                                                 " expected %v", got, testCase.expected)
128                                 }
129                         }
130                 })
131         }
132 }
133
134 func TestControllerGetHandler(t *testing.T) {
135
136         testCases := []struct {
137                 label            string
138                 expected         moduleLib.Controller
139                 name, version    string
140                 expectedCode     int
141                 controllerClient *mockControllerManager
142         }{
143                 {
144                         label:        "Get Controller",
145                         expectedCode: http.StatusOK,
146                         expected: moduleLib.Controller{
147                                 Name: "testController",
148                                 Host: "10.188.234.1",
149                                 Port: 8080,
150                         },
151                         name: "testController",
152                         controllerClient: &mockControllerManager{
153                                 Items: []moduleLib.Controller{
154                                         {
155                                                 Name: "testController",
156                                                 Host: "10.188.234.1",
157                                                 Port: 8080,
158                                         },
159                                 },
160                         },
161                 },
162                 {
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"),
169                         },
170                 },
171         }
172
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))
177
178                         //Check returned code
179                         if resp.StatusCode != testCase.expectedCode {
180                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
181                         }
182
183                         //Check returned body only if statusOK
184                         if resp.StatusCode == http.StatusOK {
185                                 got := moduleLib.Controller{}
186                                 json.NewDecoder(resp.Body).Decode(&got)
187
188                                 if reflect.DeepEqual(testCase.expected, got) == false {
189                                         t.Errorf("listHandler returned unexpected body: got %v;"+
190                                                 " expected %v", got, testCase.expected)
191                                 }
192                         }
193                 })
194         }
195 }
196
197 func TestControllerDeleteHandler(t *testing.T) {
198
199         testCases := []struct {
200                 label            string
201                 name             string
202                 version          string
203                 expectedCode     int
204                 controllerClient *mockControllerManager
205         }{
206                 {
207                         label:            "Delete Controller",
208                         expectedCode:     http.StatusNoContent,
209                         name:             "testController",
210                         controllerClient: &mockControllerManager{},
211                 },
212                 {
213                         label:        "Delete Non-Existing Controller",
214                         expectedCode: http.StatusInternalServerError,
215                         name:         "testController",
216                         controllerClient: &mockControllerManager{
217                                 Err: pkgerrors.New("Internal Error"),
218                         },
219                 },
220         }
221
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))
226
227                         //Check returned code
228                         if resp.StatusCode != testCase.expectedCode {
229                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
230                         }
231                 })
232         }
233 }