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..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 @@ -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,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() { 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()); }