-
Notifications
You must be signed in to change notification settings - Fork 2
/
topic_test.go
294 lines (242 loc) · 8.08 KB
/
topic_test.go
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
package dbaas
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const topicID = "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4"
const testTopicNotFoundResponse = `{
"error": {
"code": 404,
"title": "Not Found",
"message": "topic %s not found."
}
}`
const testCreateTopicInvalidDatastoreIDResponse = `{
"error": {
"code": 400,
"title": "Bad Request",
"message":
"Validation failure: {'topic.datastore_id': \"'20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f' is not a 'UUID'\"}"
}
}`
const testUpdateTopicInvalidPartitionsResponse = `{
"error": {
"code": 400,
"title": "Bad Request",
"message":
"Validation failure: {'topic.partitions': \"'4001 is greater than the maximum of 4000'\"}"
}
}`
const testTopicResponse = `{
"topic": {
"id": "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
"created_at": "1970-01-01T00:00:00",
"updated_at": "1970-01-01T00:00:00",
"project_id": "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
"datastore_id": "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
"name": "topic1",
"partitions": 1,
"status": "ACTIVE"
}
}`
const testTopicsResponse = `
{
"topics": [
{
"id": "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
"created_at": "1970-01-01T00:00:00",
"updated_at": "1970-01-01T00:00:00",
"project_id": "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
"datastore_id": "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
"name": "topic1",
"partitions": 1,
"status": "ACTIVE"
},
{
"id": "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f5",
"created_at": "1970-01-01T00:00:00",
"updated_at": "1970-01-01T00:00:00",
"project_id": "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
"datastore_id": "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
"name": "topic2",
"partitions": 2,
"status": "ACTIVE"
}
]
}`
var TopicResponse = Topic{ //nolint
ID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
CreatedAt: "1970-01-01T00:00:00",
UpdatedAt: "1970-01-01T00:00:00",
ProjectID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
DatastoreID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
Name: "topic1",
Partitions: 1,
Status: StatusActive,
}
var TopicExpected Topic = Topic{ //nolint
ID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
CreatedAt: "1970-01-01T00:00:00",
UpdatedAt: "1970-01-01T00:00:00",
ProjectID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
DatastoreID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
Name: "topic1",
Partitions: 1,
Status: StatusActive,
}
func TestTopics(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", testClient.Endpoint+TopicsURI,
httpmock.NewStringResponder(200, testTopicsResponse))
expected := []Topic{
{
ID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
CreatedAt: "1970-01-01T00:00:00",
UpdatedAt: "1970-01-01T00:00:00",
ProjectID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
DatastoreID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
Name: "topic1",
Partitions: 1,
Status: StatusActive,
},
{
ID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f5",
CreatedAt: "1970-01-01T00:00:00",
UpdatedAt: "1970-01-01T00:00:00",
ProjectID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
DatastoreID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
Name: "topic2",
Partitions: 2,
Status: StatusActive,
},
}
actual, err := testClient.Topics(context.Background(), nil)
require.NoError(t, err)
assert.Equal(t, expected, actual)
}
func TestTopic(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", testClient.Endpoint+TopicsURI+"/"+topicID,
httpmock.NewStringResponder(200, testTopicResponse))
actual, err := testClient.Topic(context.Background(), topicID)
require.NoError(t, err)
assert.Equal(t, TopicExpected, actual)
}
func TestTopicNotFound(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()
notFoundResponse := fmt.Sprintf(testTopicNotFoundResponse, NotFoundEntityID)
httpmock.RegisterResponder("GET", testClient.Endpoint+TopicsURI+"/"+NotFoundEntityID,
httpmock.NewStringResponder(404, notFoundResponse))
expected := &DBaaSAPIError{}
expected.APIError.Code = 404
expected.APIError.Title = ErrorNotFoundTitle
expected.APIError.Message = fmt.Sprintf("topic %s not found.", NotFoundEntityID)
_, err := testClient.Topic(context.Background(), NotFoundEntityID)
require.ErrorAs(t, err, &expected)
}
func TestCreateToopic(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("POST", testClient.Endpoint+TopicsURI,
func(req *http.Request) (*http.Response, error) {
if err := json.NewDecoder(req.Body).Decode(&TopicCreateOpts{}); err != nil {
return httpmock.NewStringResponse(400, ""), err
}
topics := make(map[string]Topic)
TopicCreateResponse := TopicResponse
TopicCreateResponse.Status = StatusPendingCreate
topics["topic"] = TopicResponse
resp, err := httpmock.NewJsonResponse(200, topics)
if err != nil {
return httpmock.NewStringResponse(500, ""), err
}
return resp, nil
})
createTopicOpts := TopicCreateOpts{
DatastoreID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
Name: "topic1",
Partitions: 1,
}
actual, err := testClient.CreateTopic(context.Background(), createTopicOpts)
TopicCreateExpexted := TopicExpected
TopicCreateExpexted.Status = StatusPendingCreate
require.NoError(t, err)
assert.Equal(t, TopicExpected, actual)
}
func TestCreateTopicInvalidDatastoreID(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("POST", testClient.Endpoint+TopicsURI,
httpmock.NewStringResponder(400, testCreateTopicInvalidDatastoreIDResponse))
expected := &DBaaSAPIError{}
expected.APIError.Code = 400
expected.APIError.Title = ErrorBadRequestTitle
expected.APIError.Message = `Validation failure:
{'topic.datastore_id': \"'20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f' is not a 'UUID'\"}`
createTopicOpts := TopicCreateOpts{
DatastoreID: "20d7bcf4-f8d6-4bf6-b8f6-46cb440a87f4",
Name: "topic1",
Partitions: 1,
}
_, err := testClient.CreateTopic(context.Background(), createTopicOpts)
require.ErrorAs(t, err, &expected)
}
func TestUpdateTopic(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("PUT", testClient.Endpoint+TopicsURI+"/"+topicID,
func(req *http.Request) (*http.Response, error) {
if err := json.NewDecoder(req.Body).Decode(&TopicUpdateOpts{}); err != nil {
return httpmock.NewStringResponse(400, ""), err
}
topics := make(map[string]Topic)
TopicUpdateResponse := TopicResponse
TopicUpdateResponse.Status = StatusPendingUpdate
topics["topic"] = TopicUpdateResponse
resp, err := httpmock.NewJsonResponse(200, topics)
if err != nil {
return httpmock.NewStringResponse(500, ""), err
}
return resp, nil
})
updateTopicOpts := TopicUpdateOpts{
Partitions: 2,
}
actual, err := testClient.UpdateTopic(context.Background(), topicID, updateTopicOpts)
TopicUpdateExpected := TopicExpected
TopicUpdateExpected.Status = StatusPendingUpdate
require.NoError(t, err)
assert.Equal(t, TopicUpdateExpected, actual)
}
func TestUpdateTopicInvalidPartitions(t *testing.T) {
httpmock.Activate()
testClient := SetupTestClient()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("PUT", testClient.Endpoint+TopicsURI+"/"+topicID,
httpmock.NewStringResponder(400, testUpdateTopicInvalidPartitionsResponse))
expected := &DBaaSAPIError{}
expected.APIError.Code = 400
expected.APIError.Title = ErrorBadRequestTitle
expected.APIError.Message = `Validation failure:
{'topic.partitions': \"'4001 is greater than the maximum of 4000'\"}`
updateTopicOpts := TopicUpdateOpts{
Partitions: 4001,
}
_, err := testClient.UpdateTopic(context.Background(), topicID, updateTopicOpts)
require.ErrorAs(t, err, &expected)
}