// Path: src/java/com/google/cloud/bigquery/dwhassessment/extractiontool/db/AvroHelper.java
// public static void dumpResults(
//     ImmutableList<GenericRecord> records, OutputStream outputStream, Schema schema)
//     throws IOException {
//   GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
//   DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(writer);
//   dataFileWriter.create(schema, outputStream);
// 
//   records.stream()
//       .forEach(
//           record -> {
//             try {
//               dataFileWriter.append(record);
//             } catch (IOException e) {
//               throw new IllegalStateException(
//                   String.format(
//                       "Failed to encode query result %s to file with error message: %s",
//                       record.toString(), e.getMessage()));
//             }
//           });
//   dataFileWriter.close();
//   outputStream.close();
// }
// 
// Path: src/java/com/google/cloud/bigquery/dwhassessment/extractiontool/db/AvroHelper.java
// public static Schema getAvroSchema(
//     String schemaName, String namespace, ResultSetMetaData metaData) throws SQLException {
//   Preconditions.checkArgument(!schemaName.isEmpty(), "Schema name cannot be empty.");
//   Preconditions.checkArgument(!namespace.isEmpty(), "Namespace cannot be empty.");
//   SchemaBuilder.FieldAssembler<Schema> schemaAssembler =
//       SchemaBuilder.record(schemaName).namespace(namespace).fields();
// 
//   for (int columnIndex = 1; columnIndex <= metaData.getColumnCount(); columnIndex++) {
//     SchemaBuilder.FieldBuilder<Schema> fieldBuilder =
//         schemaAssembler.name(metaData.getColumnName(columnIndex));
//     AvroHelper.convertColumnTypeToAvroType(fieldBuilder, metaData, columnIndex);
//   }
//   return schemaAssembler.endRecord();
// }
// 
// Path: src/java/com/google/cloud/bigquery/dwhassessment/extractiontool/db/AvroHelper.java
// public static GenericRecord parseRowToAvro(ResultSet row, Schema schema) throws SQLException {
//   GenericRecordBuilder recordBuilder = new GenericRecordBuilder(schema);
//   ResultSetMetaData metaData = row.getMetaData();
//   for (int columnIndex = 1; columnIndex <= row.getMetaData().getColumnCount(); columnIndex++) {
//     recordBuilder.set(
//         metaData.getColumnName(columnIndex), getRowObject(metaData, row, columnIndex));
//   }
//   return recordBuilder.build();
// }


import static com.google.cloud.bigquery.dwhassessment.extractiontool.db.AvroHelper.dumpResults;
import static com.google.cloud.bigquery.dwhassessment.extractiontool.db.AvroHelper.getAvroSchema;
import static com.google.cloud.bigquery.dwhassessment.extractiontool.db.AvroHelper.parseRowToAvro;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import com.google.common.collect.ImmutableList;
import java.io.ByteArrayOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import org.apache.avro.Schema;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.SeekableByteArrayInput;
import org.apache.avro.generic.GenericData.Record;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.generic.GenericRecordBuilder;
import org.apache.avro.io.DatumReader;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
        "CREATE TABLE SIMPLE_TABLE (ID INTEGER, NAME VARCHAR(100), CHAR_COL CHAR(20))");
    baseStmt.execute("INSERT INTO SIMPLE_TABLE VALUES (0, 'name_0', '  two  words')");
    baseStmt.close();
    connection.commit();

    metaData = connection.createStatement().executeQuery("SELECT * FROM T0").getMetaData();
  }

  @Test
  public void getAvroSchemaTest() throws Exception {
    Schema schema = getAvroSchema("schemaName", "namespace", metaData);
    assertThat(schema).isEqualTo(new Schema.Parser().parse(TEST_SCHEMA));
  }

  @Test
  public void getAvroSchema_emptySchemaName() throws Exception {
    assertThrows(IllegalArgumentException.class, () -> getAvroSchema("", "namespace", metaData));
  }

  @Test
  public void getAvroSchema_emptyNamespace() throws Exception {
    assertThrows(IllegalArgumentException.class, () -> getAvroSchema("schemaName", "", metaData));
  }

  @Test
  public void parseRowToAvroTest() throws Exception {
    Schema testSchema = new Schema.Parser().parse(SIMPLE_TEST_SCHEMA);
    ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM SIMPLE_TABLE");

    while (resultSet.next()) {
// Path: src/java/com/google/cloud/bigquery/dwhassessment/extractiontool/db/AvroHelper.java
// public static void dumpResults(
//     ImmutableList<GenericRecord> records, OutputStream outputStream, Schema schema)
//     throws IOException {
//   GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
//   DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(writer);
//   dataFileWriter.create(schema, outputStream);
// 
//   records.stream()
//       .forEach(
//           record -> {
//             try {
//               dataFileWriter.append(record);
//             } catch (IOException e) {
//               throw new IllegalStateException(
//                   String.format(
//                       "Failed to encode query result %s to file with error message: %s",
//                       record.toString(), e.getMessage()));
//             }
//           });
//   dataFileWriter.close();
//   outputStream.close();
// }
// 
// Path: src/java/com/google/cloud/bigquery/dwhassessment/extractiontool/db/AvroHelper.java
// public static Schema getAvroSchema(
//     String schemaName, String namespace, ResultSetMetaData metaData) throws SQLException {
//   Preconditions.checkArgument(!schemaName.isEmpty(), "Schema name cannot be empty.");
//   Preconditions.checkArgument(!namespace.isEmpty(), "Namespace cannot be empty.");
//   SchemaBuilder.FieldAssembler<Schema> schemaAssembler =
//       SchemaBuilder.record(schemaName).namespace(namespace).fields();
// 
//   for (int columnIndex = 1; columnIndex <= metaData.getColumnCount(); columnIndex++) {
//     SchemaBuilder.FieldBuilder<Schema> fieldBuilder =
//         schemaAssembler.name(metaData.getColumnName(columnIndex));
//     AvroHelper.convertColumnTypeToAvroType(fieldBuilder, metaData, columnIndex);
//   }
//   return schemaAssembler.endRecord();
// }
// 
// Path: src/java/com/google/cloud/bigquery/dwhassessment/extractiontool/db/AvroHelper.java
// public static GenericRecord parseRowToAvro(ResultSet row, Schema schema) throws SQLException {
//   GenericRecordBuilder recordBuilder = new GenericRecordBuilder(schema);
//   ResultSetMetaData metaData = row.getMetaData();
//   for (int columnIndex = 1; columnIndex <= row.getMetaData().getColumnCount(); columnIndex++) {
//     recordBuilder.set(
//         metaData.getColumnName(columnIndex), getRowObject(metaData, row, columnIndex));
//   }
//   return recordBuilder.build();
// }


// Path: src/javatests/com/google/cloud/bigquery/dwhassessment/extractiontool/db/AvroHelperTest.java
import static com.google.cloud.bigquery.dwhassessment.extractiontool.db.AvroHelper.dumpResults;
import static com.google.cloud.bigquery.dwhassessment.extractiontool.db.AvroHelper.getAvroSchema;
import static com.google.cloud.bigquery.dwhassessment.extractiontool.db.AvroHelper.parseRowToAvro;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import com.google.common.collect.ImmutableList;
import java.io.ByteArrayOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import org.apache.avro.Schema;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.SeekableByteArrayInput;
import org.apache.avro.generic.GenericData.Record;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.generic.GenericRecordBuilder;
import org.apache.avro.io.DatumReader;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

        "CREATE TABLE SIMPLE_TABLE (ID INTEGER, NAME VARCHAR(100), CHAR_COL CHAR(20))");
    baseStmt.execute("INSERT INTO SIMPLE_TABLE VALUES (0, 'name_0', '  two  words')");
    baseStmt.close();
    connection.commit();

    metaData = connection.createStatement().executeQuery("SELECT * FROM T0").getMetaData();
  }

  @Test
  public void getAvroSchemaTest() throws Exception {
    Schema schema = getAvroSchema("schemaName", "namespace", metaData);
    assertThat(schema).isEqualTo(new Schema.Parser().parse(TEST_SCHEMA));
  }

  @Test
  public void getAvroSchema_emptySchemaName() throws Exception {
    assertThrows(IllegalArgumentException.class, () -> getAvroSchema("", "namespace", metaData));
  }

  @Test
  public void getAvroSchema_emptyNamespace() throws Exception {
    assertThrows(IllegalArgumentException.class, () -> getAvroSchema("schemaName", "", metaData));
  }

  @Test
  public void parseRowToAvroTest() throws Exception {
    Schema testSchema = new Schema.Parser().parse(SIMPLE_TEST_SCHEMA);
    ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM SIMPLE_TABLE");

    while (resultSet.next()) {
      GenericRecord result = parseRowToAvro(resultSet, testSchema);