forked from mostafa/xk6-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
99 lines (91 loc) · 2.81 KB
/
json_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
package kafka
import (
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
// TestSerializeJSON tests the serialization of a JSON object.
func TestSerializeJSON(t *testing.T) {
jsonSerde := &JSONSerde{}
expected := []byte(`{"key":"value"}`)
actual, err := jsonSerde.Serialize(map[string]interface{}{"key": "value"}, nil)
assert.Nil(t, err)
assert.Equal(t, expected, actual)
}
// TestSerializeJSONWithSchema tests the serialization of a JSON object with a schema.
func TestSerializeJSONWithSchema(t *testing.T) {
jsonSerde := &JSONSerde{}
expected := []byte(`{"key":"value"}`)
schema := &Schema{
ID: 1,
Schema: `{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"key": {"type": "string"}
},
"required": ["key"]
}`,
Version: 1,
Subject: "json-schema",
}
actual, err := jsonSerde.Serialize(map[string]interface{}{"key": "value"}, schema)
assert.Nil(t, err)
assert.Equal(t, expected, actual)
}
// TestSerializeJSONFailsOnInvalidSchema tests the serialization of a JSON object
// and fails on an invalid schema.
func TestSerializeJSONFailsOnInvalidSchema(t *testing.T) {
jsonSerde := &JSONSerde{}
schema := &Schema{
ID: 1,
Schema: `{`,
Version: 1,
Subject: "json-schema",
}
actual, err := jsonSerde.Serialize(map[string]interface{}{"value": "key"}, schema)
assert.Nil(t, actual)
assert.NotNil(t, err)
assert.Equal(t, ErrInvalidSchema, err)
}
// TestSerializeJSONFailsOnValidation tests the serialization of a JSON object
// and fails on schema validation errors.
func TestSerializeJSONFailsOnValidation(t *testing.T) {
jsonSerde := &JSONSerde{}
schema := &Schema{
ID: 1,
Schema: `{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"key": {"type": "string"}
},
"required": ["key"]
}`,
Version: 1,
Subject: "json-schema",
}
actual, err := jsonSerde.Serialize(map[string]interface{}{"value": "key"}, schema)
assert.Nil(t, actual)
assert.NotNil(t, err)
assert.Equal(t, "Failed to validate JSON against schema", err.Message)
assert.Equal(t, failedValidateJSON, err.Code)
}
// TestSerializeJSONFailsOnBadType tests the serialization of a JSON object
// and fails on nested invalid data type.
func TestSerializeJSONFailsOnBadType(t *testing.T) {
jsonSerde := &JSONSerde{}
actual, err := jsonSerde.Serialize(map[string]interface{}{"key": unsafe.Pointer(nil)}, nil)
assert.Nil(t, actual)
assert.NotNil(t, err)
assert.Equal(t, ErrInvalidDataType, err)
}
// TestSerializeJSONFailsOnInvalidDataType tests the serialization of a JSON object
// and fails on invalid data type.
func TestSerializeJSONFailsOnInvalidDataType(t *testing.T) {
jsonSerde := &JSONSerde{}
actual, err := jsonSerde.Serialize(1, nil)
assert.Nil(t, actual)
assert.NotNil(t, err)
assert.Equal(t, ErrInvalidDataType, err)
}