Search in sources :

Example 31 with TrackedEntityInstance

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

the class TrackedEntityInstanceAggregateTest method testTrackedEntityInstanceRelationshipsTei2Event.

@Test
void testTrackedEntityInstanceRelationshipsTei2Event() {
    final String[] relationshipItemsUid = new String[2];
    doInTransaction(() -> {
        org.hisp.dhis.trackedentity.TrackedEntityInstance t1 = this.persistTrackedEntityInstance();
        org.hisp.dhis.trackedentity.TrackedEntityInstance t2 = this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        sessionFactory.getCurrentSession().flush();
        sessionFactory.getCurrentSession().clear();
        t2 = manager.getByUid(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, Collections.singletonList(t2.getUid())).get(0);
        ProgramInstance pi = t2.getProgramInstances().iterator().next();
        final ProgramStageInstance psi = pi.getProgramStageInstances().iterator().next();
        this.persistRelationship(t1, psi);
        relationshipItemsUid[0] = t1.getUid();
        relationshipItemsUid[1] = psi.getUid();
    });
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setIncludeAllAttributes(true);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    params.setIncludeRelationships(true);
    params.setIncludeEnrollments(true);
    params.setIncludeEvents(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().getEvent().getEvent(), 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) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Example 32 with TrackedEntityInstance

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

the class TrackedEntityInstanceAggregateTest method testFetchTrackedEntityInstancesWithExplicitUid.

@Test
void testFetchTrackedEntityInstancesWithExplicitUid() {
    final String[] teiUid = new String[2];
    doInTransaction(() -> {
        org.hisp.dhis.trackedentity.TrackedEntityInstance t1 = this.persistTrackedEntityInstance();
        org.hisp.dhis.trackedentity.TrackedEntityInstance t2 = this.persistTrackedEntityInstance();
        teiUid[0] = t1.getUid();
        teiUid[1] = t2.getUid();
    });
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.getTrackedEntityInstanceUids().add(teiUid[0]);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(trackedEntityInstances, hasSize(1));
    assertThat(trackedEntityInstances.get(0).getTrackedEntityInstance(), is(teiUid[0]));
    // Query 2 tei uid explicitly
    queryParams.getTrackedEntityInstanceUids().add(teiUid[1]);
    final List<TrackedEntityInstance> multiTrackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(multiTrackedEntityInstances, hasSize(2));
    Set<String> teis = multiTrackedEntityInstances.stream().map(t -> t.getTrackedEntityInstance()).collect(Collectors.toSet());
    assertTrue(teis.contains(teiUid[0]));
    assertTrue(teis.contains(teiUid[1]));
}
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) 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 33 with TrackedEntityInstance

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

the class TrackedEntityInstanceAggregateTest method testFetchTrackedEntityInstancesWithLastUpdatedParameter.

@Test
void testFetchTrackedEntityInstancesWithLastUpdatedParameter() {
    doInTransaction(() -> {
        this.persistTrackedEntityInstance();
        this.persistTrackedEntityInstance();
        this.persistTrackedEntityInstance();
        this.persistTrackedEntityInstance();
    });
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setLastUpdatedStartDate(Date.from(Instant.now().minus(1, ChronoUnit.DAYS)));
    queryParams.setLastUpdatedEndDate(new Date());
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(trackedEntityInstances, hasSize(4));
    assertThat(trackedEntityInstances.get(0).getEnrollments(), hasSize(0));
    // Update last updated start date to today
    queryParams.setLastUpdatedStartDate(Date.from(Instant.now().plus(1, ChronoUnit.DAYS)));
    final List<TrackedEntityInstance> limitedTTrackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(limitedTTrackedEntityInstances, hasSize(0));
}
Also used : TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Date(java.util.Date) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Example 34 with TrackedEntityInstance

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

the class TrackedEntityInstanceAggregateTest method testEnrollmentFollowup.

@Test
void testEnrollmentFollowup() {
    Map<String, Object> enrollmentValues = new HashMap<>();
    enrollmentValues.put("followup", Boolean.TRUE);
    doInTransaction(() -> this.persistTrackedEntityInstanceWithEnrollmentAndEvents(enrollmentValues));
    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);
    assertThat(enrollment.getFollowup(), is(true));
    assertThat(event.getFollowup(), is(true));
}
Also used : HashMap(java.util.HashMap) 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) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Example 35 with TrackedEntityInstance

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

the class TrackedEntityInstanceAggregateTest method testFetchTrackedEntityInstancesWithEventFilters.

@Test
void testFetchTrackedEntityInstancesWithEventFilters() {
    doInTransaction(() -> {
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
    });
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setProgram(programA);
    queryParams.setEventStatus(EventStatus.COMPLETED);
    queryParams.setEventStartDate(Date.from(Instant.now().minus(10, ChronoUnit.DAYS)));
    queryParams.setEventEndDate(Date.from(Instant.now().plus(10, ChronoUnit.DAYS)));
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(trackedEntityInstances, hasSize(4));
    // Update status to active
    queryParams.setEventStatus(EventStatus.ACTIVE);
    final List<TrackedEntityInstance> limitedTrackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(limitedTrackedEntityInstances, hasSize(0));
    // Update status to overdue
    queryParams.setEventStatus(EventStatus.OVERDUE);
    final List<TrackedEntityInstance> limitedTrackedEntityInstances2 = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(limitedTrackedEntityInstances2, hasSize(0));
    // Update status to schedule
    queryParams.setEventStatus(EventStatus.SCHEDULE);
    final List<TrackedEntityInstance> limitedTrackedEntityInstances3 = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(limitedTrackedEntityInstances3, hasSize(0));
    // Update status to schedule
    queryParams.setEventStatus(EventStatus.SKIPPED);
    final List<TrackedEntityInstance> limitedTrackedEntityInstances4 = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(limitedTrackedEntityInstances4, hasSize(0));
    // Update status to visited
    queryParams.setEventStatus(EventStatus.VISITED);
    final List<TrackedEntityInstance> limitedTrackedEntityInstances5 = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(limitedTrackedEntityInstances5, hasSize(0));
}
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)

Aggregations

TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)61 Test (org.junit.jupiter.api.Test)57 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)50 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)37 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)34 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)29 Date (java.util.Date)26 TrackerTest (org.hisp.dhis.dxf2.TrackerTest)26 ArrayList (java.util.ArrayList)25 List (java.util.List)25 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)25 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)24 Event (org.hisp.dhis.dxf2.events.event.Event)23 Program (org.hisp.dhis.program.Program)22 ProgramInstance (org.hisp.dhis.program.ProgramInstance)21 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)20 Collectors (java.util.stream.Collectors)19 User (org.hisp.dhis.user.User)17 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)16 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)16