Search in sources :

Example 11 with AvscParseResult

use of com.linkedin.avroutil1.parser.avsc.AvscParseResult in project avro-util by linkedin.

the class AvscParserTest method testParsingProperties.

@Test
public void testParsingProperties() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecordWithProperties.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
    Assert.assertNotNull(schema);
    Assert.assertEquals(schema.propertyNames(), // order is important
    Arrays.asList("extraNullProp", "extraBooleanProp", "extraIntProp", "extraFloatProp", "extraStringProp", "extraArrayProp", "extraObjectProp"));
    Assert.assertNull(schema.getPropertyAsJsonLiteral("noSuchProp"));
    Assert.assertNull(schema.getPropertyAsObject("noSuchProp"));
    Assert.assertEquals(schema.getPropertyAsJsonLiteral("extraNullProp"), "null");
    Assert.assertEquals(schema.getPropertyAsObject("extraNullProp"), JsonPropertiesContainer.NULL_VALUE);
    Assert.assertEquals(schema.getPropertyAsJsonLiteral("extraBooleanProp"), "true");
    Assert.assertEquals(schema.getPropertyAsObject("extraBooleanProp"), Boolean.TRUE);
    Assert.assertEquals(schema.getPropertyAsJsonLiteral("extraIntProp"), "42");
    Assert.assertEquals(schema.getPropertyAsObject("extraIntProp"), new BigDecimal(42));
    Assert.assertEquals(schema.getPropertyAsJsonLiteral("extraFloatProp"), "4.2");
    // new BigDecimal(4.2d) is actually 4.20000000000000017763568394002504646778106689453125
    // we love floating point precision
    Assert.assertEquals(schema.getPropertyAsObject("extraFloatProp"), new BigDecimal("4.2"));
    Assert.assertEquals(schema.getPropertyAsJsonLiteral("extraStringProp"), "\"a string\"");
    Assert.assertEquals(schema.getPropertyAsObject("extraStringProp"), "a string");
    JSONAssert.assertEquals("[null, 0, false, \"wow\", {\"this\" : \"makes\", \"little\" : \"sense\"}]", schema.getPropertyAsJsonLiteral("extraArrayProp"), JSONCompareMode.STRICT);
    Assert.assertEquals(schema.getPropertyAsObject("extraArrayProp"), Arrays.asList(JsonPropertiesContainer.NULL_VALUE, new BigDecimal(0), Boolean.FALSE, "wow", new LinkedHashMap<String, Object>() {

        {
            put("this", "makes");
            put("little", "sense");
        }
    }));
    JSONAssert.assertEquals("{\"thats\": [\"all\", \"folks\"]}", schema.getPropertyAsJsonLiteral("extraObjectProp"), JSONCompareMode.STRICT);
    Assert.assertEquals(schema.getPropertyAsObject("extraObjectProp"), new LinkedHashMap<String, Object>() {

        {
            put("thats", Arrays.asList("all", "folks"));
        }
    });
    AvroSchemaField stringField = schema.getField("stringField");
    Assert.assertEquals(stringField.propertyNames(), Collections.singletonList("fieldStringProp"));
    Assert.assertEquals(stringField.getPropertyAsJsonLiteral("fieldStringProp"), "\"fieldStringValue\"");
    Assert.assertEquals(stringField.getPropertyAsObject("fieldStringProp"), "fieldStringValue");
    AvroSchema stringSchema = stringField.getSchema();
    Assert.assertEquals(stringSchema.propertyNames(), Arrays.asList("avro.java.string", "typeStringProp"));
    AvroSchema uuidSchema = schema.getField("uuidField").getSchema();
    Assert.assertEquals(uuidSchema.propertyNames(), Collections.singletonList("logicalType"));
}
Also used : AvroSchema(com.linkedin.avroutil1.model.AvroSchema) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) BigDecimal(java.math.BigDecimal) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Example 12 with AvscParseResult

use of com.linkedin.avroutil1.parser.avsc.AvscParseResult in project avro-util by linkedin.

the class AvscParserTest method testResolveReferencesWithNonNullNamespace.

@Test
public void testResolveReferencesWithNonNullNamespace() throws IOException {
    String referencingAvsc = TestUtil.load("schemas/TestRecordWithInternalNonNullNamespaceReference.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result1 = parser.parse(referencingAvsc);
    AvroRecordSchema schema = (AvroRecordSchema) result1.getTopLevelSchema();
    Assert.assertNotNull(schema.getField("testField").getSchema());
}
Also used : AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) Test(org.testng.annotations.Test)

Example 13 with AvscParseResult

use of com.linkedin.avroutil1.parser.avsc.AvscParseResult in project avro-util by linkedin.

the class AvscParserTest method testSelfReference.

@Test
public void testSelfReference() throws Exception {
    String avsc = TestUtil.load("schemas/LongList.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
    // schema.next[1] == schema
    AvroSchemaField nextField = schema.getField("next");
    AvroUnionSchema union = (AvroUnionSchema) nextField.getSchema();
    SchemaOrRef secondBranch = union.getTypes().get(1);
    Assert.assertSame(secondBranch.getSchema(), schema);
}
Also used : SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroUnionSchema(com.linkedin.avroutil1.model.AvroUnionSchema) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Example 14 with AvscParseResult

use of com.linkedin.avroutil1.parser.avsc.AvscParseResult in project avro-util by linkedin.

the class AvscParserTest method testParsingStringTypes.

@Test
public void testParsingStringTypes() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecordWithStringTypes.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
    Assert.assertNotNull(schema);
    for (AvroSchemaField field : schema.getFields()) {
        Assert.assertEquals(field.getSchema().type(), AvroType.STRING);
        AvroPrimitiveSchema strSchema = (AvroPrimitiveSchema) field.getSchema();
        switch(field.getName()) {
            case "vanillaStringField":
                Assert.assertNull(strSchema.getJavaStringRepresentation());
                break;
            case "stringFieldWithStringJavaType":
                Assert.assertEquals(strSchema.getJavaStringRepresentation(), AvroJavaStringRepresentation.STRING);
                break;
            case "stringFieldWithMisplacedCharSequenceJavaType":
                Assert.assertNull(strSchema.getJavaStringRepresentation());
                // TODO - look for a warning about the misplaced value
                break;
            case "stringFieldWithCharSequenceJavaType":
                Assert.assertEquals(strSchema.getJavaStringRepresentation(), AvroJavaStringRepresentation.CHAR_SEQUENCE);
                break;
            case "stringFieldWithUtf8JavaType":
                Assert.assertEquals(strSchema.getJavaStringRepresentation(), AvroJavaStringRepresentation.UTF8);
                break;
        }
    }
}
Also used : AvroPrimitiveSchema(com.linkedin.avroutil1.model.AvroPrimitiveSchema) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Example 15 with AvscParseResult

use of com.linkedin.avroutil1.parser.avsc.AvscParseResult in project avro-util by linkedin.

the class AvscParserTest method testParsingExternalReferencesWithNoNamespace.

@Test
public void testParsingExternalReferencesWithNoNamespace() throws Exception {
    String referencingAvsc = TestUtil.load("schemas/TestRecordWithExternalReferenceAndInferredNamespace.avsc");
    String referencedAvsc = TestUtil.load("schemas/TestRecord.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result1 = parser.parse(referencingAvsc);
    AvroRecordSchema outerSchema = (AvroRecordSchema) result1.getTopLevelSchema();
    try {
        outerSchema.getField("testRecordField").getSchema();
        Assert.fail("accessing unresolved ref expected to fail");
    } catch (UnresolvedReferenceException expected) {
        Assert.assertTrue(expected.getMessage().contains("unresolved"));
        Assert.assertTrue(expected.getMessage().contains("TestRecord"));
    }
    AvscParseResult result2 = parser.parse(referencedAvsc);
    AvroParseContext overall = new AvroParseContext();
    overall.add(result1);
    overall.add(result2);
    overall.resolveReferences();
    // now reference is resolved just fine
    Assert.assertNotNull(outerSchema.getField("testRecordField").getSchema());
}
Also used : UnresolvedReferenceException(com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)18 AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)14 AvroSchemaField (com.linkedin.avroutil1.model.AvroSchemaField)9 AvroSchema (com.linkedin.avroutil1.model.AvroSchema)6 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)6 AvscParseResult (com.linkedin.avroutil1.parser.avsc.AvscParseResult)6 AvscParser (com.linkedin.avroutil1.parser.avsc.AvscParser)6 AvroEnumSchema (com.linkedin.avroutil1.model.AvroEnumSchema)5 AvroFixedSchema (com.linkedin.avroutil1.model.AvroFixedSchema)4 UnresolvedReferenceException (com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException)4 JavaFileObject (javax.tools.JavaFileObject)4 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)3 AvroMapSchema (com.linkedin.avroutil1.model.AvroMapSchema)3 AvroPrimitiveSchema (com.linkedin.avroutil1.model.AvroPrimitiveSchema)3 AvroUnionSchema (com.linkedin.avroutil1.model.AvroUnionSchema)3 AvroSyntaxException (com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)3 JsonParseException (com.linkedin.avroutil1.parser.exceptions.JsonParseException)3 BigDecimal (java.math.BigDecimal)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3