Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output validation results earlier (TEDEFO-3685) #61

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/main/java/eu/europa/ted/eforms/sdk/analysis/SdkAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -22,29 +23,41 @@ private SdkAnalyzer() {}
public static int analyze(final Path sdkRoot) throws Exception {
logger.info("Analyzing SDK under folder [{}]", sdkRoot);

List<String> warnings = new ArrayList<>();
List<String> errors = new ArrayList<>();
List<ValidationResult> warnings = new ArrayList<>();
List<ValidationResult> errors = new ArrayList<>();

List<Validator> 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<ValidationResult> foundWarnings = validator.getWarnings();
if (!foundWarnings.isEmpty()) {
logger.warn("Warnings from {}:\n{}", validatorName, StringUtils.join(foundWarnings, '\n'));
}
Set<ValidationResult> 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());
}

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

Expand All @@ -24,23 +23,19 @@ default Set<ValidationResult> getResults(EnumSet<ValidationStatusEnum> statuses)
.collect(Collectors.toSet());
}

default String[] getWarnings() {
return getResults(EnumSet.of(ValidationStatusEnum.WARNING)).stream()
.map(ValidationResult::toString)
.toArray(String[]::new);
default Set<ValidationResult> 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<ValidationResult> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down