forked from anstoli/go-kafka-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cached_schema_registry_test.go
303 lines (280 loc) · 9.31 KB
/
cached_schema_registry_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
295
296
297
298
299
300
301
302
303
package kafkaavro_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/hamba/avro"
kafkaavro "github.com/mycujoo/go-kafka-avro/v2"
)
// Portions of the code are taken from https://github.com/dangkaka/go-kafka-avro
type mockSchemaRegistryClient struct{}
func (mockSchemaRegistryClient) GetSchemaByID(id int) (avro.Schema, error) {
return avro.Parse("string")
}
func (mockSchemaRegistryClient) RegisterNewSchema(subject string, schema avro.Schema) (int, error) {
return 1, nil
}
type TestObject struct {
MockServer *httptest.Server
Schema avro.Schema
Subject string
ID int
Count int
}
type schemaVersionResponse struct {
Subject string `json:"subject"`
Version int `json:"version"`
Schema string `json:"schema"`
ID int `json:"id"`
}
type idResponse struct {
ID int `json:"id"`
}
const (
schemaByID = "/schemas/ids/%d"
subjects = "/subjects"
subjectVersions = "/subjects/%s/versions"
deleteSubject = "/subjects/%s"
subjectByVersion = "/subjects/%s/versions/%s"
)
func createSchemaRegistryTestObject(t *testing.T, subject string, id int) *TestObject {
testObject := &TestObject{}
testObject.Subject = subject
testObject.ID = id
testObject.Count = 0
schema, err := avro.Parse(`{"type": "record", "name": "test", "fields" : [{"name": "val", "type": "int", "default": 0}]}`)
if err != nil {
t.Errorf("Could not create schema %v", err)
}
testObject.Schema = schema
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
testObject.Count++
if r.Method == "POST" {
switch r.URL.String() {
case fmt.Sprintf(subjectVersions, subject), fmt.Sprintf(deleteSubject, subject):
response := idResponse{id}
str, _ := json.Marshal(response)
fmt.Fprintf(w, string(str))
case fmt.Sprintf(subjectVersions, subject+"2"), fmt.Sprintf(deleteSubject, subject+"2"):
response := idResponse{id}
str, _ := json.Marshal(response)
fmt.Fprintf(w, string(str))
}
} else if r.Method == "GET" {
switch r.URL.String() {
case fmt.Sprintf(schemaByID, id):
escapedSchema := strings.Replace(schema.String(), "\"", "\\\"", -1)
fmt.Fprintf(w, `{"schema": "%s"}`, escapedSchema)
case subjects:
response := []string{subject}
str, _ := json.Marshal(response)
fmt.Fprintf(w, string(str))
case fmt.Sprintf(subjectVersions, subject):
response := []int{id}
str, _ := json.Marshal(response)
fmt.Fprintf(w, string(str))
case fmt.Sprintf(subjectByVersion, subject, "1"), fmt.Sprintf(subjectByVersion, subject, "latest"):
response := schemaVersionResponse{subject, 1, schema.String(), id}
str, _ := json.Marshal(response)
fmt.Fprintf(w, string(str))
}
} else if r.Method == "DELETE" {
switch r.URL.String() {
case fmt.Sprintf(deleteSubject, subject),
fmt.Sprintf(subjectByVersion, subject, fmt.Sprintf("%d", 1)):
fmt.Fprintf(w, "[1]")
}
}
}))
testObject.MockServer = server
return testObject
}
func TestCachedSchemaRegistryClient_GetSchemaByID(t *testing.T) {
testObject := createSchemaRegistryTestObject(t, "test", 1)
mockServer := testObject.MockServer
defer mockServer.Close()
client, err := kafkaavro.NewCachedSchemaRegistryClient(mockServer.URL)
if nil != err {
t.Errorf("Error creating cached schema registry client: %s", err.Error())
return
}
_, err = client.GetSchemaByID(1)
if nil != err {
t.Errorf("Error getting schema: %s", err.Error())
}
responseSchema, err := client.GetSchemaByID(1)
if nil != err {
t.Errorf("Error getting schema: %s", err.Error())
return
}
if responseSchema.String() != testObject.Schema.String() {
t.Errorf("Schemas do not match. Expected: %s, got: %s", testObject.Schema.String(), responseSchema.String())
}
if testObject.Count > 1 {
t.Errorf("Expected call count of 1, got %d", testObject.Count)
}
}
func TestCachedSchemaRegistryClient_Subjects(t *testing.T) {
testObject := createSchemaRegistryTestObject(t, "test", 1)
mockServer := testObject.MockServer
defer mockServer.Close()
client, err := kafkaavro.NewCachedSchemaRegistryClient(mockServer.URL)
if nil != err {
t.Errorf("Error creating cached schema registry client: %s", err.Error())
return
}
subjects, err := client.Subjects()
if nil != err {
t.Errorf("Error getting subjects: %v", err)
}
if !containsStr(subjects, testObject.Subject) {
t.Errorf("Could not find subject")
}
}
func TestCachedSchemaRegistryClient_GetVersions(t *testing.T) {
testObject := createSchemaRegistryTestObject(t, "test", 1)
mockServer := testObject.MockServer
defer mockServer.Close()
client, err := kafkaavro.NewCachedSchemaRegistryClient(mockServer.URL)
if nil != err {
t.Errorf("Error creating cached schema registry client: %s", err.Error())
}
versions, err := client.Versions(testObject.Subject)
if nil != err {
t.Errorf("Error getting versions: %v", err)
}
if !containsInt(versions, testObject.ID) {
t.Errorf("Could not find version")
}
}
func TestCachedSchemaRegistryClient_GetSchemaByVersion(t *testing.T) {
testObject := createSchemaRegistryTestObject(t, "test", 1)
mockServer := testObject.MockServer
defer mockServer.Close()
client, err := kafkaavro.NewCachedSchemaRegistryClient(mockServer.URL)
if nil != err {
t.Errorf("Error creating cached schema registry client: %s", err.Error())
}
responseSchema, err := client.GetSchemaBySubject(testObject.Subject, 1)
if nil != err {
t.Errorf("Error getting schema versions: %v", err)
}
if responseSchema.String() != testObject.Schema.String() {
t.Errorf("Schemas do not match. Expected: %s, got: %s", testObject.Schema.String(), responseSchema.String())
}
}
func TestCachedSchemaRegistryClient_GetLatestSchema(t *testing.T) {
testObject := createSchemaRegistryTestObject(t, "test", 1)
mockServer := testObject.MockServer
defer mockServer.Close()
client, err := kafkaavro.NewCachedSchemaRegistryClient(mockServer.URL)
if nil != err {
t.Errorf("Error creating cached schema registry client: %s", err.Error())
}
responseSchema, err := client.GetLatestSchema(testObject.Subject)
if nil != err {
t.Errorf("Error getting latest schema: %v", err)
}
if responseSchema.String() != testObject.Schema.String() {
t.Errorf("Schemas do not match. Expected: %s, got: %s", testObject.Schema.String(), responseSchema.String())
}
}
func TestCachedSchemaRegistryClient_CreateSubject(t *testing.T) {
testObject := createSchemaRegistryTestObject(t, "test", 1)
mockServer := testObject.MockServer
defer mockServer.Close()
client, err := kafkaavro.NewCachedSchemaRegistryClient(mockServer.URL)
if nil != err {
t.Errorf("Error creating cached schema registry client: %s", err.Error())
}
id, err := client.RegisterNewSchema(testObject.Subject, testObject.Schema)
if nil != err {
t.Errorf("Error getting schema: %s", err.Error())
}
if id != testObject.ID {
t.Errorf("Ids do not match. Expected: %d, got: %d", testObject.ID, id)
}
sameid, err := client.RegisterNewSchema(testObject.Subject, testObject.Schema)
if nil != err {
t.Errorf("Error getting schema: %s", err.Error())
}
if sameid != id {
t.Errorf("Ids do not match. Expected: %d, got: %d", id, sameid)
}
if testObject.Count > 1 {
t.Errorf("Expected call count of 1, got %d", testObject.Count)
}
newid, err := client.RegisterNewSchema("test2", testObject.Schema)
if nil != err {
t.Errorf("Error getting schema: %s", err.Error())
}
// we still get same id, just for a different subject
if newid != id {
t.Errorf("Ids do not match. Expected: %d, got: %d", id, sameid)
}
if testObject.Count != 2 {
t.Errorf("Expected call count of 2, got %d", testObject.Count)
}
newid, err = client.RegisterNewSchema("test2", testObject.Schema)
if nil != err {
t.Errorf("Error getting schema: %s", err.Error())
}
// we still get same id, just for a different subject
if newid != id {
t.Errorf("Ids do not match. Expected: %d, got: %d", id, sameid)
}
if testObject.Count != 2 {
t.Errorf("Expected call count of 2, got %d", testObject.Count)
}
}
func TestCachedSchemaRegistryClient_IsSchemaRegistered(t *testing.T) {
testObject := createSchemaRegistryTestObject(t, "test", 1)
mockServer := testObject.MockServer
defer mockServer.Close()
client, err := kafkaavro.NewCachedSchemaRegistryClient(mockServer.URL)
if nil != err {
t.Errorf("Error creating cached schema registry client: %s", err.Error())
}
found, schema, err := client.IsSchemaRegistered(testObject.Subject, testObject.Schema)
if nil != err {
t.Errorf("Error getting schema id: %v", err)
}
if !found {
t.Error("Error getting schema")
}
if schema.ID != testObject.ID {
t.Errorf("Ids do not match. Expected: %d, got: %d", testObject.ID, schema.ID)
}
}
func TestCachedSchemaRegistryClient_DeleteSubject(t *testing.T) {
testObject := createSchemaRegistryTestObject(t, "test", 1)
mockServer := testObject.MockServer
defer mockServer.Close()
client, err := kafkaavro.NewCachedSchemaRegistryClient(mockServer.URL)
if nil != err {
t.Errorf("Error creating cached schema registry client: %s", err.Error())
}
_, err = client.DeleteSubject(testObject.Subject)
if nil != err {
t.Errorf("Error delete subject: %v", err)
}
}
func containsStr(array []string, value string) bool {
for _, v := range array {
if v == value {
return true
}
}
return false
}
func containsInt(array []int, value int) bool {
for _, v := range array {
if v == value {
return true
}
}
return false
}