use of com.linkedin.avroutil1.parser.avsc.AvscParser 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());
}
use of com.linkedin.avroutil1.parser.avsc.AvscParser 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");
}
}
use of com.linkedin.avroutil1.parser.avsc.AvscParser 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());
}
}
use of com.linkedin.avroutil1.parser.avsc.AvscParser 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")));
}
use of com.linkedin.avroutil1.parser.avsc.AvscParser 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;
}
}
}
Aggregations