Search in sources :

Example 1 with UnresolvedReferenceException

use of com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException 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 2 with UnresolvedReferenceException

use of com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException 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

AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)2 UnresolvedReferenceException (com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException)2 Test (org.testng.annotations.Test)2 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)1