Ejemplo n.º 1
0
        public void TestUnionResultingInNumber()
        {
            var schema = SchemaBuilder.Record(
                "typename",
                Field("anUnion", Union(IntType(), FloatType())));

            EventType eventType = SupportAvroUtil.MakeAvroSupportEventType(schema);

            AssertPropertyType(typeof(object), null, eventType, "anUnion");

            Consumer <Object> asserterFromDatum = (value) => {
                var datum = new GenericRecord(schema);
                datum.Put("anUnion", value);
                Assert.AreEqual(value, new AvroGenericDataEventBean(datum, eventType).Get("anUnion"));
            };

            asserterFromDatum.Invoke(1);
            asserterFromDatum.Invoke(2f);

            BiConsumer <String, Object> asserterFromJson = (
                json,
                value) => {
                var datum = SupportAvroUtil.ParseQuoted(schema, json);
                Assert.AreEqual(value, new AvroGenericDataEventBean(datum, eventType).Get("anUnion"));
            };

            asserterFromJson.Invoke("{'anUnion':{'int':1}}", 1);
            asserterFromJson.Invoke("{'anUnion':{'float':2}}", 2f);
        }
Ejemplo n.º 2
0
        private void RunAssertionNullableOrOptTypes(RecordSchema schema)
        {
            EventType eventType = SupportAvroUtil.MakeAvroSupportEventType(schema);

            AssertTypesBoxed(eventType);

            var datum = GetRecordWithValues(schema);

            AssertValuesRequired(new AvroGenericDataEventBean(datum, eventType));

            var jsonWValues =
                "{" +
                "'myInt': {'int': 10}, " +
                "'myCharSeq': {'string': 'x'}, " +
                "'myString': {'string': 'y'}," +
                "'myBoolean': {'boolean': true}, " +
                "'myBytes': {'bytes': '\\u00AA\'}, " +
                "'myDouble': {'double': 50}, " +
                "'myFloat': {'float': 100}, " +
                "'myLong': {'long': 20}" +
                "}";

            datum = SupportAvroUtil.ParseQuoted(schema, jsonWValues);
            AssertValuesRequired(new AvroGenericDataEventBean(datum, eventType));

            var jsonWNull = "{'myInt': null, 'myCharSeq': null, 'myString':null," +
                            "'myBoolean': null, 'myBytes': null, " +
                            "'myDouble': null, 'myFloat': null, 'myLong': null}";

            AssertValuesNull(new AvroGenericDataEventBean(SupportAvroUtil.ParseQuoted(schema, jsonWNull), eventType));
        }
Ejemplo n.º 3
0
        public void TestEnumSymbol()
        {
            var schema = SchemaBuilder.Record(
                "typename", ...);

            var schema = SchemaBuilder.Record("typename")
                         .fields()
                         .name("aEnum")
                         .type()
                         .enumeration("myEnum")
                         .symbols("a", "b")
                         .enumDefault("x")
                         .endRecord();
            EventType eventType = SupportAvroUtil.MakeAvroSupportEventType(schema);

            AssertPropertyType(typeof(GenericEnum), null, eventType, "aEnum");

            Consumer <EventBean> asserter = eventBean => {
                GenericEnum v = (GenericEnum)eventBean.Get("aEnum");
                Assert.AreEqual("b", v.ToString());
            };

            var datum = new GenericRecord(schema);

            datum.Put("aEnum", new GenericEnum(schema.GetField("aEnum").Schema, "b"));
            asserter.Invoke(new AvroGenericDataEventBean(datum, eventType));

            var jsonWValues = "{'aEnum': 'b'}";

            datum = SupportAvroUtil.ParseQuoted(schema, jsonWValues);
            asserter.Invoke(new AvroGenericDataEventBean(datum, eventType));
        }
Ejemplo n.º 4
0
        public void TestFixed()
        {
            var schema = SchemaBuilder.Record(
                "typename",
                Field("aFixed", TypeBuilder.Fixed(
                          "abc",                      // name
                          2,                          // size
                          new ByteBuffer(new byte[0]) // default
                          )));

            EventType eventType = SupportAvroUtil.MakeAvroSupportEventType(schema);

            AssertPropertyType(typeof(GenericFixed), null, eventType, "aFixed");

            Consumer <EventBean> asserter = eventBean => {
                var @fixed = (GenericFixed)eventBean.Get("aFixed");
                Assert.IsTrue(Arrays.AreEqual(@fixed.Bytes, new byte[] { 1, 2 }));
            };

            var datum = new GenericRecord(schema);

            datum.Put("aFixed", new GenericFixed(
                          schema.GetField("aFixed").Schema.AsFixedSchema(), new byte[] { 1, 2 }));
            asserter.Invoke(new AvroGenericDataEventBean(datum, eventType));

            var jsonWValues = "{'aFixed': '\\u0001\\u0002\'}";

            datum = SupportAvroUtil.ParseQuoted(schema, jsonWValues);
            asserter.Invoke(new AvroGenericDataEventBean(datum, eventType));
        }
Ejemplo n.º 5
0
        public void TestMapOfString()
        {
            var schema = SchemaBuilder.Record(
                "typename",
                Field(
                    "anMap",
                    Map(
                        StringType(
                            Property(PROP_STRING_KEY, PROP_STRING_VALUE)),
                        Property(PROP_STRING_KEY, PROP_STRING_VALUE))));

            var eventType = SupportAvroUtil.MakeAvroSupportEventType(schema);

            AssertPropertyType(typeof(IDictionary <string, string>), typeof(string), eventType, "anMap");

            Consumer <EventBean> asserter = eventBean => {
                Assert.AreEqual("myValue", eventBean.Get("anMap('myKey')"));
                Assert.AreEqual("myValue", eventType.GetGetter("anMap('myKey')").Get(eventBean));
            };

            var datum = new GenericRecord(schema);

            datum.Put("anMap", Collections.SingletonDataMap("myKey", "myValue"));
            asserter.Invoke(new AvroGenericDataEventBean(datum, eventType));

            var jsonWValues = "{'anMap':{'myKey':'myValue'}}";

            datum = SupportAvroUtil.ParseQuoted(schema, jsonWValues);
            asserter.Invoke(new AvroGenericDataEventBean(datum, eventType));
        }
Ejemplo n.º 6
0
        public void TestArrayOfPrimitive()
        {
            var schema = SchemaBuilder.Record(
                "typename",
                Field("intArray", Array(IntType())));
            var eventType = SupportAvroUtil.MakeAvroSupportEventType(schema);

            AssertPropertyType(typeof(int[]), typeof(int), eventType, "intArray");

            Consumer <EventBean> asserter = eventBean => {
                Assert.AreEqual(1, eventBean.Get("intArray[0]"));
                Assert.AreEqual(2, eventBean.Get("intArray[1]"));
                Assert.AreEqual(1, eventType.GetGetter("intArray[0]").Get(eventBean));
                Assert.AreEqual(2, eventType.GetGetter("intArray[1]").Get(eventBean));
            };

            var datum = new GenericRecord(schema);

            datum.Put("intArray", Collections.List(1, 2));
            asserter.Invoke(new AvroGenericDataEventBean(datum, eventType));

            var jsonWValues = "{'intArray':[1,2]}";

            datum = SupportAvroUtil.ParseQuoted(schema, jsonWValues);
            asserter.Invoke(new AvroGenericDataEventBean(datum, eventType));
        }
Ejemplo n.º 7
0
        public void TestNestedSimple()
        {
            var schemaText = "{" +
                             "  'type' : 'record'," +
                             "  'name' : 'MyEvent'," +
                             "  'fields' : [ {" +
                             "    'name' : 'innerEvent'," +
                             "    'type' : {" +
                             "      'type' : 'record'," +
                             "      'name' : 'innerEventTypeName'," +
                             "      'fields' : [ {" +
                             "        'name' : 'innerValue'," +
                             "        'type' : {'type':'string','avro.string':'String'}" +
                             "      } ]" +
                             "    }" +
                             "  }]" +
                             "}";

            var       schema    = Schema.Parse(schemaText.Replace("'", "\"")).AsRecordSchema();
            EventType eventType = SupportAvroUtil.MakeAvroSupportEventType(schema);

            AssertPropertyType(typeof(GenericRecord), null, eventType, "innerEvent");

            var propNames = "innerEvent".SplitCsv();

            EPAssertionUtil.AssertEqualsExactOrder(eventType.PropertyNames, propNames);
            Assert.IsTrue(eventType.IsProperty("innerEvent"));

            var datumInner = new GenericRecord(schema.GetField("innerEvent").Schema.AsRecordSchema());

            datumInner.Put("innerValue", "i1");
            var datum = new GenericRecord(schema);

            datum.Put("innerEvent", datumInner);

            AssertValuesNested(datum, new AvroGenericDataEventBean(datum, eventType));

            var jsonWValues = "{'innerEvent': {'innerValue' : 'i1'}}";

            datum = SupportAvroUtil.ParseQuoted(schema, jsonWValues);
            AssertValuesNested(datum, new AvroGenericDataEventBean(datum, eventType));
        }
Ejemplo n.º 8
0
        public void TestRequiredType()
        {
            var schema = SchemaBuilder.Record(
                "typename",
                RequiredInt("myInt"),
                RequiredString("myCharSeq"),
                Field("myString", StringType(Property(PROP_STRING_KEY, PROP_STRING_VALUE))),
                RequiredBoolean("myBoolean"),
                RequiredBytes("myBytes"),
                RequiredDouble("myDouble"),
                RequiredFloat("myFloat"),
                RequiredLong("myLong"));

            var       propNames = "myInt,myCharSeq,myString,myBoolean,myBytes,myDouble,myFloat,myLong".SplitCsv();
            EventType eventType = SupportAvroUtil.MakeAvroSupportEventType(schema);

            EPAssertionUtil.AssertEqualsExactOrder(eventType.PropertyNames, propNames);
            Assert.AreEqual(typeof(GenericRecord), eventType.UnderlyingType);
            Assert.IsNull(eventType.SuperTypes);

            AssertPropertyType(typeof(int), null, eventType, "myInt");
            AssertPropertyType(typeof(string), typeof(char), eventType, "myString");
            AssertPropertyType(typeof(bool), null, eventType, "myBoolean");
            AssertPropertyType(typeof(byte[]), null, eventType, "myBytes");
            AssertPropertyType(typeof(double), null, eventType, "myDouble");
            AssertPropertyType(typeof(float), null, eventType, "myFloat");
            AssertPropertyType(typeof(long), null, eventType, "myLong");

            foreach (var propName in propNames)
            {
                Assert.IsTrue(eventType.IsProperty(propName));
            }

            var datum = GetRecordWithValues(schema);

            AssertValuesRequired(new AvroGenericDataEventBean(datum, eventType));

            var jsonWValues = "{'myInt': 10, 'myCharSeq': 'x', 'myString': 'y', 'myBoolean': true, 'myBytes': '\\u00AA\'," +
                              "'myDouble' : 50, 'myFloat':100, 'myLong':20}";

            AssertValuesRequired(new AvroGenericDataEventBean(SupportAvroUtil.ParseQuoted(schema, jsonWValues), eventType));
        }
Ejemplo n.º 9
0
        public void TestUnionResultingInObject()
        {
            var schema = SchemaBuilder.Record(
                "typename",
                Field(
                    "anUnion",
                    Union(
                        IntType(),
                        StringType(Property(PROP_STRING_KEY, PROP_STRING_VALUE)),
                        NullType())));

            EventType eventType = SupportAvroUtil.MakeAvroSupportEventType(schema);

            AssertPropertyType(typeof(object), null, eventType, "anUnion");

            Consumer <Object> asserterFromDatum = (value) => {
                var datum = new GenericRecord(schema);
                datum.Put("anUnion", value);
                Assert.AreEqual(value, new AvroGenericDataEventBean(datum, eventType).Get("anUnion"));
            };

            asserterFromDatum.Invoke("a");
            asserterFromDatum.Invoke(1);
            asserterFromDatum.Invoke(null);

            BiConsumer <String, Object> asserterFromJson = (
                json,
                value) => {
                var datum = SupportAvroUtil.ParseQuoted(schema, json);
                Assert.AreEqual(value, new AvroGenericDataEventBean(datum, eventType).Get("anUnion"));
            }
            ;

            asserterFromJson.Invoke("{'anUnion':{'int':1}}", 1);
            asserterFromJson.Invoke("{'anUnion':{'string':'abc'}}", "abc");
            asserterFromJson.Invoke("{'anUnion':null}", null);
        }