Search in sources :

Example 71 with Event

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

the class EventTrackerConverterServiceTest method fromForRuleEngineGivenExistingEventMergesNewDataValuesWithDBOnes.

@Test
void fromForRuleEngineGivenExistingEventMergesNewDataValuesWithDBOnes() {
    setUpMocks();
    ProgramStageInstance existingPsi = programStageInstance();
    EventDataValue existingDataValue = eventDataValue(CodeGenerator.generateUid(), "658");
    existingPsi.setEventDataValues(Set.of(existingDataValue));
    DataElement dataElement = new DataElement();
    dataElement.setUid(CodeGenerator.generateUid());
    when(preheat.get(DataElement.class, dataElement.getUid())).thenReturn(dataElement);
    // event refers to a different dataElement then currently associated
    // with the event in the DB; thus both
    // dataValues will be merged
    DataValue newDataValue = dataValue(dataElement.getUid(), "900");
    Event event = event(existingPsi.getUid(), newDataValue);
    when(preheat.getEvent(TrackerIdScheme.UID, existingPsi.getUid())).thenReturn(existingPsi);
    ProgramStageInstance programStageInstance = converter.fromForRuleEngine(preheat, event);
    assertEquals(2, programStageInstance.getEventDataValues().size());
    EventDataValue expect1 = new EventDataValue();
    expect1.setDataElement(existingDataValue.getDataElement());
    expect1.setValue(existingDataValue.getValue());
    EventDataValue expect2 = new EventDataValue();
    expect2.setDataElement(newDataValue.getDataElement());
    expect2.setValue(newDataValue.getValue());
    assertContainsOnly(programStageInstance.getEventDataValues(), expect1, expect2);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Event(org.hisp.dhis.tracker.domain.Event) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 72 with Event

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

the class EventDataValuesValidationHook method validateMandatoryDataValues.

private void validateMandatoryDataValues(Event event, TrackerImportValidationContext context, ValidationErrorReporter reporter) {
    if (StringUtils.isNotEmpty(event.getProgramStage())) {
        ProgramStage programStage = context.getProgramStage(event.getProgramStage());
        final List<String> mandatoryDataElements = programStage.getProgramStageDataElements().stream().filter(ProgramStageDataElement::isCompulsory).map(de -> context.getIdentifiers().getDataElementIdScheme().getIdentifier(de.getDataElement())).collect(Collectors.toList());
        List<String> missingDataValue = validateMandatoryDataValue(programStage, event, mandatoryDataElements);
        missingDataValue.forEach(de -> reporter.addError(event, E1303, de));
    }
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) Event(org.hisp.dhis.tracker.domain.Event) FileResource(org.hisp.dhis.fileresource.FileResource) ValidationUtils(org.hisp.dhis.system.util.ValidationUtils) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) E1076(org.hisp.dhis.tracker.report.TrackerErrorCode.E1076) Set(java.util.Set) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) E1084(org.hisp.dhis.tracker.report.TrackerErrorCode.E1084) ValidationUtils.needsToValidateDataValues(org.hisp.dhis.tracker.validation.hooks.ValidationUtils.needsToValidateDataValues) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) ProgramStage(org.hisp.dhis.program.ProgramStage) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) DataElement(org.hisp.dhis.dataelement.DataElement) List(java.util.List) Component(org.springframework.stereotype.Component) E1009(org.hisp.dhis.tracker.report.TrackerErrorCode.E1009) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) E1303(org.hisp.dhis.tracker.report.TrackerErrorCode.E1303) Optional(java.util.Optional) ValidationUtils.validateMandatoryDataValue(org.hisp.dhis.tracker.validation.hooks.ValidationUtils.validateMandatoryDataValue) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 73 with Event

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

the class EventRuleValidationHook method validateEvent.

@Override
public void validateEvent(ValidationErrorReporter reporter, Event event) {
    TrackerImportValidationContext context = reporter.getValidationContext();
    List<ProgramRuleIssue> programRuleIssues = validators.stream().flatMap(v -> v.validateEvents(context.getBundle()).getOrDefault(event.getEvent(), Lists.newArrayList()).stream()).collect(Collectors.toList());
    addIssuesToReporter(reporter, event, programRuleIssues);
}
Also used : ValidationUtils.addIssuesToReporter(org.hisp.dhis.tracker.validation.hooks.ValidationUtils.addIssuesToReporter) Event(org.hisp.dhis.tracker.domain.Event) List(java.util.List) Component(org.springframework.stereotype.Component) Lists(com.google.common.collect.Lists) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Autowired(org.springframework.beans.factory.annotation.Autowired) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Collectors(java.util.stream.Collectors) RuleActionImplementer(org.hisp.dhis.tracker.programrule.RuleActionImplementer) ProgramRuleIssue(org.hisp.dhis.tracker.programrule.ProgramRuleIssue) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) ProgramRuleIssue(org.hisp.dhis.tracker.programrule.ProgramRuleIssue)

Example 74 with Event

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

the class EventCategoryOptValidationHookTest method setUp.

@BeforeEach
public void setUp() {
    initServices();
    hook = new EventCategoryOptValidationHook(i18nManager);
    catOption = createCategoryOption('A');
    category = createCategory('A', catOption);
    catCombo = createCategoryCombo('A', category);
    attOptionCombo = createCategoryOptionCombo(catCombo, catOption);
    defaultCatCombo = new CategoryCombo();
    defaultCatCombo.setName(DEFAULT_CATEGORY_COMBO_NAME);
    defaultCatOption = new CategoryOption();
    defaultCatOption.setName(DEFAULT_NAME);
    defaultCatOptionCombo = createCategoryOptionCombo(defaultCatCombo, defaultCatOption);
    program = createProgram('A');
    program.setCategoryCombo(catCombo);
    event = new Event();
    event.setEvent(CodeGenerator.generateUid());
    event.setProgram(program.getUid());
    event.setOccurredAt(EVENT_INSTANT);
    User user = createUser('A');
    TrackerBundle bundle = TrackerBundle.builder().user(user).build();
    when(validationContext.getBundle()).thenReturn(bundle);
    when(validationContext.getProgram(program.getUid())).thenReturn(program);
    reporter = new ValidationErrorReporter(validationContext);
}
Also used : User(org.hisp.dhis.user.User) CategoryCombo(org.hisp.dhis.category.CategoryCombo) CategoryOption(org.hisp.dhis.category.CategoryOption) Event(org.hisp.dhis.tracker.domain.Event) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 75 with Event

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

the class PreCheckSecurityOwnershipValidationHookTest method verifyValidationSuccessForEventUsingUpdateStrategyAndUserWithAuthority.

@Test
void verifyValidationSuccessForEventUsingUpdateStrategyAndUserWithAuthority() {
    String enrollmentUid = CodeGenerator.generateUid();
    Event event = Event.builder().enrollment(enrollmentUid).orgUnit(ORG_UNIT_ID).programStage(PS_ID).program(PROGRAM_ID).build();
    when(ctx.getStrategy(event)).thenReturn(TrackerImportStrategy.UPDATE);
    when(ctx.getProgramStage(event.getProgramStage())).thenReturn(programStage);
    ProgramInstance programInstance = getEnrollment(enrollmentUid);
    ProgramStageInstance programStageInstance = getEvent();
    programStageInstance.setProgramInstance(programInstance);
    when(ctx.getProgramStageInstance(event.getEvent())).thenReturn(programStageInstance);
    when(ctx.getProgramInstance(event.getEnrollment())).thenReturn(programInstance);
    when(ctx.getProgram(PROGRAM_ID)).thenReturn(program);
    when(ctx.getOrganisationUnit(ORG_UNIT_ID)).thenReturn(organisationUnit);
    when(aclService.canDataRead(user, program.getTrackedEntityType())).thenReturn(true);
    when(aclService.canDataRead(user, program)).thenReturn(true);
    when(aclService.canDataWrite(user, programStage)).thenReturn(true);
    when(organisationUnitService.isInUserHierarchyCached(user, organisationUnit)).thenReturn(true);
    bundle.setUser(changeCompletedEventAuthorisedUser());
    reporter = new ValidationErrorReporter(ctx);
    validatorToTest.validateEvent(reporter, event);
    assertFalse(reporter.hasErrors());
}
Also used : ProgramInstance(org.hisp.dhis.program.ProgramInstance) Event(org.hisp.dhis.tracker.domain.Event) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

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