Skip to content

Commit

Permalink
Merge pull request #3 from naviqore/feature/reader-error-handing
Browse files Browse the repository at this point in the history
Feature/reader error handing
  • Loading branch information
munterfi authored Apr 27, 2024
2 parents 0e8781d + a9e1180 commit f004c27
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/main/java/ch/naviqore/gtfs/schedule/GtfsScheduleFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public enum GtfsScheduleFile {
AGENCY("agency.txt"),
CALENDAR("calendar.txt"),
CALENDAR_DATES("calendar_dates.txt"),
FARE_ATTRIBUTES("fare_attributes.txt"),
FARE_RULES("fare_rules.txt"),
FREQUENCIES("frequencies.txt"),
// FARE_ATTRIBUTES("fare_attributes.txt"),
// FARE_RULES("fare_rules.txt"),
// FREQUENCIES("frequencies.txt"),
STOPS("stops.txt"),
ROUTES("routes.txt"),
SHAPES("shapes.txt"),
// SHAPES("shapes.txt"),
TRIPS("trips.txt"),
STOP_TIMES("stop_times.txt");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

/**
Expand All @@ -38,13 +37,8 @@ public GtfsScheduleParser(GtfsScheduleBuilder builder) {
}

public void parse(CSVRecord record, GtfsScheduleFile fileType) {
Set<GtfsScheduleFile> warnings = EnumSet.noneOf(GtfsScheduleFile.class);
parsers.getOrDefault(fileType, r -> {
if (!warnings.contains(fileType)) {
log.warn("Unsupported GTFS file type for parsing: {}", fileType);
} else {
warnings.add(fileType);
}
throw new IllegalArgumentException("Unsupported GTFS file type for parsing: " + fileType);
}).accept(record);
}

Expand Down
31 changes: 14 additions & 17 deletions src/test/java/ch/naviqore/gtfs/schedule/model/GtfsScheduleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

class GtfsScheduleTest {

private static final LocalDate START_DATE = LocalDate.of(2024, Month.APRIL, 1);
private static final LocalDate END_DATE = START_DATE.plusMonths(1);
private static final LocalDateTime WEEKDAY_8_AM = LocalDateTime.of(2024, Month.APRIL, 26, 8, 0);
private static final LocalDateTime WEEKDAY_9_AM = WEEKDAY_8_AM.plusHours(1);
private static final LocalDateTime SATURDAY_9_AM = LocalDateTime.of(2024, Month.APRIL, 27, 9, 0);

private GtfsSchedule schedule;

@BeforeEach
Expand All @@ -31,8 +37,7 @@ void setUp() {
.addRoute("route1", "agency1", "101", "Main Line", RouteType.BUS)
.addRoute("route2", "agency1", "102", "Cross Town", RouteType.BUS)
.addRoute("route3", "agency1", "103", "Circulator", RouteType.BUS)
.addCalendar("weekdays", EnumSet.range(DayOfWeek.MONDAY, DayOfWeek.FRIDAY), LocalDate.now(),
LocalDate.now().plusMonths(1))
.addCalendar("weekdays", EnumSet.range(DayOfWeek.MONDAY, DayOfWeek.FRIDAY), START_DATE, END_DATE)
.addCalendar("weekends", EnumSet.of(DayOfWeek.SATURDAY), LocalDate.now(), LocalDate.now().plusMonths(1))
.addTrip("trip1", "route1", "weekdays")
.addTrip("trip2", "route2", "weekdays")
Expand Down Expand Up @@ -96,29 +101,22 @@ class NextDepartures {

@Test
void shouldReturnNextDeparturesOnWeekday() {
// 8:00 AM on a weekday
LocalDateTime now = LocalDateTime.of(2024, Month.APRIL, 26, 8, 0);
assertThat(schedule.getNextDepartures("stop1", now, Integer.MAX_VALUE)).hasSize(1);
assertThat(schedule.getNextDepartures("stop1", WEEKDAY_8_AM, Integer.MAX_VALUE)).hasSize(1);
}

@Test
void shouldReturnNoNextDeparturesOnWeekday() {
// 9:00 AM on a weekday
LocalDateTime now = LocalDateTime.of(2024, Month.APRIL, 26, 9, 0);
assertThat(schedule.getNextDepartures("stop1", now, Integer.MAX_VALUE)).isEmpty();
assertThat(schedule.getNextDepartures("stop1", WEEKDAY_9_AM, Integer.MAX_VALUE)).isEmpty();
}

@Test
void shouldReturnNextDeparturesOnSaturday() {
// 9:00 AM on a Saturday
LocalDateTime now = LocalDateTime.of(2024, Month.APRIL, 27, 8, 0);
assertThat(schedule.getNextDepartures("stop1", now, Integer.MAX_VALUE)).hasSize(1);
assertThat(schedule.getNextDepartures("stop1", SATURDAY_9_AM, Integer.MAX_VALUE)).hasSize(1);
}

@Test
void shouldReturnNoDeparturesFromUnknownStop() {
LocalDateTime now = LocalDateTime.of(2024, Month.APRIL, 26, 10, 0);
assertThatThrownBy(() -> schedule.getNextDepartures("unknown", now, 1)).isInstanceOf(
assertThatThrownBy(() -> schedule.getNextDepartures("unknown", LocalDateTime.now(), 1)).isInstanceOf(
IllegalArgumentException.class).hasMessage("Stop unknown not found");
}
}
Expand All @@ -128,22 +126,21 @@ class ActiveTrips {

@Test
void shouldReturnActiveTripsOnWeekday() {
assertThat(schedule.getActiveTrips(LocalDate.now())).hasSize(2)
assertThat(schedule.getActiveTrips(WEEKDAY_8_AM.toLocalDate())).hasSize(2)
.extracting("id")
.containsOnly("trip1", "trip2");
}

@Test
void shouldReturnActiveTripsOnWeekend() {
assertThat(schedule.getActiveTrips(LocalDate.now().with(DayOfWeek.SATURDAY))).hasSize(1)
assertThat(schedule.getActiveTrips(SATURDAY_9_AM.toLocalDate())).hasSize(1)
.extracting("id")
.containsOnly("trip3");
}

@Test
void shouldReturnNoActiveTripsForNonServiceDay() {
LocalDate nonServiceDay = LocalDate.now().with(DayOfWeek.SUNDAY).plusWeeks(2);
assertThat(schedule.getActiveTrips(nonServiceDay)).isEmpty();
assertThat(schedule.getActiveTrips(END_DATE.plusMonths(1))).isEmpty();
}
}
}

0 comments on commit f004c27

Please sign in to comment.