Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: move TrackedEntitiesExportControllerTest to postgres DHIS2-18541 #19672

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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) {
Expand All @@ -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<Enrollment> getEnrollments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public class TrackedEntityOperationParams {
/** Tracked entity type to fetch. */
private UID trackedEntityType;

private OrganisationUnitSelectionMode orgUnitMode;
@Builder.Default
private OrganisationUnitSelectionMode orgUnitMode = OrganisationUnitSelectionMode.ACCESSIBLE;

@Getter @Builder.Default
private AssignedUserQueryParam assignedUserQueryParam = AssignedUserQueryParam.ALL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,59 @@ public static void assertNotEmpty(Collection<?> actual, String message) {
assertFalse(actual.isEmpty(), message);
}

/**
* Asserts that the given collection is not null and not empty.
*
* @param actual the collection.
* @param messageSupplier fails with this supplied message
*/
public static void assertNotEmpty(Collection<?> actual, Supplier<String> messageSupplier) {
assertNotNull(actual, messageSupplier);
assertFalse(actual.isEmpty(), messageSupplier);
}

/**
* Asserts that the given collection contains the expected number of elements.
*
* @param actual the collection.
*/
public static void assertHasSize(int expected, Collection<?> actual) {
assert expected > 0 : "use assertIsEmpty";

assertNotEmpty(actual);
assertEquals(
assertHasSize(
expected,
actual.size(),
actual,
() ->
String.format(
"expected collection to contain %d elements, it has %d instead: '%s'",
expected, actual.size(), actual));
}

/**
* Asserts that the given collection contains the expected number of elements.
*
* @param actual the collection.
* @param messageSupplier fails with this supplied message
*/
public static void assertHasSize(
int expected, Collection<?> actual, Supplier<String> messageSupplier) {
assert expected > 0 : "use assertIsEmpty";

assertNotEmpty(actual);
assertEquals(expected, actual.size(), messageSupplier);
}

/**
* Asserts that the given collection contains the expected number of elements.
*
* @param actual the collection.
* @param message fails with this message
*/
public static void assertHasSize(int expected, Collection<?> actual, String message) {
assert expected > 0 : "use assertIsEmpty";

assertNotEmpty(actual);
assertEquals(expected, actual.size(), message);
}

/**
* Asserts that the given string starts with the expected prefix.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@
"attribute": {
"id": "j45AR9cBQKc"
},
"value": "programStage qLZC0lvvxQH"
"value": "multi-program-stage-attribute"
}
],
"autoGenerateEvent": true,
Expand Down Expand Up @@ -1126,15 +1126,7 @@
}
],
"validationStrategy": "ON_UPDATE_AND_INSERT",
"featureType": "POINT",
"attributeValues": [
{
"attribute": {
"id": "j45AR9cBQKc"
},
"value": "multi-program-stage-attribute"
}
]
"featureType": "POINT"
},
{
"id": "SKNvpoLioON",
Expand Down Expand Up @@ -1851,6 +1843,29 @@
},
"relationshipEntity": "PROGRAM_STAGE_INSTANCE"
}
},
{
"id": "m1575931405",
"displayFromToName": "Parent Of",
"displayName": "Parent to Child",
"fromConstraint": {
"relationshipEntity": "TRACKED_ENTITY_INSTANCE",
"trackedEntityType": {
"id": "ja8NY4PW7Xm"
}
},
"fromToName": "Child Of",
"name": "Tracked entity to tracked entity",
"sharing": {
"owner": null,
"public": "r-------"
},
"toConstraint": {
"relationshipEntity": "TRACKED_ENTITY_INSTANCE",
"trackedEntityType": {
"id": "ja8NY4PW7Xm"
}
}
}
],
"trackedEntityAttributes": [
Expand Down Expand Up @@ -2230,6 +2245,41 @@
],
"username": "trackeradmin"
},
{
"id": "Z7870757a75",
"access": {
"delete": true,
"externalize": true,
"manage": true,
"read": true,
"update": true,
"write": true
},
"dataViewOrganisationUnits": [
{
"id": "h4w96yEMlzO"
}
],
"displayName": "basicuser",
"email": "basic@dhis2.org",
"firstName": "basic",
"name": "user",
"organisationUnits": [
{
"id": "h4w96yEMlzO"
}
],
"password": "Test123###...",
"passwordLastUpdated": "2020-05-31T08:57:59.060",
"phoneNumber": "+4740332255",
"surname": "basic",
"userRoles": [
{
"id": "UbhT3bXWUyb"
}
],
"username": "basicuser"
},
{
"id": "o1HMTIzBGo7",
"access": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ void shouldReturnEnrollmentsFromSpecifiedProgramWhenRequestingSingleTrackedEntit
trackedEntityService.getTrackedEntity(
UID.of(trackedEntityA), UID.of(programA), TrackedEntityParams.TRUE);

assertContainsOnly(Set.of(enrollmentA), trackedEntity.getEnrollments());
assertContainsOnly(Set.of(enrollmentA), trackedEntity.getEnrollments(), Enrollment::getUid);
}

@Test
Expand All @@ -740,7 +740,9 @@ void shouldReturnAllEnrollmentsWhenRequestingSingleTrackedEntityAndNoProgramSpec
UID.of(trackedEntityA), null, TrackedEntityParams.TRUE);

assertContainsOnly(
Set.of(enrollmentA, enrollmentB, enrollmentProgramB), trackedEntity.getEnrollments());
Set.of(enrollmentA, enrollmentB, enrollmentProgramB),
trackedEntity.getEnrollments(),
Enrollment::getUid);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")));
Expand Down Expand Up @@ -689,13 +694,13 @@ void getRelationshipsByTrackedEntityAndEnrollmentWithAttributes() {

JsonList<JsonAttribute> 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<JsonAttribute> 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
Expand Down
Loading
Loading