Search in sources :

Example 11 with TrackerValidationReport

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

the class DefaultTrackerImportService method importTracker.

@Override
public TrackerImportReport importTracker(TrackerImportParams params) {
    User user = trackerUserService.getUser(params.getUserId());
    params.setUser(user);
    TrackerTimingsStats opsTimer = new TrackerTimingsStats();
    startImport(params);
    TrackerValidationReport validationReport = new TrackerValidationReport();
    TrackerBundleReport bundleReport;
    try {
        TrackerBundle trackerBundle = preHeat(params, opsTimer);
        Map<TrackerType, Integer> bundleSize = calculatePayloadSize(trackerBundle);
        preProcess(opsTimer, trackerBundle);
        if (addToValidationReport(params, opsTimer, validationReport, trackerBundle)) {
            return buildReportAndNotify(params, validationReport, opsTimer, bundleSize);
        }
        bundleReport = commit(params, opsTimer, trackerBundle);
        postCommit(trackerBundle);
        TrackerImportReport trackerImportReport = TrackerImportReport.withImportCompleted(TrackerStatus.OK, bundleReport, validationReport, opsTimer.stopTimer(), bundleSize);
        endImport(params, trackerImportReport);
        return trackerImportReport;
    } catch (Exception e) {
        log.error("Exception thrown during import.", e);
        TrackerImportReport report = TrackerImportReport.withError("Exception:" + e.getMessage(), validationReport, opsTimer.stopTimer());
        endImportWithError(params, report, e);
        return report;
    }
}
Also used : TrackerImportReport(org.hisp.dhis.tracker.report.TrackerImportReport) User(org.hisp.dhis.user.User) TrackerTimingsStats(org.hisp.dhis.tracker.report.TrackerTimingsStats) TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackerBundleReport(org.hisp.dhis.tracker.report.TrackerBundleReport)

Example 12 with TrackerValidationReport

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

the class DefaultTrackerImportService method execRuleEngine.

private TrackerValidationReport execRuleEngine(TrackerImportParams params, TrackerTimingsStats opsTimer, TrackerBundle trackerBundle) {
    opsTimer.execVoid(PROGRAMRULE_OPS, () -> runRuleEngine(trackerBundle));
    notifyOps(params, PROGRAMRULE_OPS, opsTimer);
    TrackerValidationReport report = opsTimer.exec(VALIDATE_PROGRAMRULE_OPS, () -> validateRuleEngine(trackerBundle));
    notifyOps(params, VALIDATE_PROGRAMRULE_OPS, opsTimer);
    return report;
}
Also used : TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport)

Example 13 with TrackerValidationReport

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

the class DefaultTrackerValidationServiceTest method needsToRunPreventsHookExecutionIfReturnsFalse.

@Test
void needsToRunPreventsHookExecutionIfReturnsFalse() {
    Event invalidEvent = event();
    TrackerBundle bundle = newBundle().events(events(invalidEvent)).build();
    ValidationHook hook1 = ValidationHook.builder().needsToRun(false).validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E1032)).build();
    service = new DefaultTrackerValidationService(List.of(hook1), Collections.emptyList());
    TrackerValidationReport report = service.validate(bundle);
    assertFalse(report.hasErrors());
}
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 14 with TrackerValidationReport

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

the class DefaultTrackerValidationServiceTest method warningsDoNotInvalidateAndRemoveEntities.

@Test
void warningsDoNotInvalidateAndRemoveEntities() {
    Event validEvent = event();
    TrackerBundle bundle = newBundle().events(events(validEvent)).build();
    ValidationHook hook = ValidationHook.builder().validateEvent((reporter, event) -> {
        if (validEvent.equals(event)) {
            reporter.addWarning(event, TrackerErrorCode.E1120);
        }
    }).build();
    service = new DefaultTrackerValidationService(List.of(hook), Collections.emptyList());
    TrackerValidationReport report = service.validate(bundle);
    assertFalse(report.hasErrors());
    assertTrue(report.hasWarnings());
    assertEquals(1, report.getWarnings().size());
    assertHasWarning(report, TrackerErrorCode.E1120, validEvent);
    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 15 with TrackerValidationReport

use of org.hisp.dhis.tracker.report.TrackerValidationReport 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)

Aggregations

TrackerValidationReport (org.hisp.dhis.tracker.report.TrackerValidationReport)18 Test (org.junit.jupiter.api.Test)11 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)10 Event (org.hisp.dhis.tracker.domain.Event)10 User (org.hisp.dhis.user.User)10 ArrayList (java.util.ArrayList)9 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)9 Arrays (java.util.Arrays)8 Collections (java.util.Collections)8 List (java.util.List)8 Objects (java.util.Objects)8 BiConsumer (java.util.function.BiConsumer)8 Builder (lombok.Builder)8 CodeGenerator (org.hisp.dhis.common.CodeGenerator)8 TrackerImportStrategy (org.hisp.dhis.tracker.TrackerImportStrategy)8 ValidationMode (org.hisp.dhis.tracker.ValidationMode)8 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)8 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)8 TrackerErrorCode (org.hisp.dhis.tracker.report.TrackerErrorCode)8 AbstractTrackerDtoValidationHook (org.hisp.dhis.tracker.validation.hooks.AbstractTrackerDtoValidationHook)8