public void GenerateClass_ClassWithEnum_GeneratedModelIsAsExpected()
        {
            //Arrange
            var schema = AvroConvert.GenerateSchema(typeof(ClassWithEnum));

            //Act
            string resultSchema = AvroConvert.GenerateModel(schema);


            //Assert
            Assert.Equal(
                "public class ClassWithEnum\r\n" +
                "{\r\n" +
                "\tpublic TestEnum? EnumProp { get; set; }\r\n" +
                "}\r\n" +
                "\r\n" +
                "public enum TestEnum\r\n" +
                "{\r\n" +
                "\ta,\r\n" +
                "\tbe,\r\n" +
                "\tca,\r\n" +
                "\tdlo\r\n" +
                "}\r\n" +
                "\r\n",
                resultSchema);
        }
        public void GenerateClass_NestedClass_OutputIsEqualToExpected()
        {
            //Arrange
            var schema = AvroConvert.GenerateSchema(typeof(BaseTestClass));

            //Act
            string resultSchema = AvroConvert.GenerateModel(schema);


            //Assert
            Assert.Equal(
                "public class BaseTestClass\r\n" +
                "{\r\n" +
                "\tpublic string justSomeProperty { get; set; }\r\n" +
                "\tpublic long andLongProperty { get; set; }\r\n" +
                "\tpublic User objectProperty { get; set; }\r\n" +
                "}\r\n" +
                "\r\n" +
                "public class User\r\n" +
                "{\r\n" +
                "\tpublic string name { get; set; }\r\n" +
                "\tpublic int? favorite_number { get; set; }\r\n" +
                "\tpublic string favorite_color { get; set; }\r\n" +
                "}\r\n" +
                "\r\n",
                resultSchema);
        }
        public void Component_HeadlessList_ResultIsTheSameAsInput()
        {
            //Arrange
            List <int> list = _fixture.Create <List <int> >();

            var schema = AvroConvert.GenerateSchema(typeof(List <int>));

            //Act
            var serialized = AvroConvert.SerializeHeadless(list, schema);

            var result = new List <int>();

            using (var reader = AvroConvert.OpenDeserializer <int>(new MemoryStream(serialized)))
            {
                while (reader.HasNext())
                {
                    var item = reader.ReadNext();

                    result.Add(item);
                }
            }


            //Assert
            Assert.NotNull(result);
            Assert.NotNull(serialized);
            Assert.Equal(list, result);
        }
Пример #4
0
        public void Component_AvroAttributeClass_ResultIsEqualToInput()
        {
            //Arrange
            AttributeClass toSerialize = new AttributeClass
            {
                AndAnotherString    = "anotherString",
                NullableIntProperty = 1,
                NullableIntPropertyWithDefaultValue = null,
                NullableStringProperty = "nullableString"
            };
            string schema = AvroConvert.GenerateSchema(typeof(AttributeClass));


            //Act
            var result = AvroConvert.SerializeHeadless(toSerialize, schema);

            var deserialized = AvroConvert.DeserializeHeadless <AttributeClass>(result, schema);


            //Assert
            Assert.NotNull(result);
            Assert.NotNull(deserialized);
            Assert.Equal(toSerialize.NullableIntProperty, deserialized.NullableIntProperty);
            Assert.Equal(toSerialize.AndAnotherString, deserialized.AndAnotherString);
            Assert.Equal(toSerialize.NullableStringProperty, deserialized.NullableStringProperty);
            Assert.Equal(2137, deserialized.NullableIntPropertyWithDefaultValue);
        }
Пример #5
0
        public void Deserialize_CustomSchema_OnlyValuesFromCustomSchemaAreReturned()
        {
            //Arrange
            string customSchema = AvroConvert.GenerateSchema(typeof(UserNameClass));

            Dictionary <string, object> result1 = new Dictionary <string, object>();

            result1.Add("name", "Alyssa");

            Dictionary <string, object> result2 = new Dictionary <string, object>();

            result2.Add("name", "Ben");

            List <object> expectedResult = new List <object>();

            expectedResult.Add(result1);
            expectedResult.Add(result2);


            //Act
            var result = AvroConvert.Deserialize(_avroBytes, customSchema);


            //Assert
            Assert.Equal(expectedResult, result);
        }
Пример #6
0
        static void Main(string[] args)
        {
            IntegrationTest.Invoke();

            var fixture = new Fixture();
            var data    = fixture.CreateMany <Dataset>(30000).ToArray();


            Console.WriteLine("AvroConvert Benchmark - fire!");
            Console.WriteLine("");
            Console.WriteLine("The Benchmark compares Apache.Avro, AvroConvert (nuget version) and AvroConvert local version");

            int noOfRuns = 30;

            Console.WriteLine($"Number of runs: {noOfRuns}");

            string schema = AvroConvert.GenerateSchema(typeof(Dataset[]));


            //Act
            List <BenchmarkResult> benchmarkResults = new List <BenchmarkResult>();

            for (int i = 0; i < noOfRuns; i++)
            {
                benchmarkResults.Add(RunBenchmark(data, schema));
                if ((i + 1) % 10 == 0)
                {
                    Console.WriteLine($"Progress: {i + 1}/{noOfRuns}");
                }
            }


            BenchmarkResult mean = new BenchmarkResult();

            mean.AvroConvertVNextGzipSerializeTime   = benchmarkResults.Sum(r => r.AvroConvertVNextGzipSerializeTime) / noOfRuns;
            mean.AvroConvertVNextGzipDeserializeTime = benchmarkResults.Sum(r => r.AvroConvertVNextGzipDeserializeTime) / noOfRuns;
            mean.AvroConvertVNextGzipSize            = benchmarkResults.Sum(r => r.AvroConvertVNextGzipSize) / noOfRuns;

            mean.ApacheAvroSerializeTime   = benchmarkResults.Sum(r => r.ApacheAvroSerializeTime) / noOfRuns;
            mean.ApacheAvroDeserializeTime = benchmarkResults.Sum(r => r.ApacheAvroDeserializeTime) / noOfRuns;
            mean.ApacheAvroSize            = benchmarkResults.Sum(r => r.ApacheAvroSize) / noOfRuns;

            mean.AvroConvertHeadlessSerializeTime   = benchmarkResults.Sum(r => r.AvroConvertHeadlessSerializeTime) / noOfRuns;
            mean.AvroConvertHeadlessDeserializeTime = benchmarkResults.Sum(r => r.AvroConvertHeadlessDeserializeTime) / noOfRuns;
            mean.AvroConvertHeadlessSize            = benchmarkResults.Sum(r => r.AvroConvertHeadlessSize) / noOfRuns;

            mean.AvroConvertGzipSerializeTime   = benchmarkResults.Sum(r => r.AvroConvertGzipSerializeTime) / noOfRuns;
            mean.AvroConvertGzipDeserializeTime = benchmarkResults.Sum(r => r.AvroConvertGzipDeserializeTime) / noOfRuns;
            mean.AvroConvertGzipSize            = benchmarkResults.Sum(r => r.AvroConvertGzipSize) / noOfRuns;

            mean.AvroConvertVNextHeadlessSerializeTime   = benchmarkResults.Sum(r => r.AvroConvertVNextHeadlessSerializeTime) / noOfRuns;
            mean.AvroConvertVNextHeadlessDeserializeTime = benchmarkResults.Sum(r => r.AvroConvertVNextHeadlessDeserializeTime) / noOfRuns;
            mean.AvroConvertVNextSize = benchmarkResults.Sum(r => r.AvroConvertVNextSize) / noOfRuns;

            ConstructLog(mean);

            Console.ReadLine();
        }
Пример #7
0
        public void GenerateSchema_PropertiesAreSystemNullable_SchemaIndicatesFieldIsNullable()
        {
            //Arrange

            //Act
            string schema = AvroConvert.GenerateSchema(typeof(User));

            // Assert
            Assert.Contains("{\"name\":\"favorite_number\",\"type\":[\"null\",\"int\"]}", schema);
        }
Пример #8
0
        public void GenerateSchema_PropertiesAreDecoratedWithDefaultValueAttributes_SchemaPositionsOriginalTypeBeforeNullWhenDefaultIsNotNull()
        {
            //Arrange

            //Act
            string schema = AvroConvert.GenerateSchema(typeof(DefaultValueClass));

            //Assert
            Assert.Contains("{\"name\":\"justSomeProperty\",\"type\":[\"string\",\"null\"],\"default\":\"Let's go\"}", schema);
        }
Пример #9
0
        public void GenerateSchema_Enum_SchemaIsCorrect()
        {
            //Arrange

            //Act
            string schema = AvroConvert.GenerateSchema(typeof(TestEnum));

            //Assert
            Assert.Contains("{\"type\":\"enum\",\"name\":\"TestEnum\",\"namespace\":\"AvroConvertComponentTests\",\"symbols\":[\"a\",\"be\",\"ca\",\"dlo\"]}", schema);
        }
Пример #10
0
        public void GenerateSchema_ClassWithMixedMembersAttributesAndNon_AfterIncludingOnlyMembersNonAttributedAreIgnored()
        {
            //Arrange

            //Act
            string schema = AvroConvert.GenerateSchema(typeof(MixedDataMembers), includeOnlyDataContractMembers: true);

            //Assert
            Assert.Contains("{\"type\":\"record\",\"name\":\"MixedDataMembers\",\"namespace\":\"AvroConvertComponentTests\",\"fields\":[{\"name\":\"savedValues\",\"type\":{\"type\":\"array\",\"items\":\"int\"}},{\"name\":\"andAnother\",\"type\":[\"null\",\"long\"]}]}", schema);
        }
Пример #11
0
        public void GenerateSchema_PropertiesIncludeNullableVersionsOfTypes_SchemaIncludesNullTypeInTypesArray()
        {
            //Arrange

            //Act
            string schema = AvroConvert.GenerateSchema(typeof(DefaultValueClass));

            //Assert
            Assert.Contains("{\"name\":\"andLongProperty\",\"type\":[\"null\",\"long\"]", schema);
        }
Пример #12
0
        public void GenerateSchema_PropertiesAreDecoratedWithDefaultValueAttributes_SchemaPositionsNullTypeAfterOtherInTheTypeArrayWhenDefaultIsNotNull()
        {
            //Arrange

            //Act
            string schema = AvroConvert.GenerateSchema(typeof(DefaultValueClass));

            //Assert
            Assert.Contains("{\"name\":\"andLongBigDefaultedProperty\",\"type\":[\"long\",\"null\"],\"default\":9200000000000000007}", schema);
        }
Пример #13
0
        public void GenerateSchema_PropertiesAreDecoratedWithNullableSchemaAttribute_SchemaIndicatesFieldIsNullable()
        {
            //Arrange

            //Act
            string schema = AvroConvert.GenerateSchema(typeof(AttributeClass));

            // Assert
            Assert.Contains("{\"name\":\"favorite_number\",\"aliases\":[\"NullableIntProperty\"],\"type\":[\"null\",\"int\"]}",
                            schema);
        }
Пример #14
0
        public void GenerateSchema_PropertiesAreNullableReferenceTypes_SchemaIndicatesMembersAreNullable()
        {
            //Arrange

            //Act
            string schema = AvroConvert.GenerateSchema(typeof(ClassWithNullableMembers));

            // Assert
            Assert.Contains("{\"name\":\"NullableStringProperty\",\"type\":[\"null\",\"string\"]}", schema);
            Assert.Contains("{\"name\":\"NullableField\",\"type\":[\"null\",\"string\"]}", schema);
        }
Пример #15
0
        public void GenerateSchema_PropertiesAreDecoratedWithDefaultValueAttributes_SchemaPositionsTypesEffectivelyRegardlessOfMismatchBetweenDefaultValueAndPropertyType()
        {
            //Arrange

            //Act
            string schema = AvroConvert.GenerateSchema(typeof(DefaultValueClass));

            // Assert - The DefaultValue is an int, (100)  but the property is a long, matching
            // isn't necessary, all that's required is that the 'not null' type is first in the schema list
            Assert.Contains("{\"name\":\"andLongSmallDefaultedProperty\",\"type\":[\"long\",\"null\"],\"default\":100}", schema);
        }
        public void GenerateSchema_ClassIsChildOfAbstractClass_SchemaIsGenerated()
        {
            //Arrange


            //Act
            string schema = AvroConvert.GenerateSchema(typeof(Farm));


            //Assert
            Assert.Equal(@"{""type"":""record"",""name"":""Farm"",""namespace"":""AvroConvertComponentTests.GenerateSchema"",""fields"":[{""name"":""Name"",""type"":""string""},{""name"":""Animals"",""type"":{""type"":""array"",""items"":{""type"":""record"",""name"":""Animal"",""namespace"":""AvroConvertComponentTests.GenerateSchema"",""fields"":[{""name"":""IsOutside"",""type"":""boolean""}]}}},{""name"":""Plants"",""type"":{""type"":""array"",""items"":{""type"":""record"",""name"":""IPlant"",""namespace"":""AvroConvertComponentTests.GenerateSchema"",""fields"":[{""name"":""Color"",""type"":""string""}]}}}]}", schema);
        }
Пример #17
0
        public void GenerateSchema_PropertiesAreDecoratedWithDefaultValueAttributes_SchemaContainsDefaultFieldForMembers()
        {
            //Arrange

            //Act
            string schema = AvroConvert.GenerateSchema(typeof(DefaultValueClass));

            //Assert
            Assert.Contains("\"default\":\"Let's go\"", schema);
            Assert.Contains("\"default\":9200000000000000007", schema);
            Assert.Contains("\"default\":null", schema);
        }
Пример #18
0
        //Decimals are handled atm as strings
        // [InlineData(typeof(decimal?), @"[""null"",{""type"":""bytes"",""logicalType"":""decimal"",""precision"":29,""scale"":14}]")]
        // [InlineData(typeof(decimal), @"{""type"":""bytes"",""logicalType"":""decimal"",""precision"":29,""scale"":14}")]
        public void GenerateSchema_DotNetTypeCompatibleWithLogicType(Type type, string schema)
        {
            //Arrange


            //Act
            var result = AvroConvert.GenerateSchema(type);


            //Assert
            Assert.Equal(schema, result);
        }
        public void GenerateSchema_ClassContainsNoDefaultConstructor_SchemaIsGenerated()
        {
            //Arrange


            //Act
            string schema = AvroConvert.GenerateSchema(typeof(ClassWithoutDefaultConstructor));


            //Assert
            Assert.Equal(@"{""type"":""record"",""name"":""ClassWithoutDefaultConstructor"",""namespace"":""AvroConvertComponentTests"",""fields"":[{""name"":""Name"",""type"":""string""}]}", schema);
        }
        public async Task <byte[]> SerializeAsync(T data, SerializationContext context)
        {
            string subject = $"{context.Topic }-{context.Component}";
            int    id      = 0;
            var    schema  = AvroConvert.GenerateSchema(typeof(T));

            if (cache.ContainsKey(subject))
            {
                id = cache[subject];
            }
            else
            {
                var existingSchema = await RegistryClient.GetLatestSchemaAsync(subject);

                if (existingSchema.SchemaString == schema)
                {
                    cache.AddOrUpdate(subject, existingSchema.Id, (key, oldValue) => existingSchema.Id);
                }
                else
                {
                    Confluent.SchemaRegistry.Schema confluentSchema = new Confluent.SchemaRegistry.Schema(
                        subject,
                        existingSchema.Version + 1,
                        0,
                        schema
                        );

                    id = await RegistryClient.RegisterSchemaAsync(subject, confluentSchema.ToString());

                    cache.AddOrUpdate(subject, id, (key, oldValue) => id);
                }
            }

            using (var stream = new MemoryStream())
            {
                //Confluent Kafka format:
                //https://docs.confluent.io/current/schema-registry/docs/serializer-formatter.html#wire-format

                //Magic number
                stream.WriteByte(0x00);

                //Id
                var idAsBytes = BitConverter.GetBytes(id);
                stream.Write(idAsBytes, 0, idAsBytes.Length);

                //Data
                var serializedData = AvroConvert.SerializeHeadless(data, schema);
                stream.Write(serializedData, 0, serializedData.Length);

                return(stream.ToArray());
            }
        }
Пример #21
0
        public void Component_DeserializeWithMissingFields_NoError()
        {
            //Arrange
            BaseTestClass toSerialize = _fixture.Create <BaseTestClass>();
            string        schema      = AvroConvert.GenerateSchema(typeof(BaseTestClass));

            //Act
            var result = AvroConvert.SerializeHeadless(toSerialize, schema);

            var deserialized = AvroConvert.DeserializeHeadless <ReducedBaseTestClass>(result, schema);

            //Assert
            Assert.NotNull(result);
            Assert.NotNull(deserialized);
            Assert.Equal(toSerialize.justSomeProperty, deserialized.justSomeProperty);
        }
Пример #22
0
        public void Component_SerializeHeadlessBiggerObjectAndReadSmaller_NoError()
        {
            //Arrange
            VeryComplexClass toSerialize = _fixture.Create <VeryComplexClass>();
            string           schema      = AvroConvert.GenerateSchema(typeof(VeryComplexClass));

            //Act
            var result = AvroConvert.SerializeHeadless(toSerialize, schema);

            var deserialized = AvroConvert.DeserializeHeadless <VeryComplexClass>(result, schema);

            //Assert
            Assert.NotNull(result);
            Assert.NotNull(deserialized);
            Assert.Equal(toSerialize, deserialized);
        }
Пример #23
0
        public void Component_SerializeHeadlessBiggerObjectAndReadSmaller_NoError()
        {
            //Arrange
            ExtendedBaseTestClass toSerialize = _fixture.Create <ExtendedBaseTestClass>();
            string schema = AvroConvert.GenerateSchema(typeof(BaseTestClass));

            //Act
            var result = AvroConvert.SerializeHeadless(toSerialize, schema);

            var deserialized = AvroConvert.DeserializeHeadless <BaseTestClass>(result, schema);

            //Assert
            Assert.NotNull(result);
            Assert.NotNull(deserialized);
            Assert.Equal(toSerialize.andLongProperty, deserialized.andLongProperty);
            Assert.Equal(toSerialize.justSomeProperty, deserialized.justSomeProperty);
        }
        public void Avro2Json_ConvertArray_ProducedDesiredJson()
        {
            //Arrange
            var arrayTestObject = new int[] { 2, 1, 3, 7, 453, 1, 6, };

            var expectedJson = JsonConvert.SerializeObject(arrayTestObject);

            var schema         = AvroConvert.GenerateSchema(arrayTestObject.GetType());
            var avroSerialized = AvroConvert.SerializeHeadless(arrayTestObject, schema);


            //Act
            var resultJson = AvroConvert.Avro2Json(avroSerialized, schema);


            //Assert
            Assert.Equal(expectedJson, resultJson);
        }
        public void Avro2Json_ConvertDouble_ProducedDesiredJson()
        {
            //Arrange
            var doubleTestObject = 21.34;

            var expectedJson = JsonConvert.SerializeObject(doubleTestObject);

            var schema         = AvroConvert.GenerateSchema(doubleTestObject.GetType());
            var avroSerialized = AvroConvert.SerializeHeadless(doubleTestObject, schema);


            //Act
            var resultJson = AvroConvert.Avro2Json(avroSerialized, schema);


            //Assert
            Assert.Equal(expectedJson, resultJson);
        }
        public void Avro2Json_ConvertNull_ProducedDesiredJson()
        {
            //Arrange
            string nullTestObject = null;

            var expectedJson = JsonConvert.SerializeObject(nullTestObject);

            var schema         = AvroConvert.GenerateSchema(nullTestObject?.GetType());
            var avroSerialized = AvroConvert.SerializeHeadless(nullTestObject, schema);


            //Act
            var resultJson = AvroConvert.Avro2Json(avroSerialized, schema);


            //Assert
            Assert.Equal(expectedJson, resultJson);
        }
        public void Avro2Json_ConvertInt_ProducedDesiredJson()
        {
            //Arrange
            var @int = 2137;

            var expectedJson = JsonConvert.SerializeObject(@int);

            var schema         = AvroConvert.GenerateSchema(@int.GetType());
            var avroSerialized = AvroConvert.SerializeHeadless(@int, schema);


            //Act
            var resultJson = AvroConvert.Avro2Json(avroSerialized, schema);


            //Assert
            Assert.Equal(expectedJson, resultJson);
        }
        public void Avro2Json_ConvertString_ProducedDesiredJson()
        {
            //Arrange
            var @string = "I am the serialization string";

            var expectedJson = JsonConvert.SerializeObject(@string);

            var schema         = AvroConvert.GenerateSchema(@string.GetType());
            var avroSerialized = AvroConvert.SerializeHeadless(@string, schema);


            //Act
            var resultJson = AvroConvert.Avro2Json(avroSerialized, schema);


            //Assert
            Assert.Equal(expectedJson, resultJson);
        }
        public void GenerateClass_ClassWithLogicalTypes_OutputIsEqualToExpected()
        {
            //Arrange
            var schema = AvroConvert.GenerateSchema(typeof(LogicalTypesClass));

            //Act
            string resultSchema = AvroConvert.GenerateModel(schema);


            //Assert
            Assert.Equal(
                "public class LogicalTypesClass\r\n" +
                "{\r\n" +
                "\tpublic decimal One { get; set; }\r\n" +
                "\tpublic Guid? Two { get; set; }\r\n" +
                "\tpublic TimeSpan Three { get; set; }\r\n" +
                "\tpublic DateTime? Four { get; set; }\r\n" +
                "\tpublic DateTime Five { get; set; }\r\n" +
                "}\r\n" +
                "\r\n",
                resultSchema);
        }
        public void Avro2Json_ConvertComplexType_ProducedDesiredJson()
        {
            //Arrange
            var user = new User();

            user.favorite_color  = "blue";
            user.favorite_number = 2137;
            user.name            = "red";

            var expectedJson = JsonConvert.SerializeObject(user);

            var schema         = AvroConvert.GenerateSchema(user.GetType());
            var avroSerialized = AvroConvert.SerializeHeadless(user, schema);


            //Act
            var resultJson = AvroConvert.Avro2Json(avroSerialized, schema);


            //Assert
            Assert.Equal(expectedJson, resultJson);
        }