/*
 *  Copyright 2014 TWO SIGMA OPEN SOURCE, LLC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.twosigma.beaker.fileloader;

import com.github.lwhite1.tablesaw.api.Table;
import org.junit.Test;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class CsvPlotReaderTest {

  private static final boolean IS_WINDOWS = System.getProperty("os.name").contains("indow");

  @Test
  public void shouldReturnDataForPlot() throws Exception {
    //given
    URI uriToTableRowTest = getClass().getClassLoader().getResource("tableRowsTest.csv").toURI();
    String osAppropriatePath =
        IS_WINDOWS
            ? uriToTableRowTest.getSchemeSpecificPart().substring(1)
            : uriToTableRowTest.getSchemeSpecificPart();
    //when
    CsvPlotReader reader = new CsvPlotReader();
    Table values = reader.read(osAppropriatePath);
    //then
    assertThat(reader.convert(values).get(2).get("m3")).isEqualTo(8.0f);
    assertThat(reader.convert(values).get(2).get("time"))
        .isEqualTo(new SimpleDateFormat("yyyy-MM-dd").parse("1990-03-31").getTime());
  }

  @Test
  public void shouldReturnDataAsListForPlot() throws Exception {
    //given
    URI pathToTableRowTest = getClass().getClassLoader().getResource("tableRowsTest.csv").toURI();
    //when
    List<Map<String, Object>> values = new CsvPlotReader().readAsList(pathToTableRowTest.getSchemeSpecificPart());
    //then
    assertThat(values.get(2).get("m3")).isEqualTo(8.0f);
    assertThat(values.get(2).get("time")).isEqualTo(new SimpleDateFormat("yyyy-MM-dd").parse("1990-03-31").getTime());
  }


}

@@ -1099,293 +1099,8 @@
 ven%0A
-    URI uriToTableRowTest = getClass().getClassLoader().getResource(%22tableRowsTest.csv%22).toURI();%0A    String osAppropriatePath =%0A        IS_WINDOWS%0A            ? uriToTableRowTest.getSchemeSpecificPart().substring(1)%0A            : uriToTableRowTest.getSchemeSpecificPart();%0A    //when%0A
     
@@ -1139,24 +1139,35 @@
 otReader();%0A
+    //when%0A
     Table va
@@ -1185,17 +1185,20 @@
 er.read(
-o
+getO
 sAppropr
@@ -1205,16 +1205,37 @@
 iatePath
+(%22tableRowsTest.csv%22)
 );%0A    /
@@ -1539,119 +1539,8 @@
 n %7B%0A
-    //given%0A    URI pathToTableRowTest = getClass().getClassLoader().getResource(%22tableRowsTest.csv%22).toURI();%0A
     
@@ -1584,16 +1584,24 @@
 values =
+%0A       
  new Csv
@@ -1628,49 +1628,48 @@
 ist(
-pathToT
+getOsAppropriatePath(%22t
 ableRow
+s
 Test.
-getSchemeSpecificPart(
+csv%22
 ));%0A
@@ -1777,16 +1777,25 @@
 %22time%22))
+%0A        
 .isEqual
@@ -1869,11 +1869,291 @@
 );%0A  %7D%0A%0A
+  private String getOsAppropriatePath(String fileName) throws Exception %7B%0A    URI uriToFile = getClass().getClassLoader().getResource(fileName).toURI();%0A    return IS_WINDOWS%0A        ? uriToFile.getSchemeSpecificPart().substring(1)%0A        : uriToFile.getSchemeSpecificPart();%0A  %7D
 %0A%7D%0A

fix CsvPlotReaderTest for Windows (#133)