Search in sources :

Example 56 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class EventProgramPreProcessorTest method testProgramStageHasNoReferenceToProgram.

@Test
void testProgramStageHasNoReferenceToProgram() {
    // Given
    ProgramStage programStage = new ProgramStage();
    programStage.setUid("LGSWs20XFvy");
    when(preheat.get(ProgramStage.class, "LGSWs20XFvy")).thenReturn(programStage);
    Event event = new Event();
    event.setProgramStage(programStage.getUid());
    TrackerBundle bundle = TrackerBundle.builder().events(Collections.singletonList(event)).preheat(preheat).build();
    // When
    preProcessorToTest.process(bundle);
    verify(preheat, never()).put(TrackerIdentifier.UID, programStage.getProgram());
}
Also used : Event(org.hisp.dhis.tracker.domain.Event) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ProgramStage(org.hisp.dhis.program.ProgramStage) DhisConvenienceTest.createProgramStage(org.hisp.dhis.DhisConvenienceTest.createProgramStage) Test(org.junit.jupiter.api.Test)

Example 57 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class DefaultTrackerValidationServiceTest method needsToRunPreventsHookExecutionIfReturnsFalse.

@Test
void needsToRunPreventsHookExecutionIfReturnsFalse() {
    Event invalidEvent = event();
    TrackerBundle bundle = newBundle().events(events(invalidEvent)).build();
    ValidationHook hook1 = ValidationHook.builder().needsToRun(false).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);
    assertFalse(report.hasErrors());
}
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)

Example 58 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class DefaultTrackerValidationServiceTest method warningsDoNotInvalidateAndRemoveEntities.

@Test
void warningsDoNotInvalidateAndRemoveEntities() {
    Event validEvent = event();
    TrackerBundle bundle = newBundle().events(events(validEvent)).build();
    ValidationHook hook = ValidationHook.builder().validateEvent((reporter, event) -> {
        if (validEvent.equals(event)) {
            reporter.addWarning(event, TrackerErrorCode.E1120);
        }
    }).build();
    service = new DefaultTrackerValidationService(List.of(hook), Collections.emptyList());
    TrackerValidationReport report = service.validate(bundle);
    assertFalse(report.hasErrors());
    assertTrue(report.hasWarnings());
    assertEquals(1, report.getWarnings().size());
    assertHasWarning(report, TrackerErrorCode.E1120, validEvent);
    assertTrue(bundle.getEvents().contains(validEvent));
}
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)

Example 59 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class DefaultTrackerValidationServiceTest method fullValidationModeAddsAllErrorsToReport.

@Test
void fullValidationModeAddsAllErrorsToReport() {
    // Test shows
    // in ValidationMode==FULL all hooks are called even with entities that
    // are already invalid (i.e. have an error
    // in the validation report)
    Event validEvent = event();
    Event invalidEvent = event();
    TrackerBundle bundle = newBundle().events(events(invalidEvent, validEvent)).build();
    ValidationHook hook1 = ValidationHook.builder().removeOnError(false).validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E1032)).build();
    ValidationHook hook2 = ValidationHook.builder().removeOnError(false).validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E9999)).build();
    service = new DefaultTrackerValidationService(List.of(hook1, hook2), Collections.emptyList());
    TrackerValidationReport report = service.validate(bundle);
    assertTrue(report.hasErrors());
    assertEquals(2, report.getErrors().size(), "both hooks should add 1 error each");
    assertHasError(report, TrackerErrorCode.E1032, invalidEvent);
    assertHasError(report, TrackerErrorCode.E9999, invalidEvent);
    assertFalse(bundle.getEvents().contains(invalidEvent));
    assertTrue(bundle.getEvents().contains(validEvent));
}
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)

Example 60 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class DefaultTrackerValidationServiceTest method removeOnErrorHookPreventsFurtherValidationOfInvalidEntityEvenInFullValidationMode.

@Test
void removeOnErrorHookPreventsFurtherValidationOfInvalidEntityEvenInFullValidationMode() {
    // Test shows
    // 1. Hooks with removeOnError==true remove invalid entities from the
    // TrackerBundle to prevent
    // subsequent hooks from validating it
    // 2. the TrackerBundle is mutated to only contain valid DTOs after
    // validation
    // 
    // Currently, the bundle is mutated by
    // 1. AbstractTrackerDtoValidationHook removes invalid DTOs if the hook
    // has removeOnError == true
    // 2. DefaultTrackerValidationService removes invalid DTOs if they were
    // not previously removed
    // by AbstractTrackerDtoValidationHook i.e. hooks having removeOnError
    // == false
    Event validEvent = event();
    Event invalidEvent = event();
    TrackerBundle bundle = newBundle().events(events(invalidEvent, validEvent)).build();
    ValidationHook removeOnError = ValidationHook.builder().removeOnError(true).validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E1032)).build();
    // using default AbstractTrackerDtoValidationHook behavior of
    // removeOnError==false
    ValidationHook doNotRemoveOnError = ValidationHook.builder().validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E9999)).build();
    service = new DefaultTrackerValidationService(List.of(removeOnError, doNotRemoveOnError), Collections.emptyList());
    TrackerValidationReport report = service.validate(bundle);
    assertTrue(report.hasErrors());
    assertEquals(1, report.getErrors().size(), "only remove on error hook should add 1 error");
    assertHasError(report, TrackerErrorCode.E1032, invalidEvent);
    assertFalse(bundle.getEvents().contains(invalidEvent));
    assertTrue(bundle.getEvents().contains(validEvent));
}
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

TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)74 Test (org.junit.jupiter.api.Test)43 Event (org.hisp.dhis.tracker.domain.Event)29 User (org.hisp.dhis.user.User)21 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)19 List (java.util.List)17 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)15 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)14 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)14 Mockito.mock (org.mockito.Mockito.mock)14 TrackerErrorCode (org.hisp.dhis.tracker.report.TrackerErrorCode)13 TrackerImportStrategy (org.hisp.dhis.tracker.TrackerImportStrategy)12 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)12 TrackerImportValidationContext (org.hisp.dhis.tracker.validation.TrackerImportValidationContext)12 Collections (java.util.Collections)11 ProgramStage (org.hisp.dhis.program.ProgramStage)11 Objects (java.util.Objects)10 Program (org.hisp.dhis.program.Program)10 ValidationMode (org.hisp.dhis.tracker.ValidationMode)10 Relationship (org.hisp.dhis.tracker.domain.Relationship)10