-
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.
feat: Flex - Removed required field false positive for stop_id + adde…
…d Foreign key violation for location_groups_id (#1834) Removed required field false positive for stop_id + added Foreign key violation for location_groups_id (#1834)
- Loading branch information
Showing
6 changed files
with
240 additions
and
4 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
core/src/main/java/org/mobilitydata/gtfsvalidator/notice/ForbiddenGeographyIdNotice.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,52 @@ | ||
/* | ||
* Copyright 2024 MobilityData | ||
* | ||
* 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.notice; | ||
|
||
import static org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice.SectionRef.FILE_REQUIREMENTS; | ||
import static org.mobilitydata.gtfsvalidator.notice.SeverityLevel.ERROR; | ||
|
||
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice; | ||
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice.SectionRefs; | ||
|
||
/** | ||
* A stop_time entry has more than one geographical id defined. | ||
* | ||
* <p>In stop_times.txt, you can have only one of stop_id, location_group_id or location_id defined | ||
* for given entry. | ||
*/ | ||
@GtfsValidationNotice(severity = ERROR, sections = @SectionRefs(FILE_REQUIREMENTS)) | ||
public class ForbiddenGeographyIdNotice extends ValidationNotice { | ||
|
||
/** The row of the faulty record. */ | ||
private final int csvRowNumber; | ||
|
||
/** The sThe id that already exists. */ | ||
private final String stopId; | ||
|
||
/** The id that already exists. */ | ||
private final String locationGroupId; | ||
|
||
/** The id that already exists. */ | ||
private final String locationId; | ||
|
||
public ForbiddenGeographyIdNotice( | ||
int csvRowNumber, String stopId, String locationGroupId, String locationId) { | ||
this.csvRowNumber = csvRowNumber; | ||
this.stopId = stopId; | ||
this.locationGroupId = locationGroupId; | ||
this.locationId = locationId; | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
.../java/org/mobilitydata/gtfsvalidator/validator/StopTimesGeographyIdPresenceValidator.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,64 @@ | ||
/* | ||
* Copyright 2024 MobilityData | ||
* | ||
* 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 org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; | ||
import org.mobilitydata.gtfsvalidator.notice.ForbiddenGeographyIdNotice; | ||
import org.mobilitydata.gtfsvalidator.notice.MissingRequiredFieldNotice; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsStopTime; | ||
|
||
/** | ||
* Validates that only one of stop_id, location_group_id or location_id is defined in a given record | ||
* of stop_times.txt | ||
* | ||
* <p>Generated notice: {@link MissingRequiredFieldNotice}. | ||
* | ||
* <p>Generated notice: {@link ForbiddenGeographyIdNotice}. | ||
*/ | ||
@GtfsValidator | ||
public class StopTimesGeographyIdPresenceValidator extends SingleEntityValidator<GtfsStopTime> { | ||
|
||
@Override | ||
public void validate(GtfsStopTime stopTime, NoticeContainer noticeContainer) { | ||
int presenceCount = 0; | ||
if (stopTime.hasStopId()) { | ||
presenceCount++; | ||
} | ||
if (stopTime.hasLocationGroupId()) { | ||
presenceCount++; | ||
} | ||
|
||
if (stopTime.hasLocationId()) { | ||
presenceCount++; | ||
} | ||
|
||
if (presenceCount == 0) { | ||
// None of the 3 geography IDs are present, but we need at least stop_id | ||
noticeContainer.addValidationNotice( | ||
new MissingRequiredFieldNotice( | ||
GtfsStopTime.FILENAME, stopTime.csvRowNumber(), GtfsStopTime.STOP_ID_FIELD_NAME)); | ||
} else if (presenceCount > 1) { | ||
// More than one geography ID is present, but only one is allowed | ||
noticeContainer.addValidationNotice( | ||
new ForbiddenGeographyIdNotice( | ||
stopTime.csvRowNumber(), | ||
stopTime.stopId(), | ||
stopTime.locationGroupId(), | ||
stopTime.locationId())); | ||
} | ||
} | ||
} |
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
100 changes: 100 additions & 0 deletions
100
...a/org/mobilitydata/gtfsvalidator/validator/StopTimesGeographyIdPresenceValidatorTest.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,100 @@ | ||
/* | ||
* 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 java.util.List; | ||
import org.junit.Test; | ||
import org.mobilitydata.gtfsvalidator.notice.ForbiddenGeographyIdNotice; | ||
import org.mobilitydata.gtfsvalidator.notice.MissingRequiredFieldNotice; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsStopTime; | ||
|
||
public class StopTimesGeographyIdPresenceValidatorTest { | ||
|
||
@Test | ||
public void NoGeographyIdShouldGenerateMissingRequiredFieldNotice() { | ||
assertThat(validationNoticesFor(new GtfsStopTime.Builder().setCsvRowNumber(2).build())) | ||
.containsExactly(new MissingRequiredFieldNotice("stop_times.txt", 2, "stop_id")); | ||
} | ||
|
||
@Test | ||
public void OneGeographyIdShouldGenerateNothing() { | ||
assertThat( | ||
validationNoticesFor( | ||
new GtfsStopTime.Builder().setCsvRowNumber(2).setStopId("stop_id").build())) | ||
.isEmpty(); | ||
assertThat( | ||
validationNoticesFor( | ||
new GtfsStopTime.Builder().setCsvRowNumber(2).setLocationGroupId("id").build())) | ||
.isEmpty(); | ||
assertThat( | ||
validationNoticesFor( | ||
new GtfsStopTime.Builder().setCsvRowNumber(2).setLocationId("id").build())) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void MultipleGeographyIdShouldGenerateNotice() { | ||
assertThat( | ||
validationNoticesFor( | ||
new GtfsStopTime.Builder() | ||
.setStopId("stop_id") | ||
.setLocationGroupId("location_group_id") | ||
.setCsvRowNumber(2) | ||
.build())) | ||
.containsExactly(new ForbiddenGeographyIdNotice(2, "stop_id", "location_group_id", "")); | ||
|
||
assertThat( | ||
validationNoticesFor( | ||
new GtfsStopTime.Builder() | ||
.setStopId("stop_id") | ||
.setLocationId("location_id") | ||
.setCsvRowNumber(2) | ||
.build())) | ||
.containsExactly(new ForbiddenGeographyIdNotice(2, "stop_id", "", "location_id")); | ||
|
||
assertThat( | ||
validationNoticesFor( | ||
new GtfsStopTime.Builder() | ||
.setLocationGroupId("location_group_id") | ||
.setLocationId("location_id") | ||
.setCsvRowNumber(2) | ||
.build())) | ||
.containsExactly(new ForbiddenGeographyIdNotice(2, "", "location_group_id", "location_id")); | ||
|
||
assertThat( | ||
validationNoticesFor( | ||
new GtfsStopTime.Builder() | ||
.setStopId("stop_id") | ||
.setLocationGroupId("location_group_id") | ||
.setLocationId("location_id") | ||
.setCsvRowNumber(2) | ||
.build())) | ||
.containsExactly( | ||
new ForbiddenGeographyIdNotice(2, "stop_id", "location_group_id", "location_id")); | ||
} | ||
|
||
private List<ValidationNotice> validationNoticesFor(GtfsStopTime entity) { | ||
StopTimesGeographyIdPresenceValidator validator = new StopTimesGeographyIdPresenceValidator(); | ||
NoticeContainer noticeContainer = new NoticeContainer(); | ||
validator.validate(entity, noticeContainer); | ||
return noticeContainer.getValidationNotices(); | ||
} | ||
} |
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