-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Added a validator for bad networkId foreign key in fareLegRules (#…
…1804) Added a custom validator for networkId foreign key that depends on 2 files.
- Loading branch information
Showing
3 changed files
with
189 additions
and
2 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
80 changes: 80 additions & 0 deletions
80
...org/mobilitydata/gtfsvalidator/validator/GtfsFareLegRuleNetworkIdForeignKeyValidator.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,80 @@ | ||
/* | ||
* Copyright 2020 Google 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 org.mobilitydata.gtfsvalidator.validator; | ||
|
||
import javax.inject.Inject; | ||
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; | ||
import org.mobilitydata.gtfsvalidator.notice.ForeignKeyViolationNotice; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsFareLegRule; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsFareLegRuleTableContainer; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsNetwork; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsNetworkTableContainer; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsRoute; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsRouteTableContainer; | ||
|
||
/** | ||
* Validates that network_id field in "fare_leg_rules.txt" references a valid network_id in | ||
* "routes.txt" or "networks.txt". | ||
* | ||
* <p>Generated notice: {@link ForeignKeyViolationNotice}. | ||
*/ | ||
@GtfsValidator | ||
public class GtfsFareLegRuleNetworkIdForeignKeyValidator extends FileValidator { | ||
private final GtfsRouteTableContainer routeParentContainer; | ||
private final GtfsNetworkTableContainer networkParentContainer; | ||
|
||
private final GtfsFareLegRuleTableContainer fareLegRuleChildContainer; | ||
|
||
@Inject | ||
GtfsFareLegRuleNetworkIdForeignKeyValidator( | ||
GtfsFareLegRuleTableContainer fareLegRuleChildContainer, | ||
GtfsRouteTableContainer routeParentContainer, | ||
GtfsNetworkTableContainer networkParentContainer) { | ||
this.fareLegRuleChildContainer = fareLegRuleChildContainer; | ||
this.routeParentContainer = routeParentContainer; | ||
this.networkParentContainer = networkParentContainer; | ||
} | ||
|
||
@Override | ||
public void validate(NoticeContainer noticeContainer) { | ||
for (GtfsFareLegRule fareLegRule : fareLegRuleChildContainer.getEntities()) { | ||
String foreignKey = fareLegRule.networkId(); | ||
if (foreignKey.isEmpty()) { | ||
continue; | ||
} | ||
if (!hasReferencedKey(foreignKey, routeParentContainer, networkParentContainer)) { | ||
noticeContainer.addValidationNotice( | ||
new ForeignKeyViolationNotice( | ||
GtfsFareLegRule.FILENAME, | ||
GtfsFareLegRule.NETWORK_ID_FIELD_NAME, | ||
GtfsRoute.FILENAME + " or " + GtfsNetwork.FILENAME, | ||
GtfsFareLegRule.NETWORK_ID_FIELD_NAME, | ||
foreignKey, | ||
fareLegRule.csvRowNumber())); | ||
} | ||
} | ||
} | ||
|
||
private boolean hasReferencedKey( | ||
String foreignKey, | ||
GtfsRouteTableContainer routeContainer, | ||
GtfsNetworkTableContainer networkContainer) { | ||
return !(routeContainer.byNetworkId(foreignKey).isEmpty() | ||
&& networkContainer.byNetworkId(foreignKey).isEmpty()); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...mobilitydata/gtfsvalidator/validator/GtfsFareLegRuleNetworkIdForeignKeyValidatorTest.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,103 @@ | ||
/* | ||
* Copyright 2020 Google LLC, MobilityData IO | ||
* | ||
* 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 org.mobilitydata.gtfsvalidator.validator; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.List; | ||
import org.junit.Test; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsFareLegRule; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsFareLegRuleTableContainer; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsNetwork; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsNetworkTableContainer; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsRoute; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsRouteTableContainer; | ||
|
||
public class GtfsFareLegRuleNetworkIdForeignKeyValidatorTest { | ||
|
||
public static GtfsFareLegRule createFareLegRule(String networkId) { | ||
return new GtfsFareLegRule.Builder().setCsvRowNumber(1).setNetworkId(networkId).build(); | ||
} | ||
|
||
private static GtfsRoute createRoute(String networkId) { | ||
return new GtfsRoute.Builder() | ||
.setCsvRowNumber(1) | ||
.setRouteId("testRoute") | ||
.setRouteType(3) // Bus, but could be other type | ||
.setNetworkId(networkId) | ||
.build(); | ||
} | ||
|
||
private static GtfsNetwork createNetwork(String networkId) { | ||
return new GtfsNetwork.Builder().setCsvRowNumber(1).setNetworkId(networkId).build(); | ||
} | ||
|
||
private static List<ValidationNotice> generateNotices( | ||
GtfsFareLegRule fareLegRule, GtfsRoute route, GtfsNetwork network) { | ||
NoticeContainer noticeContainer = new NoticeContainer(); | ||
new GtfsFareLegRuleNetworkIdForeignKeyValidator( | ||
GtfsFareLegRuleTableContainer.forEntities( | ||
ImmutableList.of(fareLegRule), noticeContainer), | ||
GtfsRouteTableContainer.forEntities(ImmutableList.of(route), noticeContainer), | ||
GtfsNetworkTableContainer.forEntities(ImmutableList.of(network), noticeContainer)) | ||
.validate(noticeContainer); | ||
return noticeContainer.getValidationNotices(); | ||
} | ||
|
||
@Test | ||
public void networkIdNotInRouteOrNetworkShouldGenerateNotice() { | ||
List<?> notices = | ||
generateNotices( | ||
createFareLegRule("testNetworkId"), | ||
createRoute("otherNetworkId"), | ||
createNetwork("otherNetworkId")); | ||
assertThat(notices).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
public void networkIdInRouteButNotInNetworkShouldNotGenerateNotice() { | ||
List<?> notices = | ||
generateNotices( | ||
createFareLegRule("testNetworkId"), | ||
createRoute("testNetworkId"), | ||
createNetwork("otherNetworkId")); | ||
assertThat(notices).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void networkIdNotInRouteButInNetworkShouldNotGenerateNotice() { | ||
List<?> notices = | ||
generateNotices( | ||
createFareLegRule("testNetworkId"), | ||
createRoute("otherNetworkId"), | ||
createNetwork("testNetworkId")); | ||
assertThat(notices).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void networkIdInBothRouteAndNetworkShouldNotGenerateNotice() { | ||
List<?> notices = | ||
generateNotices( | ||
createFareLegRule("testNetworkId"), | ||
createRoute("testNetworkId"), | ||
createNetwork("testNetworkId")); | ||
assertThat(notices).isEmpty(); | ||
} | ||
} |