@Test public void testChoiceOfRecordTypes() throws IOException, MalformedRecordException { final Schema avroSchema = new Schema.Parser().parse(new File("src/test/resources/json/record-choice.avsc")); final RecordSchema recordSchema = AvroTypeUtil.createSchema(avroSchema); try (final InputStream in = new FileInputStream(new File("src/test/resources/json/elements-for-record-choice.json")); final JsonTreeRowRecordReader reader = new JsonTreeRowRecordReader(in, Mockito.mock(ComponentLog.class), recordSchema, dateFormat, timeFormat, timestampFormat)) { final Record firstRecord = reader.nextRecord(); assertNotNull(firstRecord); final RecordSchema firstOuterSchema = firstRecord.getSchema(); assertEquals(Arrays.asList("id", "child"), firstOuterSchema.getFieldNames()); assertEquals("1234", firstRecord.getValue("id")); assertTrue(firstOuterSchema.getDataType("child").get().getFieldType() == RecordFieldType.CHOICE); final List<DataType> firstSubTypes = ((ChoiceDataType) firstOuterSchema.getDataType("child").get()).getPossibleSubTypes(); assertEquals(2, firstSubTypes.size()); assertEquals(2L, firstSubTypes.stream().filter(type -> type.getFieldType() == RecordFieldType.RECORD).count()); final Object childObject = firstRecord.getValue("child"); assertTrue(childObject instanceof Record); final Record firstChildRecord = (Record) childObject; final RecordSchema firstChildSchema = firstChildRecord.getSchema(); assertEquals(Arrays.asList("id"), firstChildSchema.getFieldNames()); final Record secondRecord = reader.nextRecord(); assertNotNull(secondRecord); final RecordSchema secondOuterSchema = secondRecord.getSchema(); assertEquals(Arrays.asList("id", "child"), secondOuterSchema.getFieldNames()); assertEquals("1234", secondRecord.getValue("id")); assertTrue(secondOuterSchema.getDataType("child").get().getFieldType() == RecordFieldType.CHOICE); final List<DataType> secondSubTypes = ((ChoiceDataType) secondOuterSchema.getDataType("child").get()).getPossibleSubTypes(); assertEquals(2, secondSubTypes.size()); assertEquals(2L, secondSubTypes.stream().filter(type -> type.getFieldType() == RecordFieldType.RECORD).count()); final Object secondChildObject = secondRecord.getValue("child"); assertTrue(secondChildObject instanceof Record); final Record secondChildRecord = (Record) secondChildObject; final RecordSchema secondChildSchema = secondChildRecord.getSchema(); assertEquals(Arrays.asList("name"), secondChildSchema.getFieldNames()); assertNull(reader.nextRecord()); } }
@Override public RecordSchema getSchema() { return schema; }
JsonTreeRowRecordReader extends AbstractJsonRowRecordReader { @Override public RecordSchema getSchema() { return schema; } }
JsonTreeRowRecordReader extends AbstractJsonRowRecordReader { @Override public RecordSchema getSchema() { return schema; } JsonTreeRowRecordReader(final InputStream in, final ComponentLog logger, final RecordSchema schema,
        final String dateFormat, final String timeFormat, final String timestampFormat); }
JsonTreeRowRecordReader extends AbstractJsonRowRecordReader { @Override public RecordSchema getSchema() { return schema; } JsonTreeRowRecordReader(final InputStream in, final ComponentLog logger, final RecordSchema schema,
        final String dateFormat, final String timeFormat, final String timestampFormat); @Override RecordSchema getSchema(); }
JsonTreeRowRecordReader extends AbstractJsonRowRecordReader { @Override public RecordSchema getSchema() { return schema; } JsonTreeRowRecordReader(final InputStream in, final ComponentLog logger, final RecordSchema schema,
        final String dateFormat, final String timeFormat, final String timestampFormat); @Override RecordSchema getSchema();  }