Search in sources :

Example 11 with TrackedEntityInstanceParams

use of org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams in project dhis2-core by dhis2.

the class TrackedEntityInstanceAggregateTest method testTrackedEntityInstanceMapping.

@Test
void testTrackedEntityInstanceMapping() {
    final Date currentTime = new Date();
    doInTransaction(this::persistTrackedEntityInstanceWithEnrollmentAndEvents);
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setIncludeAllAttributes(true);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    params.setIncludeEnrollments(false);
    params.setIncludeEvents(false);
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    TrackedEntityInstance trackedEntityInstance = trackedEntityInstances.get(0);
    assertThat(trackedEntityInstance.getTrackedEntityType(), is(trackedEntityTypeA.getUid()));
    assertTrue(CodeGenerator.isValidUid(trackedEntityInstance.getTrackedEntityInstance()));
    assertThat(trackedEntityInstance.getOrgUnit(), is(organisationUnitA.getUid()));
    assertThat(trackedEntityInstance.isInactive(), is(false));
    assertThat(trackedEntityInstance.isDeleted(), is(false));
    assertThat(trackedEntityInstance.getFeatureType(), is(FeatureType.NONE));
    // Dates
    checkDate(currentTime, trackedEntityInstance.getCreated(), 50L);
    checkDate(currentTime, trackedEntityInstance.getCreatedAtClient(), 50L);
    checkDate(currentTime, trackedEntityInstance.getLastUpdatedAtClient(), 50L);
    checkDate(currentTime, trackedEntityInstance.getLastUpdated(), 300L);
    // get stored by is always null
    assertThat(trackedEntityInstance.getStoredBy(), is(nullValue()));
}
Also used : TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) Date(java.util.Date) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Example 12 with TrackedEntityInstanceParams

use of org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams in project dhis2-core by dhis2.

the class TrackedEntityInstanceAggregateTest method testIncludeDeletedIsPropagetedFromTeiToEnrollmentsAndEvents.

@Test
void testIncludeDeletedIsPropagetedFromTeiToEnrollmentsAndEvents() {
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setIncludeDeleted(true);
    TrackedEntityInstanceParams params = TrackedEntityInstanceParams.TRUE;
    doInTransaction(() -> {
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
    });
    List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(trackedEntityInstances, hasSize(4));
    assertThat(trackedEntityInstances.get(0).getEnrollments(), hasSize(1));
    assertFalse(trackedEntityInstances.get(0).getEnrollments().get(0).isDeleted());
    assertThat(trackedEntityInstances.get(1).getEnrollments().get(0).getEvents(), hasSize(5));
    assertFalse(trackedEntityInstances.get(1).getEnrollments().get(0).getEvents().stream().anyMatch(Event::isDeleted));
    this.deleteOneEnrollment(trackedEntityInstances.get(0));
    this.deleteOneEvent(trackedEntityInstances.get(1).getEnrollments().get(0));
    trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(trackedEntityInstances, hasSize(4));
    assertThat(trackedEntityInstances.get(0).getEnrollments(), hasSize(1));
    assertTrue(trackedEntityInstances.get(0).getEnrollments().get(0).isDeleted());
    assertThat(trackedEntityInstances.get(1).getEnrollments().get(0).getEvents(), hasSize(5));
    assertTrue(trackedEntityInstances.get(1).getEnrollments().get(0).getEvents().stream().anyMatch(Event::isDeleted));
    queryParams.setIncludeDeleted(false);
    trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(trackedEntityInstances, hasSize(4));
    assertThat(trackedEntityInstances.get(0).getEnrollments(), hasSize(0));
    assertThat(trackedEntityInstances.get(1).getEnrollments().get(0).getEvents(), hasSize(4));
    assertFalse(trackedEntityInstances.get(1).getEnrollments().get(0).getEvents().stream().anyMatch(Event::isDeleted));
}
Also used : TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Example 13 with TrackedEntityInstanceParams

use of org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams in project dhis2-core by dhis2.

the class TrackedEntityInstanceAggregateTest method testEnrollmentMapping.

@Test
void testEnrollmentMapping() {
    final Date currentTime = new Date();
    doInTransaction(this::persistTrackedEntityInstanceWithEnrollmentAndEvents);
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setIncludeAllAttributes(true);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    params.setIncludeEnrollments(true);
    params.setIncludeEvents(false);
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    Enrollment enrollment = trackedEntityInstances.get(0).getEnrollments().get(0);
    assertThat("Tracked Entity Type does not match", enrollment.getTrackedEntityType(), is(trackedEntityTypeA.getUid()));
    assertThat("Tracked Entity Instance UID does not match", enrollment.getTrackedEntityInstance(), is(trackedEntityInstances.get(0).getTrackedEntityInstance()));
    assertThat("Org Unit UID does not match", enrollment.getOrgUnit(), is(organisationUnitA.getUid()));
    assertThat("Org Unit Name does not match", enrollment.getOrgUnitName(), is(organisationUnitA.getName()));
    assertTrue(CodeGenerator.isValidUid(enrollment.getEnrollment()));
    assertThat(enrollment.getProgram(), is(programA.getUid()));
    assertThat(enrollment.getStatus(), is(EnrollmentStatus.COMPLETED));
    assertThat(enrollment.isDeleted(), is(false));
    assertThat(enrollment.getStoredBy(), is("system-process"));
    assertThat(enrollment.getFollowup(), is(nullValue()));
    // Dates
    checkDate(currentTime, enrollment.getCreated(), 200L);
    checkDate(currentTime, enrollment.getCreatedAtClient(), 200L);
    checkDate(currentTime, enrollment.getLastUpdatedAtClient(), 200L);
    checkDate(currentTime, enrollment.getLastUpdated(), 300L);
    checkDate(currentTime, enrollment.getEnrollmentDate(), 300L);
    checkDate(currentTime, enrollment.getIncidentDate(), 300L);
    checkDate(currentTime, enrollment.getCompletedDate(), 200L);
    assertThat(enrollment.getCompletedBy(), is("hello-world"));
    // The Enrollment ID is not serialized to JSON
    assertThat(enrollment.getId(), is(notNullValue()));
}
Also used : TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) Date(java.util.Date) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Example 14 with TrackedEntityInstanceParams

use of org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams in project dhis2-core by dhis2.

the class TrackedEntityInstanceAggregateTest method testTrackedEntityInstanceRelationshipsTei2Enrollment.

@Test
void testTrackedEntityInstanceRelationshipsTei2Enrollment() {
    final String[] relationshipItemsUid = new String[2];
    doInTransaction(() -> {
        org.hisp.dhis.trackedentity.TrackedEntityInstance t1 = this.persistTrackedEntityInstance();
        org.hisp.dhis.trackedentity.TrackedEntityInstance t2 = this.persistTrackedEntityInstanceWithEnrollment();
        ProgramInstance pi = t2.getProgramInstances().iterator().next();
        this.persistRelationship(t1, pi);
        relationshipItemsUid[0] = t1.getUid();
        relationshipItemsUid[1] = pi.getUid();
    });
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setIncludeAllAttributes(true);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    params.setIncludeRelationships(true);
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    // Fetch the TEI which is the vertex of the relationship TEI <-->
    // ENROLLMENT
    Optional<TrackedEntityInstance> trackedEntityInstance = trackedEntityInstances.stream().filter(t -> t.getTrackedEntityInstance().equals(relationshipItemsUid[0])).findFirst();
    if (trackedEntityInstance.isPresent()) {
        assertThat(trackedEntityInstance.get().getRelationships(), hasSize(1));
        final Relationship relationship = trackedEntityInstance.get().getRelationships().get(0);
        assertThat(relationship.getFrom().getTrackedEntityInstance().getTrackedEntityInstance(), is(relationshipItemsUid[0]));
        assertThat(relationship.getTo().getEnrollment().getEnrollment(), is(relationshipItemsUid[1]));
    } else {
        fail();
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) QueryItem(org.hisp.dhis.common.QueryItem) Date(java.util.Date) ValueType(org.hisp.dhis.common.ValueType) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) DateTimeFormatMatcher.hasDateTimeFormat(org.hisp.dhis.matchers.DateTimeFormatMatcher.hasDateTimeFormat) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) EnrollmentStatus(org.hisp.dhis.dxf2.events.enrollment.EnrollmentStatus) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ProgramOwner(org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner) User(org.hisp.dhis.user.User) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) TrackedEntityProgramOwnerService(org.hisp.dhis.trackedentity.TrackedEntityProgramOwnerService) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) SessionFactory(org.hibernate.SessionFactory) QueryOperator(org.hisp.dhis.common.QueryOperator) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Set(java.util.Set) AggregationType(org.hisp.dhis.analytics.AggregationType) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) EventStatus(org.hisp.dhis.event.EventStatus) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Event(org.hisp.dhis.dxf2.events.event.Event) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) List(java.util.List) FeatureType(org.hisp.dhis.organisationunit.FeatureType) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) TrackerTest(org.hisp.dhis.dxf2.TrackerTest) Collections(java.util.Collections) DateUtils(org.hisp.dhis.util.DateUtils) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Example 15 with TrackedEntityInstanceParams

use of org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams in project dhis2-core by dhis2.

the class TrackedEntityInstanceAggregateTest method testEventMapping.

@Test
void testEventMapping() {
    final Date currentTime = new Date();
    doInTransaction(this::persistTrackedEntityInstanceWithEnrollmentAndEvents);
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setIncludeAllAttributes(true);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    params.setIncludeEnrollments(true);
    params.setIncludeEvents(true);
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    TrackedEntityInstance tei = trackedEntityInstances.get(0);
    Enrollment enrollment = tei.getEnrollments().get(0);
    Event event = enrollment.getEvents().get(0);
    assertNotNull(event);
    // The id is not serialized to JSON
    assertThat(event.getId(), is(notNullValue()));
    assertThat(event.getUid(), is(nullValue()));
    assertTrue(CodeGenerator.isValidUid(event.getEvent()));
    assertThat(event.getStatus(), is(EventStatus.COMPLETED));
    assertThat(event.getProgram(), is(programA.getUid()));
    assertThat(event.getProgramStage(), is(programStageA1.getUid()));
    assertThat(event.getEnrollment(), is(enrollment.getEnrollment()));
    assertThat(event.getEnrollmentStatus(), is(enrollment.getStatus()));
    assertThat(event.getOrgUnit(), is(organisationUnitA.getUid()));
    assertThat(event.getOrgUnitName(), is(organisationUnitA.getName()));
    assertThat(event.getTrackedEntityInstance(), is(tei.getTrackedEntityInstance()));
    assertThat(event.getAttributeOptionCombo(), is(DEF_COC_UID));
    assertThat(event.isDeleted(), is(false));
    assertThat(event.getStoredBy(), is("[Unknown]"));
    assertThat(event.getFollowup(), is(nullValue()));
    // Dates
    checkDate(currentTime, event.getCreated(), 500L);
    checkDate(currentTime, event.getLastUpdated(), 500L);
    assertThat(event.getEventDate(), is(notNullValue()));
    checkDate(currentTime, event.getDueDate(), 500L);
    checkDate(currentTime, event.getCreatedAtClient(), 500L);
    checkDate(currentTime, event.getLastUpdatedAtClient(), 500L);
    checkDate(currentTime, event.getCompletedDate(), 500L);
    assertThat(event.getCompletedBy(), is("[Unknown]"));
}
Also used : TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) Event(org.hisp.dhis.dxf2.events.event.Event) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) Date(java.util.Date) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Aggregations

TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)30 TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)26 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)26 TrackerTest (org.hisp.dhis.dxf2.TrackerTest)24 Test (org.junit.jupiter.api.Test)24 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)8 Date (java.util.Date)7 List (java.util.List)6 Event (org.hisp.dhis.dxf2.events.event.Event)6 ProgramOwner (org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner)6 Collectors (java.util.stream.Collectors)5 Relationship (org.hisp.dhis.dxf2.events.trackedentity.Relationship)5 User (org.hisp.dhis.user.User)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Set (java.util.Set)4 QueryItem (org.hisp.dhis.common.QueryItem)4 TrackedEntityInstanceService (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService)4 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)4 Sets (com.google.common.collect.Sets)3