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();
}
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");
}
}
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;
}
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))));
}
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);
}
Aggregations