github.com/confluentinc/confluent-kafka-go@v1.9.2/schemaregistry/serde/avro/avro_generic_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 TestGenericAvroSerdeWithSimple(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 := NewGenericSerializer(client, serde.ValueSerde, NewSerializerConfig())
    36  	serde.MaybeFail("Serializer configuration", err)
    37  
    38  	obj := GenericDemoSchema{}
    39  	obj.IntField = 123
    40  	obj.DoubleField = 45.67
    41  	obj.StringField = "hi"
    42  	obj.BoolField = true
    43  	obj.BytesField = []byte{1, 2}
    44  	bytes, err := ser.Serialize("topic1", &obj)
    45  	serde.MaybeFail("serialization", err)
    46  
    47  	deser, err := NewGenericDeserializer(client, serde.ValueSerde, NewDeserializerConfig())
    48  	serde.MaybeFail("Deserializer configuration", err)
    49  	deser.Client = ser.Client
    50  
    51  	var newobj GenericDemoSchema
    52  	err = deser.DeserializeInto("topic1", bytes, &newobj)
    53  	serde.MaybeFail("deserialization", err, serde.Expect(newobj, obj))
    54  }
    55  
    56  func TestGenericAvroSerdeWithNested(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 := NewGenericSerializer(client, serde.ValueSerde, NewSerializerConfig())
    65  	serde.MaybeFail("Serializer configuration", err)
    66  
    67  	nested := GenericDemoSchema{}
    68  	nested.IntField = 123
    69  	nested.DoubleField = 45.67
    70  	nested.StringField = "hi"
    71  	nested.BoolField = true
    72  	nested.BytesField = []byte{1, 2}
    73  	obj := GenericNestedTestRecord{
    74  		OtherField: nested,
    75  	}
    76  
    77  	bytes, err := ser.Serialize("topic1", &obj)
    78  	serde.MaybeFail("serialization", err)
    79  
    80  	deser, err := NewGenericDeserializer(client, serde.ValueSerde, NewDeserializerConfig())
    81  	serde.MaybeFail("Deserializer configuration", err)
    82  	deser.Client = ser.Client
    83  
    84  	var newobj GenericNestedTestRecord
    85  	err = deser.DeserializeInto("topic1", bytes, &newobj)
    86  	serde.MaybeFail("deserialization", err, serde.Expect(newobj, obj))
    87  }
    88  
    89  func TestGenericAvroSerdeWithCycle(t *testing.T) {
    90  	serde.MaybeFail = serde.InitFailFunc(t)
    91  	var err error
    92  	conf := schemaregistry.NewConfig("mock://")
    93  
    94  	client, err := schemaregistry.NewClient(conf)
    95  	serde.MaybeFail("Schema Registry configuration", err)
    96  
    97  	ser, err := NewGenericSerializer(client, serde.ValueSerde, NewSerializerConfig())
    98  	serde.MaybeFail("Serializer configuration", err)
    99  
   100  	nested := GenericLinkedList{
   101  		Value: 456,
   102  	}
   103  	obj := GenericLinkedList{
   104  		Value: 123,
   105  		Next:  &nested,
   106  	}
   107  
   108  	bytes, err := ser.Serialize("topic1", &obj)
   109  	serde.MaybeFail("serialization", err)
   110  
   111  	deser, err := NewGenericDeserializer(client, serde.ValueSerde, NewDeserializerConfig())
   112  	serde.MaybeFail("Deserializer configuration", err)
   113  	deser.Client = ser.Client
   114  
   115  	var newobj GenericLinkedList
   116  	err = deser.DeserializeInto("topic1", bytes, &newobj)
   117  	serde.MaybeFail("deserialization", err, serde.Expect(newobj, obj))
   118  }
   119  
   120  type GenericDemoSchema struct {
   121  	IntField int32 `json:"IntField"`
   122  
   123  	DoubleField float64 `json:"DoubleField"`
   124  
   125  	StringField string `json:"StringField"`
   126  
   127  	BoolField bool `json:"BoolField"`
   128  
   129  	BytesField test.Bytes `json:"BytesField"`
   130  }
   131  
   132  type GenericNestedTestRecord struct {
   133  	OtherField GenericDemoSchema
   134  }
   135  
   136  type GenericLinkedList struct {
   137  	Value int32
   138  	Next  *GenericLinkedList
   139  }