Search in sources :

Example 56 with Event

use of org.hisp.dhis.tracker.domain.Event in project dhis2-core by dhis2.

the class PreCheckSecurityOwnershipValidationHookTest method verifyValidationSuccessForTrackerEventCreation.

@Test
void verifyValidationSuccessForTrackerEventCreation() {
    Event event = Event.builder().enrollment(CodeGenerator.generateUid()).orgUnit(ORG_UNIT_ID).programStage(PS_ID).program(PROGRAM_ID).build();
    when(ctx.getStrategy(event)).thenReturn(TrackerImportStrategy.CREATE);
    when(ctx.getProgramStage(event.getProgramStage())).thenReturn(programStage);
    when(ctx.getProgramInstance(event.getEnrollment())).thenReturn(getEnrollment(null));
    when(ctx.getProgram(PROGRAM_ID)).thenReturn(program);
    when(ctx.getOrganisationUnit(ORG_UNIT_ID)).thenReturn(organisationUnit);
    when(organisationUnitService.isInUserHierarchyCached(user, organisationUnit)).thenReturn(true);
    when(aclService.canDataRead(user, program.getTrackedEntityType())).thenReturn(true);
    when(aclService.canDataRead(user, program)).thenReturn(true);
    when(aclService.canDataWrite(user, programStage)).thenReturn(true);
    reporter = new ValidationErrorReporter(ctx);
    validatorToTest.validateEvent(reporter, event);
    assertFalse(reporter.hasErrors());
}
Also used : 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)

Example 57 with Event

use of org.hisp.dhis.tracker.domain.Event in project dhis2-core by dhis2.

the class AssignedUserValidationHookTest method testEventWithNullEnabledUserAssignment.

@Test
void testEventWithNullEnabledUserAssignment() {
    // given
    Event event = new Event();
    event.setEvent(CodeGenerator.generateUid());
    event.setAssignedUser(USER_ID);
    event.setProgramStage(PROGRAM_STAGE);
    ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
    // when
    ProgramStage programStage = new ProgramStage();
    programStage.setEnableUserAssignment(null);
    when(validationContext.getProgramStage(PROGRAM_STAGE)).thenReturn(programStage);
    this.hookToTest.validateEvent(reporter, event);
    // then
    assertFalse(reporter.hasErrors());
    assertTrue(reporter.hasWarnings());
    assertTrue(reporter.hasWarningReport(r -> E1120.equals(r.getWarningCode()) && TrackerType.EVENT.equals(r.getTrackerType()) && event.getUid().equals(r.getUid())));
}
Also used : Event(org.hisp.dhis.tracker.domain.Event) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Mock(org.mockito.Mock) AssertValidationErrorReporter.hasTrackerError(org.hisp.dhis.tracker.validation.hooks.AssertValidationErrorReporter.hasTrackerError) E1120(org.hisp.dhis.tracker.report.TrackerErrorCode.E1120) Mockito.when(org.mockito.Mockito.when) TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) TrackerIdentifier(org.hisp.dhis.tracker.TrackerIdentifier) ProgramStage(org.hisp.dhis.program.ProgramStage) E1118(org.hisp.dhis.tracker.report.TrackerErrorCode.E1118) Test(org.junit.jupiter.api.Test) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) User(org.hisp.dhis.user.User) CodeGenerator(org.hisp.dhis.common.CodeGenerator) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) 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 58 with Event

use of org.hisp.dhis.tracker.domain.Event in project dhis2-core by dhis2.

the class AssignedUserValidationHookTest method testEventWithUserNotPresentInPreheat.

@Test
void testEventWithUserNotPresentInPreheat() {
    // given
    Event event = new Event();
    event.setEvent(CodeGenerator.generateUid());
    event.setAssignedUser(USER_ID);
    event.setProgramStage(PROGRAM_STAGE);
    ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
    // when
    TrackerBundle bundle = TrackerBundle.builder().build();
    bundle.setPreheat(new TrackerPreheat());
    when(validationContext.getBundle()).thenReturn(bundle);
    this.hookToTest.validateEvent(reporter, event);
    // then
    hasTrackerError(reporter, E1118, TrackerType.EVENT, event.getUid());
}
Also used : Event(org.hisp.dhis.tracker.domain.Event) 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 59 with Event

use of org.hisp.dhis.tracker.domain.Event in project dhis2-core by dhis2.

the class DefaultTrackerValidationServiceTest method needsToRunExecutesHookIfReturnsTrue.

@Test
void needsToRunExecutesHookIfReturnsTrue() {
    Event invalidEvent = event();
    TrackerBundle bundle = newBundle().events(events(invalidEvent)).build();
    ValidationHook hook1 = ValidationHook.builder().needsToRun(true).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);
    assertTrue(report.hasErrors());
    assertHasError(report, TrackerErrorCode.E1032, invalidEvent);
}
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 Event

use of org.hisp.dhis.tracker.domain.Event in project dhis2-core by dhis2.

the class DefaultTrackerValidationServiceTest method needsToRunPreventsHookExecutionOnImportStrategyDeleteByDefault.

@Test
void needsToRunPreventsHookExecutionOnImportStrategyDeleteByDefault() {
    Event invalidEvent = event();
    TrackerBundle bundle = newBundle().importStrategy(TrackerImportStrategy.DELETE).events(events(invalidEvent)).build();
    // StrategyPreProcessor sets the ImportStrategy in the bundle for every
    // dto
    bundle.setStrategy(invalidEvent, TrackerImportStrategy.DELETE);
    ValidationHook hook1 = ValidationHook.builder().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)

Aggregations

Event (org.hisp.dhis.tracker.domain.Event)192 Test (org.junit.jupiter.api.Test)153 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)114 ProgramStage (org.hisp.dhis.program.ProgramStage)68 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)64 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)53 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)43 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)41 Mockito.when (org.mockito.Mockito.when)38 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)37 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)36 DataElement (org.hisp.dhis.dataelement.DataElement)35 Program (org.hisp.dhis.program.Program)35 ProgramInstance (org.hisp.dhis.program.ProgramInstance)35 DataValue (org.hisp.dhis.tracker.domain.DataValue)35 TrackerPreheat (org.hisp.dhis.tracker.preheat.TrackerPreheat)35 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)34 TrackerErrorCode (org.hisp.dhis.tracker.report.TrackerErrorCode)33 CodeGenerator (org.hisp.dhis.common.CodeGenerator)32 BeforeEach (org.junit.jupiter.api.BeforeEach)32