-
Notifications
You must be signed in to change notification settings - Fork 38
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 #345 from conveyal/dev
Release PR
- Loading branch information
Showing
4 changed files
with
79 additions
and
1 deletion.
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
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
56 changes: 56 additions & 0 deletions
56
src/main/java/com/conveyal/gtfs/validator/RouteTypeValidator.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,56 @@ | ||
package com.conveyal.gtfs.validator; | ||
|
||
import com.conveyal.gtfs.error.SQLErrorStorage; | ||
import com.conveyal.gtfs.loader.Feed; | ||
import com.conveyal.gtfs.model.Route; | ||
|
||
import java.util.List; | ||
|
||
import static com.conveyal.gtfs.error.NewGTFSErrorType.ROUTE_TYPE_INVALID; | ||
|
||
/** | ||
* Validates route types based on a configured list of values. | ||
*/ | ||
public class RouteTypeValidator extends FeedValidator { | ||
private final List<Integer> configuredRouteTypes; | ||
|
||
/** | ||
* Constructor used for tests in the same package. | ||
*/ | ||
RouteTypeValidator(List<Integer> routeTypes) { | ||
this(null, null, routeTypes); | ||
} | ||
|
||
/** | ||
* Constructor for building a route type validator given a set of valid route types. | ||
*/ | ||
public RouteTypeValidator(Feed feed, SQLErrorStorage errorStorage, List<Integer> routeTypes) { | ||
super(feed, errorStorage); | ||
this.configuredRouteTypes = routeTypes; | ||
} | ||
|
||
@Override | ||
public void validate() { | ||
feed.routes.forEach(this::validateRouteType); | ||
} | ||
|
||
/** | ||
* @return true if routeType is one of the configured route types, false otherwise. | ||
*/ | ||
public boolean isRouteTypeValid(int routeType) { | ||
return configuredRouteTypes.contains(routeType); | ||
} | ||
|
||
/** | ||
* Checks that the route type is valid, reports a validation error if not. | ||
* @param route The containing GTFS route. | ||
* @return true if the route type for the given route is valid, false otherwise. | ||
*/ | ||
public boolean validateRouteType(Route route) { | ||
if (!isRouteTypeValid(route.route_type)) { | ||
if (errorStorage != null) registerError(route, ROUTE_TYPE_INVALID, route.route_type); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/com/conveyal/gtfs/validator/RouteTypeValidatorTest.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,21 @@ | ||
package com.conveyal.gtfs.validator; | ||
|
||
import com.google.common.collect.Lists; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class RouteTypeValidatorTest { | ||
private static final List<Integer> TEST_ROUTE_TYPES = Lists.newArrayList(3, 101); | ||
private static final RouteTypeValidator ROUTE_TYPE_VALIDATOR = new RouteTypeValidator(TEST_ROUTE_TYPES); | ||
|
||
@Test | ||
void validateRouteType() { | ||
assertTrue(ROUTE_TYPE_VALIDATOR.isRouteTypeValid(3)); // Bus | ||
assertFalse(ROUTE_TYPE_VALIDATOR.isRouteTypeValid(36)); // Invalid | ||
assertTrue(ROUTE_TYPE_VALIDATOR.isRouteTypeValid(101)); // High Speed Rail | ||
} | ||
} |