Skip to content

Commit

Permalink
refactor: Replace CurrentDateTime with DateForValidation and use Loca…
Browse files Browse the repository at this point in the history
…lDate (#1636)
  • Loading branch information
bradyhunsaker authored Jan 12, 2024
1 parent 4f49949 commit 6f9aec7
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@

package org.mobilitydata.gtfsvalidator.input;

import java.time.ZonedDateTime;
import java.time.LocalDate;

/** Represents the current date and time */
public class CurrentDateTime {
/** The date to be used for validation rules. */
public class DateForValidation {

private final ZonedDateTime now;
private final LocalDate date;

public CurrentDateTime(ZonedDateTime now) {
this.now = now;
public DateForValidation(LocalDate date) {
this.date = date;
}

public ZonedDateTime getNow() {
return now;
public LocalDate getDate() {
return date;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other instanceof CurrentDateTime) {
return this.now.equals(((CurrentDateTime) other).now);
if (other instanceof DateForValidation) {
return this.date.equals(((DateForValidation) other).date);
}
return false;
}

@Override
public int hashCode() {
return now.hashCode();
return date.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.mobilitydata.gtfsvalidator.input.CountryCode;
import org.mobilitydata.gtfsvalidator.input.CurrentDateTime;
import org.mobilitydata.gtfsvalidator.input.DateForValidation;
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice;
import org.mobilitydata.gtfsvalidator.table.AnyTableLoader;
Expand All @@ -41,7 +40,7 @@
public class LoadingHelper {

private CountryCode countryCode = CountryCode.forStringOrUnknown("ca");
private ZonedDateTime currentTime = ZonedDateTime.of(2021, 1, 1, 14, 30, 0, 0, ZoneOffset.UTC);
private LocalDate dateForValidation = LocalDate.of(2021, 1, 1);

private NoticeContainer noticeContainer = new NoticeContainer();

Expand All @@ -68,7 +67,7 @@ public <X extends GtfsEntity, Y extends GtfsTableContainer<X>> Y load(
ValidationContext context =
ValidationContext.builder()
.setCountryCode(countryCode)
.setCurrentDateTime(new CurrentDateTime(currentTime))
.setDateForValidation(new DateForValidation(dateForValidation))
.build();
ValidatorProvider provider = new DefaultValidatorProvider(context, validatorLoader);
return (Y) AnyTableLoader.load(tableDescriptor, provider, in, noticeContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.google.common.collect.ImmutableMap;
import org.mobilitydata.gtfsvalidator.input.CountryCode;
import org.mobilitydata.gtfsvalidator.input.CurrentDateTime;
import org.mobilitydata.gtfsvalidator.input.DateForValidation;

/**
* A read-only context passed to particular validator objects. It gives information relevant for
Expand Down Expand Up @@ -57,8 +57,8 @@ public CountryCode countryCode() {
*
* @return The time when validation started as @code{ZonedDateTime}
*/
public CurrentDateTime currentDateTime() {
return get(CurrentDateTime.class);
public DateForValidation dateForValidation() {
return get(DateForValidation.class);
}

/** Returns a member of the context with requested class. */
Expand All @@ -82,8 +82,8 @@ public Builder setCountryCode(CountryCode countryCode) {
}

/** Sets the current time. */
public Builder setCurrentDateTime(CurrentDateTime currentDateTime) {
return set(CurrentDateTime.class, currentDateTime);
public Builder setDateForValidation(DateForValidation dateForValidation) {
return set(DateForValidation.class, dateForValidation);
}

/** Sets a member of the context with requested class. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.time.LocalDate;
import java.util.List;
import org.mobilitydata.gtfsvalidator.input.CountryCode;
import org.mobilitydata.gtfsvalidator.input.CurrentDateTime;
import org.mobilitydata.gtfsvalidator.input.DateForValidation;
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice;
import org.mobilitydata.gtfsvalidator.validator.ValidationContext;
Expand All @@ -32,7 +32,7 @@ public static InputStream toInputStream(String s) {
public static ValidationContext contextForTest() {
return ValidationContext.builder()
.setCountryCode(CountryCode.forStringOrUnknown("ca"))
.setCurrentDateTime(new CurrentDateTime(ZonedDateTime.now()))
.setDateForValidation(new DateForValidation(LocalDate.now()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,23 @@

import static com.google.common.truth.Truth.assertThat;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.LocalDate;
import org.junit.Test;

public class CurrentDateTimeTest {
private static final ZonedDateTime TEST_NOW =
ZonedDateTime.of(2021, 1, 1, 14, 30, 0, 0, ZoneOffset.UTC);
private static final ZonedDateTime OTHER_DATE_TIME =
ZonedDateTime.of(2021, 4, 1, 14, 30, 0, 0, ZoneOffset.UTC);
public class DateForValidationTest {
private static final LocalDate TEST_NOW = LocalDate.of(2021, 1, 1);
private static final LocalDate OTHER_DATE_TIME = LocalDate.of(2021, 4, 1);

@Test
public void getNow() {
CurrentDateTime currentDateTime = new CurrentDateTime(TEST_NOW);
assertThat(currentDateTime.getNow()).isEqualTo(TEST_NOW);
public void getDate() {
DateForValidation dateForValidation = new DateForValidation(TEST_NOW);
assertThat(dateForValidation.getDate()).isEqualTo(TEST_NOW);
}

@Test
public void testEquals() {
assertThat(new CurrentDateTime(TEST_NOW).equals(new CurrentDateTime(TEST_NOW))).isTrue();
assertThat(new CurrentDateTime(TEST_NOW).equals(new CurrentDateTime(OTHER_DATE_TIME)))
assertThat(new DateForValidation(TEST_NOW).equals(new DateForValidation(TEST_NOW))).isTrue();
assertThat(new DateForValidation(TEST_NOW).equals(new DateForValidation(OTHER_DATE_TIME)))
.isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@

import javax.inject.Inject;
import org.mobilitydata.gtfsvalidator.input.CountryCode;
import org.mobilitydata.gtfsvalidator.input.CurrentDateTime;
import org.mobilitydata.gtfsvalidator.input.DateForValidation;
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
import org.mobilitydata.gtfsvalidator.validator.SingleEntityValidator;

public class GtfsTestEntityValidator extends SingleEntityValidator<GtfsTestEntity> {
private final CountryCode countryCode;
private final CurrentDateTime currentDateTime;
private final DateForValidation dateForValidation;

@Inject
public GtfsTestEntityValidator(CountryCode countryCode, CurrentDateTime currentDateTime) {
public GtfsTestEntityValidator(CountryCode countryCode, DateForValidation dateForValidation) {
this.countryCode = countryCode;
this.currentDateTime = currentDateTime;
this.dateForValidation = dateForValidation;
}

@Override
Expand All @@ -39,7 +39,7 @@ public CountryCode getCountryCode() {
return countryCode;
}

public CurrentDateTime getCurrentDateTime() {
return currentDateTime;
public DateForValidation getDateForValidation() {
return dateForValidation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@

import javax.inject.Inject;
import org.mobilitydata.gtfsvalidator.input.CountryCode;
import org.mobilitydata.gtfsvalidator.input.CurrentDateTime;
import org.mobilitydata.gtfsvalidator.input.DateForValidation;
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
import org.mobilitydata.gtfsvalidator.validator.FileValidator;

public class GtfsTestSingleFileValidator extends FileValidator {

private final GtfsTestTableContainer table;
private final CountryCode countryCode;
private final CurrentDateTime currentDateTime;
private final DateForValidation dateForValidation;

@Inject
public GtfsTestSingleFileValidator(
GtfsTestTableContainer table, CountryCode countryCode, CurrentDateTime currentDateTime) {
GtfsTestTableContainer table, CountryCode countryCode, DateForValidation dateForValidation) {
this.table = table;
this.countryCode = countryCode;
this.currentDateTime = currentDateTime;
this.dateForValidation = dateForValidation;
}

@Override
Expand All @@ -47,7 +47,7 @@ public CountryCode getCountryCode() {
return countryCode;
}

public CurrentDateTime getCurrentDateTime() {
return currentDateTime;
public DateForValidation getDateForValidation() {
return dateForValidation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import javax.inject.Inject;
import org.mobilitydata.gtfsvalidator.input.CountryCode;
import org.mobilitydata.gtfsvalidator.input.CurrentDateTime;
import org.mobilitydata.gtfsvalidator.input.DateForValidation;
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
import org.mobilitydata.gtfsvalidator.table.GtfsFeedContainer;
import org.mobilitydata.gtfsvalidator.validator.FileValidator;
Expand All @@ -27,14 +27,16 @@ public class WholeFeedValidator extends FileValidator {
private final GtfsFeedContainer feedContainer;
private final CountryCode countryCode;

private final CurrentDateTime currentDateTime;
private final DateForValidation dateForValidation;

@Inject
public WholeFeedValidator(
GtfsFeedContainer feedContainer, CountryCode countryCode, CurrentDateTime currentDateTime) {
GtfsFeedContainer feedContainer,
CountryCode countryCode,
DateForValidation dateForValidation) {
this.feedContainer = feedContainer;
this.countryCode = countryCode;
this.currentDateTime = currentDateTime;
this.dateForValidation = dateForValidation;
}

@Override
Expand All @@ -48,7 +50,7 @@ public CountryCode getCountryCode() {
return countryCode;
}

public CurrentDateTime getCurrentDateTime() {
return currentDateTime;
public DateForValidation getDateForValidation() {
return dateForValidation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,22 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.LocalDate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mobilitydata.gtfsvalidator.input.CountryCode;
import org.mobilitydata.gtfsvalidator.input.CurrentDateTime;
import org.mobilitydata.gtfsvalidator.input.DateForValidation;

@RunWith(JUnit4.class)
public final class ValidationContextTest {
private static final CountryCode COUNTRY_CODE = CountryCode.forStringOrUnknown("AU");
private static final CurrentDateTime CURRENT_DATE_TIME =
new CurrentDateTime(ZonedDateTime.of(2021, 1, 1, 14, 30, 0, 0, ZoneOffset.UTC));
private static final DateForValidation CURRENT_DATE =
new DateForValidation(LocalDate.of(2021, 1, 1));
private static final ValidationContext VALIDATION_CONTEXT =
ValidationContext.builder()
.setCountryCode(COUNTRY_CODE)
.setCurrentDateTime(CURRENT_DATE_TIME)
.setDateForValidation(CURRENT_DATE)
.build();

@Test
Expand All @@ -44,14 +43,14 @@ public void get_countryCode_successful() {
}

@Test
public void get_currentDateTime_successful() {
assertThat(VALIDATION_CONTEXT.get(CurrentDateTime.class)).isEqualTo(CURRENT_DATE_TIME);
public void get_dateForValidation_successful() {
assertThat(VALIDATION_CONTEXT.get(DateForValidation.class)).isEqualTo(CURRENT_DATE);
}

@Test
public void get_unsupported_throws() {
assertThrows(
IllegalArgumentException.class, () -> VALIDATION_CONTEXT.get(ChildCurrentDateTime.class));
IllegalArgumentException.class, () -> VALIDATION_CONTEXT.get(ChildDateForValidation.class));
}

@Test
Expand All @@ -64,10 +63,10 @@ public void get_extraIntegerObject_successful() {
.isEqualTo(10);
}

private static class ChildCurrentDateTime extends CurrentDateTime {
private static class ChildDateForValidation extends DateForValidation {

public ChildCurrentDateTime(ZonedDateTime now) {
super(now);
public ChildDateForValidation(LocalDate date) {
super(date);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableList;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.LocalDate;
import org.junit.Test;
import org.mobilitydata.gtfsvalidator.input.CountryCode;
import org.mobilitydata.gtfsvalidator.input.CurrentDateTime;
import org.mobilitydata.gtfsvalidator.input.DateForValidation;
import org.mobilitydata.gtfsvalidator.table.GtfsFeedContainer;
import org.mobilitydata.gtfsvalidator.table.GtfsTableContainer.TableStatus;
import org.mobilitydata.gtfsvalidator.testgtfs.GtfsTestEntityValidator;
Expand All @@ -34,12 +33,12 @@

public class ValidatorLoaderTest {
private static final CountryCode COUNTRY_CODE = CountryCode.forStringOrUnknown("AU");
private static final CurrentDateTime CURRENT_DATE_TIME =
new CurrentDateTime(ZonedDateTime.of(2021, 1, 1, 14, 30, 0, 0, ZoneOffset.UTC));
private static final DateForValidation CURRENT_DATE =
new DateForValidation(LocalDate.of(2021, 1, 1));
private static final ValidationContext VALIDATION_CONTEXT =
ValidationContext.builder()
.setCountryCode(COUNTRY_CODE)
.setCurrentDateTime(CURRENT_DATE_TIME)
.setDateForValidation(CURRENT_DATE)
.build();

@Test
Expand All @@ -51,7 +50,7 @@ public void createValidatorWithContext_injectsContext()
.validator();

assertThat(validator.getCountryCode()).isEqualTo(VALIDATION_CONTEXT.countryCode());
assertThat(validator.getCurrentDateTime()).isEqualTo(VALIDATION_CONTEXT.currentDateTime());
assertThat(validator.getDateForValidation()).isEqualTo(VALIDATION_CONTEXT.dateForValidation());
}

@Test
Expand All @@ -65,7 +64,7 @@ public void createSingleFileValidator_injectsTableContainerAndContext()
.validator();

assertThat(validator.getCountryCode()).isEqualTo(VALIDATION_CONTEXT.countryCode());
assertThat(validator.getCurrentDateTime()).isEqualTo(VALIDATION_CONTEXT.currentDateTime());
assertThat(validator.getDateForValidation()).isEqualTo(VALIDATION_CONTEXT.dateForValidation());
assertThat(validator.getStopTable()).isEqualTo(table);
}

Expand All @@ -83,7 +82,7 @@ public void createMultiFileValidator_injectsFeedContainerAndContext()

WholeFeedValidator validator = validatorWithStatus.validator();
assertThat(validator.getCountryCode()).isEqualTo(VALIDATION_CONTEXT.countryCode());
assertThat(validator.getCurrentDateTime()).isEqualTo(VALIDATION_CONTEXT.currentDateTime());
assertThat(validator.getDateForValidation()).isEqualTo(VALIDATION_CONTEXT.dateForValidation());
assertThat(validator.getFeedContainer()).isEqualTo(feedContainer);
}

Expand Down
Loading

0 comments on commit 6f9aec7

Please sign in to comment.