package com.conveyal.gtfs.loader;

import com.conveyal.gtfs.TestUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import javax.sql.DataSource;
import java.io.IOException;

import static com.conveyal.gtfs.GTFS.load;
import static com.conveyal.gtfs.GTFS.validate;
import static com.conveyal.gtfs.TestUtils.assertThatSqlCountQueryYieldsExpectedCount;

/**
 * Load in a GTFS feed with GTFS flex features, and ensure all needed fields are imported correctly.
 * TODO: update feed to use more features, and test for these.
 */
public class GtfsFlexTest {
    private static String testDBName;
    private static DataSource testDataSource;
    private static String testNamespace;

    @BeforeAll
    public static void setUpClass() throws IOException {
        // Create a new database
        testDBName = TestUtils.generateNewDB();
        String dbConnectionUrl = String.format("jdbc:postgresql://localhost/%s", testDBName);
        testDataSource = TestUtils.createTestDataSource(dbConnectionUrl);
        // load feed into db
        String zipFileName = TestUtils.zipFolderFiles(
            "real-world-gtfs-feeds/washington-park-shuttle-with-flex-additions",
            true);
        FeedLoadResult feedLoadResult = load(zipFileName, testDataSource);
        testNamespace = feedLoadResult.uniqueIdentifier;
        validate(testNamespace, testDataSource);
    }

    @Test
    void canImportContinuousPickupAndDropoff() {
        String cp_0 = generateStopTimeQuery(
                testNamespace,
                "continuous_pickup",
                0);
        String cp_1 = generateStopTimeQuery(
                testNamespace,
                "continuous_pickup",
                1);
        String cp_2 = generateStopTimeQuery(
                testNamespace,
                "continuous_pickup",
                2);

        String cd_0 = generateStopTimeQuery(
                testNamespace,
                "continuous_drop_off",
                0);
        String cd_1 = generateStopTimeQuery(
                testNamespace,
                "continuous_drop_off",
                1);
        String cd_2 = generateStopTimeQuery(
                testNamespace,
                "continuous_drop_off",
                2);


        assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cp_0, 3);
        assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cp_1, 5);
        assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cp_2, 1);
        assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cd_0, 2);
        assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cd_1, 5);
        assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cd_2, 2);

    }

    private String generateStopTimeQuery(String namespace, String field, int value) {
        return String.format("select count(*) from %s.stop_times where %s = '%s'",
                testNamespace,
                field,
                value);
    }
}

@@ -133,16 +133,139 @@
 ter.
-api.Test
+params.ParameterizedTest;%0Aimport org.junit.jupiter.params.provider.Arguments;%0Aimport org.junit.jupiter.params.provider.MethodSource
 ;%0A%0Ai
@@ -319,16 +319,48 @@
 ception;
+%0Aimport java.util.stream.Stream;
 %0A%0Aimport
@@ -1558,31 +1558,51 @@
    @
-Test%0A    void canImport
+ParameterizedTest%0A    @MethodSource(%22create
 Cont
@@ -1624,193 +1624,215 @@
 Drop
-off() %7B%0A        String cp_0 = generateStopTimeQuery(%0A                testNamespace,%0A                %22continuous_pickup%22,%0A                0);%0A        String cp_1 = generateStopTimeQuery(
+OffChecks%22)%0A    void continuousPickupAndDropOffTests(String namespace, String field, int value, int expectedCount) %7B%0A        String query = String.format(%22select count(*) from %25s.stop_times where %25s = '%25s'%22,
 %0A   
@@ -1836,37 +1836,33 @@
                 
-testN
+n
 amespace,%0A      
@@ -1871,35 +1871,21 @@
         
-%22continuous_pickup%22
+field
 ,%0A      
@@ -1886,33 +1886,37 @@
                 
-1
+value
 );%0A        Strin
@@ -1914,94 +1914,141 @@
     
-String cp_2 = generateStopTimeQuery(%0A        
+assertThatSqlCountQueryYieldsExpectedCount(testDataSource, query, expectedCount);%0A
     
+%7D%0A%0A
     
+priva
 te
+ 
 st
-Namespace,%0A                %22c
+atic Stream%3CArguments%3E createC
 onti
@@ -2056,82 +2056,59 @@
 uous
-_p
+P
 ickup
-%22,%0A                2);%0A%0A        String cd_0 = generateStopTimeQuery
+AndDropOffChecks() %7B%0A        return Stream.of
 (%0A  
@@ -2113,32 +2113,45 @@
                 
+Arguments.of(
 testNamespace,%0A 
@@ -2152,188 +2152,109 @@
 ace,
-%0A                %22continuous_drop_off%22,%0A                0);%0A        String cd_1 = generateStopTimeQuery(%0A                testNamespace,%0A                %22continuous_drop_off%22,%0A     
+ %22continuous_pickup%22, 0, 3),%0A                Arguments.of(testNamespace, %22continuous_pickup%22, 1, 5),%0A
     
@@ -2262,535 +2262,218 @@
     
-  1);%0A
        
- String cd_2 = generateStopTimeQuery(%0A                testNamespace,%0A                %22continuous_drop_off%22,%0A                2);%0A%0A%0A        assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cp_0, 3);%0A        assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cp_1, 5);%0A        assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cp_2, 1);%0A        assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cd_0, 2);%0A        assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cd_
+Arguments.of(testNamespace, %22continuous_pickup%22, 2, 1),%0A                Arguments.of(testNamespace, %22continuous_drop_off%22, 0, 2),%0A                Arguments.of(testNamespace, %22continuous_drop_off%22, 
 1, 5)
-;
+,
 %0A   
@@ -2481,329 +2481,81 @@
     
-assertThatSqlCountQueryYieldsExpectedCount(testDataSource, cd_2, 2);%0A%0A    %7D%0A%0A    private String generateStopTimeQuery(String namespace, String field, int value) %7B%0A        return String.format(%22select count(*) from %25s.stop_times where %25s = '%25s'%22,%0A                testNamespace,%0A                field,%0A                value
+        Arguments.of(testNamespace, %22continuous_drop_off%22, 2, 2)%0A        
 );%0A 

clean test by moving to paramterized test