package nxm.sys.test.junit.lib;

import nxm.sys.lib.CsvFile;
import nxm.sys.lib.Midas;
import nxm.sys.test.junit.NMTestCase;

/**
 * This will test the CsvFile class
 * @author Kevin Esterheld
 * @since NeXtMidas 3.8.0
 */
public class CsvFileTest extends NMTestCase {

  /**
   * public constructor to allow running this test case directly with JUnit.
   * @param methodName The name of the method being called.
   */
  public CsvFileTest (String methodName) {this(methodName, null);}

  /**
   * This constructor is required by NeXtMidas Unit Testing framework.
   * @param methodName  The name of the method being called.
   * @param midas       The midas context to use when running the tests.
   */
  public CsvFileTest (String methodName, Midas midas) {
    super(methodName, midas);

  }

  /**
   * Test case to confirm the functionality that is expected of the csv parser is working correctly.
   */
  public void testReadAll() {
    //instantiate the test files for reading
    Midas M = getMidas(); // This Midas context can vary between being a Java or Macro context
    CsvFile csvTest1 = new CsvFile(M,urlSysTest+"test_csv_spaces_no_lf_at_end.csv");
    CsvFile csvTest2 = new CsvFile(M,urlSysTest+"test_csv_tab_quote_at_end.csv");
    CsvFile csvTest3 = new CsvFile(M,urlSysTest+"test_csv_empty_row.csv");
    CsvFile csvTest4 = new CsvFile(M,urlSysTest+"test_csv_quotes_and_new_lines.csv");
    CsvFile csvTest5 = new CsvFile(M,urlSysTest+"test_csv_empty_quotes.csv");

    //Test spaces no lf at end
    csvTest1.isInput = true;
    csvTest1.open();
    assertEquals((String)csvTest1.getDataTable(1).get("NAME"), "John Doe");
    assertEquals((String)csvTest1.getDataTable(2).get("NAME"), "Jane Doe");
    assertEquals((String)csvTest1.getDataTable(1).get("AGE"), "21");
    assertEquals((String)csvTest1.getDataTable(2).get("AGE"), "23");
    csvTest1.close();

    //Test multiple lines in column
    csvTest2.setFlags("EmbeddedLF");
    csvTest2.isInput = true;
    csvTest2.open();
    assertEquals(csvTest2.getDataTable(1).get("ADDRESS"), "15040 Conference Center Dr,\n" +
        "Chantilly, VA 20151");
    csvTest2.close();

    //Test empty row, should warn but pass
    csvTest3.isInput = true;
    csvTest3.open();
    assertEquals(csvTest3.getDataTable(1).get("NAME"),"John Doe");
    //SHOULD BE EMPTY STRING BECAUSE THE LINE IS EMPTY
    assertEquals(csvTest3.getDataTable(2).get("NAME"),"");
    assertEquals(csvTest3.getDataTable(3).get("NAME"),"Jane Doe");
    csvTest3.close();

    //Test column with multiple rows and quotes
    csvTest4.isInput = true;
    csvTest4.setFlags("EmbeddedLF");
    csvTest4.open();
    assertEquals(csvTest4.getDataTable(1).get("QUOTE"), "this \n\"should\" \n be a row");
    assertEquals(csvTest4.getDataTable(2).get("QUOTE"), "This is one row");
    csvTest4.close();

    //Test columns with empty quotes
    csvTest5.isInput = true;
    csvTest5.open();
    assertEquals(csvTest5.getDataTable(1).get("A"), "1");
    assertEquals(csvTest5.getDataTable(1).get("B"), "");
    assertEquals(csvTest5.getDataTable(1).get("C"), "");
    csvTest5.close();

  }
}