From 19259fb57b99adba8bcfe37c89265f163a014c06 Mon Sep 17 00:00:00 2001 From: Bertrand Lorentz Date: Fri, 30 Aug 2024 11:33:16 +0200 Subject: [PATCH] Output validation results earlier (TEDEFO-3685) Log errors and warning after each validator is run, to know earlier when problems are found. Move the slowest validator (EFX) to the end to not delay the others. The complete list of warnings and errors is still logged at the end, as it was before, to get everyting in one place. --- .../ted/eforms/sdk/analysis/SdkAnalyzer.java | 31 +++++++++++++------ .../sdk/analysis/validator/Validator.java | 17 ++++------ .../analysis/cucumber/SdkValidationSteps.java | 12 +++---- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/main/java/eu/europa/ted/eforms/sdk/analysis/SdkAnalyzer.java b/src/main/java/eu/europa/ted/eforms/sdk/analysis/SdkAnalyzer.java index db535e2..676f056 100644 --- a/src/main/java/eu/europa/ted/eforms/sdk/analysis/SdkAnalyzer.java +++ b/src/main/java/eu/europa/ted/eforms/sdk/analysis/SdkAnalyzer.java @@ -2,8 +2,8 @@ import java.nio.file.Path; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; +import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,6 +13,7 @@ import eu.europa.ted.eforms.sdk.analysis.validator.TextValidator; import eu.europa.ted.eforms.sdk.analysis.validator.Validator; import eu.europa.ted.eforms.sdk.analysis.validator.XmlSchemaValidator; +import eu.europa.ted.eforms.sdk.analysis.vo.ValidationResult; public class SdkAnalyzer { private static final Logger logger = LoggerFactory.getLogger(SdkAnalyzer.class); @@ -22,29 +23,41 @@ private SdkAnalyzer() {} public static int analyze(final Path sdkRoot) throws Exception { logger.info("Analyzing SDK under folder [{}]", sdkRoot); - List warnings = new ArrayList<>(); - List errors = new ArrayList<>(); + List warnings = new ArrayList<>(); + List errors = new ArrayList<>(); List validators = List.of( new XmlSchemaValidator(sdkRoot), - new EfxValidator(sdkRoot), new TextValidator(sdkRoot), new SchematronValidator(sdkRoot), - new SdkValidator(sdkRoot)); + new SdkValidator(sdkRoot), + new EfxValidator(sdkRoot)); for (Validator validator : validators) { + String validatorName = validator.getClass().getSimpleName(); + logger.info("Starting validation with {}", validatorName); validator.validate(); - warnings.addAll(Arrays.asList(validator.getWarnings())); - errors.addAll(Arrays.asList(validator.getErrors())); + + Set foundWarnings = validator.getWarnings(); + if (!foundWarnings.isEmpty()) { + logger.warn("Warnings from {}:\n{}", validatorName, StringUtils.join(foundWarnings, '\n')); + } + Set foundErrors = validator.getErrors(); + if (!foundErrors.isEmpty()) { + logger.error("Errors from {}:\n{}", validatorName, StringUtils.join(foundErrors, '\n')); + } + + warnings.addAll(foundWarnings); + errors.addAll(foundErrors); } if (!warnings.isEmpty() && logger.isWarnEnabled()) { - logger.warn("Validation warnings:\n{}", StringUtils.join(warnings, '\n')); + logger.warn("All validation warnings:\n{}", StringUtils.join(warnings, '\n')); logger.warn("Total number of validation warnings: {}", warnings.size()); } if (!errors.isEmpty() && logger.isErrorEnabled()) { - logger.error("Validation errors:\n{}", StringUtils.join(errors, '\n')); + logger.error("All validation errors:\n{}", StringUtils.join(errors, '\n')); logger.error("Total number of validation errors: {}", errors.size()); } diff --git a/src/main/java/eu/europa/ted/eforms/sdk/analysis/validator/Validator.java b/src/main/java/eu/europa/ted/eforms/sdk/analysis/validator/Validator.java index 88d531c..8ccf8f8 100644 --- a/src/main/java/eu/europa/ted/eforms/sdk/analysis/validator/Validator.java +++ b/src/main/java/eu/europa/ted/eforms/sdk/analysis/validator/Validator.java @@ -4,7 +4,6 @@ import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.ArrayUtils; import eu.europa.ted.eforms.sdk.analysis.enums.ValidationStatusEnum; import eu.europa.ted.eforms.sdk.analysis.vo.ValidationResult; @@ -24,23 +23,19 @@ default Set getResults(EnumSet statuses) .collect(Collectors.toSet()); } - default String[] getWarnings() { - return getResults(EnumSet.of(ValidationStatusEnum.WARNING)).stream() - .map(ValidationResult::toString) - .toArray(String[]::new); + default Set getWarnings() { + return getResults(EnumSet.of(ValidationStatusEnum.WARNING)); } - default String[] getErrors() { - return getResults(EnumSet.of(ValidationStatusEnum.ERROR)).stream() - .map(ValidationResult::toString) - .toArray(String[]::new); + default Set getErrors() { + return getResults(EnumSet.of(ValidationStatusEnum.ERROR)); } default boolean hasWarnings() { - return ArrayUtils.isNotEmpty(getWarnings()); + return CollectionUtils.isNotEmpty(getWarnings()); } default boolean hasErrors() { - return ArrayUtils.isNotEmpty(getErrors()); + return CollectionUtils.isNotEmpty(getErrors()); } } diff --git a/src/test/java/eu/europa/ted/eforms/sdk/analysis/cucumber/SdkValidationSteps.java b/src/test/java/eu/europa/ted/eforms/sdk/analysis/cucumber/SdkValidationSteps.java index 17f40c8..ed7d5c3 100644 --- a/src/test/java/eu/europa/ted/eforms/sdk/analysis/cucumber/SdkValidationSteps.java +++ b/src/test/java/eu/europa/ted/eforms/sdk/analysis/cucumber/SdkValidationSteps.java @@ -262,32 +262,32 @@ public void rule_should_have_been_fired(String expectedRule) { @Then("^I should get (.*) SDK validation errors?$") public void i_should_get_sdk_validation_errors(int errorsCount) { - assertEquals(errorsCount, sdkValidator.getErrors().length); + assertEquals(errorsCount, sdkValidator.getErrors().size()); } @Then("^I should get (.*) SDK validation warnings?$") public void i_should_get_sdk_validation_warnings(int warningsCount) { - assertEquals(warningsCount, sdkValidator.getWarnings().length); + assertEquals(warningsCount, sdkValidator.getWarnings().size()); } @Then("^I should get (.*) EFX validation errors?$") public void i_should_get_efx_validation_errors(int errorsCount) { - assertEquals(errorsCount, efxValidator.getErrors().length); + assertEquals(errorsCount, efxValidator.getErrors().size()); } @Then("^I should get (.*) schema validation errors?$") public void i_should_get_schema_validation_errors(int errorsCount) { - assertEquals(errorsCount, schemaValidator.getErrors().length); + assertEquals(errorsCount, schemaValidator.getErrors().size()); } @Then("^I should get (.*) text validation errors?$") public void i_should_get_text_validation_errors(int errorsCount) { - assertEquals(errorsCount, textValidator.getErrors().length); + assertEquals(errorsCount, textValidator.getErrors().size()); } @Then("^I should get (.*) schematron validation errors?$") public void i_should_get_schematron_validation_errors(int errorsCount) { - assertEquals(errorsCount, schematronValidator.getErrors().length); + assertEquals(errorsCount, schematronValidator.getErrors().size()); } @Then("I should get not found exception for file {string}")