Search in sources :

Example 16 with AvscParseResult

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

the class AvscParserTest method testResolveReferencesWithNullNamespace.

@Test
public void testResolveReferencesWithNullNamespace() throws IOException {
    String referencingAvsc = TestUtil.load("schemas/TestRecordWithInternalNullNamespaceReference.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 17 with AvscParseResult

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

the class SpecificRecordClassGeneratorTest method testHugeEnum.

@Test
public void testHugeEnum() throws Exception {
    String avsc = TestUtil.load("schemas/SimpleEnumWithHugeDoc.avsc");
    SpecificRecordClassGenerator generator = new SpecificRecordClassGenerator();
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroEnumSchema enumSchema = (AvroEnumSchema) result.getTopLevelSchema();
    Assert.assertNotNull(enumSchema);
    JavaFileObject javaSourceFile = generator.generateSpecificRecordClass(enumSchema, SpecificRecordGenerationConfig.BROAD_COMPATIBILITY);
    CompilerHelper.assertCompiles(javaSourceFile);
}
Also used : AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) JavaFileObject(javax.tools.JavaFileObject) AvscParser(com.linkedin.avroutil1.parser.avsc.AvscParser) AvscParseResult(com.linkedin.avroutil1.parser.avsc.AvscParseResult) Test(org.testng.annotations.Test)

Example 18 with AvscParseResult

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

the class AvscParserTest method testParsingHorribleLogicalTypes.

@Test
public void testParsingHorribleLogicalTypes() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecordWithHorribleLogicalTypes.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.assertNull(field.getSchema().logicalType(), "field " + field.getName() + " should not have a successfully-parsed logicalType");
    }
}
Also used : AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Example 19 with AvscParseResult

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

the class AvscParserTest method testMisleadingNamespace.

@Test
public void testMisleadingNamespace() throws Exception {
    String avsc = TestUtil.load("schemas/TestMisleadingNamespaceRecord.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
    Assert.assertEquals(schema.getFullName(), "com.acme.TestMisleadingNamespaceRecord");
    AvroRecordSchema inner1 = (AvroRecordSchema) schema.getField("f1").getSchema();
    AvroRecordSchema inner2 = (AvroRecordSchema) schema.getField("f2").getSchema();
    Assert.assertEquals(inner1.getFullName(), "com.acme.SimpleName");
    Assert.assertEquals(inner2.getFullName(), "not.so.SimpleName");
    Assert.assertEquals(result.getIssues().size(), 4);
}
Also used : AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) Test(org.testng.annotations.Test)

Example 20 with AvscParseResult

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

the class AvscSchemaWriterTest method testParsingCycle.

/**
 * given an avsc, parses and re-prints it using our code
 * and compares the result to vanilla avro.
 * @param avsc
 */
private void testParsingCycle(String avsc) {
    Schema reference = Schema.parse(avsc);
    AvscParser parser = new AvscParser();
    AvscParseResult parseResults = parser.parse(avsc);
    List<AvscIssue> parseIssues = parseResults.getIssues();
    Assert.assertTrue(parseIssues == null || parseIssues.isEmpty(), "parse issues: " + parseIssues);
    AvroSchema parsed = parseResults.getTopLevelSchema();
    Assert.assertNotNull(parsed);
    AvscSchemaWriter writer = new AvscSchemaWriter();
    AvscFile file = writer.writeSingle(parsed);
    Assert.assertNotNull(file);
    if (HelperConsts.NAMED_TYPES.contains(reference.getType())) {
        // for named schemas the file path is determined by schema name
        Assert.assertNotNull(file.getPathFromRoot());
        String expectedFileName = reference.getFullName().replaceAll("\\.", Matcher.quoteReplacement(File.separator)) + ".avsc";
        Assert.assertEquals(file.getPathFromRoot().toString(), expectedFileName);
    } else {
        // cant auto-name files containing other schema types
        Assert.assertNull(file.getPathFromRoot());
    }
    String avsc2 = file.getContents();
    Schema afterCycle = Schema.parse(avsc2);
    Assert.assertEquals(reference, afterCycle);
}
Also used : AvroSchema(com.linkedin.avroutil1.model.AvroSchema) AvscParser(com.linkedin.avroutil1.parser.avsc.AvscParser) AvscIssue(com.linkedin.avroutil1.parser.avsc.AvscIssue) Schema(org.apache.avro.Schema) AvroSchema(com.linkedin.avroutil1.model.AvroSchema) AvscParseResult(com.linkedin.avroutil1.parser.avsc.AvscParseResult)

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