package atmsdriver.model;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import junit.framework.TestCase;

/**
 *
 * @author jdalbey
 */
public class LoadHighwaysTest extends TestCase {

    public LoadHighwaysTest(String testName) {
        super(testName);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new FileWriter("test/atmsdriver/model/lds_loadhighways_sample.txt"));
            
            writer.println("lds_id	line 	drop sch lineinfo	system_key	sch_seq glo_seq		count	freeway	Dir	ca_pm	lds_name");
            writer.println("1203081     6       17      2       2       1123005691      26491   1357639         19      55      N       4.58    BRISTOL");
            writer.println("1210163     6       20      2       2       1123005691      26491   1357639         19      55      N       5.51    PAULARINO 1");
            writer.println("1203244     50      4       13      13      1123005873      24148   1357650         19      55      N       10      MCFADDEN");
            writer.println("1203211     50      5       13      13      1123005873      24148   1357650         19      55      N       9.41    EDINGER 2");
            
            writer.println("1203261     23      14      23      21      1123005995      26479   1357626         18      55      S       10.4    S OF 5");
            writer.println("1203083     6       18      2       2       1123005691      26491   1357639         19      55      S       4.7     BAKER 1");
            writer.println("1210174     6       19      2       2       1123005691      26491   1357639         19      55      S       5.06    BAKER 2");
            writer.println("1203270     23      15      23      21      1123005995      26479   1357626         18      55      S       10.5    N OF 5");
            
            writer.println("1205238     22      16      4       4       1123005726      26490   1357624         19      5       S       32.25   17TH 1");
            writer.println("1205208     22      17      4       4       1123005726      26490   1357624         19      5       S       31.6    GRAND 1");
            writer.println("1205270	5	19	1	1	1123005673	26492	1357648		20	5	S	33	MAIN 1   ");
            

            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        Path path = FileSystems.getDefault().getPath("test/atmsdriver/model", "lds_loadhighways_sample.txt");
        Files.delete(path);
    }

    /**
     * Test of toXML method, of class Network.
     */
    public void testLoadHighways() {
        System.out.println("toXML");
        Highways highways = new Highways(
                "config/vds_data/highways_fullmap.txt",
                "localhost", 8080);
        
        // Test for correct number of highways
        ArrayList<Highway> result = highways.highways;
        assertEquals(2, result.size());
        
        // Test 55 N was loaded
        Highway fiftyfiveN = result.get(0);
        assertEquals(new Integer(55), fiftyfiveN.routeNumber);
        
        // Test 55 N stations are sorted by postmile
        ArrayList<Station> stations = fiftyfiveN.stations;
        ArrayList<Double> stationsPostmiles = new ArrayList<>();
        for(Station station : stations)
        {
            stationsPostmiles.add(station.postmile);
        }
        
        // Create expected station postmile list (sorted)
        ArrayList<Double> expectedStationsPostmiles = new ArrayList<>();
        expectedStationsPostmiles.add(new Double(4.58));
        expectedStationsPostmiles.add(new Double(4.7));
        expectedStationsPostmiles.add(new Double(5.06));
        expectedStationsPostmiles.add(new Double(5.51));
        expectedStationsPostmiles.add(new Double(9.41));
        expectedStationsPostmiles.add(new Double(10));
        expectedStationsPostmiles.add(new Double(10.4));
        expectedStationsPostmiles.add(new Double(10.5));
        for(int i = 0; i < 8; i++)
        {
            assertEquals(expectedStationsPostmiles.get(i), 
                    stationsPostmiles.get(i));
        }
        
        // Test 55 S was loaded
        Highway fiftyfiveS = result.get(1);
        assertEquals(new Integer(5), fiftyfiveS.routeNumber);
        
        // Test 55 S stations are sorted by postmile
        stations = fiftyfiveS.stations;
        stationsPostmiles.clear();
        for(Station station : stations)
        {
            stationsPostmiles.add(station.postmile);
        }
        expectedStationsPostmiles.clear();
        expectedStationsPostmiles.add(new Double(31.6));
        expectedStationsPostmiles.add(new Double(32.25));
        expectedStationsPostmiles.add(new Double(33));
        for(int i = 0; i < 3; i++)
        {
            assertEquals(expectedStationsPostmiles.get(i), 
                    stationsPostmiles.get(i));
        }
    }
}
