use of org.apache.samza.sql.avro.schemas.ComplexUnion in project samza by apache.
the class TestAvroRelConversion method testComplexUnionConversionShouldWorkWithBothStringAndIntTypes.
@Test
public void testComplexUnionConversionShouldWorkWithBothStringAndIntTypes() throws Exception {
// ComplexUnion is a nested avro non-nullable union-type with both String and Integer type
// Test the complex-union conversion for String type.
GenericData.Record record = new GenericData.Record(ComplexUnion.SCHEMA$);
record.put("non_nullable_union_value", testStrValue);
ComplexUnion complexUnion = new ComplexUnion();
complexUnion.non_nullable_union_value = testStrValue;
byte[] serializedData = bytesFromGenericRecord(record);
GenericRecord genericRecord = genericRecordFromBytes(serializedData, ComplexUnion.SCHEMA$);
SamzaSqlRelMessage message = complexUnionAvroRelConverter.convertToRelMessage(new KV<>("key", genericRecord));
Assert.assertEquals(testStrValue, message.getSamzaSqlRelRecord().getField("non_nullable_union_value").get().toString());
serializedData = encodeAvroSpecificRecord(ComplexUnion.class, complexUnion);
genericRecord = genericRecordFromBytes(serializedData, ComplexUnion.SCHEMA$);
Assert.assertEquals(testStrValue, genericRecord.get("non_nullable_union_value").toString());
// Testing the complex-union conversion for Integer type
record.put("non_nullable_union_value", Integer.valueOf(123));
complexUnion.non_nullable_union_value = Integer.valueOf(123);
serializedData = bytesFromGenericRecord(record);
genericRecord = genericRecordFromBytes(serializedData, ComplexUnion.SCHEMA$);
message = complexUnionAvroRelConverter.convertToRelMessage(new KV<>("key", genericRecord));
Assert.assertEquals(Integer.valueOf(123), message.getSamzaSqlRelRecord().getField("non_nullable_union_value").get());
serializedData = encodeAvroSpecificRecord(ComplexUnion.class, complexUnion);
genericRecord = genericRecordFromBytes(serializedData, ComplexUnion.SCHEMA$);
Assert.assertEquals(Integer.valueOf(123), genericRecord.get("non_nullable_union_value"));
}
Aggregations