From fdfff2b6b239bad86a3bf1f9db91fe2675d60073 Mon Sep 17 00:00:00 2001 From: teleivo Date: Wed, 15 Jan 2025 14:00:58 +0100 Subject: [PATCH] chore: align /tracker/relationships?trackedEntity= attributes with https://github.com/dhis2/dhis2-core/pull/17572 --- .../DefaultTrackedEntityService.java | 38 ++++--------------- .../RelationshipsExportControllerTest.java | 17 ++++++--- 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/DefaultTrackedEntityService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/DefaultTrackedEntityService.java index c0e191468601..0996d6c512d0 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/DefaultTrackedEntityService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/DefaultTrackedEntityService.java @@ -236,9 +236,7 @@ public TrackedEntity getTrackedEntity( } } - UserDetails user = getCurrentUserDetails(); - TrackedEntity trackedEntity = getTrackedEntity(trackedEntityUid, program, params, user, false); - return trackedEntity; + return getTrackedEntity(trackedEntityUid, program, params, getCurrentUserDetails()); } /** @@ -249,11 +247,7 @@ public TrackedEntity getTrackedEntity( * @throws ForbiddenException if TE owner is not in user's scope or not enough sharing access */ private TrackedEntity getTrackedEntity( - UID uid, - Program program, - TrackedEntityParams params, - UserDetails user, - boolean includeDeleted) + UID uid, Program program, TrackedEntityParams params, UserDetails user) throws NotFoundException, ForbiddenException { TrackedEntity trackedEntity = trackedEntityStore.getByUid(uid.getValue()); if (trackedEntity == null) { @@ -280,32 +274,16 @@ private TrackedEntity getTrackedEntity( } } - TrackedEntity result = new TrackedEntity(); - result.setId(trackedEntity.getId()); - result.setUid(trackedEntity.getUid()); - result.setOrganisationUnit(trackedEntity.getOrganisationUnit()); - result.setTrackedEntityType(trackedEntity.getTrackedEntityType()); - result.setCreated(trackedEntity.getCreated()); - result.setCreatedAtClient(trackedEntity.getCreatedAtClient()); - result.setLastUpdated(trackedEntity.getLastUpdated()); - result.setLastUpdatedAtClient(trackedEntity.getLastUpdatedAtClient()); - result.setInactive(trackedEntity.isInactive()); - result.setGeometry(trackedEntity.getGeometry()); - result.setDeleted(trackedEntity.isDeleted()); - result.setPotentialDuplicate(trackedEntity.isPotentialDuplicate()); - result.setStoredBy(trackedEntity.getStoredBy()); - result.setCreatedByUserInfo(trackedEntity.getCreatedByUserInfo()); - result.setLastUpdatedByUserInfo(trackedEntity.getLastUpdatedByUserInfo()); - result.setGeometry(trackedEntity.getGeometry()); if (params.isIncludeEnrollments()) { - result.setEnrollments(getEnrollments(trackedEntity, user, includeDeleted, program)); + trackedEntity.setEnrollments(getEnrollments(trackedEntity, user, false, program)); } - setRelationshipItems(result, trackedEntity, params, includeDeleted); + setRelationshipItems(trackedEntity, trackedEntity, params, false); if (params.isIncludeProgramOwners()) { - result.setProgramOwners(getTrackedEntityProgramOwners(trackedEntity, program)); + trackedEntity.setProgramOwners(getTrackedEntityProgramOwners(trackedEntity, program)); } - result.setTrackedEntityAttributeValues(getTrackedEntityAttributeValues(trackedEntity, program)); - return result; + trackedEntity.setTrackedEntityAttributeValues( + getTrackedEntityAttributeValues(trackedEntity, program)); + return trackedEntity; } private Set getEnrollments( diff --git a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/relationship/RelationshipsExportControllerTest.java b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/relationship/RelationshipsExportControllerTest.java index b10ce8864dcd..9261a496081b 100644 --- a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/relationship/RelationshipsExportControllerTest.java +++ b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/relationship/RelationshipsExportControllerTest.java @@ -27,6 +27,7 @@ */ package org.hisp.dhis.webapi.controller.tracker.export.relationship; +import static org.hisp.dhis.test.utils.Assertions.assertIsEmpty; import static org.hisp.dhis.test.utils.Assertions.assertStartsWith; import static org.hisp.dhis.webapi.controller.tracker.JsonAssertions.assertContainsAll; import static org.hisp.dhis.webapi.controller.tracker.JsonAssertions.assertEnrollmentWithinRelationship; @@ -652,7 +653,11 @@ void getRelationshipsByTrackedEntityWithEnrollments() { } @Test - void getRelationshipsByTrackedEntityAndEnrollmentWithAttributes() { + void getRelationshipsByTrackedEntityAndEnrollmentWithAttributesIsEmpty() { + // Tracked entity attribute values are owned by the tracked entity and only mapped onto the + // enrollment on export. Program tracked entity attributes are only returned by the underlying + // TE service if a program is + // provided which is not possible on the relationship endpoint. TrackedEntity to = trackedEntity(orgUnit); to.setTrackedEntityAttributeValues( Set.of(attributeValue(tea, to, "12"), attributeValue(tea2, to, "24"))); @@ -689,13 +694,13 @@ void getRelationshipsByTrackedEntityAndEnrollmentWithAttributes() { JsonList enrollmentAttr = relationships.get(0).getFrom().getEnrollment().getAttributes(); - assertContainsAll(List.of(tea2.getUid()), enrollmentAttr, JsonAttribute::getAttribute); - assertContainsAll(List.of("24"), enrollmentAttr, JsonAttribute::getValue); + assertIsEmpty( + enrollmentAttr.toList(JsonAttribute::getAttribute), + "program attributes should not be returned as no program can be provided"); JsonList teAttributes = relationships.get(0).getTo().getTrackedEntity().getAttributes(); - assertContainsAll( - List.of(tea.getUid(), tea2.getUid()), teAttributes, JsonAttribute::getAttribute); - assertContainsAll(List.of("12", "24"), teAttributes, JsonAttribute::getValue); + assertContainsAll(List.of(tea.getUid()), teAttributes, JsonAttribute::getAttribute); + assertContainsAll(List.of("12"), teAttributes, JsonAttribute::getValue); } @Test