package org.jfleet.avro;

import org.jfleet.EntityFieldType;
import org.jfleet.EntityInfo;
import org.jfleet.EntityInfoBuilder;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class AvroWriterTest {
    @Test
    void shouldConvertEntityInfoWithStringTypesToAvro() throws IOException {
        EntityInfo entityInfo = new EntityInfoBuilder<>(TestEntity.class)
                .addColumn("foo", EntityFieldType.FieldTypeEnum.STRING, TestEntity::getFoo)
                .build();

        AvroConfiguration avroConfiguration = new AvroConfiguration(entityInfo);
        AvroWriter<TestEntity> avroWriter = new AvroWriter<>(avroConfiguration);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        TestEntity testEntity = new TestEntity("foo", null, null);
        avroWriter.writeAll(outputStream, Arrays.asList(testEntity));

        assertNotNull(avroWriter);
        assertTrue(outputStream.size() > 0);
    }


}

@@ -19,16 +19,299 @@
 .avro;%0A%0A
+import org.apache.avro.Schema;%0Aimport org.apache.avro.SchemaBuilder;%0Aimport org.apache.avro.file.DataFileReader;%0Aimport org.apache.avro.generic.GenericDatumReader;%0Aimport org.apache.avro.generic.GenericRecord;%0Aimport org.apache.avro.io.DatumReader;%0Aimport org.apache.avro.util.Utf8;%0A
 import o
@@ -493,27 +493,20 @@
 java.io.
-IOException
+File
 ;%0Aimport
@@ -515,83 +515,82 @@
 ava.
-util.Arrays;%0A%0Aimport static org.junit.jupiter.api.Assertions.assertNotNull;
+io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.util.Arrays;%0A
 %0Aimp
@@ -633,26 +633,17 @@
 ertions.
-assertTrue
+*
 ;%0A%0Aclass
@@ -1397,15 +1397,704 @@
  0);
+%0A%0A        try (FileOutputStream fos = new FileOutputStream(%22/tmp/foo.avro%22)) %7B%0A            fos.write(outputStream.toByteArray());%0A        %7D%0A%0A        Schema schema = SchemaBuilder.record(%22TestEntity%22).namespace(%22org.jfleet.avro%22)%0A                .fields().requiredString(%22foo%22).endRecord();%0A        DatumReader%3CGenericRecord%3E datumReader = new GenericDatumReader%3C%3E(schema);%0A%0A        try (DataFileReader%3CGenericRecord%3E dataFileReader = new DataFileReader%3C%3E(new File(%22/tmp/foo.avro%22), datumReader)) %7B%0A            assertTrue(dataFileReader.hasNext());%0A            GenericRecord genericRecord = dataFileReader.next();%0A            assertEquals(new Utf8(%22foo%22), genericRecord.get(%22foo%22));%0A        %7D
 %0A    %7D%0A
-%0A%0A
 %7D%0A

Check avro file with string type