Skip to content

Commit

Permalink
fix: tracker/events not returning event relationships [DHIS2-16117] […
Browse files Browse the repository at this point in the history
… 2.38 ] (#15669)

* fix: include relationships in event endpoint

* fix: unsupported tests
  • Loading branch information
lucaCambi77 authored Nov 14, 2023
1 parent 7fd15fb commit e7d0024
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ protected TrackerApiResponse importRelationshipEnrollmentToTei(String enrollment
return trackerActions.postAndGetJobReport(payload).validateSuccessfulImport();
}

protected TrackerApiResponse importRelationshipEventToTei(String event, String tei) {
JsonObject payload =
new RelationshipDataBuilder()
.setFromEntity("event", event)
.setToTrackedEntity(tei)
.setRelationshipType("gdc6uOvgoji")
.array();

return trackerActions.postAndGetJobReport(payload).validateSuccessfulImport();
}

@AfterEach
public void afterEachNTI() {
loginActions.loginAsSuperUser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
Expand Down Expand Up @@ -87,6 +88,8 @@ public class TrackerExportTests extends TrackerNtiApiTest {

private static final String TEI_POTENTIAL_DUPLICATE = "Nav6inZRw1u";

private static String eventToTeiRelationship;

private static JsonObject teiWithEnrollmentAndEventsTemplate;

@BeforeAll
Expand All @@ -102,12 +105,15 @@ public void beforeAll() throws Exception {

enrollment = response.extractImportedEnrollments().get(0);

event = response.extractImportedEvents().get(0);

teiToTeiRelationship =
importRelationshipBetweenTeis(teiA, teiB).extractImportedRelationships().get(0);
enrollmentToTeiRelationship =
importRelationshipEnrollmentToTei(enrollment, teiB).extractImportedRelationships().get(0);

event = response.extractImportedEvents().get(0);
eventToTeiRelationship =
importRelationshipEventToTei(event, teiB).extractImportedRelationships().get(0);

teiWithEnrollmentAndEventsTemplate =
new FileReaderUtils()
Expand Down Expand Up @@ -400,6 +406,39 @@ public void shouldReturnRelationshipsByTei() {
.body("to.trackedEntity.trackedEntity", equalTo(teiB));
}

@Test
public void shouldReturnRelationshipsWhenEventHasRelationshipsAndFieldsIncludeRelationships() {
trackerActions
.get("events?event=" + event + "&fields=relationships")
.validate()
.statusCode(200)
.body("instances", hasSize(greaterThanOrEqualTo(1)))
.rootPath("instances[0].relationships[0]")
.body("relationship", equalTo(eventToTeiRelationship))
.body("from.event.event", equalTo(event))
.body("to.trackedEntity.trackedEntity", equalTo(teiB));
}

@Test
public void shouldNotReturnRelationshipsWhenEventHasRelationshipsAndFieldsExcludeRelationships() {
trackerActions
.get("events?event=" + event)
.validate()
.statusCode(200)
.body("instances[0].relationships", emptyOrNullString());
}

@Test
public void shouldReturnFilteredEvent() {
trackerActions
.get(
"events?enrollmentOccurredAfter=2019-08-16&enrollmentOccurredBefore=2019-08-20&event=ZwwuwNp6gVd")
.validate()
.statusCode(200)
.rootPath("instances[0]")
.body("event", equalTo("ZwwuwNp6gVd"));
}

@Test
public void
shouldReturnProgramStageListOrderedByProgramStageAscWhenFieldsAndOrderEqualToProgramStage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3152,6 +3152,41 @@
"id": "OPVIvvXzNTw"
}
]
},
{
"lastUpdated": "2020-11-20T09:06:23.348",
"id": "gdc6uOvgoji",
"created": "2020-11-20T09:06:23.348",
"name": "Person to Event",
"code": "Person to Event",
"bidirectional": false,
"displayName": "Person to Event",
"fromToName": "Person to Event",
"displayFromToName": "Person to Event",
"favorite": false,
"toConstraint": {
"relationshipEntity": "TRACKED_ENTITY_INSTANCE",
"trackedEntityType": {
"id": "Q9GufDoplCL",
"code": "TA_PERSON_TET"
}
},
"fromConstraint": {
"relationshipEntity": "PROGRAM_STAGE_INSTANCE",
"programStage": {
"id": "PaOOjwLVW23"
}
},
"sharing": {
"public": "rw------",
"external": false,
"userGroups": {
"OPVIvvXzNTw": {
"id": "OPVIvvXzNTw",
"access": "rwrw----"
}
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public PagingWrapper<ObjectNode> getEvents(
throws WebMessageException {
EventQueryParams eventQueryParams = requestToSearchParamsMapper.map(eventCriteria);

EventParams eventParams = eventsMapper.map(fields);

eventQueryParams.setIncludeRelationships(eventParams.isIncludeRelationships());

if (areAllEnrollmentsInvalid(eventCriteria, eventQueryParams)) {
return new PagingWrapper<ObjectNode>().withInstances(Collections.emptyList());
}
Expand Down

0 comments on commit e7d0024

Please sign in to comment.