Search in sources :

Example 1 with ValidationErrorReporter

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

the class EnrollmentAttributeValidationHookTest method shouldPassValidationWhenValueIsNullAndAttributeIsNotMandatory.

@Test
void shouldPassValidationWhenValueIsNullAndAttributeIsNotMandatory() {
    // given 1 attribute has null value
    Attribute attribute = Attribute.builder().attribute(trackedAttribute).valueType(ValueType.TEXT).value("value").build();
    Attribute attribute1 = Attribute.builder().attribute(trackedAttribute1).valueType(ValueType.TEXT).build();
    // when only 1 tracked attributes is mandatory
    when(program.getProgramAttributes()).thenReturn(Arrays.asList(new ProgramTrackedEntityAttribute(program, trackedEntityAttribute, false, true), new ProgramTrackedEntityAttribute(program, trackedEntityAttribute1, false, false)));
    when(enrollment.getAttributes()).thenReturn(Arrays.asList(attribute, attribute1));
    when(trackedEntityInstance.getTrackedEntityAttributeValues()).thenReturn(new HashSet<>(Arrays.asList(new TrackedEntityAttributeValue(trackedEntityAttribute, trackedEntityInstance), new TrackedEntityAttributeValue(trackedEntityAttribute1, trackedEntityInstance))));
    when(preheat.getTrackedEntity(TrackerIdScheme.UID, enrollment.getTrackedEntity())).thenReturn(trackedEntityInstance);
    ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
    hookToTest.validateEnrollment(reporter, enrollment);
    assertThat(reporter.getReportList(), hasSize(0));
}
Also used : Attribute(org.hisp.dhis.tracker.domain.Attribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 2 with ValidationErrorReporter

use of org.hisp.dhis.tracker.report.ValidationErrorReporter 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 3 with ValidationErrorReporter

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

the class EventDataValuesValidationHookTest method failsOnActiveEventWithDataElementValueNullAndValidationStrategyOnUpdate.

@Test
void failsOnActiveEventWithDataElementValueNullAndValidationStrategyOnUpdate() {
    setUpIdentifiers();
    DataElement validDataElement = dataElement();
    when(context.getDataElement(dataElementUid)).thenReturn(validDataElement);
    ProgramStage programStage = programStage(validDataElement, true);
    programStage.setValidationStrategy(ValidationStrategy.ON_UPDATE_AND_INSERT);
    when(context.getProgramStage(programStageUid)).thenReturn(programStage);
    ValidationErrorReporter reporter = new ValidationErrorReporter(context);
    DataValue validDataValue = dataValue();
    validDataValue.setValue(null);
    Event event = Event.builder().programStage(programStage.getUid()).status(EventStatus.ACTIVE).dataValues(Set.of(validDataValue)).build();
    hook.validateEvent(reporter, event);
    assertThat(reporter.getReportList(), hasSize(1));
    assertEquals(TrackerErrorCode.E1076, reporter.getReportList().get(0).getErrorCode());
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) Event(org.hisp.dhis.tracker.domain.Event) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) ProgramStage(org.hisp.dhis.program.ProgramStage) Test(org.junit.jupiter.api.Test)

Example 4 with ValidationErrorReporter

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

the class EventDataValuesValidationHookTest method failsOnCompletedEventWithDataElementValueIsNullAndValidationStrategyOnComplete.

@Test
void failsOnCompletedEventWithDataElementValueIsNullAndValidationStrategyOnComplete() {
    setUpIdentifiers();
    DataElement validDataElement = dataElement();
    when(context.getDataElement(dataElementUid)).thenReturn(validDataElement);
    ProgramStage programStage = programStage(validDataElement, true);
    programStage.setValidationStrategy(ValidationStrategy.ON_COMPLETE);
    when(context.getProgramStage(programStageUid)).thenReturn(programStage);
    ValidationErrorReporter reporter = new ValidationErrorReporter(context);
    DataValue validDataValue = dataValue();
    validDataValue.setValue(null);
    Event event = Event.builder().programStage(programStage.getUid()).status(EventStatus.COMPLETED).dataValues(Set.of(validDataValue)).build();
    hook.validateEvent(reporter, event);
    assertThat(reporter.getReportList(), hasSize(1));
    assertEquals(TrackerErrorCode.E1076, reporter.getReportList().get(0).getErrorCode());
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) Event(org.hisp.dhis.tracker.domain.Event) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) ProgramStage(org.hisp.dhis.program.ProgramStage) Test(org.junit.jupiter.api.Test)

Example 5 with ValidationErrorReporter

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

the class EventDataValuesValidationHookTest method failValidationWhenUpdatedAtIsNull.

@Test
void failValidationWhenUpdatedAtIsNull() {
    setUpIdentifiers();
    DataElement dataElement = dataElement();
    when(context.getDataElement(dataElementUid)).thenReturn(dataElement);
    ProgramStage programStage = programStage(dataElement);
    when(context.getProgramStage(programStageUid)).thenReturn(programStage);
    ValidationErrorReporter reporter = new ValidationErrorReporter(context);
    DataValue validDataValue = dataValue();
    validDataValue.setUpdatedAt(null);
    Event event = Event.builder().programStage(programStage.getUid()).status(EventStatus.SKIPPED).dataValues(Set.of(validDataValue)).build();
    hook.validateEvent(reporter, event);
    assertFalse(reporter.hasErrors());
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) Event(org.hisp.dhis.tracker.domain.Event) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) ProgramStage(org.hisp.dhis.program.ProgramStage) Test(org.junit.jupiter.api.Test)

Aggregations

ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)206 Test (org.junit.jupiter.api.Test)192 Event (org.hisp.dhis.tracker.domain.Event)89 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)48 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)48 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)43 ProgramStage (org.hisp.dhis.program.ProgramStage)38 DataElement (org.hisp.dhis.dataelement.DataElement)25 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)25 TrackerImportValidationContext (org.hisp.dhis.tracker.validation.TrackerImportValidationContext)24 Relationship (org.hisp.dhis.tracker.domain.Relationship)23 DataValue (org.hisp.dhis.tracker.domain.DataValue)21 Program (org.hisp.dhis.program.Program)19 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)19 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)17 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)16 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)12 RelationshipType (org.hisp.dhis.relationship.RelationshipType)12 BeforeEach (org.junit.jupiter.api.BeforeEach)12 ProgramInstance (org.hisp.dhis.program.ProgramInstance)11