Search in sources :

Example 16 with Note

use of org.hisp.dhis.tracker.domain.Note 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 17 with Note

use of org.hisp.dhis.tracker.domain.Note 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 18 with Note

use of org.hisp.dhis.tracker.domain.Note 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)

Aggregations

Note (org.hisp.dhis.tracker.domain.Note)18 Test (org.junit.jupiter.api.Test)15 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)10 TrackedEntityComment (org.hisp.dhis.trackedentitycomment.TrackedEntityComment)9 TrackerImportValidationContext (org.hisp.dhis.tracker.validation.TrackerImportValidationContext)7 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)6 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)5 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 Matchers.hasSize (org.hamcrest.Matchers.hasSize)4 BeanRandomizer (org.hisp.dhis.random.BeanRandomizer)4 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)4 Event (org.hisp.dhis.tracker.domain.Event)4 TrackerPreheat (org.hisp.dhis.tracker.preheat.TrackerPreheat)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 Optional (java.util.Optional)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2