forked from mostafa/xk6-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonschema_test.go
120 lines (104 loc) · 4.76 KB
/
jsonschema_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
package kafka
import (
"testing"
"github.com/stretchr/testify/assert"
)
var (
jsonConfig Configuration = Configuration{
Producer: ProducerConfiguration{
ValueSerializer: JsonSchemaSerializer,
KeySerializer: JsonSchemaSerializer,
},
Consumer: ConsumerConfiguration{
ValueDeserializer: JsonSchemaDeserializer,
KeyDeserializer: JsonSchemaDeserializer,
},
}
jsonSchema string = `{"type":"object","title":"Key","properties":{"field": {"type":"string"}},"required":["field"]}`
)
// TestSerializeDeserializeJson tests serialization and deserialization (and validation) of
// JSON data
func TestSerializeDeserializeJson(t *testing.T) {
// Test with a schema registry, which fails and manually (de)serializes the data
for _, element := range []Element{Key, Value} {
// Serialize the key or value
serialized, err := SerializeJson(jsonConfig, "topic", `{"field":"value"}`, element, jsonSchema, 0)
assert.Nil(t, err)
assert.NotNil(t, serialized)
// 4 bytes for magic byte, 1 byte for schema ID, and the rest is the data
assert.GreaterOrEqual(t, len(serialized), 10)
// Deserialize the key or value (removes the magic bytes)
deserialized, err := DeserializeJson(jsonConfig, "topic", serialized, element, jsonSchema, 0)
assert.Nil(t, err)
assert.Equal(t, map[string]interface{}{"field": "value"}, deserialized)
}
}
// TestSerializeDeserializeJsonFailsOnSchemaError tests serialization and deserialization (and
// validation) of JSON data and fails on schema error
func TestSerializeDeserializeJsonFailsOnSchemaError(t *testing.T) {
schema := `{`
for _, element := range []Element{Key, Value} {
// Serialize the key or value
serialized, err := SerializeJson(jsonConfig, "topic", `{"field":"value"}`, element, schema, 0)
assert.Nil(t, serialized)
assert.Error(t, err.Unwrap())
assert.Equal(t, "Failed to create codec for encoding JSON", err.Message)
assert.Equal(t, failedCreateJsonSchemaCodec, err.Code)
// Deserialize the key or value
deserialized, err := DeserializeJson(jsonConfig, "topic", []byte{1, 2, 3, 4, 5, 6}, element, schema, 0)
assert.Nil(t, deserialized)
assert.Error(t, err.Unwrap())
assert.Equal(t, "Failed to create codec for decoding JSON data", err.Message)
assert.Equal(t, failedCreateJsonSchemaCodec, err.Code)
}
}
// TestSerializeDeserializeJsonFailsOnWireFormatError tests serialization and deserialization (and
// validation) of JSON data and fails on wire format error
func TestSerializeDeserializeJsonFailsOnWireFormatError(t *testing.T) {
schema := `{}`
for _, element := range []Element{Key, Value} {
// Deserialize an empty key or value
deserialized, err := DeserializeJson(jsonConfig, "topic", []byte{}, element, schema, 0)
assert.Nil(t, deserialized)
assert.Error(t, err.Unwrap())
assert.Equal(t, "Failed to remove wire format from the binary data", err.Message)
assert.Equal(t, failedDecodeFromWireFormat, err.Code)
// Deserialize a broken key or value
// Proper wire-formatted message has 5 bytes (the wire format) plus data
deserialized, err = DeserializeJson(jsonConfig, "topic", []byte{1, 2, 3, 4}, element, schema, 0)
assert.Nil(t, deserialized)
assert.Error(t, err.Unwrap())
assert.Equal(t, "Failed to remove wire format from the binary data", err.Message)
assert.Equal(t, failedDecodeFromWireFormat, err.Code)
}
}
// TestSerializeDeserializeJsonFailsOnMarshalError tests serialization and deserialization (and
// validation) of JSON data and fails on JSON marshal error
func TestSerializeDeserializeJsonFailsOnMarshalError(t *testing.T) {
data := `{"nonExistingField":"`
for _, element := range []Element{Key, Value} {
serialized, err := SerializeJson(jsonConfig, "topic", data, element, jsonSchema, 0)
assert.Nil(t, serialized)
assert.Error(t, err.Unwrap())
assert.Equal(t, "Failed to unmarshal JSON data", err.Message)
assert.Equal(t, failedUnmarshalJson, err.Code)
deserialized, err := DeserializeJson(jsonConfig, "topic", []byte{1, 2, 3, 4, 5, 6}, element, jsonSchema, 0)
assert.Nil(t, deserialized)
assert.Error(t, err.Unwrap())
assert.Equal(t, "Failed to unmarshal JSON data", err.Message)
assert.Equal(t, failedUnmarshalJson, err.Code)
}
}
// TestSerializeDeserializeJsonFailsOnValidationError tests serialization and deserialization (and
// validation) of JSON data and fails on JSON validation error
func TestSerializeDeserializeJsonFailsOnValidationError(t *testing.T) {
// JSON schema validation fails, but the data is still returned
data := `{"nonExistingField":"value"}`
for _, element := range []Element{Key, Value} {
serialized, err := SerializeJson(jsonConfig, "topic", data, element, jsonSchema, 0)
assert.Nil(t, err)
assert.NotNil(t, serialized)
// 4 bytes for magic byte, 1 byte for schema ID, and the rest is the data
assert.GreaterOrEqual(t, len(serialized), 28)
}
}