github.com/confluentinc/confluent-kafka-go@v1.9.2/schemaregistry/serde/avro/avro_specific_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 avro 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 TestSpecificAvroSerdeWithSimple(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 := NewSpecificSerializer(client, serde.ValueSerde, NewSerializerConfig()) 36 serde.MaybeFail("Serializer configuration", err) 37 38 obj := test.NewDemoSchema() 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 := NewSpecificDeserializer(client, serde.ValueSerde, NewDeserializerConfig()) 48 serde.MaybeFail("Deserializer configuration", err) 49 deser.Client = ser.Client 50 51 var newobj test.DemoSchema 52 err = deser.DeserializeInto("topic1", bytes, &newobj) 53 serde.MaybeFail("deserialization", err, serde.Expect(newobj, obj)) 54 } 55 56 func TestSpecificAvroSerdeWithNested(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 := NewSpecificSerializer(client, serde.ValueSerde, NewSerializerConfig()) 65 serde.MaybeFail("Serializer configuration", err) 66 67 nested := test.NestedRecord{ 68 StringField: "hi", 69 BoolField: true, 70 BytesField: []byte{1, 2}, 71 } 72 number := test.NumberRecord{ 73 IntField: 123, 74 LongField: 456, 75 FloatField: 1.23, 76 DoubleField: 4.56, 77 } 78 obj := test.NestedTestRecord{ 79 NumberField: number, 80 OtherField: nested, 81 } 82 bytes, err := ser.Serialize("topic1", &obj) 83 serde.MaybeFail("serialization", err) 84 85 deser, err := NewSpecificDeserializer(client, serde.ValueSerde, NewDeserializerConfig()) 86 serde.MaybeFail("Deserializer configuration", err) 87 deser.Client = ser.Client 88 89 var newobj test.NestedTestRecord 90 err = deser.DeserializeInto("topic1", bytes, &newobj) 91 serde.MaybeFail("deserialization", err, serde.Expect(newobj, obj)) 92 } 93 94 func TestSpecificAvroSerdeWithCycle(t *testing.T) { 95 serde.MaybeFail = serde.InitFailFunc(t) 96 var err error 97 conf := schemaregistry.NewConfig("mock://") 98 99 client, err := schemaregistry.NewClient(conf) 100 serde.MaybeFail("Schema Registry configuration", err) 101 102 ser, err := NewSpecificSerializer(client, serde.ValueSerde, NewSerializerConfig()) 103 serde.MaybeFail("Serializer configuration", err) 104 105 inner := test.RecursiveUnionTestRecord{ 106 RecursiveField: nil, 107 } 108 wrapper := test.UnionNullRecursiveUnionTestRecord{ 109 RecursiveUnionTestRecord: inner, 110 UnionType: 1, 111 } 112 obj := test.RecursiveUnionTestRecord{ 113 RecursiveField: &wrapper, 114 } 115 bytes, err := ser.Serialize("topic1", &obj) 116 serde.MaybeFail("serialization", err) 117 118 deser, err := NewSpecificDeserializer(client, serde.ValueSerde, NewDeserializerConfig()) 119 serde.MaybeFail("Deserializer configuration", err) 120 deser.Client = ser.Client 121 122 var newobj test.RecursiveUnionTestRecord 123 err = deser.DeserializeInto("topic1", bytes, &newobj) 124 serde.MaybeFail("deserialization", err, serde.Expect(newobj, obj)) 125 }