use of ddf.catalog.validation.violation.ValidationViolation in project ddf by codice.
the class RequiredAttributesMetacardValidator method validateMetacard.
@Override
public Optional<MetacardValidationReport> validateMetacard(final Metacard metacard) {
Preconditions.checkArgument(metacard != null, "The metacard cannot be null.");
final MetacardType metacardType = metacard.getMetacardType();
if (metacardTypeName.equals(metacardType.getName())) {
final Set<ValidationViolation> violations = new HashSet<>();
for (final String attributeName : requiredAttributes) {
final Attribute attribute = metacard.getAttribute(attributeName);
if (attribute != null) {
final AttributeDescriptor descriptor = metacardType.getAttributeDescriptor(attributeName);
if (descriptor.isMultiValued()) {
if (attribute.getValues().size() == 0) {
addRequiredAttributeViolation(attributeName, violations);
}
} else if (attribute.getValue() == null) {
addRequiredAttributeViolation(attributeName, violations);
}
} else {
addRequiredAttributeViolation(attributeName, violations);
}
}
if (violations.size() > 0) {
return getReport(violations);
}
}
return Optional.empty();
}
use of ddf.catalog.validation.violation.ValidationViolation in project ddf by codice.
the class ReportingMetacardValidatorImpl method validateMetacard.
@Override
public Optional<MetacardValidationReport> validateMetacard(final Metacard metacard) {
Preconditions.checkArgument(metacard != null, "The metacard cannot be null.");
final Set<ValidationViolation> violations = new HashSet<>();
for (final AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
final String attributeName = descriptor.getName();
final Attribute attribute = metacard.getAttribute(attributeName);
if (attribute != null) {
for (final AttributeValidator validator : validatorRegistry.getValidators(attributeName)) {
validator.validate(attribute).ifPresent(report -> violations.addAll(report.getAttributeValidationViolations()));
}
}
}
if (violations.size() > 0) {
return getReport(violations);
}
return Optional.empty();
}
use of ddf.catalog.validation.violation.ValidationViolation in project ddf by codice.
the class Validator method getViolationsResult.
private Map<String, ViolationResult> getViolationsResult(Set<ValidationViolation> attributeValidationViolations) {
Map<String, ViolationResult> violationsResult = new HashMap<>();
for (ValidationViolation violation : attributeValidationViolations) {
for (String attribute : violation.getAttributes()) {
if (!violationsResult.containsKey(attribute)) {
violationsResult.put(attribute, new ViolationResult());
}
ViolationResult violationResponse = violationsResult.get(attribute);
violationResponse.setAttribute(attribute);
if (ValidationViolation.Severity.ERROR.equals(violation.getSeverity())) {
violationResponse.getErrors().add(violation.getMessage());
} else if (ValidationViolation.Severity.WARNING.equals(violation.getSeverity())) {
violationResponse.getWarnings().add(violation.getMessage());
} else {
throw new RuntimeException("Unexpected Severity Level");
}
}
}
return violationsResult;
}
use of ddf.catalog.validation.violation.ValidationViolation in project ddf by codice.
the class DuplicationValidator method reportDuplicates.
private ValidationViolation reportDuplicates(final Metacard metacard, String[] attributeNames, ValidationViolation.Severity severity) {
Set<String> duplicates = new HashSet<>();
ValidationViolation violation = null;
final Set<String> uniqueAttributeNames = Stream.of(attributeNames).filter(attribute -> metacard.getAttribute(attribute) != null).collect(Collectors.toSet());
final Set<Attribute> uniqueAttributes = uniqueAttributeNames.stream().map(attribute -> metacard.getAttribute(attribute)).collect(Collectors.toSet());
if (!uniqueAttributes.isEmpty()) {
LOGGER.debug("Checking for duplicates for id {} against attributes [{}]", metacard.getId(), collectionToString(uniqueAttributeNames));
SourceResponse response = query(uniqueAttributes, metacard.getId());
if (response != null) {
response.getResults().forEach(result -> duplicates.add(result.getMetacard().getId()));
}
if (!duplicates.isEmpty()) {
violation = createViolation(uniqueAttributeNames, duplicates, severity);
LOGGER.debug(violation.getMessage());
}
}
return violation;
}
use of ddf.catalog.validation.violation.ValidationViolation in project ddf by codice.
the class TestDuplicationValidator method testValidateMetacardWithValidationErrorAndWarning.
@Test
public void testValidateMetacardWithValidationErrorAndWarning() {
String[] checksumAttribute = { Metacard.CHECKSUM };
String[] idAttribute = { Metacard.ID };
validator.setWarnOnDuplicateAttributes(checksumAttribute);
validator.setErrorOnDuplicateAttributes(idAttribute);
Optional<MetacardValidationReport> report = validator.validateMetacard(testMetacard);
assertThat(report.isPresent(), is(true));
assertThat(report.get().getMetacardValidationViolations(), hasSize(2));
Map<ValidationViolation.Severity, ValidationViolation> violations = report.get().getMetacardValidationViolations().stream().collect(Collectors.toMap(ValidationViolation::getSeverity, Function.identity()));
ValidationViolation warnViolation = violations.get(ValidationViolation.Severity.WARNING);
ValidationViolation errorViolation = violations.get(ValidationViolation.Severity.ERROR);
assertThat(warnViolation.getAttributes(), is(new HashSet<>(Arrays.asList(checksumAttribute))));
assertThat(warnViolation.getMessage(), containsString(Metacard.CHECKSUM));
assertThat(errorViolation.getAttributes(), is(new HashSet<>(Arrays.asList(idAttribute))));
assertThat(errorViolation.getMessage(), containsString(Metacard.ID));
}
Aggregations