Skip to content

Commit

Permalink
Use enum for validation profile
Browse files Browse the repository at this point in the history
  • Loading branch information
vpaturet committed Oct 21, 2024
1 parent 93e1ce7 commit b188ec2
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 71 deletions.
11 changes: 0 additions & 11 deletions src/main/java/no/entur/antu/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/no/entur/antu/config/ValidatorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
56 changes: 32 additions & 24 deletions src/main/java/no/entur/antu/validation/NetexValidationProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
*/
public class NetexValidationProfile {

private final Map<String, NetexValidatorsRunner> netexValidatorsRunners;
private final Map<ValidationProfile, NetexValidatorsRunner> netexValidatorsRunners;
private final boolean skipSchemaValidation;
private final boolean skipNetexValidators;

public NetexValidationProfile(
Map<String, NetexValidatorsRunner> netexValidatorsRunners,
Map<ValidationProfile, NetexValidatorsRunner> netexValidatorsRunners,
boolean skipSchemaValidation,
boolean skipNetexValidators
) {
Expand Down Expand Up @@ -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
);
}

/**
Expand All @@ -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;
}
}
62 changes: 62 additions & 0 deletions src/main/java/no/entur/antu/validation/ValidationProfile.java
Original file line number Diff line number Diff line change
@@ -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<ValidationProfile> findById(String validationProfile) {
return EnumSet
.allOf(ValidationProfile.class)
.stream()
.filter(element -> element.id().equalsIgnoreCase(validationProfile))
.findFirst();
}

public String id() {
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit b188ec2

Please sign in to comment.