package org.mitre.synthea.export;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.file.Files;
import java.util.stream.Collectors;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mitre.synthea.TestHelper;
import org.mitre.synthea.engine.Generator;
import org.mitre.synthea.helpers.Config;
import org.mitre.synthea.helpers.SimpleCSV;

public class CSVExporterTest {
  /**
   * Temporary folder for any exported files, guaranteed to be deleted at the end of the test.
   */
  @Rule
  public TemporaryFolder tempFolder = new TemporaryFolder();

  @Test
  public void testCSVExport() throws Exception {
    TestHelper.exportOff();
    Config.set("exporter.csv.export", "true");
    File tempOutputFolder = tempFolder.newFolder();
    Config.set("exporter.baseDirectory", tempOutputFolder.toString());

    int numberOfPeople = 10;
    Generator generator = new Generator(numberOfPeople);
    for (int i = 0; i < numberOfPeople; i++) {
      generator.generatePerson(i);
    }

    // if we get here we at least had no exceptions

    File expectedExportFolder = tempOutputFolder.toPath().resolve("csv").toFile();

    assertTrue(expectedExportFolder.exists() && expectedExportFolder.isDirectory());

    int count = 0;
    for (File csvFile : expectedExportFolder.listFiles()) {
      if (!csvFile.getName().endsWith(".csv")) {
        continue;
      }

      String csvData = Files.readAllLines(csvFile.toPath()).stream()
          .collect(Collectors.joining("\n"));

      // the CSV exporter doesn't use the SimpleCSV class to write the data,
      // so we can use it here for a level of validation
      SimpleCSV.parse(csvData);

      count++;
    }

    assertEquals("Expected 10 CSV files in the output directory, found " + count, 10, count);
  }
}

@@ -169,44 +169,8 @@
 les;
-%0Aimport java.util.stream.Collectors;
 %0A%0Aim
@@ -1459,16 +1459,27 @@
 vData = 
+new String(
 Files.re
@@ -1487,11 +1487,11 @@
 dAll
-Lin
+Byt
 es(c
@@ -1510,61 +1510,8 @@
 h())
-.stream()%0A          .collect(Collectors.joining(%22%5Cn%22)
 );%0A%0A

fix for csv test exception