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();
}
}
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());
}
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());
}
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;
}
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));
}
Aggregations