Search in sources :

Example 6 with AvscParseResult

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

the class AvscParserTest method testParsingExternalReferences.

@Test
public void testParsingExternalReferences() throws Exception {
    String referencingAvsc = TestUtil.load("schemas/TestRecordWithExternalReference.avsc");
    String referencedAvsc = TestUtil.load("schemas/TestRecord.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result1 = parser.parse(referencingAvsc);
    Assert.assertEquals(result1.getExternalReferences().stream().map(SchemaOrRef::getRef).collect(Collectors.toList()), Collections.singletonList("com.acme.TestRecord"));
    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("com.acme.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 : SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) UnresolvedReferenceException(com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) Test(org.testng.annotations.Test)

Example 7 with AvscParseResult

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

the class AvscParserTest method testParsingDefaultValues.

@Test
public void testParsingDefaultValues() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecordWithDefaultValues.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
    for (AvroSchemaField field : schema.getFields()) {
        Assert.assertNotNull(field.getDefaultValue(), "field " + field.getName() + " has a null default");
    }
}
Also used : AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Example 8 with AvscParseResult

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

the class AvscParserTest method testParsingHorribleDefaultValues.

@Test
public void testParsingHorribleDefaultValues() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecordWithHorribleDefaultValues.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()) {
        List<AvscIssue> issuesWithField = result.getIssues(field);
        Assert.assertFalse(issuesWithField.isEmpty(), "field " + field.getName() + " has no issues?!");
        Assert.assertFalse(issuesWithField.stream().noneMatch(issue -> issue.getMessage().contains("default value")));
        Assert.assertNull(field.getDefaultValue());
    }
}
Also used : Arrays(java.util.Arrays) TestUtil(com.linkedin.avroutil1.testcommon.TestUtil) UnresolvedReferenceException(com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException) Test(org.testng.annotations.Test) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) GenericData(org.apache.avro.generic.GenericData) AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) LinkedHashMap(java.util.LinkedHashMap) BigDecimal(java.math.BigDecimal) AvroFixedSchema(com.linkedin.avroutil1.model.AvroFixedSchema) Assert(org.testng.Assert) JsonPropertiesContainer(com.linkedin.avroutil1.model.JsonPropertiesContainer) AvroLogicalType(com.linkedin.avroutil1.model.AvroLogicalType) AvroUnionSchema(com.linkedin.avroutil1.model.AvroUnionSchema) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) Schema(org.apache.avro.Schema) AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) AvroPrimitiveSchema(com.linkedin.avroutil1.model.AvroPrimitiveSchema) IOException(java.io.IOException) AvroSchema(com.linkedin.avroutil1.model.AvroSchema) Collectors(java.util.stream.Collectors) AvroJavaStringRepresentation(com.linkedin.avroutil1.model.AvroJavaStringRepresentation) List(java.util.List) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) JSONCompareMode(org.skyscreamer.jsonassert.JSONCompareMode) AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException) AvroType(com.linkedin.avroutil1.model.AvroType) Collections(java.util.Collections) JsonParseException(com.linkedin.avroutil1.parser.exceptions.JsonParseException) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Example 9 with AvscParseResult

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

the class AvscParserTest method testParseBadEnumDefault.

@Test
public void testParseBadEnumDefault() throws Exception {
    String avsc = TestUtil.load("schemas/TestBadEnumDefault.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    AvroRecordSchema recordSchema = (AvroRecordSchema) result.getTopLevelSchema();
    List<AvscIssue> issues = result.getIssues(recordSchema);
    Assert.assertFalse(issues.stream().noneMatch(issue -> issue.getMessage().contains("default value")));
    AvroEnumSchema enumSchema = (AvroEnumSchema) recordSchema.getField("enumField").getSchema();
    issues = result.getIssues(enumSchema);
    Assert.assertFalse(issues.stream().noneMatch(issue -> issue.getMessage().contains("default value")));
}
Also used : Arrays(java.util.Arrays) TestUtil(com.linkedin.avroutil1.testcommon.TestUtil) UnresolvedReferenceException(com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException) Test(org.testng.annotations.Test) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) GenericData(org.apache.avro.generic.GenericData) AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) LinkedHashMap(java.util.LinkedHashMap) BigDecimal(java.math.BigDecimal) AvroFixedSchema(com.linkedin.avroutil1.model.AvroFixedSchema) Assert(org.testng.Assert) JsonPropertiesContainer(com.linkedin.avroutil1.model.JsonPropertiesContainer) AvroLogicalType(com.linkedin.avroutil1.model.AvroLogicalType) AvroUnionSchema(com.linkedin.avroutil1.model.AvroUnionSchema) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) Schema(org.apache.avro.Schema) AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) AvroPrimitiveSchema(com.linkedin.avroutil1.model.AvroPrimitiveSchema) IOException(java.io.IOException) AvroSchema(com.linkedin.avroutil1.model.AvroSchema) Collectors(java.util.stream.Collectors) AvroJavaStringRepresentation(com.linkedin.avroutil1.model.AvroJavaStringRepresentation) List(java.util.List) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) JSONCompareMode(org.skyscreamer.jsonassert.JSONCompareMode) AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException) AvroType(com.linkedin.avroutil1.model.AvroType) Collections(java.util.Collections) JsonParseException(com.linkedin.avroutil1.parser.exceptions.JsonParseException) AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) Test(org.testng.annotations.Test)

Example 10 with AvscParseResult

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

the class AvscParserTest method testParsingLogicalTypes.

@Test
public void testParsingLogicalTypes() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecordWithLogicalTypes.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()) {
        switch(field.getName()) {
            case "bytesDecimalField":
                Assert.assertEquals(field.getSchema().type(), AvroType.BYTES);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DECIMAL);
                Assert.assertEquals(((AvroPrimitiveSchema) field.getSchema()).getScale(), 2);
                Assert.assertEquals(((AvroPrimitiveSchema) field.getSchema()).getPrecision(), 4);
                break;
            case "fixedDecimalField":
                Assert.assertEquals(field.getSchema().type(), AvroType.FIXED);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DECIMAL);
                break;
            case "fixedDurationField":
                Assert.assertEquals(field.getSchema().type(), AvroType.FIXED);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DURATION);
                break;
            case "stringUUIDField":
                Assert.assertEquals(field.getSchema().type(), AvroType.STRING);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.UUID);
                break;
            case "intDateField":
                Assert.assertEquals(field.getSchema().type(), AvroType.INT);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.DATE);
                break;
            case "intTimeMillisField":
                Assert.assertEquals(field.getSchema().type(), AvroType.INT);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIME_MILLIS);
                break;
            case "longTimeMicrosField":
                Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIME_MICROS);
                break;
            case "longTimestampMillisField":
                Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIMESTAMP_MILLIS);
                break;
            case "longTimestampMicrosField":
                Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.TIMESTAMP_MICROS);
                break;
            case "longLocalTimestampMillisField":
                Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.LOCAL_TIMESTAMP_MILLIS);
                break;
            case "longLocalTimestampMicrosField":
                Assert.assertEquals(field.getSchema().type(), AvroType.LONG);
                Assert.assertEquals(field.getSchema().logicalType(), AvroLogicalType.LOCAL_TIMESTAMP_MICROS);
                break;
            default:
                break;
        }
    }
}
Also used : AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) 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