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"));
}
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());
}
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);
}
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;
}
}
}
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());
}
Aggregations