use of org.hisp.dhis.tracker.report.TrackerValidationReport 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));
}
use of org.hisp.dhis.tracker.report.TrackerValidationReport in project dhis2-core by dhis2.
the class DefaultTrackerValidationServiceTest method childEntitiesOfInvalidParentsAreStillValidated.
@Test
void childEntitiesOfInvalidParentsAreStillValidated() {
// Test shows
// the children of a tracked entity will still be validated even if it
// as a parent is invalid
TrackedEntity invalidTrackedEntity = trackedEntity();
Enrollment invalidEnrollment = enrollment();
invalidTrackedEntity.setEnrollments(enrollments(invalidEnrollment));
Event invalidEvent = event();
invalidEnrollment.setEvents(events(invalidEvent));
TrackerBundle bundle = newBundle().validationMode(ValidationMode.FULL).trackedEntities(trackedEntities(invalidTrackedEntity)).enrollments(invalidTrackedEntity.getEnrollments()).events(invalidEnrollment.getEvents()).build();
ValidationHook hook = ValidationHook.builder().validateTrackedEntity((reporter, te) -> reporter.addErrorIf(() -> invalidTrackedEntity.equals(te), te, TrackerErrorCode.E1090)).validateEnrollment((reporter, enrollment) -> reporter.addErrorIf(() -> invalidEnrollment.equals(enrollment), enrollment, TrackerErrorCode.E1069)).validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E1032)).build();
service = new DefaultTrackerValidationService(List.of(hook), Collections.emptyList());
TrackerValidationReport report = service.validate(bundle);
assertTrue(report.hasErrors());
assertEquals(3, report.getErrors().size());
assertHasError(report, TrackerErrorCode.E1090, invalidTrackedEntity);
assertHasError(report, TrackerErrorCode.E1069, invalidEnrollment);
assertHasError(report, TrackerErrorCode.E1032, invalidEvent);
assertTrue(bundle.getTrackedEntities().isEmpty());
assertTrue(bundle.getEnrollments().isEmpty());
assertTrue(bundle.getEvents().isEmpty());
}
use of org.hisp.dhis.tracker.report.TrackerValidationReport in project dhis2-core by dhis2.
the class DefaultTrackerValidationServiceConfigOrderTest method hooksAreExecutedInTrackerValidationConfigOrder.
@Test
void hooksAreExecutedInTrackerValidationConfigOrder() {
// Test that hooks declared in TrackerValidationConfig validationHooks()
// are injected
// into the TrackerValidationService. This is important since order
// matters in the current implementation.
// Note that FAIL_FAST shows that although the event is also invalid due
// to not having an orgUnit and more it
// fails due to the first failed check which is the
// PreCheckUidValidationHook
Event event = new Event();
event.setEvent("invalidUid");
TrackerBundle bundle = TrackerBundle.builder().importMode(TrackerBundleMode.VALIDATE).validationMode(ValidationMode.FAIL_FAST).skipRuleEngine(true).events(Collections.singletonList(event)).build();
TrackerValidationReport report = trackerValidationService.validate(bundle);
assertTrue(report.hasErrors());
assertEquals(1, report.getErrors().size());
assertEquals(E1048, report.getErrors().get(0).getErrorCode());
}
Aggregations