Search in sources :

Example 81 with Enrollment

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

the class PreCheckMetaValidationHookTest method verifyEnrollmentValidationSuccessWhenTeiIsInPayload.

@Test
void verifyEnrollmentValidationSuccessWhenTeiIsInPayload() {
    // given
    Enrollment enrollment = validEnrollment();
    ValidationErrorReporter reporter = new ValidationErrorReporter(ctx);
    // when
    when(ctx.getReference(TRACKED_ENTITY_UID)).thenReturn(Optional.of(new ReferenceTrackerEntity("", "")));
    when(ctx.getOrganisationUnit(ORG_UNIT_UID)).thenReturn(new OrganisationUnit());
    when(ctx.getProgram(PROGRAM_UID)).thenReturn(new Program());
    validatorToTest.validateEnrollment(reporter, enrollment);
    // then
    assertFalse(reporter.hasErrors());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ReferenceTrackerEntity(org.hisp.dhis.tracker.preheat.ReferenceTrackerEntity) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 82 with Enrollment

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

the class PreCheckUidValidationHookTest method verifyEnrollmentWithNoteWithInvalidUidFails.

@Test
void verifyEnrollmentWithNoteWithInvalidUidFails() {
    // given
    Note note = Note.builder().note(INVALID_UID).build();
    Enrollment enrollment = Enrollment.builder().enrollment(CodeGenerator.generateUid()).notes(Lists.newArrayList(note)).build();
    ValidationErrorReporter reporter = new ValidationErrorReporter(ctx);
    validationHook.validateEnrollment(reporter, enrollment);
    // then
    hasTrackerError(reporter, E1048, ENROLLMENT, enrollment.getUid());
}
Also used : Note(org.hisp.dhis.tracker.domain.Note) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 83 with Enrollment

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

the class PreCheckUidValidationHookTest method verifyEnrollmentValidationSuccess.

@Test
void verifyEnrollmentValidationSuccess() {
    // given
    Note note = Note.builder().note(CodeGenerator.generateUid()).build();
    Enrollment enrollment = Enrollment.builder().enrollment(CodeGenerator.generateUid()).notes(Lists.newArrayList(note)).build();
    ValidationErrorReporter reporter = new ValidationErrorReporter(ctx);
    validationHook.validateEnrollment(reporter, enrollment);
    // then
    assertFalse(reporter.hasErrors());
}
Also used : Note(org.hisp.dhis.tracker.domain.Note) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 84 with Enrollment

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

the class TrackerBundleParamsConverter method convert.

/**
 * Iterates over the collections of a dataBundle. If any objects in those
 * collections have objects nested within them, they are extracted. For each
 * object we process, we make sure all references are valid as well.
 *
 * @param dataBundle containing collections to check and update.
 * @return a dataBundle with a flattened data structure, and valid uid
 *         references.
 */
@Override
public TrackerBundleParams convert(TrackerBundleParams dataBundle) {
    Map<String, TrackedEntity> trackedEntityMap = new HashMap<>();
    Map<String, Enrollment> enrollmentHashMap = new HashMap<>();
    Map<String, Event> eventHashMap = new HashMap<>();
    Map<String, Relationship> relationshipHashMap = new HashMap<>();
    // Extract all enrollments and relationships, and set parent reference.
    for (TrackedEntity te : dataBundle.getTrackedEntities()) {
        updateTrackedEntityReferences(te);
        trackedEntityMap.put(te.getTrackedEntity(), te);
        extractEnrollments(te).forEach(enrollment -> enrollmentHashMap.put(enrollment.getEnrollment(), enrollment));
        extractRelationships(te).forEach(relationship -> relationshipHashMap.put(relationship.getRelationship(), relationship));
    }
    // Set UID for all enrollments and notes
    dataBundle.getEnrollments().stream().peek(enrollment -> updateEnrollmentReferences(enrollment, enrollment.getTrackedEntity())).forEach(enrollment -> enrollmentHashMap.put(enrollment.getEnrollment(), enrollment));
    // Extract all events and relationships, and set parent references
    for (Enrollment enrollment : enrollmentHashMap.values()) {
        extractEvents(enrollment).forEach(event -> eventHashMap.put(event.getEvent(), event));
        extractRelationships(enrollment).forEach(relationship -> relationshipHashMap.put(relationship.getRelationship(), relationship));
        enrollment.setNotes(enrollment.getNotes().stream().filter(note -> !StringUtils.isEmpty(note.getValue())).peek(this::updateNoteReferences).collect(Collectors.toList()));
    }
    // Set UID for all events and notes
    dataBundle.getEvents().stream().peek(event -> updateEventReferences(event, event.getEnrollment())).forEach(event -> eventHashMap.put(event.getEvent(), event));
    // Extract all relationships
    for (Event event : eventHashMap.values()) {
        extractRelationships(event).forEach(relationship -> relationshipHashMap.put(relationship.getRelationship(), relationship));
        event.setNotes(event.getNotes().stream().filter(note -> !StringUtils.isEmpty(note.getValue())).peek(this::updateNoteReferences).collect(Collectors.toList()));
    }
    // Set UID for all relationships
    dataBundle.getRelationships().stream().peek(this::updateRelationshipReferences).forEach(relationship -> relationshipHashMap.put(relationship.getRelationship(), relationship));
    return TrackerBundleParams.builder().trackedEntities(new ArrayList<>(trackedEntityMap.values())).enrollments(new ArrayList<>(enrollmentHashMap.values())).events(new ArrayList<>(eventHashMap.values())).relationships(new ArrayList<>(relationshipHashMap.values())).build();
}
Also used : Event(org.hisp.dhis.tracker.domain.Event) StdConverter(com.fasterxml.jackson.databind.util.StdConverter) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) HashMap(java.util.HashMap) Relationship(org.hisp.dhis.tracker.domain.Relationship) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) List(java.util.List) Note(org.hisp.dhis.tracker.domain.Note) Map(java.util.Map) CodeGenerator(org.hisp.dhis.common.CodeGenerator) HashMap(java.util.HashMap) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) ArrayList(java.util.ArrayList) Relationship(org.hisp.dhis.tracker.domain.Relationship) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Event(org.hisp.dhis.tracker.domain.Event)

Example 85 with Enrollment

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

the class TrackerEnrollmentsExportController method getInstances.

@GetMapping(produces = APPLICATION_JSON_VALUE)
PagingWrapper<Enrollment> getInstances(TrackerEnrollmentCriteria trackerEnrollmentCriteria) {
    PagingWrapper<Enrollment> enrollmentPagingWrapper = new PagingWrapper<>();
    List<org.hisp.dhis.dxf2.events.enrollment.Enrollment> enrollmentList;
    if (trackerEnrollmentCriteria.getEnrollment() == null) {
        ProgramInstanceQueryParams params = enrollmentCriteriaMapper.getFromUrl(trackerEnrollmentCriteria);
        Enrollments enrollments = enrollmentService.getEnrollments(params);
        if (trackerEnrollmentCriteria.isPagingRequest()) {
            enrollmentPagingWrapper = enrollmentPagingWrapper.withPager(PagingWrapper.Pager.fromLegacy(trackerEnrollmentCriteria, enrollments.getPager()));
        }
        enrollmentList = enrollments.getEnrollments();
    } else {
        Set<String> enrollmentIds = TextUtils.splitToArray(trackerEnrollmentCriteria.getEnrollment(), TextUtils.SEMICOLON);
        enrollmentList = enrollmentIds != null ? enrollmentIds.stream().map(enrollmentService::getEnrollment).collect(Collectors.toList()) : Collections.emptyList();
    }
    return enrollmentPagingWrapper.withInstances(ENROLLMENT_MAPPER.fromCollection(enrollmentList));
}
Also used : PagingWrapper(org.hisp.dhis.webapi.controller.event.webrequest.PagingWrapper) Enrollments(org.hisp.dhis.dxf2.events.enrollment.Enrollments) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

Enrollment (org.hisp.dhis.tracker.domain.Enrollment)88 Test (org.junit.jupiter.api.Test)67 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)53 Event (org.hisp.dhis.tracker.domain.Event)25 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)25 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)24 Program (org.hisp.dhis.program.Program)21 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)19 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)17 List (java.util.List)16 Optional (java.util.Optional)15 ProgramInstance (org.hisp.dhis.program.ProgramInstance)15 TrackerPreheat (org.hisp.dhis.tracker.preheat.TrackerPreheat)15 Sets (com.google.common.collect.Sets)14 Collectors (java.util.stream.Collectors)14 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)14 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)14 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)14 BeforeEach (org.junit.jupiter.api.BeforeEach)14 Mockito.when (org.mockito.Mockito.when)14