Search in sources :

Example 61 with TrackerBundle

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

the class DefaultTrackerValidationServiceTest method childEntitiesOfInvalidParentsAreStillValidated.

@Test
void childEntitiesOfInvalidParentsAreStillValidated() {
    // Test shows
    // the children of a tracked entity will still be validated even if it
    // as a parent is invalid
    TrackedEntity invalidTrackedEntity = trackedEntity();
    Enrollment invalidEnrollment = enrollment();
    invalidTrackedEntity.setEnrollments(enrollments(invalidEnrollment));
    Event invalidEvent = event();
    invalidEnrollment.setEvents(events(invalidEvent));
    TrackerBundle bundle = newBundle().validationMode(ValidationMode.FULL).trackedEntities(trackedEntities(invalidTrackedEntity)).enrollments(invalidTrackedEntity.getEnrollments()).events(invalidEnrollment.getEvents()).build();
    ValidationHook hook = ValidationHook.builder().validateTrackedEntity((reporter, te) -> reporter.addErrorIf(() -> invalidTrackedEntity.equals(te), te, TrackerErrorCode.E1090)).validateEnrollment((reporter, enrollment) -> reporter.addErrorIf(() -> invalidEnrollment.equals(enrollment), enrollment, TrackerErrorCode.E1069)).validateEvent((reporter, event) -> reporter.addErrorIf(() -> invalidEvent.equals(event), event, TrackerErrorCode.E1032)).build();
    service = new DefaultTrackerValidationService(List.of(hook), Collections.emptyList());
    TrackerValidationReport report = service.validate(bundle);
    assertTrue(report.hasErrors());
    assertEquals(3, report.getErrors().size());
    assertHasError(report, TrackerErrorCode.E1090, invalidTrackedEntity);
    assertHasError(report, TrackerErrorCode.E1069, invalidEnrollment);
    assertHasError(report, TrackerErrorCode.E1032, invalidEvent);
    assertTrue(bundle.getTrackedEntities().isEmpty());
    assertTrue(bundle.getEnrollments().isEmpty());
    assertTrue(bundle.getEvents().isEmpty());
}
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) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) 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 62 with TrackerBundle

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

the class DefaultTrackerValidationServiceTest method shouldNotValidateMissingUser.

@Test
void shouldNotValidateMissingUser() {
    TrackerBundle bundle = newBundle().validationMode(ValidationMode.SKIP).user(null).build();
    TrackerValidationHook hook1 = mock(TrackerValidationHook.class);
    service = new DefaultTrackerValidationService(List.of(hook1), Collections.emptyList());
    service.validate(bundle);
    verifyNoInteractions(hook1);
}
Also used : TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Test(org.junit.jupiter.api.Test)

Example 63 with TrackerBundle

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

the class DefaultTrackerValidationServiceConfigOrderTest method hooksAreExecutedInTrackerValidationConfigOrder.

@Test
void hooksAreExecutedInTrackerValidationConfigOrder() {
    // Test that hooks declared in TrackerValidationConfig validationHooks()
    // are injected
    // into the TrackerValidationService. This is important since order
    // matters in the current implementation.
    // Note that FAIL_FAST shows that although the event is also invalid due
    // to not having an orgUnit and more it
    // fails due to the first failed check which is the
    // PreCheckUidValidationHook
    Event event = new Event();
    event.setEvent("invalidUid");
    TrackerBundle bundle = TrackerBundle.builder().importMode(TrackerBundleMode.VALIDATE).validationMode(ValidationMode.FAIL_FAST).skipRuleEngine(true).events(Collections.singletonList(event)).build();
    TrackerValidationReport report = trackerValidationService.validate(bundle);
    assertTrue(report.hasErrors());
    assertEquals(1, report.getErrors().size());
    assertEquals(E1048, report.getErrors().get(0).getErrorCode());
}
Also used : TrackerValidationReport(org.hisp.dhis.tracker.report.TrackerValidationReport) Event(org.hisp.dhis.tracker.domain.Event) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 64 with TrackerBundle

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

the class EnrollmentAttributeValidationHookTest method setUp.

@BeforeEach
public void setUp() {
    trackedEntityAttribute = new TrackedEntityAttribute("name", "description", ValueType.TEXT, false, false);
    trackedEntityAttribute.setUid(trackedAttribute);
    trackedEntityAttribute1 = new TrackedEntityAttribute("name1", "description1", ValueType.TEXT, false, false);
    trackedEntityAttribute1.setUid(trackedAttribute1);
    when(validationContext.getProgram(anyString())).thenReturn(program);
    when(enrollment.getProgram()).thenReturn("program");
    when(validationContext.getTrackedEntityAttribute(trackedAttribute)).thenReturn(trackedEntityAttribute);
    when(validationContext.getTrackedEntityAttribute(trackedAttribute1)).thenReturn(trackedEntityAttribute1);
    String uid = CodeGenerator.generateUid();
    when(enrollment.getUid()).thenReturn(uid);
    when(enrollment.getEnrollment()).thenReturn(uid);
    when(enrollment.getTrackerType()).thenCallRealMethod();
    enrollment.setTrackedEntity(trackedEntity);
    TrackerBundle bundle = TrackerBundle.builder().build();
    bundle.setPreheat(preheat);
    when(validationContext.getBundle()).thenReturn(bundle);
}
Also used : ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 65 with TrackerBundle

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

the class ValidationErrorReporterTest method hasErrorReportNotFound.

@Test
void hasErrorReportNotFound() {
    ValidationErrorReporter reporter = ValidationErrorReporter.emptyReporter();
    TrackerBundle bundle = mock(TrackerBundle.class);
    TrackerErrorReport error = TrackerErrorReport.builder().errorCode(TrackerErrorCode.E1000).trackerType(TrackerType.EVENT).build(bundle);
    reporter.getReportList().add(error);
    assertFalse(reporter.hasErrorReport(r -> TrackerType.TRACKED_ENTITY.equals(r.getTrackerType())));
}
Also used : Test(org.junit.jupiter.api.Test) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Mockito.mock(org.mockito.Mockito.mock) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) 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