use of org.hisp.dhis.tracker.validation.hooks.AbstractTrackerDtoValidationHook in project dhis2-core by dhis2.
the class DefaultTrackerValidationServiceTest method removeOnErrorHookPreventsFurtherValidationOfInvalidEntityEvenInFullValidationMode.
@Test
void removeOnErrorHookPreventsFurtherValidationOfInvalidEntityEvenInFullValidationMode() {
// Test shows
// 1. Hooks with removeOnError==true remove invalid entities from the
// TrackerBundle to prevent
// subsequent hooks from validating it
// 2. the TrackerBundle is mutated to only contain valid DTOs after
// validation
//
// Currently, the bundle is mutated by
// 1. AbstractTrackerDtoValidationHook removes invalid DTOs if the hook
// has removeOnError == true
// 2. DefaultTrackerValidationService removes invalid DTOs if they were
// not previously removed
// by AbstractTrackerDtoValidationHook i.e. hooks having removeOnError
// == false
Event validEvent = event();
Event invalidEvent = event();
TrackerBundle bundle = newBundle().events(events(invalidEvent, validEvent)).build();
ValidationHook removeOnError = ValidationHook.builder().removeOnError(true).validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E1032)).build();
// using default AbstractTrackerDtoValidationHook behavior of
// removeOnError==false
ValidationHook doNotRemoveOnError = ValidationHook.builder().validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E9999)).build();
service = new DefaultTrackerValidationService(List.of(removeOnError, doNotRemoveOnError), Collections.emptyList());
TrackerValidationReport report = service.validate(bundle);
assertTrue(report.hasErrors());
assertEquals(1, report.getErrors().size(), "only remove on error hook should add 1 error");
assertHasError(report, TrackerErrorCode.E1032, invalidEvent);
assertFalse(bundle.getEvents().contains(invalidEvent));
assertTrue(bundle.getEvents().contains(validEvent));
}
Aggregations