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
353
354
355
356
357
358
359
360
361
362
|
package schema
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestNewRegistryClient(t *testing.T) {
config := RegistryConfig{
URL: "http://localhost:8081",
}
client := NewRegistryClient(config)
if client.baseURL != config.URL {
t.Errorf("Expected baseURL %s, got %s", config.URL, client.baseURL)
}
if client.cacheTTL != 5*time.Minute {
t.Errorf("Expected default cacheTTL 5m, got %v", client.cacheTTL)
}
if client.httpClient.Timeout != 30*time.Second {
t.Errorf("Expected default timeout 30s, got %v", client.httpClient.Timeout)
}
}
func TestRegistryClient_GetSchemaByID(t *testing.T) {
// Mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/schemas/ids/1" {
response := map[string]interface{}{
"schema": `{"type":"record","name":"User","fields":[{"name":"id","type":"int"}]}`,
"subject": "user-value",
"version": 1,
}
json.NewEncoder(w).Encode(response)
} else if r.URL.Path == "/schemas/ids/999" {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"error_code":40403,"message":"Schema not found"}`))
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
config := RegistryConfig{
URL: server.URL,
CacheTTL: 1 * time.Minute,
}
client := NewRegistryClient(config)
t.Run("successful fetch", func(t *testing.T) {
schema, err := client.GetSchemaByID(1)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if schema.ID != 1 {
t.Errorf("Expected schema ID 1, got %d", schema.ID)
}
if schema.Subject != "user-value" {
t.Errorf("Expected subject 'user-value', got %s", schema.Subject)
}
if schema.Format != FormatAvro {
t.Errorf("Expected Avro format, got %v", schema.Format)
}
})
t.Run("schema not found", func(t *testing.T) {
_, err := client.GetSchemaByID(999)
if err == nil {
t.Fatal("Expected error for non-existent schema")
}
})
t.Run("cache hit", func(t *testing.T) {
// First call should cache the result
schema1, err := client.GetSchemaByID(1)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
// Second call should hit cache (same timestamp)
schema2, err := client.GetSchemaByID(1)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if schema1.CachedAt != schema2.CachedAt {
t.Error("Expected cache hit with same timestamp")
}
})
}
func TestRegistryClient_GetLatestSchema(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/subjects/user-value/versions/latest" {
response := map[string]interface{}{
"id": uint32(1),
"schema": `{"type":"record","name":"User","fields":[{"name":"id","type":"int"}]}`,
"subject": "user-value",
"version": 1,
}
json.NewEncoder(w).Encode(response)
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
config := RegistryConfig{URL: server.URL}
client := NewRegistryClient(config)
schema, err := client.GetLatestSchema("user-value")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if schema.LatestID != 1 {
t.Errorf("Expected schema ID 1, got %d", schema.LatestID)
}
if schema.Subject != "user-value" {
t.Errorf("Expected subject 'user-value', got %s", schema.Subject)
}
}
func TestRegistryClient_RegisterSchema(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/subjects/test-value/versions" {
response := map[string]interface{}{
"id": uint32(123),
}
json.NewEncoder(w).Encode(response)
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
config := RegistryConfig{URL: server.URL}
client := NewRegistryClient(config)
schemaStr := `{"type":"record","name":"Test","fields":[{"name":"id","type":"int"}]}`
id, err := client.RegisterSchema("test-value", schemaStr)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if id != 123 {
t.Errorf("Expected schema ID 123, got %d", id)
}
}
func TestRegistryClient_CheckCompatibility(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/compatibility/subjects/test-value/versions/latest" {
response := map[string]interface{}{
"is_compatible": true,
}
json.NewEncoder(w).Encode(response)
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
config := RegistryConfig{URL: server.URL}
client := NewRegistryClient(config)
schemaStr := `{"type":"record","name":"Test","fields":[{"name":"id","type":"int"}]}`
compatible, err := client.CheckCompatibility("test-value", schemaStr)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if !compatible {
t.Error("Expected schema to be compatible")
}
}
func TestRegistryClient_ListSubjects(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/subjects" {
subjects := []string{"user-value", "order-value", "product-key"}
json.NewEncoder(w).Encode(subjects)
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
config := RegistryConfig{URL: server.URL}
client := NewRegistryClient(config)
subjects, err := client.ListSubjects()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
expectedSubjects := []string{"user-value", "order-value", "product-key"}
if len(subjects) != len(expectedSubjects) {
t.Errorf("Expected %d subjects, got %d", len(expectedSubjects), len(subjects))
}
for i, expected := range expectedSubjects {
if subjects[i] != expected {
t.Errorf("Expected subject %s, got %s", expected, subjects[i])
}
}
}
func TestRegistryClient_DetectSchemaFormat(t *testing.T) {
config := RegistryConfig{URL: "http://localhost:8081"}
client := NewRegistryClient(config)
tests := []struct {
name string
schema string
expected Format
}{
{
name: "Avro record schema",
schema: `{"type":"record","name":"User","fields":[{"name":"id","type":"int"}]}`,
expected: FormatAvro,
},
{
name: "Avro enum schema",
schema: `{"type":"enum","name":"Color","symbols":["RED","GREEN","BLUE"]}`,
expected: FormatAvro,
},
{
name: "JSON Schema",
schema: `{"$schema":"http://json-schema.org/draft-07/schema#","type":"object"}`,
expected: FormatJSONSchema,
},
{
name: "Protobuf (non-JSON)",
schema: "syntax = \"proto3\"; message User { int32 id = 1; }",
expected: FormatProtobuf,
},
{
name: "Simple Avro primitive",
schema: `{"type":"string"}`,
expected: FormatAvro,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
format := client.detectSchemaFormat(tt.schema)
if format != tt.expected {
t.Errorf("Expected format %v, got %v", tt.expected, format)
}
})
}
}
func TestRegistryClient_CacheManagement(t *testing.T) {
config := RegistryConfig{
URL: "http://localhost:8081",
CacheTTL: 100 * time.Millisecond, // Short TTL for testing
}
client := NewRegistryClient(config)
// Add some cache entries manually
client.schemaCache[1] = &CachedSchema{
ID: 1,
Schema: "test",
CachedAt: time.Now(),
}
client.subjectCache["test"] = &CachedSubject{
Subject: "test",
CachedAt: time.Now(),
}
// Check cache stats
schemaCount, subjectCount, _ := client.GetCacheStats()
if schemaCount != 1 || subjectCount != 1 {
t.Errorf("Expected 1 schema and 1 subject in cache, got %d and %d", schemaCount, subjectCount)
}
// Clear cache
client.ClearCache()
schemaCount, subjectCount, _ = client.GetCacheStats()
if schemaCount != 0 || subjectCount != 0 {
t.Errorf("Expected empty cache after clear, got %d schemas and %d subjects", schemaCount, subjectCount)
}
}
func TestRegistryClient_HealthCheck(t *testing.T) {
t.Run("healthy registry", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/subjects" {
json.NewEncoder(w).Encode([]string{})
}
}))
defer server.Close()
config := RegistryConfig{URL: server.URL}
client := NewRegistryClient(config)
err := client.HealthCheck()
if err != nil {
t.Errorf("Expected healthy registry, got error: %v", err)
}
})
t.Run("unhealthy registry", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()
config := RegistryConfig{URL: server.URL}
client := NewRegistryClient(config)
err := client.HealthCheck()
if err == nil {
t.Error("Expected error for unhealthy registry")
}
})
}
// Benchmark tests
func BenchmarkRegistryClient_GetSchemaByID(b *testing.B) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := map[string]interface{}{
"schema": `{"type":"record","name":"User","fields":[{"name":"id","type":"int"}]}`,
"subject": "user-value",
"version": 1,
}
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
config := RegistryConfig{URL: server.URL}
client := NewRegistryClient(config)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = client.GetSchemaByID(1)
}
}
func BenchmarkRegistryClient_DetectSchemaFormat(b *testing.B) {
config := RegistryConfig{URL: "http://localhost:8081"}
client := NewRegistryClient(config)
avroSchema := `{"type":"record","name":"User","fields":[{"name":"id","type":"int"}]}`
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = client.detectSchemaFormat(avroSchema)
}
}
|