|
| 1 | +package io.cdap.plugin.gcp.bigquery.sink; |
| 2 | + |
| 3 | +import io.cdap.cdap.api.data.format.StructuredRecord; |
| 4 | +import io.cdap.cdap.api.data.schema.Schema; |
| 5 | +import io.cdap.plugin.format.avro.StructuredToAvroTransformer; |
| 6 | +import org.apache.avro.file.CodecFactory; |
| 7 | +import org.apache.avro.file.DataFileReader; |
| 8 | +import org.apache.avro.file.SeekableByteArrayInput; |
| 9 | +import org.apache.avro.generic.GenericData; |
| 10 | +import org.apache.avro.generic.GenericDatumReader; |
| 11 | +import org.apache.avro.generic.GenericRecord; |
| 12 | +import org.apache.avro.mapred.AvroKey; |
| 13 | +import org.apache.hadoop.io.NullWritable; |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.Test; |
| 16 | +import org.mockito.Mockito; |
| 17 | + |
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.OutputStream; |
| 21 | +import java.time.LocalDate; |
| 22 | +import java.time.LocalTime; |
| 23 | +import java.util.function.Supplier; |
| 24 | + |
| 25 | + |
| 26 | +public class AvroRecordWriterTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testSuccessfulWrites() throws IOException { |
| 30 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 31 | + Supplier<OutputStream> outputStreamSupplier = () -> byteArrayOutputStream; |
| 32 | + |
| 33 | + AvroRecordWriter avroRecordWriter = new AvroRecordWriter( |
| 34 | + null, |
| 35 | + GenericData.get(), |
| 36 | + CodecFactory.nullCodec(), |
| 37 | + outputStreamSupplier, |
| 38 | + 33); |
| 39 | + |
| 40 | + //Create a Generic record |
| 41 | + Schema schema = Schema.recordOf("record", |
| 42 | + Schema.Field.of("id", Schema.of(Schema.Type.LONG)), |
| 43 | + Schema.Field.of("name", Schema.of(Schema.Type.STRING)), |
| 44 | + Schema.Field.of("price", Schema.of(Schema.Type.DOUBLE)), |
| 45 | + Schema.Field.of("dt", Schema.nullableOf(Schema.of(Schema.LogicalType.DATE))), |
| 46 | + Schema.Field.of("time", Schema.of(Schema.LogicalType.TIME_MICROS)), |
| 47 | + Schema.Field.of("timestamp", |
| 48 | + Schema.nullableOf(Schema.of(Schema.LogicalType.TIMESTAMP_MICROS)))); |
| 49 | + |
| 50 | + StructuredRecord sourceRecord = StructuredRecord.builder(schema) |
| 51 | + .set("id", 1L) |
| 52 | + .set("name", "alice") |
| 53 | + .set("price", 1.2d) |
| 54 | + .setDate("dt", LocalDate.of(2018, 11, 11)) |
| 55 | + .setTime("time", LocalTime.of(11, 11, 11)) |
| 56 | + .set("timestamp", 1464181635000000L).build(); |
| 57 | + GenericRecord sourceGenericRecord = new StructuredToAvroTransformer(schema).transform(sourceRecord); |
| 58 | + AvroKey<GenericRecord> record = new AvroKey(sourceGenericRecord); |
| 59 | + |
| 60 | + //Write using avroRecordWriter |
| 61 | + avroRecordWriter.write(record, Mockito.mock(NullWritable.class)); |
| 62 | + avroRecordWriter.close(null); |
| 63 | + |
| 64 | + //Now read the avro record from the Output stream |
| 65 | + GenericDatumReader genericDatumReader = new GenericDatumReader<GenericRecord>(sourceGenericRecord.getSchema()); |
| 66 | + SeekableByteArrayInput inputStream = new SeekableByteArrayInput(byteArrayOutputStream.toByteArray()); |
| 67 | + DataFileReader dataFileReader = new DataFileReader<GenericRecord>(inputStream, genericDatumReader); |
| 68 | + |
| 69 | + //Check if it matches |
| 70 | + Assert.assertTrue(dataFileReader.iterator().hasNext()); |
| 71 | + //Record read must match the sourceGenericRecord |
| 72 | + Assert.assertEquals(dataFileReader.iterator().next(), sourceGenericRecord); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + /** |
| 77 | + * If there are no records, then the output stream supplier should not be called |
| 78 | + * If called then will throw exception |
| 79 | + * |
| 80 | + * @throws IOException |
| 81 | + */ |
| 82 | + @Test |
| 83 | + public void testZeroWrites() throws IOException { |
| 84 | + Supplier<OutputStream> outputStreamSupplier = () -> { |
| 85 | + throw new IllegalStateException("This supplier should not be called"); |
| 86 | + }; |
| 87 | + |
| 88 | + AvroRecordWriter avroRecordWriter = new AvroRecordWriter( |
| 89 | + null, |
| 90 | + GenericData.get(), |
| 91 | + CodecFactory.nullCodec(), |
| 92 | + outputStreamSupplier, |
| 93 | + 33); |
| 94 | + |
| 95 | + avroRecordWriter.close(null); |
| 96 | + } |
| 97 | +} |
0 commit comments