-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #584 from ibi-group/shared-stops-validator-repair
Repair Shared Stops Validator
- Loading branch information
Showing
9 changed files
with
194 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,10 +33,10 @@ jobs: | |
java-version: 19 | ||
distribution: 'temurin' | ||
# Install node 14 for running e2e tests (and for maven-semantic-release). | ||
- name: Use Node.js 18.x | ||
- name: Use Node.js 20.x | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: 18.x | ||
node-version: 20.x | ||
- name: Start MongoDB | ||
uses: supercharge/[email protected] | ||
with: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/com/conveyal/datatools/manager/jobs/validation/SharedStopsHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.conveyal.datatools.manager.jobs.validation; | ||
|
||
import java.util.Map; | ||
|
||
public enum SharedStopsHeader { | ||
|
||
STOP_GROUP_ID_INDEX("stop_group_id"), | ||
|
||
STOP_ID_INDEX("stop_id"), | ||
|
||
IS_PRIMARY_INDEX("is_primary"), | ||
|
||
FEED_ID_INDEX("feed_id"); | ||
|
||
public final String headerName; | ||
|
||
SharedStopsHeader(String name) { | ||
this.headerName = name; | ||
} | ||
|
||
public String getHeaderName() { | ||
return headerName; | ||
} | ||
|
||
public static SharedStopsHeader fromValue(String headerName) { | ||
for (SharedStopsHeader sharedStopsHeader : SharedStopsHeader.values()) { | ||
if (sharedStopsHeader.getHeaderName().equalsIgnoreCase(headerName)) { | ||
return sharedStopsHeader; | ||
} | ||
} | ||
throw new UnsupportedOperationException(String.format("Unknown shared stops header: %s", headerName)); | ||
} | ||
|
||
public static boolean hasRequiredHeaders(Map<SharedStopsHeader, Integer> headerIndices) { | ||
return headerIndices.size() == SharedStopsHeader.values().length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/java/com/conveyal/datatools/manager/jobs/validation/SharedStopsValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.conveyal.datatools.manager.jobs.validation; | ||
|
||
import com.conveyal.datatools.UnitTest; | ||
import com.csvreader.CsvReader; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static com.zenika.snapshotmatcher.SnapshotMatcher.matchesSnapshot; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class SharedStopsValidatorTest extends UnitTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("createCSVHeaders") | ||
void canHandleVariousCorrectCSV(String headers) { | ||
assertThat(SharedStopsValidator.getHeaderIndices(CsvReader.parse(headers)), matchesSnapshot()); | ||
} | ||
|
||
private static Stream<String> createCSVHeaders() { | ||
return Stream.of( | ||
"is_primary,feed_id,stop_id,stop_group_id", | ||
"feed_id,stop_id,stop_group_id,is_primary", | ||
"feed_id,is_primary,stop_group_id,stop_id,feed_id" | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("createInvalidCSVArguments") | ||
void attemptToParseInvalidCSV(InvalidCSVArgument invalidCSVArgument) { | ||
CsvReader configReader = CsvReader.parse(invalidCSVArgument.csv); | ||
Exception exception = assertThrows(RuntimeException.class, () -> SharedStopsValidator.getHeaderIndices(configReader)); | ||
assertEquals(invalidCSVArgument.expectedError, exception.getMessage()); | ||
} | ||
|
||
private static Stream<InvalidCSVArgument> createInvalidCSVArguments() { | ||
String invalid = "Unknown shared stops header: "; | ||
String missing = "shared_stops.csv is missing headers!"; | ||
return Stream.of( | ||
new InvalidCSVArgument("this is a great string, but it's not a shared_stops csv!", invalid + "this is a great string"), | ||
new InvalidCSVArgument("", invalid), | ||
new InvalidCSVArgument("is_primary,stop_id", missing), | ||
new InvalidCSVArgument("is_primary,feed_id,,stop_group_id", invalid), | ||
new InvalidCSVArgument("is_primary,feed_id,stop_group_id", missing), | ||
new InvalidCSVArgument("feed_id, is_primary, stop_group_id,stop_id", invalid + " is_primary") | ||
); | ||
} | ||
|
||
private static class InvalidCSVArgument { | ||
public String csv; | ||
public String expectedError; | ||
|
||
public InvalidCSVArgument(String csv, String expectedError) { | ||
this.csv = csv; | ||
this.expectedError = expectedError; | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...atools/manager/jobs/validation/SharedStopsValidatorTest/canHandleVariousCorrectCSV-0.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"STOP_GROUP_ID_INDEX" : 3, | ||
"STOP_ID_INDEX" : 2, | ||
"IS_PRIMARY_INDEX" : 0, | ||
"FEED_ID_INDEX" : 1 | ||
} |
6 changes: 6 additions & 0 deletions
6
...atools/manager/jobs/validation/SharedStopsValidatorTest/canHandleVariousCorrectCSV-1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"STOP_GROUP_ID_INDEX" : 2, | ||
"STOP_ID_INDEX" : 1, | ||
"IS_PRIMARY_INDEX" : 3, | ||
"FEED_ID_INDEX" : 0 | ||
} |
6 changes: 6 additions & 0 deletions
6
...atools/manager/jobs/validation/SharedStopsValidatorTest/canHandleVariousCorrectCSV-2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"STOP_GROUP_ID_INDEX" : 2, | ||
"STOP_ID_INDEX" : 3, | ||
"IS_PRIMARY_INDEX" : 1, | ||
"FEED_ID_INDEX" : 4 | ||
} |