Search in sources :

Example 11 with Relationship

use of org.hisp.dhis.dxf2.events.trackedentity.Relationship 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 12 with Relationship

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

the class HandleRelationshipsTrackedEntityInstanceServiceTest method testUpdateTeiWithUniDirectionalRelationshipEventToTei.

@Test
void testUpdateTeiWithUniDirectionalRelationshipEventToTei() {
    TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance(this.trackedEntityInstanceA.getUid());
    RelationshipType malariaCaseLinkedToPersonRelationshipType = createMalariaCaseLinkedToPersonRelationshipType('A', programA, trackedEntityType);
    malariaCaseLinkedToPersonRelationshipType.setBidirectional(false);
    relationshipTypeService.addRelationshipType(malariaCaseLinkedToPersonRelationshipType);
    Relationship relationship = createEventToTeiRelationship('A', malariaCaseLinkedToPersonRelationshipType, trackedEntityInstance, programStageInstanceA);
    trackedEntityInstance.setRelationships(Lists.newArrayList(relationship));
    ImportSummary importSummary = trackedEntityInstanceService.updateTrackedEntityInstance(trackedEntityInstance, null, null, true);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    assertEquals(ImportStatus.ERROR, importSummary.getRelationships().getStatus());
    assertEquals("Can't update relationship '" + relationship.getRelationship() + "': TrackedEntityInstance '" + trackedEntityInstance.getTrackedEntityInstance() + "' is not the owner of the relationship", importSummary.getRelationships().getImportSummaries().get(0).getDescription());
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 13 with Relationship

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

the class HandleRelationshipsTrackedEntityInstanceServiceTest method testUpdateTeiWithBidirectionalRelationshipEventToTei.

@Test
void testUpdateTeiWithBidirectionalRelationshipEventToTei() {
    TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance(this.trackedEntityInstanceA.getUid());
    RelationshipType relationshipType = createMalariaCaseLinkedToPersonRelationshipType('A', programA, trackedEntityType);
    relationshipTypeService.addRelationshipType(relationshipType);
    Relationship relationship = createEventToTeiRelationship('A', relationshipType, trackedEntityInstance, programStageInstanceA);
    trackedEntityInstance.setRelationships(Lists.newArrayList(relationship));
    ImportSummary importSummary = trackedEntityInstanceService.updateTrackedEntityInstance(trackedEntityInstance, null, null, true);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    assertEquals(ImportStatus.SUCCESS, importSummary.getRelationships().getStatus());
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 14 with Relationship

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

the class HandleRelationshipsTrackedEntityInstanceServiceTest method createTeiToTeiRelationship.

private Relationship createTeiToTeiRelationship(char key, RelationshipType relationshipType, TrackedEntityInstance trackedEntityInstanceA, TrackedEntityInstance trackedEntityInstanceB) {
    RelationshipItem relationshipItemTeiA = new RelationshipItem();
    relationshipItemTeiA.setTrackedEntityInstance(trackedEntityInstanceA);
    RelationshipItem relationshipItemTeiB = new RelationshipItem();
    relationshipItemTeiB.setTrackedEntityInstance(trackedEntityInstanceB);
    Relationship relationship = new Relationship();
    relationship.setFrom(relationshipItemTeiA);
    relationship.setTo(relationshipItemTeiB);
    relationship.setRelationshipType(relationshipType.getUid());
    relationship.setRelationship("UID_" + key);
    relationship.setRelationshipName("Person to person");
    relationship.setBidirectional(relationshipType.isBidirectional());
    return relationship;
}
Also used : Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) RelationshipItem(org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem)

Example 15 with Relationship

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

the class JacksonRelationshipServiceTest method verifyRelationshipIsImportedIfDoesNotExist.

@Test
void verifyRelationshipIsImportedIfDoesNotExist() {
    when(relationshipService.getRelationshipByRelationship(any(org.hisp.dhis.relationship.Relationship.class))).thenReturn(Optional.empty());
    ImportSummary importSummary = subject.addRelationship(relationship, rnd.nextObject(ImportOptions.class));
    assertThat(importSummary.getStatus(), is(ImportStatus.SUCCESS));
    assertThat(importSummary.getImportCount().getImported(), is(1));
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Test(org.junit.jupiter.api.Test)

Aggregations

Relationship (org.hisp.dhis.dxf2.events.trackedentity.Relationship)30 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)14 ArrayList (java.util.ArrayList)11 Transactional (org.springframework.transaction.annotation.Transactional)10 TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)9 Test (org.junit.jupiter.api.Test)9 List (java.util.List)8 RelationshipType (org.hisp.dhis.relationship.RelationshipType)7 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)6 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)6 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)6 Optional (java.util.Optional)5 Set (java.util.Set)5 Event (org.hisp.dhis.dxf2.events.event.Event)5 Lists (com.google.common.collect.Lists)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 CurrentUserService (org.hisp.dhis.user.CurrentUserService)4