Search in sources :

Example 1 with ValidationMode

use of org.hisp.dhis.tracker.ValidationMode in project dhis2-core by dhis2.

the class DefaultTrackerValidationServiceTest method failFastModePreventsFurtherValidationAfterFirstErrorIsAdded.

@Test
void failFastModePreventsFurtherValidationAfterFirstErrorIsAdded() {
    Event validEvent = event();
    Event invalidEvent = event();
    TrackerBundle bundle = newBundle().validationMode(ValidationMode.FAIL_FAST).events(events(invalidEvent, validEvent)).build();
    ValidationHook hook1 = ValidationHook.builder().removeOnError(false).validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E1032)).build();
    TrackerValidationHook hook2 = mock(TrackerValidationHook.class);
    service = new DefaultTrackerValidationService(List.of(hook1, hook2), Collections.emptyList());
    TrackerValidationReport report = service.validate(bundle);
    assertTrue(report.hasErrors());
    assertHasError(report, TrackerErrorCode.E1032, invalidEvent);
    assertFalse(bundle.getEvents().contains(invalidEvent));
    assertTrue(bundle.getEvents().contains(validEvent));
    verifyNoInteractions(hook2);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AssertTrackerValidationReport.assertHasWarning(org.hisp.dhis.tracker.validation.hooks.AssertTrackerValidationReport.assertHasWarning) BiConsumer(java.util.function.BiConsumer) User(org.hisp.dhis.user.User) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Event(org.hisp.dhis.tracker.domain.Event) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ValidationMode(org.hisp.dhis.tracker.ValidationMode) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) AbstractTrackerDtoValidationHook(org.hisp.dhis.tracker.validation.hooks.AbstractTrackerDtoValidationHook) Mockito.verify(org.mockito.Mockito.verify) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Builder(lombok.Builder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TrackerImportStrategy(org.hisp.dhis.tracker.TrackerImportStrategy) TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport) AssertTrackerValidationReport.assertHasError(org.hisp.dhis.tracker.validation.hooks.AssertTrackerValidationReport.assertHasError) CodeGenerator(org.hisp.dhis.common.CodeGenerator) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport) Event(org.hisp.dhis.tracker.domain.Event) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) AbstractTrackerDtoValidationHook(org.hisp.dhis.tracker.validation.hooks.AbstractTrackerDtoValidationHook) Test(org.junit.jupiter.api.Test)

Example 2 with ValidationMode

use of org.hisp.dhis.tracker.ValidationMode in project dhis2-core by dhis2.

the class DefaultTrackerValidationServiceTest method fullValidationModeAddsAllErrorsToReport.

@Test
void fullValidationModeAddsAllErrorsToReport() {
    // Test shows
    // in ValidationMode==FULL all hooks are called even with entities that
    // are already invalid (i.e. have an error
    // in the validation report)
    Event validEvent = event();
    Event invalidEvent = event();
    TrackerBundle bundle = newBundle().events(events(invalidEvent, validEvent)).build();
    ValidationHook hook1 = ValidationHook.builder().removeOnError(false).validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E1032)).build();
    ValidationHook hook2 = ValidationHook.builder().removeOnError(false).validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E9999)).build();
    service = new DefaultTrackerValidationService(List.of(hook1, hook2), Collections.emptyList());
    TrackerValidationReport report = service.validate(bundle);
    assertTrue(report.hasErrors());
    assertEquals(2, report.getErrors().size(), "both hooks should add 1 error each");
    assertHasError(report, TrackerErrorCode.E1032, invalidEvent);
    assertHasError(report, TrackerErrorCode.E9999, invalidEvent);
    assertFalse(bundle.getEvents().contains(invalidEvent));
    assertTrue(bundle.getEvents().contains(validEvent));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AssertTrackerValidationReport.assertHasWarning(org.hisp.dhis.tracker.validation.hooks.AssertTrackerValidationReport.assertHasWarning) BiConsumer(java.util.function.BiConsumer) User(org.hisp.dhis.user.User) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Event(org.hisp.dhis.tracker.domain.Event) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ValidationMode(org.hisp.dhis.tracker.ValidationMode) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) AbstractTrackerDtoValidationHook(org.hisp.dhis.tracker.validation.hooks.AbstractTrackerDtoValidationHook) Mockito.verify(org.mockito.Mockito.verify) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Builder(lombok.Builder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TrackerImportStrategy(org.hisp.dhis.tracker.TrackerImportStrategy) TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport) AssertTrackerValidationReport.assertHasError(org.hisp.dhis.tracker.validation.hooks.AssertTrackerValidationReport.assertHasError) CodeGenerator(org.hisp.dhis.common.CodeGenerator) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport) Event(org.hisp.dhis.tracker.domain.Event) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) AbstractTrackerDtoValidationHook(org.hisp.dhis.tracker.validation.hooks.AbstractTrackerDtoValidationHook) Test(org.junit.jupiter.api.Test)

Example 3 with ValidationMode

use of org.hisp.dhis.tracker.ValidationMode 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());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AssertTrackerValidationReport.assertHasWarning(org.hisp.dhis.tracker.validation.hooks.AssertTrackerValidationReport.assertHasWarning) BiConsumer(java.util.function.BiConsumer) User(org.hisp.dhis.user.User) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Event(org.hisp.dhis.tracker.domain.Event) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ValidationMode(org.hisp.dhis.tracker.ValidationMode) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) AbstractTrackerDtoValidationHook(org.hisp.dhis.tracker.validation.hooks.AbstractTrackerDtoValidationHook) Mockito.verify(org.mockito.Mockito.verify) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Builder(lombok.Builder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TrackerImportStrategy(org.hisp.dhis.tracker.TrackerImportStrategy) TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport) AssertTrackerValidationReport.assertHasError(org.hisp.dhis.tracker.validation.hooks.AssertTrackerValidationReport.assertHasError) CodeGenerator(org.hisp.dhis.common.CodeGenerator) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Event(org.hisp.dhis.tracker.domain.Event) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) AbstractTrackerDtoValidationHook(org.hisp.dhis.tracker.validation.hooks.AbstractTrackerDtoValidationHook) Test(org.junit.jupiter.api.Test)

Aggregations

ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 List (java.util.List)3 Objects (java.util.Objects)3 BiConsumer (java.util.function.BiConsumer)3 Builder (lombok.Builder)3 CodeGenerator (org.hisp.dhis.common.CodeGenerator)3 TrackerImportStrategy (org.hisp.dhis.tracker.TrackerImportStrategy)3 ValidationMode (org.hisp.dhis.tracker.ValidationMode)3 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)3 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)3 Event (org.hisp.dhis.tracker.domain.Event)3 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)3 TrackerErrorCode (org.hisp.dhis.tracker.report.TrackerErrorCode)3 TrackerValidationReport (org.hisp.dhis.tracker.report.TrackerValidationReport)3 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)3 AbstractTrackerDtoValidationHook (org.hisp.dhis.tracker.validation.hooks.AbstractTrackerDtoValidationHook)3 AssertTrackerValidationReport.assertHasError (org.hisp.dhis.tracker.validation.hooks.AssertTrackerValidationReport.assertHasError)3 AssertTrackerValidationReport.assertHasWarning (org.hisp.dhis.tracker.validation.hooks.AssertTrackerValidationReport.assertHasWarning)3