-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsearch_handler_test.go
More file actions
352 lines (327 loc) · 10.3 KB
/
search_handler_test.go
File metadata and controls
352 lines (327 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package handlers_test
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"github.com/odpf/columbus/api/handlers"
"github.com/odpf/columbus/discovery"
"github.com/odpf/columbus/lib/mock"
"github.com/stretchr/testify/assert"
testifyMock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestSearchHandlerSearch(t *testing.T) {
ctx := context.Background()
type testCase struct {
Title string
ExpectStatus int
Querystring string
InitSearcher func(testCase, *mock.RecordSearcher)
ValidateResponse func(testCase, io.Reader) error
}
var testCases = []testCase{
{
Title: "should return HTTP 400 if 'text' parameter is empty or missing",
ExpectStatus: http.StatusBadRequest,
ValidateResponse: func(tc testCase, body io.Reader) error { return nil },
Querystring: "",
},
{
Title: "should report HTTP 500 if record searcher fails",
Querystring: "text=test",
InitSearcher: func(tc testCase, searcher *mock.RecordSearcher) {
err := fmt.Errorf("service unavailable")
searcher.On("Search", ctx, testifyMock.AnythingOfType("discovery.SearchConfig")).
Return([]discovery.SearchResult{}, err)
},
ExpectStatus: http.StatusInternalServerError,
},
{
Title: "should pass filter to search config format",
Querystring: "text=resource&landscape=id,vn&filter.data.landscape=th&filter.type=topic&filter.service=kafka,rabbitmq",
InitSearcher: func(tc testCase, searcher *mock.RecordSearcher) {
cfg := discovery.SearchConfig{
Text: "resource",
TypeWhiteList: []string{"topic"},
Filters: map[string][]string{
"service": {"kafka", "rabbitmq"},
"data.landscape": {"th"},
},
Queries: make(map[string]string),
}
searcher.On("Search", ctx, cfg).Return([]discovery.SearchResult{}, nil)
},
ValidateResponse: func(tc testCase, body io.Reader) error {
return nil
},
},
{
Title: "should pass queries to search config format",
Querystring: "text=resource&landscape=id,vn&filter.data.landscape=th&filter.type=topic&filter.service=kafka,rabbitmq&query.data.columns.name=timestamp&[email protected]",
InitSearcher: func(tc testCase, searcher *mock.RecordSearcher) {
cfg := discovery.SearchConfig{
Text: "resource",
TypeWhiteList: []string{"topic"},
Filters: map[string][]string{
"service": {"kafka", "rabbitmq"},
"data.landscape": {"th"},
},
Queries: map[string]string{
"data.columns.name": "timestamp",
"owners.email": "[email protected]",
},
}
searcher.On("Search", ctx, cfg).Return([]discovery.SearchResult{}, nil)
},
ValidateResponse: func(tc testCase, body io.Reader) error {
return nil
},
},
{
Title: "should return the matched documents",
Querystring: "text=test",
InitSearcher: func(tc testCase, searcher *mock.RecordSearcher) {
cfg := discovery.SearchConfig{
Text: "test",
Filters: make(map[string][]string),
Queries: make(map[string]string),
}
response := []discovery.SearchResult{
{
Type: "test",
ID: "test-resource",
Title: "test resource",
Description: "some description",
Service: "test-service",
Labels: map[string]string{
"entity": "odpf",
"landscape": "id",
},
},
}
searcher.On("Search", ctx, cfg).Return(response, nil)
},
ValidateResponse: func(tc testCase, body io.Reader) error {
var response []handlers.SearchResponse
err := json.NewDecoder(body).Decode(&response)
if err != nil {
return fmt.Errorf("error reading response body: %v", err)
}
expectResponse := []handlers.SearchResponse{
{
ID: "test-resource",
Title: "test resource",
Description: "some description",
Service: "test-service",
Type: "test",
Labels: map[string]string{
"entity": "odpf",
"landscape": "id",
},
},
}
if reflect.DeepEqual(response, expectResponse) == false {
return fmt.Errorf("expected handler response to be %#v, was %#v", expectResponse, response)
}
return nil
},
},
{
Title: "should return the requested number of records",
Querystring: "text=resource&size=10",
InitSearcher: func(tc testCase, searcher *mock.RecordSearcher) {
cfg := discovery.SearchConfig{
Text: "resource",
MaxResults: 10,
Filters: make(map[string][]string),
Queries: make(map[string]string),
}
var results []discovery.SearchResult
for i := 0; i < cfg.MaxResults; i++ {
urn := fmt.Sprintf("resource-%d", i+1)
name := fmt.Sprintf("resource %d", i+1)
r := discovery.SearchResult{
ID: urn,
Type: "table",
Title: name,
Service: "kafka",
Labels: map[string]string{
"landscape": "id",
"entity": "odpf",
},
}
results = append(results, r)
}
searcher.On("Search", ctx, cfg).Return(results, nil)
},
ValidateResponse: func(tc testCase, body io.Reader) error {
var payload []interface{}
err := json.NewDecoder(body).Decode(&payload)
if err != nil {
return fmt.Errorf("error reading response body: %v", err)
}
expectedSize := 10
actualSize := len(payload)
if expectedSize != actualSize {
return fmt.Errorf("expected search request to return %d results, returned %d results instead", expectedSize, actualSize)
}
return nil
},
},
}
for _, testCase := range testCases {
t.Run(testCase.Title, func(t *testing.T) {
var (
recordSearcher = new(mock.RecordSearcher)
)
if testCase.InitSearcher != nil {
testCase.InitSearcher(testCase, recordSearcher)
}
defer recordSearcher.AssertExpectations(t)
params, err := url.ParseQuery(testCase.Querystring)
require.NoError(t, err)
requestURL := "/?" + params.Encode()
rr := httptest.NewRequest(http.MethodGet, requestURL, nil)
rw := httptest.NewRecorder()
service := discovery.NewService(nil, recordSearcher)
handler := handlers.NewSearchHandler(new(mock.Logger), service)
handler.Search(rw, rr)
expectStatus := testCase.ExpectStatus
if expectStatus == 0 {
expectStatus = http.StatusOK
}
assert.Equal(t, expectStatus, rw.Code)
if testCase.ValidateResponse != nil {
if err := testCase.ValidateResponse(testCase, rw.Body); err != nil {
t.Errorf("error validating handler response: %v", err)
return
}
}
})
}
}
func TestSearchHandlerSuggest(t *testing.T) {
ctx := context.Background()
type testCase struct {
Title string
ExpectStatus int
Querystring string
InitSearcher func(testCase, *mock.RecordSearcher)
ValidateResponse func(testCase, io.Reader) error
}
var testCases = []testCase{
{
Title: "should return HTTP 400 if 'text' parameter is empty or missing",
ExpectStatus: http.StatusBadRequest,
ValidateResponse: func(tc testCase, body io.Reader) error { return nil },
Querystring: "",
},
{
Title: "should report HTTP 500 if searcher fails",
Querystring: "text=test",
InitSearcher: func(tc testCase, searcher *mock.RecordSearcher) {
cfg := discovery.SearchConfig{
Text: "test",
Filters: map[string][]string{},
Queries: make(map[string]string),
}
searcher.On("Suggest", ctx, cfg).Return([]string{}, fmt.Errorf("service unavailable"))
},
ExpectStatus: http.StatusInternalServerError,
},
{
Title: "should pass filter to search config format",
Querystring: "text=resource&landscape=id,vn&query.description=this is my dashboard&filter.data.landscape=th&filter.type=topic&filter.service=kafka,rabbitmq",
InitSearcher: func(tc testCase, searcher *mock.RecordSearcher) {
cfg := discovery.SearchConfig{
Text: "resource",
TypeWhiteList: []string{"topic"},
Filters: map[string][]string{
"service": {"kafka", "rabbitmq"},
"data.landscape": {"th"},
},
Queries: map[string]string{
"description": "this is my dashboard",
},
}
searcher.On("Suggest", ctx, cfg).Return([]string{}, nil)
},
ValidateResponse: func(tc testCase, body io.Reader) error {
return nil
},
},
{
Title: "should return suggestions",
Querystring: "text=test",
InitSearcher: func(tc testCase, searcher *mock.RecordSearcher) {
cfg := discovery.SearchConfig{
Text: "test",
Filters: make(map[string][]string),
Queries: make(map[string]string),
}
response := []string{
"test",
"test2",
"t est",
"t_est",
}
searcher.On("Suggest", ctx, cfg).Return(response, nil)
},
ValidateResponse: func(tc testCase, body io.Reader) error {
var response handlers.SuggestResponse
err := json.NewDecoder(body).Decode(&response)
if err != nil {
return fmt.Errorf("error reading response body: %v", err)
}
expectResponse := handlers.SuggestResponse{
Suggestions: []string{
"test",
"test2",
"t est",
"t_est",
},
}
if reflect.DeepEqual(response, expectResponse) == false {
return fmt.Errorf("expected handler response to be %#v, was %#v", expectResponse, response)
}
return nil
},
},
}
for _, testCase := range testCases {
t.Run(testCase.Title, func(t *testing.T) {
var (
recordSearcher = new(mock.RecordSearcher)
)
if testCase.InitSearcher != nil {
testCase.InitSearcher(testCase, recordSearcher)
}
defer recordSearcher.AssertExpectations(t)
params, err := url.ParseQuery(testCase.Querystring)
require.NoError(t, err)
requestURL := "/?" + params.Encode()
rr := httptest.NewRequest(http.MethodGet, requestURL, nil)
rw := httptest.NewRecorder()
service := discovery.NewService(nil, recordSearcher)
handler := handlers.NewSearchHandler(new(mock.Logger), service)
handler.Suggest(rw, rr)
expectStatus := testCase.ExpectStatus
if expectStatus == 0 {
expectStatus = http.StatusOK
}
assert.Equal(t, expectStatus, rw.Code)
if testCase.ValidateResponse != nil {
if err := testCase.ValidateResponse(testCase, rw.Body); err != nil {
t.Errorf("error validating handler response: %v", err)
return
}
}
})
}
}