Skip to content

Commit

Permalink
Adding unittest for UnexpectedWaitTimeAndActiveDatesValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
mansoor-sajjad committed Nov 26, 2024
1 parent a68967b commit 7e19284
Show file tree
Hide file tree
Showing 6 changed files with 1,065 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import no.entur.antu.validation.validator.interchange.duplicate.DuplicateInterchangesValidator;
import no.entur.antu.validation.validator.interchange.mandatoryfields.MandatoryFieldsValidator;
import no.entur.antu.validation.validator.interchange.stoppoints.StopPointsInVehicleJourneyValidator;
import no.entur.antu.validation.validator.interchange.waittime.UnexpectedWaitTimeValidator;
import no.entur.antu.validation.validator.interchange.waittime.UnexpectedWaitTimeAndActiveDatesValidator;
import no.entur.antu.validation.validator.journeypattern.stoppoint.distance.UnexpectedDistanceBetweenStopPointsValidator;
import no.entur.antu.validation.validator.journeypattern.stoppoint.identicalstoppoints.IdenticalStopPointsValidator;
import no.entur.antu.validation.validator.journeypattern.stoppoint.samequayref.SameQuayRefValidator;
Expand Down Expand Up @@ -260,13 +260,13 @@ public DuplicateLineNameValidator duplicateLineNameValidator(
}

@Bean
public UnexpectedWaitTimeValidator unexpectedWaitTimeValidator(
public UnexpectedWaitTimeAndActiveDatesValidator unexpectedWaitTimeValidator(
@Qualifier(
"validationReportEntryFactory"
) ValidationReportEntryFactory validationReportEntryFactory,
NetexDataRepository netexDataRepository
) {
return new UnexpectedWaitTimeValidator(
return new UnexpectedWaitTimeAndActiveDatesValidator(
validationReportEntryFactory,
netexDataRepository
);
Expand Down Expand Up @@ -303,7 +303,7 @@ public NetexValidatorsRunner timetableDataValidatorsRunner(
StopPointsInVehicleJourneyValidator stopPointsInVehicleJourneyValidator,
DuplicateLineNameValidator duplicateLineNameValidator,
MissingReplacementValidator missingReplacementValidator,
UnexpectedWaitTimeValidator unexpectedWaitTimeValidator,
UnexpectedWaitTimeAndActiveDatesValidator unexpectedWaitTimeAndActiveDatesValidator,
LineInfoCollector lineInfoCollector,
ServiceJourneyStopsCollector serviceJourneyStopsCollector,
ServiceJourneyInterchangeInfoCollector serviceJourneyInterchangeInfoCollector,
Expand Down Expand Up @@ -348,7 +348,7 @@ public NetexValidatorsRunner timetableDataValidatorsRunner(
List<DatasetValidator> netexTimetableDatasetValidators = List.of(
duplicateLineNameValidator,
stopPointsInVehicleJourneyValidator,
unexpectedWaitTimeValidator
unexpectedWaitTimeAndActiveDatesValidator
);

List<NetexDataCollector> commonDataCollectors = List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.entur.netex.validation.validator.model.ServiceJourneyInterchangeInfo;
import org.entur.netex.validation.validator.model.ServiceJourneyStop;

public record UnexpectedWaitTimeContext(
public record UnexpectedWaitTimeAndActiveDatesContext(
ServiceJourneyInterchangeInfo serviceJourneyInterchangeInfo,
// ServiceJourneyStop at the fromStopPoint in fromJourneyRef from Cache
ServiceJourneyStop fromServiceJourneyStop,
Expand Down Expand Up @@ -43,7 +43,7 @@ public Builder(
this.netexDataRepository = netexDataRepository;
}

public UnexpectedWaitTimeContext.Builder primeCache() {
public UnexpectedWaitTimeAndActiveDatesContext.Builder primeCache() {
serviceJourneyIdListMap =
netexDataRepository.serviceJourneyStops(validationReportId);
serviceJourneyDayTypesMap =
Expand All @@ -54,10 +54,10 @@ public UnexpectedWaitTimeContext.Builder primeCache() {
return this;
}

public UnexpectedWaitTimeContext build(
public UnexpectedWaitTimeAndActiveDatesContext build(
ServiceJourneyInterchangeInfo serviceJourneyInterchangeInfo
) {
return new UnexpectedWaitTimeContext(
return new UnexpectedWaitTimeAndActiveDatesContext(
serviceJourneyInterchangeInfo,
serviceJourneyStopAtScheduleStopPoint(
serviceJourneyInterchangeInfo.fromJourneyRef(),
Expand Down Expand Up @@ -126,6 +126,7 @@ private ServiceJourneyStop serviceJourneyStopAtScheduleStopPoint(
.scheduledStopPointId()
.equals(scheduledStopPointId)
)
.map(ServiceJourneyStop::fixMissingTimeValues)
.findFirst()
)
.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import no.entur.antu.validation.ValidationError;
import no.entur.antu.validation.utilities.Comparison;

public record UnexpectedWaitTimeError(
public record UnexpectedWaitTimeAndActiveDatesError(
RuleCode ruleCode,
String interchangeId,
String fromStopPointName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,25 @@
* 3-Interchange-8-2,
* 3-Interchange-10
*/
public class UnexpectedWaitTimeValidator extends AbstractDatasetValidator {
public class UnexpectedWaitTimeAndActiveDatesValidator
extends AbstractDatasetValidator {

private static final Logger LOGGER = LoggerFactory.getLogger(
UnexpectedWaitTimeValidator.class
UnexpectedWaitTimeAndActiveDatesValidator.class
);

// Marduk/Chouette config parameter: interchange_max_wait_seconds = 3600 Seconds

// Warning wait time for interchange is 1 hour
private static final int INTERCHANGE_WARNING_WAIT_TIME_MILLIS = 3600000; // 1 Hour

// Maximum wait time for interchange is 3 hours
private static final int INTERCHANGE_ERROR_WAIT_TIME_MILLIS =
INTERCHANGE_WARNING_WAIT_TIME_MILLIS * 3; // 3 Hours

private final NetexDataRepository netexDataRepository;

public UnexpectedWaitTimeValidator(
public UnexpectedWaitTimeAndActiveDatesValidator(
ValidationReportEntryFactory validationReportEntryFactory,
NetexDataRepository netexDataRepository
) {
Expand All @@ -58,8 +68,8 @@ public ValidationReport validate(ValidationReport validationReport) {
return validationReport;
}

UnexpectedWaitTimeContext.Builder builder =
new UnexpectedWaitTimeContext.Builder(
UnexpectedWaitTimeAndActiveDatesContext.Builder builder =
new UnexpectedWaitTimeAndActiveDatesContext.Builder(
validationReport.getValidationReportId(),
netexDataRepository
);
Expand All @@ -70,7 +80,7 @@ public ValidationReport validate(ValidationReport validationReport) {
.stream()
.map(builder::build)
.filter(Objects::nonNull)
.filter(UnexpectedWaitTimeContext::isValid)
.filter(UnexpectedWaitTimeAndActiveDatesContext::isValid)
.map(this::validateWaitTime)
.filter(Objects::nonNull)
.forEach(validationReport::addValidationReportEntry);
Expand All @@ -79,7 +89,7 @@ public ValidationReport validate(ValidationReport validationReport) {
}

private ValidationReportEntry validateWaitTime(
UnexpectedWaitTimeContext context
UnexpectedWaitTimeAndActiveDatesContext context
) {
long MILLIS_PER_DAY = 86400000L;

Expand All @@ -105,13 +115,6 @@ private ValidationReportEntry validateWaitTime(
dayOffsetDiff--;
}

// interchange_max_wait_seconds
int INTERCHANGE_MAX_WAIT_TIME_SECONDS = 3600;
int INTERCHANGE_WARNING_WAIT_TIME_SECONDS =
INTERCHANGE_MAX_WAIT_TIME_SECONDS * 1000;
int INTERCHANGE_ERROR_WAIT_TIME_SECONDS =
INTERCHANGE_WARNING_WAIT_TIME_SECONDS * 3;

if (!hasSharedActiveDate(context, dayOffsetDiff)) {
return createValidationReportEntry(
"NO_SHARED_ACTIVE_DATE_FOUND_IN_INTERCHANGE",
Expand All @@ -121,11 +124,11 @@ private ValidationReportEntry validateWaitTime(
context.toServiceJourneyStop(),
Comparison.of(
String.valueOf(msWait / 1000),
String.valueOf(INTERCHANGE_ERROR_WAIT_TIME_SECONDS / 1000)
String.valueOf(INTERCHANGE_ERROR_WAIT_TIME_MILLIS / 1000)
)
);
} else if (msWait > INTERCHANGE_WARNING_WAIT_TIME_SECONDS) {
if (msWait > INTERCHANGE_ERROR_WAIT_TIME_SECONDS) {
} else if (msWait > INTERCHANGE_WARNING_WAIT_TIME_MILLIS) {
if (msWait > INTERCHANGE_ERROR_WAIT_TIME_MILLIS) {
return createValidationReportEntry(
"WAIT_TIME_IN_INTERCHANGE_EXCEEDS_MAX_LIMIT",
context.serviceJourneyInterchangeInfo().interchangeId(),
Expand All @@ -134,7 +137,7 @@ private ValidationReportEntry validateWaitTime(
context.toServiceJourneyStop(),
Comparison.of(
String.valueOf(msWait / 1000),
String.valueOf(INTERCHANGE_ERROR_WAIT_TIME_SECONDS / 1000)
String.valueOf(INTERCHANGE_ERROR_WAIT_TIME_MILLIS / 1000)
)
);
} else {
Expand All @@ -146,7 +149,7 @@ private ValidationReportEntry validateWaitTime(
context.toServiceJourneyStop(),
Comparison.of(
String.valueOf(msWait / 1000),
String.valueOf(INTERCHANGE_WARNING_WAIT_TIME_SECONDS / 1000)
String.valueOf(INTERCHANGE_WARNING_WAIT_TIME_MILLIS / 1000)
)
);
}
Expand All @@ -155,7 +158,7 @@ private ValidationReportEntry validateWaitTime(
}

private boolean hasSharedActiveDate(
UnexpectedWaitTimeContext context,
UnexpectedWaitTimeAndActiveDatesContext context,
int daysOffset
) {
List<LocalDate> fromServiceJourneyActiveDates =
Expand Down Expand Up @@ -186,7 +189,7 @@ private ValidationReportEntry createValidationReportEntry(
ruleCode,
new DataLocation(interchangeId, filename, 0, 0),
String.format(
"Wait time between stops (%s) and (%s) is expected %s but was %s.",
"Wait time between stops (%s) and (%s) is expected %s sec. but was %s sec.",
fromJourneyStop.scheduledStopPointId(),
toJourneyStop.scheduledStopPointId(),
comparison.expected(),
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/no/entur/antu/validation/ValidationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ protected void mockGetServiceJourneyStops(
.thenReturn(serviceJourneyStops);
}

protected void mockGetServiceJourneyDayTypes(
Map<ServiceJourneyId, List<DayTypeId>> serviceJourneyDayTypes
) {
Mockito
.when(netexDataRepositoryMock.serviceJourneyDayTypes(anyString()))
.thenReturn(serviceJourneyDayTypes);
}

protected void mockGetServiceJourneyOperatingDays(
Map<ServiceJourneyId, List<OperatingDayId>> serviceJourneyOperatingDays
) {
Mockito
.when(netexDataRepositoryMock.serviceJourneyOperatingDays(anyString()))
.thenReturn(serviceJourneyOperatingDays);
}

protected void mockGetActiveDays(
Map<ActiveDatesId, ActiveDates> activeDates
) {
Mockito
.when(netexDataRepositoryMock.activeDates(anyString()))
.thenReturn(activeDates);
}

protected void mockGetServiceJourneyInterchangeInfo(
List<ServiceJourneyInterchangeInfo> serviceJourneyInterchangeInfos
) {
Expand Down
Loading

0 comments on commit 7e19284

Please sign in to comment.