diff --git a/src/main/java/no/entur/antu/Constants.java b/src/main/java/no/entur/antu/Constants.java index e00bd617..220968a6 100644 --- a/src/main/java/no/entur/antu/Constants.java +++ b/src/main/java/no/entur/antu/Constants.java @@ -95,17 +95,6 @@ public final class Constants { public static final String VALIDATION_CLIENT_MARDUK = "Marduk"; public static final String VALIDATION_CLIENT_KAKKA = "Kakka"; - public static final String VALIDATION_PROFILE_TIMETABLE = "Timetable"; - public static final String VALIDATION_PROFILE_TIMETABLE_FLEX = - "TimetableFlexibleTransport"; - public static final String VALIDATION_PROFILE_IMPORT_TIMETABLE_FLEX = - "ImportTimetableFlexibleTransport"; - public static final String VALIDATION_PROFILE_TIMETABLE_FLEX_MERGING = - "TimetableFlexibleTransportMerging"; - public static final String VALIDATION_PROFILE_TIMETABLE_SWEDEN = - "TimetableSweden"; - public static final String VALIDATION_PROFILE_STOP = "Stop"; - public static final String CAMEL_ALL_HTTP_HEADERS = "CamelHttp*"; public static final String VALIDATION_REPORT_PREFIX = "/validation-report-"; public static final String VALIDATION_REPORT_SUFFIX = ".json"; diff --git a/src/main/java/no/entur/antu/config/ValidatorConfig.java b/src/main/java/no/entur/antu/config/ValidatorConfig.java index 2850ab90..745ebd34 100644 --- a/src/main/java/no/entur/antu/config/ValidatorConfig.java +++ b/src/main/java/no/entur/antu/config/ValidatorConfig.java @@ -16,12 +16,7 @@ package no.entur.antu.config; -import static no.entur.antu.Constants.VALIDATION_PROFILE_IMPORT_TIMETABLE_FLEX; -import static no.entur.antu.Constants.VALIDATION_PROFILE_STOP; -import static no.entur.antu.Constants.VALIDATION_PROFILE_TIMETABLE; -import static no.entur.antu.Constants.VALIDATION_PROFILE_TIMETABLE_FLEX; -import static no.entur.antu.Constants.VALIDATION_PROFILE_TIMETABLE_FLEX_MERGING; -import static no.entur.antu.Constants.VALIDATION_PROFILE_TIMETABLE_SWEDEN; +import static no.entur.antu.validation.ValidationProfile.*; import java.util.ArrayList; import java.util.List; @@ -193,17 +188,17 @@ public NetexValidationProfile netexValidationProfile( ) { return new NetexValidationProfile( Map.of( - VALIDATION_PROFILE_TIMETABLE, + TIMETABLE, timetableDataValidatorsRunner, - VALIDATION_PROFILE_TIMETABLE_FLEX, + TIMETABLE_FLEXIBLE_TRANSPORT, flexTimetableDataValidatorsRunner, - VALIDATION_PROFILE_IMPORT_TIMETABLE_FLEX, + IMPORT_TIMETABLE_FLEXIBLE_TRANSPORT, importFlexTimetableDataValidatorsRunner, - VALIDATION_PROFILE_TIMETABLE_FLEX_MERGING, + TIMETABLE_FLEXIBLE_TRANSPORT_MERGING, flexMergingTimetableDataValidatorsRunner, - VALIDATION_PROFILE_TIMETABLE_SWEDEN, + TIMETABLE_SWEDEN, timetableSwedenDataValidatorsRunner, - VALIDATION_PROFILE_STOP, + STOP, stopDataValidatorsRunner ), skipSchemaValidation, diff --git a/src/main/java/no/entur/antu/validation/NetexValidationProfile.java b/src/main/java/no/entur/antu/validation/NetexValidationProfile.java index eb81e469..5a0d3eaa 100644 --- a/src/main/java/no/entur/antu/validation/NetexValidationProfile.java +++ b/src/main/java/no/entur/antu/validation/NetexValidationProfile.java @@ -12,12 +12,12 @@ */ public class NetexValidationProfile { - private final Map netexValidatorsRunners; + private final Map netexValidatorsRunners; private final boolean skipSchemaValidation; private final boolean skipNetexValidators; public NetexValidationProfile( - Map netexValidatorsRunners, + Map netexValidatorsRunners, boolean skipSchemaValidation, boolean skipNetexValidators ) { @@ -50,24 +50,18 @@ public ValidationReport validate( if (codespace == null) { throw new AntuException("Missing codespace"); } - NetexValidatorsRunner netexValidatorsRunner = netexValidatorsRunners.get( + NetexValidatorsRunner netexValidatorsRunner = getNetexValidatorsRunner( validationProfile ); - if (netexValidatorsRunner == null) { - throw new AntuException( - "Unknown validation profile " + validationProfile - ); - } else { - return netexValidatorsRunner.validate( - codespace, - validationReportId, - filename, - fileContent, - skipSchemaValidation, - skipNetexValidators, - netexValidationProgressCallBack - ); - } + return netexValidatorsRunner.validate( + codespace, + validationReportId, + filename, + fileContent, + skipSchemaValidation, + skipNetexValidators, + netexValidationProgressCallBack + ); } /** @@ -86,18 +80,32 @@ public ValidationReport validateDataset( if (validationProfile == null) { throw new AntuException("Missing validation profile"); } - NetexValidatorsRunner netexValidatorsRunner = netexValidatorsRunners.get( + NetexValidatorsRunner netexValidatorsRunner = getNetexValidatorsRunner( validationProfile ); + + return netexValidatorsRunner.runNetexDatasetValidators( + validationReport, + netexValidationProgressCallBack + ); + } + + private NetexValidatorsRunner getNetexValidatorsRunner( + String validationProfile + ) { + ValidationProfile profile = ValidationProfile + .findById(validationProfile) + .orElseThrow(() -> + new AntuException("Unknown validation profile: " + validationProfile) + ); + NetexValidatorsRunner netexValidatorsRunner = netexValidatorsRunners.get( + profile + ); if (netexValidatorsRunner == null) { throw new AntuException( "Unknown validation profile " + validationProfile ); - } else { - return netexValidatorsRunner.runNetexDatasetValidators( - validationReport, - netexValidationProgressCallBack - ); } + return netexValidatorsRunner; } } diff --git a/src/main/java/no/entur/antu/validation/ValidationProfile.java b/src/main/java/no/entur/antu/validation/ValidationProfile.java new file mode 100644 index 00000000..31a8982a --- /dev/null +++ b/src/main/java/no/entur/antu/validation/ValidationProfile.java @@ -0,0 +1,62 @@ +package no.entur.antu.validation; + +import java.util.EnumSet; +import java.util.Objects; +import java.util.Optional; + +/** + * A validation profile is a collection of validation rules customized for a specific validation + * use case. + */ +public enum ValidationProfile { + /** + * Default profile for timetable data. + */ + TIMETABLE("Timetable"), + + /** + * Profile for flexible lines created in NPlan. + */ + TIMETABLE_FLEXIBLE_TRANSPORT("TimetableFlexibleTransport"), + + /** + * Profile for flexible lines imported through the operator portal. + */ + IMPORT_TIMETABLE_FLEXIBLE_TRANSPORT("ImportTimetableFlexibleTransport"), + + /** + * Profile for validating a dataset that contains both static data and flex data. + */ + TIMETABLE_FLEXIBLE_TRANSPORT_MERGING("TimetableFlexibleTransportMerging"), + + /** + * Profile for validating swedish timetable data. + */ + TIMETABLE_SWEDEN("TimetableSweden"), + + /** + * Profile for stop dataset. + */ + STOP("Stop"); + + private final String id; + + ValidationProfile(String id) { + this.id = Objects.requireNonNull(id); + } + + /** + * Return a ValidationProfile for a given name if it exists. + */ + public static Optional findById(String validationProfile) { + return EnumSet + .allOf(ValidationProfile.class) + .stream() + .filter(element -> element.id().equalsIgnoreCase(validationProfile)) + .findFirst(); + } + + public String id() { + return id; + } +} diff --git a/src/test/java/no/entur/antu/routes/validation/InitValidationRouteBuilderTest.java b/src/test/java/no/entur/antu/routes/validation/InitValidationRouteBuilderTest.java index 3b5bee49..65e27015 100644 --- a/src/test/java/no/entur/antu/routes/validation/InitValidationRouteBuilderTest.java +++ b/src/test/java/no/entur/antu/routes/validation/InitValidationRouteBuilderTest.java @@ -43,13 +43,13 @@ import static no.entur.antu.Constants.VALIDATION_CLIENT_KAKKA; import static no.entur.antu.Constants.VALIDATION_CLIENT_MARDUK; import static no.entur.antu.Constants.VALIDATION_CORRELATION_ID_HEADER; -import static no.entur.antu.Constants.VALIDATION_PROFILE_STOP; -import static no.entur.antu.Constants.VALIDATION_PROFILE_TIMETABLE; import static no.entur.antu.Constants.VALIDATION_REPORT_ID_HEADER; import static no.entur.antu.Constants.VALIDATION_REPORT_PREFIX; import static no.entur.antu.Constants.VALIDATION_REPORT_SUFFIX; import static no.entur.antu.Constants.VALIDATION_STAGE_HEADER; import static no.entur.antu.util.TestValidationReportUtil.getValidationReport; +import static no.entur.antu.validation.ValidationProfile.STOP; +import static no.entur.antu.validation.ValidationProfile.TIMETABLE; import java.io.InputStream; import java.util.HashMap; @@ -155,10 +155,7 @@ void testValidateAuthority() throws Exception { VALIDATION_STAGE_PREVALIDATION ); headers.put(Constants.VALIDATION_CLIENT_HEADER, VALIDATION_CLIENT_MARDUK); - headers.put( - Constants.VALIDATION_PROFILE_HEADER, - VALIDATION_PROFILE_TIMETABLE - ); + headers.put(Constants.VALIDATION_PROFILE_HEADER, TIMETABLE.id()); initDatasetValidation.sendBodyAndHeaders(" ", headers); notifyStatus.assertIsSatisfied(); Assertions.assertTrue( @@ -282,7 +279,7 @@ void testValidateStopPlaceData() throws Exception { VALIDATION_STAGE_PREVALIDATION ); headers.put(Constants.VALIDATION_CLIENT_HEADER, VALIDATION_CLIENT_KAKKA); - headers.put(Constants.VALIDATION_PROFILE_HEADER, VALIDATION_PROFILE_STOP); + headers.put(Constants.VALIDATION_PROFILE_HEADER, STOP.id()); initDatasetValidation.sendBodyAndHeaders(" ", headers); notifyStatus.assertIsSatisfied(); Assertions.assertTrue( @@ -423,10 +420,7 @@ void testValidateSchemaMoreThanMaxError() throws Exception { VALIDATION_STAGE_PREVALIDATION ); headers.put(Constants.VALIDATION_CLIENT_HEADER, VALIDATION_CLIENT_MARDUK); - headers.put( - Constants.VALIDATION_PROFILE_HEADER, - VALIDATION_PROFILE_TIMETABLE - ); + headers.put(Constants.VALIDATION_PROFILE_HEADER, TIMETABLE.id()); initDatasetValidation.sendBodyAndHeaders(" ", headers); notifyStatus.assertIsSatisfied(); @@ -528,10 +522,7 @@ void testValidateNoDuplicatedId() throws Exception { VALIDATION_STAGE_PREVALIDATION ); headers.put(Constants.VALIDATION_CLIENT_HEADER, VALIDATION_CLIENT_MARDUK); - headers.put( - Constants.VALIDATION_PROFILE_HEADER, - VALIDATION_PROFILE_TIMETABLE - ); + headers.put(Constants.VALIDATION_PROFILE_HEADER, TIMETABLE.id()); initDatasetValidation.sendBodyAndHeaders(" ", headers); notifyStatus.assertIsSatisfied(); @@ -655,10 +646,7 @@ void testValidateDuplicatedId() throws Exception { VALIDATION_STAGE_PREVALIDATION ); headers.put(Constants.VALIDATION_CLIENT_HEADER, VALIDATION_CLIENT_MARDUK); - headers.put( - Constants.VALIDATION_PROFILE_HEADER, - VALIDATION_PROFILE_TIMETABLE - ); + headers.put(Constants.VALIDATION_PROFILE_HEADER, TIMETABLE.id()); initDatasetValidation.sendBodyAndHeaders(" ", headers); notifyStatus.assertIsSatisfied(); diff --git a/src/test/java/no/entur/antu/routes/validation/SwedenDatasetValidationTest.java b/src/test/java/no/entur/antu/routes/validation/SwedenDatasetValidationTest.java index 86cc5a90..ad75c135 100644 --- a/src/test/java/no/entur/antu/routes/validation/SwedenDatasetValidationTest.java +++ b/src/test/java/no/entur/antu/routes/validation/SwedenDatasetValidationTest.java @@ -42,11 +42,11 @@ import static no.entur.antu.Constants.VALIDATION_CLIENT_HEADER; import static no.entur.antu.Constants.VALIDATION_CLIENT_MARDUK; import static no.entur.antu.Constants.VALIDATION_CORRELATION_ID_HEADER; -import static no.entur.antu.Constants.VALIDATION_PROFILE_TIMETABLE_SWEDEN; import static no.entur.antu.Constants.VALIDATION_REPORT_ID_HEADER; import static no.entur.antu.Constants.VALIDATION_REPORT_PREFIX; import static no.entur.antu.Constants.VALIDATION_REPORT_SUFFIX; import static no.entur.antu.Constants.VALIDATION_STAGE_HEADER; +import static no.entur.antu.validation.ValidationProfile.TIMETABLE_SWEDEN; import java.io.InputStream; import java.util.HashMap; @@ -151,10 +151,7 @@ void testValidateDataset() throws Exception { VALIDATION_STAGE_PREVALIDATION ); headers.put(Constants.VALIDATION_CLIENT_HEADER, VALIDATION_CLIENT_MARDUK); - headers.put( - Constants.VALIDATION_PROFILE_HEADER, - VALIDATION_PROFILE_TIMETABLE_SWEDEN - ); + headers.put(Constants.VALIDATION_PROFILE_HEADER, TIMETABLE_SWEDEN.id()); initDatasetValidation.sendBodyAndHeaders(" ", headers); notifyStatus.assertIsSatisfied(); Assertions.assertTrue(