github.com/confluentinc/confluent-kafka-go@v1.9.2/schemaregistry/serde/jsonschema/json_schema_test.go (about) 1 /** 2 * Copyright 2022 Confluent Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package jsonschema 18 19 import ( 20 "testing" 21 22 "github.com/confluentinc/confluent-kafka-go/schemaregistry" 23 "github.com/confluentinc/confluent-kafka-go/schemaregistry/serde" 24 "github.com/confluentinc/confluent-kafka-go/schemaregistry/test" 25 ) 26 27 func TestJSONSchemaSerdeWithSimple(t *testing.T) { 28 serde.MaybeFail = serde.InitFailFunc(t) 29 var err error 30 conf := schemaregistry.NewConfig("mock://") 31 32 client, err := schemaregistry.NewClient(conf) 33 serde.MaybeFail("Schema Registry configuration", err) 34 35 ser, err := NewSerializer(client, serde.ValueSerde, NewSerializerConfig()) 36 serde.MaybeFail("Serializer configuration", err) 37 38 obj := JSONDemoSchema{} 39 obj.IntField = 123 40 obj.DoubleField = 45.67 41 obj.StringField = "hi" 42 obj.BoolField = true 43 obj.BytesField = []byte{0, 0, 0, 1} 44 bytes, err := ser.Serialize("topic1", &obj) 45 serde.MaybeFail("serialization", err) 46 47 deser, err := NewDeserializer(client, serde.ValueSerde, NewDeserializerConfig()) 48 serde.MaybeFail("Deserializer configuration", err) 49 deser.Client = ser.Client 50 51 var newobj JSONDemoSchema 52 err = deser.DeserializeInto("topic1", bytes, &newobj) 53 serde.MaybeFail("deserialization", err, serde.Expect(newobj, obj)) 54 } 55 56 func TestJSONSchemaSerdeWithNested(t *testing.T) { 57 serde.MaybeFail = serde.InitFailFunc(t) 58 var err error 59 conf := schemaregistry.NewConfig("mock://") 60 61 client, err := schemaregistry.NewClient(conf) 62 serde.MaybeFail("Schema Registry configuration", err) 63 64 ser, err := NewSerializer(client, serde.ValueSerde, NewSerializerConfig()) 65 serde.MaybeFail("Serializer configuration", err) 66 67 nested := JSONDemoSchema{} 68 nested.IntField = 123 69 nested.DoubleField = 45.67 70 nested.StringField = "hi" 71 nested.BoolField = true 72 nested.BytesField = []byte{0, 0, 0, 1} 73 obj := JSONNestedTestRecord{ 74 OtherField: nested, 75 } 76 bytes, err := ser.Serialize("topic1", &obj) 77 serde.MaybeFail("serialization", err) 78 79 deser, err := NewDeserializer(client, serde.ValueSerde, NewDeserializerConfig()) 80 serde.MaybeFail("Deserializer configuration", err) 81 deser.Client = ser.Client 82 83 var newobj JSONNestedTestRecord 84 err = deser.DeserializeInto("topic1", bytes, &newobj) 85 serde.MaybeFail("deserialization", err, serde.Expect(newobj, obj)) 86 } 87 88 type JSONDemoSchema struct { 89 IntField int32 `json:"IntField"` 90 91 DoubleField float64 `json:"DoubleField"` 92 93 StringField string `json:"StringField"` 94 95 BoolField bool `json:"BoolField"` 96 97 BytesField test.Bytes `json:"BytesField"` 98 } 99 100 type JSONNestedTestRecord struct { 101 OtherField JSONDemoSchema 102 } 103 104 type JSONLinkedList struct { 105 Value int32 106 Next *JSONLinkedList 107 }