use of org.hisp.dhis.tracker.report.ValidationErrorReporter in project dhis2-core by dhis2.
the class EnrollmentAttributeValidationHookTest method shouldPassValidationWhenValueIsNullAndAttributeIsNotMandatory.
@Test
void shouldPassValidationWhenValueIsNullAndAttributeIsNotMandatory() {
// given 1 attribute has null value
Attribute attribute = Attribute.builder().attribute(trackedAttribute).valueType(ValueType.TEXT).value("value").build();
Attribute attribute1 = Attribute.builder().attribute(trackedAttribute1).valueType(ValueType.TEXT).build();
// when only 1 tracked attributes is mandatory
when(program.getProgramAttributes()).thenReturn(Arrays.asList(new ProgramTrackedEntityAttribute(program, trackedEntityAttribute, false, true), new ProgramTrackedEntityAttribute(program, trackedEntityAttribute1, false, false)));
when(enrollment.getAttributes()).thenReturn(Arrays.asList(attribute, attribute1));
when(trackedEntityInstance.getTrackedEntityAttributeValues()).thenReturn(new HashSet<>(Arrays.asList(new TrackedEntityAttributeValue(trackedEntityAttribute, trackedEntityInstance), new TrackedEntityAttributeValue(trackedEntityAttribute1, trackedEntityInstance))));
when(preheat.getTrackedEntity(TrackerIdScheme.UID, enrollment.getTrackedEntity())).thenReturn(trackedEntityInstance);
ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
hookToTest.validateEnrollment(reporter, enrollment);
assertThat(reporter.getReportList(), hasSize(0));
}
use of org.hisp.dhis.tracker.report.ValidationErrorReporter in project dhis2-core by dhis2.
the class DefaultTrackerValidationService method validate.
private TrackerValidationReport validate(TrackerBundle bundle, List<TrackerValidationHook> hooks) {
TrackerValidationReport validationReport = new TrackerValidationReport();
User user = bundle.getUser();
if ((user == null || user.isSuper()) && ValidationMode.SKIP == bundle.getValidationMode()) {
log.warn("Skipping validation for metadata import by user '" + bundle.getUsername() + "'. Not recommended.");
return validationReport;
}
// Note that the bundle gets cloned internally, so the original bundle
// is always available
TrackerImportValidationContext context = new TrackerImportValidationContext(bundle);
ValidationErrorReporter reporter = new ValidationErrorReporter(context);
try {
for (TrackerValidationHook hook : hooks) {
Timer hookTimer = Timer.startTimer();
hook.validate(reporter, context);
validationReport.addTiming(new Timing(hook.getClass().getName(), hookTimer.toString()));
}
} catch (ValidationFailFastException e) {
// exit early when in FAIL_FAST validation mode
}
validationReport.addErrors(reporter.getReportList()).addWarnings(reporter.getWarningsReportList());
removeInvalidObjects(bundle, reporter);
return validationReport;
}
use of org.hisp.dhis.tracker.report.ValidationErrorReporter in project dhis2-core by dhis2.
the class EventDataValuesValidationHookTest method failsOnActiveEventWithDataElementValueNullAndValidationStrategyOnUpdate.
@Test
void failsOnActiveEventWithDataElementValueNullAndValidationStrategyOnUpdate() {
setUpIdentifiers();
DataElement validDataElement = dataElement();
when(context.getDataElement(dataElementUid)).thenReturn(validDataElement);
ProgramStage programStage = programStage(validDataElement, true);
programStage.setValidationStrategy(ValidationStrategy.ON_UPDATE_AND_INSERT);
when(context.getProgramStage(programStageUid)).thenReturn(programStage);
ValidationErrorReporter reporter = new ValidationErrorReporter(context);
DataValue validDataValue = dataValue();
validDataValue.setValue(null);
Event event = Event.builder().programStage(programStage.getUid()).status(EventStatus.ACTIVE).dataValues(Set.of(validDataValue)).build();
hook.validateEvent(reporter, event);
assertThat(reporter.getReportList(), hasSize(1));
assertEquals(TrackerErrorCode.E1076, reporter.getReportList().get(0).getErrorCode());
}
use of org.hisp.dhis.tracker.report.ValidationErrorReporter in project dhis2-core by dhis2.
the class EventDataValuesValidationHookTest method failsOnCompletedEventWithDataElementValueIsNullAndValidationStrategyOnComplete.
@Test
void failsOnCompletedEventWithDataElementValueIsNullAndValidationStrategyOnComplete() {
setUpIdentifiers();
DataElement validDataElement = dataElement();
when(context.getDataElement(dataElementUid)).thenReturn(validDataElement);
ProgramStage programStage = programStage(validDataElement, true);
programStage.setValidationStrategy(ValidationStrategy.ON_COMPLETE);
when(context.getProgramStage(programStageUid)).thenReturn(programStage);
ValidationErrorReporter reporter = new ValidationErrorReporter(context);
DataValue validDataValue = dataValue();
validDataValue.setValue(null);
Event event = Event.builder().programStage(programStage.getUid()).status(EventStatus.COMPLETED).dataValues(Set.of(validDataValue)).build();
hook.validateEvent(reporter, event);
assertThat(reporter.getReportList(), hasSize(1));
assertEquals(TrackerErrorCode.E1076, reporter.getReportList().get(0).getErrorCode());
}
use of org.hisp.dhis.tracker.report.ValidationErrorReporter in project dhis2-core by dhis2.
the class EventDataValuesValidationHookTest method failValidationWhenUpdatedAtIsNull.
@Test
void failValidationWhenUpdatedAtIsNull() {
setUpIdentifiers();
DataElement dataElement = dataElement();
when(context.getDataElement(dataElementUid)).thenReturn(dataElement);
ProgramStage programStage = programStage(dataElement);
when(context.getProgramStage(programStageUid)).thenReturn(programStage);
ValidationErrorReporter reporter = new ValidationErrorReporter(context);
DataValue validDataValue = dataValue();
validDataValue.setUpdatedAt(null);
Event event = Event.builder().programStage(programStage.getUid()).status(EventStatus.SKIPPED).dataValues(Set.of(validDataValue)).build();
hook.validateEvent(reporter, event);
assertFalse(reporter.hasErrors());
}
Aggregations