Beispiel #1
0
        public virtual void TestAllowNullBytesSchemaEncodeAndDecode()
        {
            var fooAvroSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barAvroSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());

            var bar = new Bar
            {
                Field1 = true
            };

            var foo = new Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = "3",
                Field4 = bar,
                Color  = Color.RED
            };

            var fooBytes = fooAvroSchema.Encode(foo);
            var barBytes = barAvroSchema.Encode(bar);

            var encodeBytes = ISchema <object> .KvBytes().Encode(new KeyValue <byte[], byte[]>(fooBytes, barBytes));

            var decodeKV = ISchema <object> .KvBytes().Decode(encodeBytes);

            var fooBack = fooAvroSchema.Decode(decodeKV.Key);
            var barBack = barAvroSchema.Decode(decodeKV.Value);

            Assert.True(foo.Equals(fooBack));
            Assert.True(bar.Equals(barBack));
        }
Beispiel #2
0
        public virtual void TestNotAllowNullEncodeAndDecode()
        {
            var jsonSchema = JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build());

            var foo1 = new Foo
            {
                Field1 = "foo1",
                Field2 = "bar1",
                Field4 = new Bar()
            };

            var foo2 = new Foo
            {
                Field1 = "foo2",
                Field2 = "bar2"
            };

            var bytes1  = jsonSchema.Encode(foo1);
            var object1 = jsonSchema.Decode(bytes1);

            Assert.True(bytes1.Length > 0);
            Assert.True(object1.Equals(foo1));

            try
            {
                jsonSchema.Encode(foo2);
            }
            catch (Exception e)
            {
                Assert.True(e is SchemaSerializationException);
            }
        }
Beispiel #3
0
        public virtual void TestSchemaDefinition()
        {
            var schema1 = typeof(DefaultStruct).GetSchema();
            var schema2 = AvroSchema <StructWithAnnotations> .Of(typeof(StructWithAnnotations));

            var schemaDef1 = schema1.ToString();
            var schemaDef2 = Encoding.UTF8.GetString(schema2.SchemaInfo.Schema);

            Assert.NotEqual(schemaDef1, schemaDef2);

            var schema3 = AvroSchema <StructWithAnnotations> .Of(ISchemaDefinition <StructWithAnnotations> .Builder().WithJsonDef(schemaDef1).Build());

            var schemaDef3 = Encoding.UTF8.GetString(schema3.SchemaInfo.Schema);

            Assert.True(schemaDef1.Contains("DefaultStruct") && schemaDef3.Contains("DefaultStruct"));
            Assert.NotEqual(schemaDef2, schemaDef3);

            var @struct = new StructWithAnnotations
            {
                Field1 = 5678
            };

            // schema2 is using the schema generated from POJO,
            // it allows field2 to be nullable, and field3 has default value.
            schema2.Encode(@struct);
            // schema3 is using the schema passed in, which doesn't allow nullable
            schema3.Encode(@struct);
        }
Beispiel #4
0
        public virtual void TestJsonSchemaCreate()
        {
            var fooSchema = JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build());

            var barSchema = JSONSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).WithAlwaysAllowNull(false).Build());

            var keyValueSchema1 = ISchema <object> .KeyValue(fooSchema, barSchema);

            var keyValueSchema2 = ISchema <object> .KeyValue(JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build()), JSONSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).WithAlwaysAllowNull(false).Build()));

            var keyValueSchema3 = ISchema <object> .KeyValue(JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build()), JSONSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).WithAlwaysAllowNull(false).Build()));

            Assert.Equal(keyValueSchema1.SchemaInfo.Type, SchemaType.KeyValue);
            Assert.Equal(keyValueSchema2.SchemaInfo.Type, SchemaType.KeyValue);
            Assert.Equal(keyValueSchema3.SchemaInfo.Type, SchemaType.KeyValue);

            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema1).KeySchema.SchemaInfo.Type, SchemaType.JSON);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema1).ValueSchema.SchemaInfo.Type, SchemaType.JSON);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema2).KeySchema.SchemaInfo.Type, SchemaType.JSON);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema2).ValueSchema.SchemaInfo.Type, SchemaType.JSON);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema3).KeySchema.SchemaInfo.Type, SchemaType.JSON);
            Assert.Equal(((KeyValueSchema <Foo, Bar>)keyValueSchema3).ValueSchema.SchemaInfo.Type, SchemaType.JSON);

            var schemaInfo1 = Encoding.UTF8.GetString(keyValueSchema1.SchemaInfo.Schema);
            var schemaInfo2 = Encoding.UTF8.GetString(keyValueSchema2.SchemaInfo.Schema);
            var schemaInfo3 = Encoding.UTF8.GetString(keyValueSchema3.SchemaInfo.Schema);

            Assert.Equal(schemaInfo1, schemaInfo2);
            Assert.Equal(schemaInfo1, schemaInfo3);
        }
Beispiel #5
0
        public virtual void TestNotAllowNullSchemaEncodeAndDecode()
        {
            var keyValueSchema = ISchema <object> .KeyValue(JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build())
                                                            , JSONSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).WithAlwaysAllowNull(false).Build()));

            var bar = new Bar
            {
                Field1 = true
            };

            var foo = new Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = "3",
                Field4 = bar,
                Color  = Color.RED
            };

            var encodeBytes = keyValueSchema.Encode(new KeyValue <Foo, Bar>(foo, bar));

            Assert.True(encodeBytes.Length > 0);

            var keyValue = keyValueSchema.Decode(encodeBytes);
            var fooBack  = keyValue.Key;
            var barBack  = keyValue.Value;

            Assert.True(foo.Equals(fooBack));
            Assert.True(bar.Equals(barBack));
        }
Beispiel #6
0
        public virtual void TestInlineKeyValueEncodingTypeSchemaEncodeAndDecode()
        {
            var fooSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());

            var keyValueSchema = ISchema <object> .KeyValue(fooSchema, barSchema, KeyValueEncodingType.INLINE);


            var bar = new Bar
            {
                Field1 = true
            };

            var foo = new Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = "3",
                Field4 = bar,
                Color  = Color.RED
            };

            // Check kv.encoding.type INLINE
            var encodeBytes = keyValueSchema.Encode(new KeyValue <Foo, Bar>(foo, bar));

            Assert.True(encodeBytes.Length > 0);
            var keyValue = keyValueSchema.Decode(encodeBytes);
            var fooBack  = keyValue.Key;
            var barBack  = keyValue.Value;

            Assert.True(foo.Equals(fooBack));
            Assert.True(bar.Equals(barBack));
        }
Beispiel #7
0
        public virtual void TestDefaultGetProducerDataAssigned()
        {
            var fooSchema = AvroSchema <SchemaTestUtils.Foo> .Of(ISchemaDefinition <SchemaTestUtils.Foo> .Builder().WithPojo(typeof(SchemaTestUtils.Foo)).Build());

            var barSchema = AvroSchema <SchemaTestUtils.Bar> .Of(ISchemaDefinition <SchemaTestUtils.Bar> .Builder().WithPojo(typeof(SchemaTestUtils.Bar)).Build());

            var keyValueSchema = ISchema <object> .KeyValue(fooSchema, barSchema);

            var foo = new SchemaTestUtils.Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = 3
            };
            var bar = new SchemaTestUtils.Bar
            {
                Field1 = true
            };

            // // Check kv.encoding.type default, not set value
            var encodeBytes = keyValueSchema.Encode(new KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar>(foo, bar));
            var builder     = new MessageMetadata();

            builder.ProducerName = "default";

            var msg = Message <KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar> > .Create(builder, new ReadOnlySequence <byte>(encodeBytes), keyValueSchema);

            var keyValue = msg.Value;

            Assert.Equal(keyValue.Key, foo);
            Assert.Equal(keyValue.Value, bar);
            Assert.False(builder.ShouldSerializePartitionKey());
        }
Beispiel #8
0
        public virtual void TestSeparatedGetProducerDataAssigned()
        {
            var fooSchema = AvroSchema <SchemaTestUtils.Foo> .Of(ISchemaDefinition <SchemaTestUtils.Foo> .Builder().WithPojo(typeof(SchemaTestUtils.Foo)).Build());

            var barSchema = AvroSchema <SchemaTestUtils.Bar> .Of(ISchemaDefinition <SchemaTestUtils.Bar> .Builder().WithPojo(typeof(SchemaTestUtils.Bar)).Build());

            var keyValueSchema = ISchema <int> .KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);

            var foo = new SchemaTestUtils.Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = 3
            };
            var bar = new SchemaTestUtils.Bar
            {
                Field1 = true
            };

            // Check kv.encoding.type SPRAERATE
            var encodeBytes = keyValueSchema.Encode(new KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar>(foo, bar));
            var builder     = new MessageMetadata();

            builder.ProducerName           = "separated";
            builder.PartitionKey           = Convert.ToBase64String(fooSchema.Encode(foo));
            builder.PartitionKeyB64Encoded = true;
            var msg = Message <KeyValue <SchemaTestUtils.Foo, SchemaTestUtils.Bar> > .Create(builder, new ReadOnlySequence <byte>(encodeBytes), keyValueSchema);

            var keyValue = msg.Value;

            Assert.Equal(keyValue.Key, foo);
            Assert.Equal(keyValue.Value, bar);
            Assert.True(builder.ShouldSerializePartitionKey());
        }
Beispiel #9
0
        public GenericAvroReaderTest()
        {
            fooSchema = AvroSchema <SchemaTestUtils.Foo> .Of(typeof(SchemaTestUtils.Foo));

            fooV2Schema = AvroSchema <SchemaTestUtils.FooV2> .Of(typeof(SchemaTestUtils.FooV2));

            fooSchemaNotNull = AvroSchema <SchemaTestUtils.Foo> .Of(ISchemaDefinition <SchemaTestUtils.Foo> .Builder().WithAlwaysAllowNull(false).WithPojo(typeof(SchemaTestUtils.Foo)).Build());

            fooOffsetSchema = AvroSchema <SchemaTestUtils.Foo> .Of(typeof(SchemaTestUtils.Foo));

            fooOffsetSchema.SchemaInfo.Properties.Add(GenericAvroSchema.OFFSET_PROP, "5");
            var prop = fooOffsetSchema.AvroSchema.GetProperty(GenericAvroSchema.OFFSET_PROP);

            foo = new SchemaTestUtils.Foo
            {
                Field1          = "foo1",
                Field2          = "bar1",
                Field4          = new SchemaTestUtils.Bar(),
                FieldUnableNull = "notNull"
            };

            fooV2 = new SchemaTestUtils.FooV2
            {
                Field1 = ("foo1"),
                Field3 = (10)
            };
        }
Beispiel #10
0
        public virtual void TestFillParametersToSchemainfo()
        {
            var fooSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());

            fooSchema.SchemaInfo.Name = "foo";
            fooSchema.SchemaInfo.Type = SchemaType.AVRO;
            IDictionary <string, string> keyProperties = new Dictionary <string, string>
            {
                ["foo.key1"] = "value",
                ["foo.key2"] = "value"
            };

            fooSchema.SchemaInfo.Properties = keyProperties;
            barSchema.SchemaInfo.Name       = "bar";
            barSchema.SchemaInfo.Type       = SchemaType.AVRO;
            IDictionary <string, string> valueProperties = new Dictionary <string, string>
            {
                ["bar.key"] = "key"
            };

            barSchema.SchemaInfo.Properties = valueProperties;
            var keyValueSchema1 = ISchema <object> .KeyValue(fooSchema, barSchema);

            Assert.Equal("foo", keyValueSchema1.SchemaInfo.Properties["key.schema.name"]);
            Assert.Equal(SchemaType.AVRO.ToString(), keyValueSchema1.SchemaInfo.Properties["key.schema.type"]);
            Assert.Equal(@"{""foo.key1"":""value"",""foo.key2"":""value""}", keyValueSchema1.SchemaInfo.Properties["key.schema.properties"]);
            Assert.Equal("bar", keyValueSchema1.SchemaInfo.Properties["value.schema.name"]);
            Assert.Equal(SchemaType.AVRO.ToString(), keyValueSchema1.SchemaInfo.Properties["value.schema.type"]);
            Assert.Equal(@"{""bar.key"":""key""}", keyValueSchema1.SchemaInfo.Properties["value.schema.properties"]);
        }
        public GenericAvroSchemaTest()
        {
            var avroFooV2Schema = AvroSchema <SchemaTestUtils.FooV2> .Of(ISchemaDefinition <SchemaTestUtils.FooV2> .Builder().WithAlwaysAllowNull(false).WithPojo(typeof(SchemaTestUtils.FooV2)).Build());

            //var avroFooSchema = AvroSchema<SchemaTestUtils.Foo>.Of(ISchemaDefinition<SchemaTestUtils.Foo>.Builder().WithAlwaysAllowNull(false).WithPojo(typeof(SchemaTestUtils.Foo)).Build());
            writerSchema = new GenericAvroSchema(avroFooV2Schema.SchemaInfo);
            readerSchema = new GenericAvroSchema(avroFooV2Schema.SchemaInfo);
        }
Beispiel #12
0
        public virtual void TestNotAllowNullSchema()
        {
            var jsonSchema = JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).WithAlwaysAllowNull(false).Build());

            Assert.Equal(jsonSchema.SchemaInfo.Type, SchemaType.JSON);
            var schemaJson = jsonSchema.SchemaInfo.SchemaDefinition;
            var schema     = Avro.Schema.Parse(schemaJson);

            Assert.NotNull(schema);
        }
Beispiel #13
0
        public void TestDateTimeDecimalLogicalType()
        {
            var avroSchema = AvroSchema <LogicalMessage> .Of(ISchemaDefinition <LogicalMessage> .Builder().WithPojo(typeof(LogicalMessage)).WithJSR310ConversionEnabled(true).Build());

            var logMsg = new LogicalMessage {
                Schema = Avro.Schema.Parse(avroSchema.SchemaInfo.SchemaDefinition), CreatedTime = DateTime.Now, DayOfWeek = "Saturday", Size = new AvroDecimal(102.65M)
            };

            var bytes1 = avroSchema.Encode(logMsg);

            Assert.True(bytes1.Length > 0);

            var msg = avroSchema.Decode(bytes1);

            Assert.NotNull(msg);
            Assert.True(msg.Size == 102.65M);
        }
Beispiel #14
0
        public virtual void TestSeparatedKeyValueEncodingTypeSchemaEncodeAndDecode()
        {
            var fooSchema = AvroSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var barSchema = AvroSchema <Bar> .Of(ISchemaDefinition <Bar> .Builder().WithPojo(typeof(Bar)).Build());


            var keyValueSchema = ISchema <object> .KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED);

            var bar = new Bar
            {
                Field1 = true
            };

            var foo = new Foo
            {
                Field1 = "field1",
                Field2 = "field2",
                Field3 = "3",
                Field4 = bar,
                Color  = Color.RED
            };

            // Check kv.encoding.type SEPARATED
            var encodeBytes = keyValueSchema.Encode(new KeyValue <Foo, Bar>(foo, bar));

            Assert.True(encodeBytes.Length > 0);
            try
            {
                keyValueSchema.Decode(encodeBytes);
                Assert.True(false, "This method cannot be used under this SEPARATED encoding type");
            }
            catch (SchemaSerializationException e)
            {
                Assert.Contains("This method cannot be used under this SEPARATED encoding type", e.Message);
            }
            var keyValue = ((KeyValueSchema <Foo, Bar>)keyValueSchema).Decode(fooSchema.Encode(foo), encodeBytes, null);
            var fooBack  = keyValue.Key;
            var barBack  = keyValue.Value;

            Assert.True(foo.Equals(fooBack));
            Assert.True(bar.Equals(barBack));
        }
Beispiel #15
0
        public void TestAvroSchemaUserDefinedReadAndWriter()
        {
            var reader           = new JsonReader <Foo>();
            var writer           = new JsonWriter <Foo>();
            var schemaDefinition = ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Bar)).WithSchemaReader(reader).WithSchemaWriter(writer).Build();

            var schema = AvroSchema <Foo> .Of(schemaDefinition);

            var foo = new Foo();

            foo.Color = Color.RED;
            var field1 = "test";

            foo.Field1 = field1;
            var encoded = schema.Encode(foo);

            foo = schema.Decode(encoded);
            Assert.Equal(Color.RED, foo.Color);
            Assert.Equal(field1, foo.Field1);
        }
Beispiel #16
0
        public void TestLogicalType()
        {
            var avroSchema = AvroSchema <SchemaLogicalType> .Of(ISchemaDefinition <SchemaLogicalType> .Builder().WithPojo(typeof(SchemaLogicalType)).WithJSR310ConversionEnabled(true).Build());

            var schemaLogicalType = new SchemaLogicalType
            {
                TimestampMicros = DateTimeHelper.CurrentUnixTimeMillis() * 1000,
                TimestampMillis = DateTime.Parse("2019-03-26T04:39:58.469Z").Ticks,
                Decimal         = 12.34D,
                Date            = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
                TimeMicros      = DateTimeHelper.CurrentUnixTimeMillis() * 1000,
                TimeMillis      = (DateTime.Now - DateTime.Today).Ticks
            };

            var bytes1 = avroSchema.Encode(schemaLogicalType);

            Assert.True(bytes1.Length > 0);

            var object1 = avroSchema.Decode(bytes1);

            Assert.True(schemaLogicalType.Equals(object1));
        }
Beispiel #17
0
        public virtual void TestAllowNullEncodeAndDecode()
        {
            var jsonSchema = JSONSchema <Foo> .Of(ISchemaDefinition <Foo> .Builder().WithPojo(typeof(Foo)).Build());

            var bar = new Bar
            {
                Field1 = true
            };

            var foo1 = new Foo
            {
                Field1 = "foo1",
                Field2 = "bar1",
                Field4 = bar,
                Color  = Color.BLUE
            };

            var foo2 = new Foo
            {
                Field1 = "foo2",
                Field2 = "bar2"
            };

            var bytes1 = jsonSchema.Encode(foo1);

            Assert.True(bytes1.Length > 0);

            var bytes2 = jsonSchema.Encode(foo2);

            Assert.True(bytes2.Length > 0);

            var object1 = jsonSchema.Decode(bytes1);
            var object2 = jsonSchema.Decode(bytes2);

            Assert.True(object1.Equals(foo1));
            Assert.True(object2.Equals(foo2));
        }
Beispiel #18
0
 public static JSONSchema <T> Of(Type Pojo)
 {
     return(JSONSchema <T> .Of(ISchemaDefinition <T> .Builder().WithPojo(Pojo).Build()));
 }
Beispiel #19
0
 public static JSONSchema <T> Of <T>(Type Pojo, IDictionary <string, string> Properties)
 {
     return(JSONSchema <T> .Of(ISchemaDefinition <T> .Builder().WithPojo(Pojo).WithProperties(Properties).Build()));
 }
Beispiel #20
0
 public static AvroSchema <T> Of(Type pojo, IDictionary <string, string> properties)
 {
     return(Of(ISchemaDefinition <T> .Builder().WithPojo(pojo).WithProperties(properties).Build()));
 }
Beispiel #21
0
 public static AvroSchema <T> Of(Type pojo)
 {
     return(Of(ISchemaDefinition <T> .Builder().WithPojo(pojo).Build()));
 }