Search in sources :

Example 1 with TrackerImportValidationContext

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

the class PreCheckDataRelationsValidationHookTest method setUp.

@BeforeEach
void setUp() {
    hook = new PreCheckDataRelationsValidationHook(categoryService);
    bundle = TrackerBundle.builder().preheat(preheat).build();
    ctx = new TrackerImportValidationContext(bundle);
    reporter = new ValidationErrorReporter(ctx);
}
Also used : TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with TrackerImportValidationContext

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

the class EnrollmentNoteValidationHookTest method testNoteWithExistingUidWarnings.

@Test
void testNoteWithExistingUidWarnings() {
    // Given
    final Note note = rnd.nextObject(Note.class);
    TrackerBundle trackerBundle = mock(TrackerBundle.class);
    TrackerImportValidationContext ctx = mock(TrackerImportValidationContext.class);
    TrackerPreheat preheat = mock(TrackerPreheat.class);
    when(ctx.getBundle()).thenReturn(trackerBundle);
    when(trackerBundle.getValidationMode()).thenReturn(ValidationMode.FULL);
    when(trackerBundle.getPreheat()).thenReturn(preheat);
    when(ctx.getNote(note.getNote())).thenReturn(Optional.of(new TrackedEntityComment()));
    ValidationErrorReporter reporter = new ValidationErrorReporter(ctx);
    enrollment.setNotes(Collections.singletonList(note));
    // When
    this.hook.validateEnrollment(reporter, enrollment);
    // Then
    assertTrue(reporter.hasWarnings());
    assertTrue(reporter.hasWarningReport(warn -> E1119.equals(warn.getWarningCode()) && ENROLLMENT.equals(warn.getTrackerType()) && enrollment.getUid().equals(warn.getUid())));
    assertThat(enrollment.getNotes(), hasSize(0));
}
Also used : TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) Strictness(org.mockito.quality.Strictness) BeforeEach(org.junit.jupiter.api.BeforeEach) BeanRandomizer(org.hisp.dhis.random.BeanRandomizer) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) E1119(org.hisp.dhis.tracker.report.TrackerErrorCode.E1119) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Note(org.hisp.dhis.tracker.domain.Note) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ENROLLMENT(org.hisp.dhis.tracker.TrackerType.ENROLLMENT) Mockito.when(org.mockito.Mockito.when) ValidationMode(org.hisp.dhis.tracker.ValidationMode) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Collections(java.util.Collections) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) Mockito.mock(org.mockito.Mockito.mock) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Note(org.hisp.dhis.tracker.domain.Note) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 3 with TrackerImportValidationContext

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

the class EnrollmentNoteValidationHookTest method testNotesAreValidWhenUidDoesNotExist.

@Test
void testNotesAreValidWhenUidDoesNotExist() {
    // Given
    final List<Note> notes = rnd.objects(Note.class, 5).collect(Collectors.toList());
    TrackerBundle trackerBundle = mock(TrackerBundle.class);
    TrackerImportValidationContext ctx = mock(TrackerImportValidationContext.class);
    when(ctx.getBundle()).thenReturn(trackerBundle);
    when(trackerBundle.getValidationMode()).thenReturn(ValidationMode.FULL);
    ValidationErrorReporter reporter = new ValidationErrorReporter(ctx);
    enrollment.setNotes(notes);
    // When
    this.hook.validateEnrollment(reporter, enrollment);
    // Then
    assertFalse(reporter.hasErrors());
    assertThat(enrollment.getNotes(), hasSize(5));
}
Also used : TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Note(org.hisp.dhis.tracker.domain.Note) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 4 with TrackerImportValidationContext

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

the class PreCheckMetaValidationHook method validateEvent.

@Override
public void validateEvent(ValidationErrorReporter reporter, Event event) {
    TrackerImportValidationContext context = reporter.getValidationContext();
    OrganisationUnit organisationUnit = context.getOrganisationUnit(event.getOrgUnit());
    reporter.addErrorIfNull(organisationUnit, event, E1011, event.getOrgUnit());
    Program program = context.getProgram(event.getProgram());
    reporter.addErrorIfNull(program, event, E1010, event.getProgram());
    ProgramStage programStage = context.getProgramStage(event.getProgramStage());
    reporter.addErrorIfNull(programStage, event, E1013, event.getProgramStage());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 5 with TrackerImportValidationContext

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

the class PreCheckSecurityOwnershipValidationHook method validateEvent.

@Override
public void validateEvent(ValidationErrorReporter reporter, Event event) {
    TrackerImportValidationContext context = reporter.getValidationContext();
    TrackerImportStrategy strategy = context.getStrategy(event);
    TrackerBundle bundle = context.getBundle();
    User user = bundle.getUser();
    checkNotNull(user, USER_CANT_BE_NULL);
    checkNotNull(event, EVENT_CANT_BE_NULL);
    checkNotNull(event.getOrgUnit(), ORGANISATION_UNIT_CANT_BE_NULL);
    OrganisationUnit organisationUnit = context.getOrganisationUnit(event.getOrgUnit());
    ProgramStage programStage = context.getProgramStage(event.getProgramStage());
    Program program = context.getProgram(event.getProgram());
    // has to be checked
    if (program.isWithoutRegistration() || strategy.isCreate() || strategy.isDelete()) {
        checkOrgUnitInCaptureScope(reporter, event, organisationUnit);
    }
    String teiUid = getTeiUidFromEvent(context, event, program);
    CategoryOptionCombo categoryOptionCombo = bundle.getPreheat().getCategoryOptionCombo(event.getAttributeOptionCombo());
    OrganisationUnit ownerOrgUnit = context.getOwnerOrganisationUnit(teiUid, program.getUid());
    // Check acting user is allowed to change existing/write event
    if (strategy.isUpdateOrDelete()) {
        ProgramStageInstance programStageInstance = context.getProgramStageInstance(event.getEvent());
        TrackedEntityInstance entityInstance = programStageInstance.getProgramInstance().getEntityInstance();
        validateUpdateAndDeleteEvent(reporter, event, programStageInstance, entityInstance == null ? null : entityInstance.getUid(), ownerOrgUnit);
    } else {
        validateCreateEvent(reporter, event, user, categoryOptionCombo, programStage, teiUid, organisationUnit, ownerOrgUnit, program, event.isCreatableInSearchScope());
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) TrackerImportStrategy(org.hisp.dhis.tracker.TrackerImportStrategy) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ProgramStage(org.hisp.dhis.program.ProgramStage) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Aggregations

TrackerImportValidationContext (org.hisp.dhis.tracker.validation.TrackerImportValidationContext)46 Program (org.hisp.dhis.program.Program)14 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)14 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)13 ProgramStage (org.hisp.dhis.program.ProgramStage)11 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)10 TrackerImportStrategy (org.hisp.dhis.tracker.TrackerImportStrategy)8 Test (org.junit.jupiter.api.Test)8 List (java.util.List)7 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)7 Event (org.hisp.dhis.tracker.domain.Event)7 Note (org.hisp.dhis.tracker.domain.Note)7 Collectors (java.util.stream.Collectors)6 ProgramInstance (org.hisp.dhis.program.ProgramInstance)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 TrackerErrorCode (org.hisp.dhis.tracker.report.TrackerErrorCode)5 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)4 TrackedEntityComment (org.hisp.dhis.trackedentitycomment.TrackedEntityComment)4 TrackerType (org.hisp.dhis.tracker.TrackerType)4 User (org.hisp.dhis.user.User)4