Search in sources :

Example 1 with TrackerValidationReport

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

the class DefaultTrackerImportService method buildImportReport.

/**
 * Clone the TrackerImportReport and filters out validation data based on
 * the provided {@link TrackerBundleReport}.
 *
 * @return a copy of the current TrackerImportReport
 */
@Override
public TrackerImportReport buildImportReport(TrackerImportReport originalImportReport, TrackerBundleReportMode reportMode) {
    TrackerImportReport.TrackerImportReportBuilder importReportBuilder = TrackerImportReport.builder().status(originalImportReport.getStatus()).stats(originalImportReport.getStats()).bundleReport(originalImportReport.getBundleReport()).message(originalImportReport.getMessage());
    TrackerValidationReport originalValidationReport = originalImportReport.getValidationReport();
    TrackerValidationReport validationReport = new TrackerValidationReport();
    if (originalValidationReport != null) {
        validationReport.addErrors(originalValidationReport.getErrors());
    }
    if (originalValidationReport != null && TrackerBundleReportMode.WARNINGS == reportMode) {
        validationReport.addWarnings(originalValidationReport.getWarnings());
    } else if (originalValidationReport != null && TrackerBundleReportMode.FULL == reportMode) {
        validationReport.addWarnings(originalValidationReport.getWarnings()).addTimings(originalValidationReport.getTimings());
        importReportBuilder.timingsStats(originalImportReport.getTimingsStats());
    }
    importReportBuilder.validationReport(validationReport);
    return importReportBuilder.build();
}
Also used : TrackerImportReport(org.hisp.dhis.tracker.report.TrackerImportReport) TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport)

Example 2 with TrackerValidationReport

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

the class TrackerImportControllerTest method verifyShouldFindJobReport.

@Test
void verifyShouldFindJobReport() throws Exception {
    String uid = CodeGenerator.generateUid();
    TrackerImportReport trackerImportReport = TrackerImportReport.withImportCompleted(TrackerStatus.OK, TrackerBundleReport.builder().status(TrackerStatus.OK).build(), new TrackerValidationReport(), new TrackerTimingsStats(), new HashMap<>());
    // When
    when(notifier.getJobSummaryByJobId(JobType.TRACKER_IMPORT_JOB, uid)).thenReturn(trackerImportReport);
    when(trackerImportService.buildImportReport(any(), any())).thenReturn(trackerImportReport);
    // Then
    String contentAsString = mockMvc.perform(get(ENDPOINT + "/jobs/" + uid + "/report").content("{}").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(jsonPath("$.message").doesNotExist()).andExpect(content().contentType("application/json")).andReturn().getResponse().getContentAsString();
    verify(notifier).getJobSummaryByJobId(JobType.TRACKER_IMPORT_JOB, uid);
    verify(trackerImportService).buildImportReport(any(), any());
    try {
        renderService.fromJson(contentAsString, TrackerImportReport.class);
    } catch (Exception e) {
        fail("response content : " + contentAsString + "\n" + " is not of TrackerImportReport type");
    }
}
Also used : TrackerImportReport(org.hisp.dhis.tracker.report.TrackerImportReport) TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport) TrackerTimingsStats(org.hisp.dhis.tracker.report.TrackerTimingsStats) NotFoundException(org.hisp.dhis.webapi.controller.exception.NotFoundException) Test(org.junit.jupiter.api.Test)

Example 3 with TrackerValidationReport

use of org.hisp.dhis.tracker.report.TrackerValidationReport 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;
}
Also used : User(org.hisp.dhis.user.User) TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport) Timer(org.hisp.dhis.commons.timer.Timer) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Timing(org.hisp.dhis.tracker.report.Timing)

Example 4 with TrackerValidationReport

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

the class EnrollmentImportValidationTest method testActiveEnrollmentAlreadyExists.

@Test
void testActiveEnrollmentAlreadyExists() throws IOException {
    TrackerImportParams trackerImportParams = createBundleFromJson("tracker/validations/enrollments_double-tei-enrollment_part1.json");
    TrackerImportReport trackerImportReport = trackerImportService.importTracker(trackerImportParams);
    TrackerValidationReport validationReport = trackerImportReport.getValidationReport();
    assertEquals(0, validationReport.getErrors().size());
    TrackerImportParams trackerImportParams1 = createBundleFromJson("tracker/validations/enrollments_double-tei-enrollment_part2.json");
    trackerImportReport = trackerImportService.importTracker(trackerImportParams1);
    validationReport = trackerImportReport.getValidationReport();
    assertEquals(1, validationReport.getErrors().size());
    assertThat(validationReport.getErrors(), hasItem(hasProperty("errorCode", equalTo(TrackerErrorCode.E1015))));
}
Also used : TrackerImportParams(org.hisp.dhis.tracker.TrackerImportParams) TrackerImportReport(org.hisp.dhis.tracker.report.TrackerImportReport) TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport) Test(org.junit.jupiter.api.Test)

Example 5 with TrackerValidationReport

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

the class DefaultTrackerValidationServiceTest method needsToRunExecutesHookIfReturnsTrue.

@Test
void needsToRunExecutesHookIfReturnsTrue() {
    Event invalidEvent = event();
    TrackerBundle bundle = newBundle().events(events(invalidEvent)).build();
    ValidationHook hook1 = ValidationHook.builder().needsToRun(true).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);
    assertTrue(report.hasErrors());
    assertHasError(report, TrackerErrorCode.E1032, invalidEvent);
}
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