From fcbe7143924b39e5eeac37598577a2307a6b83a0 Mon Sep 17 00:00:00 2001 From: luca Date: Mon, 13 Nov 2023 10:40:56 +0100 Subject: [PATCH 1/2] fix: include relationships in event endpoint --- .../hisp/dhis/tracker/TrackerNtiApiTest.java | 11 ++++ .../tracker/importer/TrackerExportTests.java | 63 ++++++++++++++++++- .../resources/setup/tracker_metadata.json | 35 +++++++++++ .../export/TrackerEventsExportController.java | 4 ++ 4 files changed, 112 insertions(+), 1 deletion(-) diff --git a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/TrackerNtiApiTest.java b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/TrackerNtiApiTest.java index 2fde6e9b76ce..51f9d298482f 100644 --- a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/TrackerNtiApiTest.java +++ b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/TrackerNtiApiTest.java @@ -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(); diff --git a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/importer/TrackerExportTests.java b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/importer/TrackerExportTests.java index b8bdf058370f..7fc7cb899f91 100644 --- a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/importer/TrackerExportTests.java +++ b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/importer/TrackerExportTests.java @@ -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; @@ -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 @@ -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() @@ -400,6 +406,61 @@ 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 shouldReturnDescOrderedEventByTEIAttribute() { + ApiResponse response = + trackerActions.get("events?order=dIVt4l5vIOa:desc&event=olfXZzSGacW;ZwwuwNp6gVd"); + + response.validate().statusCode(200).body("instances", hasSize(equalTo(2))); + List events = response.extractList("instances.event.flatten()"); + assertEquals( + List.of("olfXZzSGacW", "ZwwuwNp6gVd"), events, "Events are not in the correct order"); + } + + @Test + public void shouldReturnAscOrderedEventByTEIAttribute() { + ApiResponse response = + trackerActions.get("events?order=dIVt4l5vIOa:asc&event=olfXZzSGacW;ZwwuwNp6gVd"); + + response.validate().statusCode(200).body("instances", hasSize(equalTo(2))); + List events = response.extractList("instances.event.flatten()"); + assertEquals( + List.of("ZwwuwNp6gVd", "olfXZzSGacW"), events, "Events are not in the correct order"); + } + @Test public void shouldReturnProgramStageListOrderedByProgramStageAscWhenFieldsAndOrderEqualToProgramStage() { diff --git a/dhis-2/dhis-test-e2e/src/test/resources/setup/tracker_metadata.json b/dhis-2/dhis-test-e2e/src/test/resources/setup/tracker_metadata.json index 61704d09a20d..719ed59044dc 100644 --- a/dhis-2/dhis-test-e2e/src/test/resources/setup/tracker_metadata.json +++ b/dhis-2/dhis-test-e2e/src/test/resources/setup/tracker_metadata.json @@ -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----" + } + } + } } ] } \ No newline at end of file diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/export/TrackerEventsExportController.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/export/TrackerEventsExportController.java index 21d59f7eb47d..dd9f321fa29e 100644 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/export/TrackerEventsExportController.java +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/export/TrackerEventsExportController.java @@ -101,6 +101,10 @@ public PagingWrapper getEvents( throws WebMessageException { EventQueryParams eventQueryParams = requestToSearchParamsMapper.map(eventCriteria); + EventParams eventParams = eventsMapper.map(fields); + + eventQueryParams.setIncludeRelationships(eventParams.isIncludeRelationships()); + if (areAllEnrollmentsInvalid(eventCriteria, eventQueryParams)) { return new PagingWrapper().withInstances(Collections.emptyList()); } From bdc24bbb40ca0d0d6ea901217b66d0c0d0ad10c3 Mon Sep 17 00:00:00 2001 From: luca Date: Mon, 13 Nov 2023 11:10:30 +0100 Subject: [PATCH 2/2] fix: unsupported tests --- .../tracker/importer/TrackerExportTests.java | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/importer/TrackerExportTests.java b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/importer/TrackerExportTests.java index 7fc7cb899f91..528f7f17d9ff 100644 --- a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/importer/TrackerExportTests.java +++ b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/tracker/importer/TrackerExportTests.java @@ -439,28 +439,6 @@ public void shouldReturnFilteredEvent() { .body("event", equalTo("ZwwuwNp6gVd")); } - @Test - public void shouldReturnDescOrderedEventByTEIAttribute() { - ApiResponse response = - trackerActions.get("events?order=dIVt4l5vIOa:desc&event=olfXZzSGacW;ZwwuwNp6gVd"); - - response.validate().statusCode(200).body("instances", hasSize(equalTo(2))); - List events = response.extractList("instances.event.flatten()"); - assertEquals( - List.of("olfXZzSGacW", "ZwwuwNp6gVd"), events, "Events are not in the correct order"); - } - - @Test - public void shouldReturnAscOrderedEventByTEIAttribute() { - ApiResponse response = - trackerActions.get("events?order=dIVt4l5vIOa:asc&event=olfXZzSGacW;ZwwuwNp6gVd"); - - response.validate().statusCode(200).body("instances", hasSize(equalTo(2))); - List events = response.extractList("instances.event.flatten()"); - assertEquals( - List.of("ZwwuwNp6gVd", "olfXZzSGacW"), events, "Events are not in the correct order"); - } - @Test public void shouldReturnProgramStageListOrderedByProgramStageAscWhenFieldsAndOrderEqualToProgramStage() {