Search in sources :

Example 41 with TrackerImportValidationContext

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

the class EnrollmentNoteValidationHookTest method testNoteWithExistingUidAndNoTextIsIgnored.

@Test
void testNoteWithExistingUidAndNoTextIsIgnored() {
    // Given
    final Note note = rnd.nextObject(Note.class);
    note.setValue(null);
    TrackerBundle trackerBundle = mock(TrackerBundle.class);
    TrackerImportValidationContext ctx = mock(TrackerImportValidationContext.class);
    when(ctx.getBundle()).thenReturn(trackerBundle);
    when(trackerBundle.getValidationMode()).thenReturn(ValidationMode.FULL);
    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
    assertFalse(reporter.hasErrors());
    assertThat(enrollment.getNotes(), hasSize(0));
}
Also used : TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) 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 42 with TrackerImportValidationContext

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

the class EventNoteValidationHookTest 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);
    event.setNotes(Collections.singletonList(note));
    // When
    this.hook.validateEvent(reporter, event);
    // Then
    assertTrue(reporter.hasWarnings());
    assertTrue(reporter.hasWarningReport(r -> TrackerErrorCode.E1119.equals(r.getWarningCode()) && TrackerType.EVENT.equals(r.getTrackerType()) && event.getUid().equals(r.getUid())));
    assertThat(event.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) 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) Event(org.hisp.dhis.tracker.domain.Event) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Mockito.when(org.mockito.Mockito.when) TrackerType(org.hisp.dhis.tracker.TrackerType) ValidationMode(org.hisp.dhis.tracker.ValidationMode) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Collectors(java.util.stream.Collectors) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) 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 43 with TrackerImportValidationContext

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

the class EventNoteValidationHookTest 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);
    event.setNotes(notes);
    // When
    this.hook.validateEvent(reporter, event);
    // Then
    assertFalse(reporter.hasErrors());
    assertThat(event.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 44 with TrackerImportValidationContext

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

the class RepeatedEventsValidationHookTest method setUp.

@BeforeEach
public void setUp() {
    validatorToTest = new RepeatedEventsValidationHook();
    bundle = TrackerBundle.builder().build();
    bundle.setPreheat(preheat);
    ctx = new TrackerImportValidationContext(bundle);
    when(preheat.get(ProgramStage.class, NOT_REPEATABLE_PROGRAM_STAGE_WITH_REGISTRATION)).thenReturn(notRepeatebleProgramStageWithRegistration());
    when(preheat.get(ProgramStage.class, REPEATABLE_PROGRAM_STAGE_WITH_REGISTRATION)).thenReturn(repeatebleProgramStageWithRegistration());
    when(preheat.get(ProgramStage.class, NOT_REPEATABLE_PROGRAM_STAGE_WITHOUT_REGISTRATION)).thenReturn(notRepeatebleProgramStageWithoutRegistration());
    when(preheat.get(ProgramStage.class, REPEATABLE_PROGRAM_STAGE_WITHOUT_REGISTRATION)).thenReturn(repeatebleProgramStageWithoutRegistration());
}
Also used : TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 45 with TrackerImportValidationContext

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

the class RepeatedEventsValidationHookTest method testOneEventInNotRepeatableProgramStageAndOneAlreadyOnDBAreNotPassingValidation.

@Test
void testOneEventInNotRepeatableProgramStageAndOneAlreadyOnDBAreNotPassingValidation() {
    // given
    Event event = notRepeatableEvent("A");
    ProgramInstance programInstance = new ProgramInstance();
    programInstance.setUid(event.getEnrollment());
    // when
    bundle.setStrategy(event, TrackerImportStrategy.CREATE);
    when(preheat.getEnrollment(TrackerIdScheme.UID, event.getEnrollment())).thenReturn(programInstance);
    when(preheat.getProgramStageWithEvents()).thenReturn(Lists.newArrayList(Pair.of(event.getProgramStage(), event.getEnrollment())));
    bundle.setEvents(Lists.newArrayList(event));
    ValidationErrorReporter errorReporter = new ValidationErrorReporter(new TrackerImportValidationContext(bundle));
    validatorToTest.validate(errorReporter, ctx);
    // then
    assertEquals(1, errorReporter.getReportList().size());
    assertTrue(errorReporter.hasErrorReport(err -> E1039.equals(err.getErrorCode()) && EVENT.equals(err.getTrackerType()) && event.getUid().equals(err.getUid())));
    assertThat(errorReporter.getReportList().get(0).getErrorMessage(), is("ProgramStage: `" + NOT_REPEATABLE_PROGRAM_STAGE_WITH_REGISTRATION + "`, is not repeatable and an event already exists."));
}
Also used : Strictness(org.mockito.quality.Strictness) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) Mock(org.mockito.Mock) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Program(org.hisp.dhis.program.Program) EVENT(org.hisp.dhis.tracker.TrackerType.EVENT) Lists(com.google.common.collect.Lists) Pair(org.apache.commons.lang3.tuple.Pair) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) E1039(org.hisp.dhis.tracker.report.TrackerErrorCode.E1039) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackerIdScheme(org.hisp.dhis.tracker.TrackerIdScheme) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Event(org.hisp.dhis.tracker.domain.Event) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Mockito.when(org.mockito.Mockito.when) TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ProgramStage(org.hisp.dhis.program.ProgramStage) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProgramType(org.hisp.dhis.program.ProgramType) TrackerImportStrategy(org.hisp.dhis.tracker.TrackerImportStrategy) Matchers.is(org.hamcrest.Matchers.is) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Event(org.hisp.dhis.tracker.domain.Event) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

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