forked from dangkaka/go-kafka-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemaRegistry_test.go
128 lines (119 loc) · 3.68 KB
/
schemaRegistry_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
package kafka
import (
"encoding/json"
"fmt"
"github.com/linkedin/goavro"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
type TestObject struct {
MockServer *httptest.Server
Codec *goavro.Codec
Subject string
Id int
Count int
}
func createSchemaRegistryTestObject(t *testing.T, subject string, id int) *TestObject {
testObject := &TestObject{}
testObject.Subject = subject
testObject.Id = id
testObject.Count = 0
codec, err := goavro.NewCodec(`{"type": "record", "name": "test", "fields" : [{"name": "val", "type": "int", "default": 0}]}`)
if err != nil {
t.Errorf("Could not create codec %v", err)
}
testObject.Codec = codec
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))
}
} else if r.Method == "GET" {
switch r.URL.String() {
case fmt.Sprintf(schemaByID, id):
escapedSchema := strings.Replace(codec.Schema(), "\"", "\\\"", -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, codec.Schema(), 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 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
}
func TestSchemaRegistryClient_Retries(t *testing.T) {
count := 0
response := []string{"test"}
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
count++
w.Header().Set("Content-Type", contentType)
if count < 3 {
http.Error(w, `{"error_code": 500, "message": "Error in the backend datastore"}`, 500)
} else {
str, _ := json.Marshal(response)
fmt.Fprintf(w, string(str))
}
}))
SchemaRegistryClient := NewSchemaRegistryClientWithRetries([]string{mockServer.URL}, 2)
subjects, err := SchemaRegistryClient.GetSubjects()
if err != nil {
t.Errorf("Found error %s", err)
}
if !reflect.DeepEqual(subjects, response) {
t.Errorf("Subjects did not match expected %s, got %s", response, subjects)
}
expectedCallCount := 3
if count != expectedCallCount {
t.Errorf("Expected error count to be %d, got %d", expectedCallCount, count)
}
}
func TestSchemaRegistryClient_Error(t *testing.T) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, `{"error_code": 500, "message": "Error in the backend datastore"}`, 500)
}))
SchemaRegistryClient := NewSchemaRegistryClient([]string{mockServer.URL})
_, err := SchemaRegistryClient.GetSubjects()
expectedErr := Error{500, "Error in the backend datastore"}
if err.Error() != expectedErr.Error() {
t.Errorf("Expected error to be %s, got %s", expectedErr.Error(), err.Error())
}
}