Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcpitre committed Sep 10, 2024
1 parent 074f4d8 commit d9f4f7a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* for given entry.
*/
@GtfsValidationNotice(severity = ERROR, sections = @SectionRefs(FILE_REQUIREMENTS))
public class ForbiddenGeograaphyIdNotice extends ValidationNotice {
public class ForbiddenGeographyIdNotice extends ValidationNotice {

/** The row of the faulty record. */
private final int csvRowNumber;
Expand All @@ -42,7 +42,7 @@ public class ForbiddenGeograaphyIdNotice extends ValidationNotice {
/** The id that already exists. */
private final String locationId;

public ForbiddenGeograaphyIdNotice(
public ForbiddenGeographyIdNotice(
int csvRowNumber, String stopId, String locationGroupId, String locationId) {
this.csvRowNumber = csvRowNumber;
this.stopId = stopId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.mobilitydata.gtfsvalidator.validator;

import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator;
import org.mobilitydata.gtfsvalidator.notice.ForbiddenGeograaphyIdNotice;
import org.mobilitydata.gtfsvalidator.notice.ForbiddenGeographyIdNotice;
import org.mobilitydata.gtfsvalidator.notice.MissingRequiredFieldNotice;
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
import org.mobilitydata.gtfsvalidator.table.GtfsStopTime;
Expand All @@ -27,7 +27,7 @@
*
* <p>Generated notice: {@link MissingRequiredFieldNotice}.
*
* <p>Generated notice: {@link ForbiddenGeograaphyIdNotice}.
* <p>Generated notice: {@link ForbiddenGeographyIdNotice}.
*/
@GtfsValidator
public class StopTimesGeographyIdPresenceValidator extends SingleEntityValidator<GtfsStopTime> {
Expand All @@ -54,7 +54,7 @@ public void validate(GtfsStopTime stopTime, NoticeContainer noticeContainer) {
} else if (presenceCount > 1) {
// More than one geography ID is present, but only one is allowed
noticeContainer.addValidationNotice(
new ForbiddenGeograaphyIdNotice(
new ForbiddenGeographyIdNotice(
stopTime.csvRowNumber(),
stopTime.stopId(),
stopTime.locationGroupId(),
Expand Down
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();
}
}

0 comments on commit d9f4f7a

Please sign in to comment.