Skip to content

Commit

Permalink
(chore) (tests) Update _has parameter in patient provider and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Francisco committed Oct 17, 2024
1 parent 055c60d commit 827ccdb
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.HasAndListParam;
Expand All @@ -37,8 +36,8 @@
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.hibernate.sql.JoinType;
import org.openmrs.Cohort;
import org.openmrs.CohortMembership;
import org.openmrs.Patient;
import org.openmrs.PatientIdentifierType;
Expand Down Expand Up @@ -141,7 +140,10 @@ protected void handleHasAndListParam(Criteria criteria, HasAndListParam hasAndLi
case FhirConstants.GROUP:
switch (hasParam.getReferenceFieldName()) {
case FhirConstants.INCLUDE_MEMBER_PARAM:
groupIds.add(paramValue);
switch (hasParam.getParameterName()) {
case "id":
groupIds.add(paramValue);
}
break;
}
break;
Expand All @@ -164,12 +166,11 @@ private void verifyPatientInGroups(Criteria criteria, List<String> groupIds) {
}

private List<Integer> getGroupMemberIds(String groupId) {
Cohort cohort = groupDao.get(groupId);
if (cohort != null) {
return cohort.getMemberships().stream().map(CohortMembership::getPatientId).collect(Collectors.toList());
} else {
return Collections.emptyList();
}
Criteria subquery = getSessionFactory().getCurrentSession().createCriteria(CohortMembership.class, "cm")
.createAlias("cm.cohort", "co").add(eq("co.uuid", groupId))
.setProjection(Projections.property("cm.patientId"));

return subquery.list();
}

private void handlePatientQuery(Criteria criteria, @Nonnull StringAndListParam query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ public class FhirPatientDaoImplTest extends BaseModuleContextSensitiveTest {

private static final String GROUP_B = "a25ce1d7-326c-43ff-a87f-63d9d2f60f11";

private static final String PATIENT_GROUP_A = "61b38324-e2fd-4feb-95b7-9e9a2a4400df";
private static final String GROUP_C = "6f4816fb-0b75-4e25-aac0-4944a6d3b697";

private static final String PATIENT1_GROUP_A = "256ccf6d-6b41-455c-9be2-51ff4386ae76";

private static final String PATIENT2_GROUP_A = "30e2aa2a-4ed1-415d-84c5-ba29016c14b7";

private static final String PATIENT3_GROUP_A = "8d703ff2-c3e2-4070-9737-73e713d5a50d";

private static final String PATIENT4_GROUP_A = "ca17fcc5-ec96-487f-b9ea-42973c8973e3";

private static final String PATIENT1_GROUP_C = "c7c1416f9-3beb-40fe-9043-1ce70ea9df53";

private static final String[] PATIENT_SEARCH_DATA_FILES = {
"org/openmrs/module/fhir2/api/dao/impl/FhirPatientDaoImplTest_initial_data.xml",
Expand Down Expand Up @@ -97,15 +107,36 @@ public void getPatientByUuid_shouldReturnNullIfPatientNotFound() {
}

@Test
public void getSearchResults_shouldReturnPatientSearchResults() {
public void getSearchResults_shouldReturnPatientsSearchResults() {
HasAndListParam groupParam = new HasAndListParam().addAnd(
new HasOrListParam().add(new HasParam(FhirConstants.GROUP, FhirConstants.INCLUDE_MEMBER_PARAM, "id", GROUP_A)));

SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.HAS_SEARCH_HANDLER, groupParam);
List<Patient> result = dao.getSearchResults(theParams);

assertThat(result, notNullValue());
assertThat(result.get(0).getUuid(), equalTo(PATIENT_GROUP_A));
assertThat(result.size(), equalTo(4));
assertThat(result.get(0).getUuid(), equalTo(PATIENT1_GROUP_A));
assertThat(result.get(1).getUuid(), equalTo(PATIENT2_GROUP_A));
assertThat(result.get(2).getUuid(), equalTo(PATIENT3_GROUP_A));
assertThat(result.get(3).getUuid(), equalTo(PATIENT4_GROUP_A));
}

@Test
public void getSearchResults_shouldReturnPatientsFromTwoGroupsSearchResults() {
HasAndListParam groupParam = new HasAndListParam().addAnd(
new HasOrListParam().add(new HasParam(FhirConstants.GROUP, FhirConstants.INCLUDE_MEMBER_PARAM, "id", GROUP_A))
.add(new HasParam(FhirConstants.GROUP, FhirConstants.INCLUDE_MEMBER_PARAM, "id", GROUP_C)));

SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.HAS_SEARCH_HANDLER, groupParam);
List<Patient> result = dao.getSearchResults(theParams);

assertThat(result, notNullValue());
assertThat(result.get(0).getUuid(), equalTo(PATIENT1_GROUP_A));
assertThat(result.get(1).getUuid(), equalTo(PATIENT2_GROUP_A));
assertThat(result.get(2).getUuid(), equalTo(PATIENT3_GROUP_A));
assertThat(result.get(3).getUuid(), equalTo(PATIENT4_GROUP_A));
assertThat(result.get(4).getUuid(), equalTo(PATIENT1_GROUP_C));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@
import javax.servlet.ServletException;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.HasAndListParam;
import ca.uhn.fhir.rest.param.HasOrListParam;
import ca.uhn.fhir.rest.param.StringAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.param.TokenParam;
Expand Down Expand Up @@ -495,6 +499,32 @@ public void shouldGetPatientByLastUpdatedDate() throws Exception {
equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
}

@Test
public void shouldHandleHasAndListParameterForGroupMemberId() throws Exception {
verifyUri("/Patient?_has:Group:member:id=123e4567-e89b-12d3-a456-426614174000,abcdefab-1234-abcd-1234-abcdefabcdef");

verify(patientService).searchForPatients(patientSearchParamsCaptor.capture());
HasAndListParam hasAndListParam = patientSearchParamsCaptor.getValue().getHasAndListParam();

List<HasOrListParam> hasOrListParams = hasAndListParam.getValuesAsQueryTokens();
assertThat(hasOrListParams.size(), equalTo(1));

List<String> valuesFound = new ArrayList<>();
for (HasOrListParam hasOrListParam : hasOrListParams) {
hasOrListParam.getValuesAsQueryTokens().forEach(hasParam -> {
assertThat(hasParam.getTargetResourceType(), equalTo(FhirConstants.GROUP));
assertThat(hasParam.getReferenceFieldName(), equalTo(FhirConstants.INCLUDE_MEMBER_PARAM));
assertThat(hasParam.getParameterName(), equalTo("id"));
valuesFound.add(hasParam.getParameterValue());
});
}
Collections.sort(valuesFound);

assertThat(valuesFound.size(), equalTo(2));
assertThat(valuesFound.get(0), equalTo("123e4567-e89b-12d3-a456-426614174000"));
assertThat(valuesFound.get(1), equalTo("abcdefab-1234-abcd-1234-abcdefabcdef"));
}

@Test
public void shouldAddReverseIncludedObservationsToReturnedResults() throws Exception {
verifyUri("/Patient?_revinclude=Observation:patient");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,34 @@
<person person_id="5" gender="M" dead="false" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="30e2aa2a-4ed1-415d-84c5-ba29016c14b7"/>
<person person_id="6" gender="M" dead="false" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="8d703ff2-c3e2-4070-9737-73e713d5a50d"/>
<person person_id="7" gender="M" dead="false" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="ca17fcc5-ec96-487f-b9ea-42973c8973e3"/>
<person person_id="8" gender="M" dead="false" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="c7c1416f9-3beb-40fe-9043-1ce70ea9df53"/>
<person_name person_name_id="2" preferred="true" person_id="2" given_name="John" middle_name="F" family_name="Doe" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="7e2acadc-5073-4a39-914a-debcbec8c1c9"/>
<person_name person_name_id="3" preferred="true" person_id="3" given_name="I" middle_name="am" family_name="voided" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="37e60810-0ba9-4f32-ba80-0af59f59e966"/>
<person_name person_name_id="4" preferred="true" person_id="4" given_name="Jean Claude" middle_name=" " family_name="Doe" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="89232176-29cd-47e5-9ce2-ae375436fb15"/>
<person_name person_name_id="5" preferred="true" person_id="5" given_name="Jean" middle_name=" " family_name="Claude" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="b33c207b-2096-4023-b0ba-ec3b670e5ba4"/>
<person_name person_name_id="6" preferred="true" person_id="6" given_name="Jeannette" middle_name=" " family_name="Claudent" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="355d1361-a63d-4897-b853-0f2c389138d0"/>
<person_name person_name_id="7" preferred="true" person_id="7" given_name="John" middle_name="Ho" family_name="Claudio" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="f7ea8cea-43de-4cff-8a44-21db2aaaabb8"/>
<person_name person_name_id="8" preferred="true" person_id="8" given_name="Carl" middle_name="Oliver" family_name="Charis" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="c4150da8-cb99-4b77-9483-cb5baf7d4d31"/>
<person_name person_name_id="33" preferred="false" person_id="2" given_name="Some other name" middle_name=" " family_name="Last Name of other name" creator="1" date_created="2004-01-01 00:00:00.0" voided="false" uuid="c9714a2d-a97e-4673-80f8-ac8dd5ced083"/>
<patient patient_id="2" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>
<patient patient_id="3" creator="1" date_created="2005-01-01 00:00:00.0" voided="true"/>
<patient patient_id="4" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>
<patient patient_id="5" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>
<patient patient_id="6" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>
<patient patient_id="7" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>
<patient patient_id="8" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>
<patient_identifier patient_identifier_id="1" patient_id="2" identifier="1234-4" identifier_type="1" preferred="1" location_id="1" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="e997ac86-4d8a-40f3-bedb-da84d35917b8"/>
<patient_identifier patient_identifier_id="2" patient_id="2" identifier="12345-5" identifier_type="1" preferred="0" location_id="1" creator="1" date_created="2004-01-01 00:00:00.0" voided="false" uuid="504c83c7-cfbf-4ae7-a4da-bdfa3236689f"/>
<patient_identifier patient_identifier_id="3" patient_id="3" identifier="4567-4" identifier_type="1" preferred="1" location_id="1" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="acc345a8-0ae9-4cad-92ec-7a5b0b25752f"/>
<patient_identifier patient_identifier_id="4" patient_id="4" identifier="5634-1" identifier_type="1" preferred="1" location_id="1" creator="1" date_created="2005-01-01 00:00:00.0" voided="true" uuid="e1bcf220-d116-4d98-93a4-2adf6282f797"/>
<patient_identifier patient_identifier_id="5" patient_id="5" identifier="563422-5" identifier_type="1" preferred="1" location_id="1" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="f4798e60-b9ef-47d8-afa7-54a1dc9e27c1"/>
<patient_identifier patient_identifier_id="6" patient_id="6" identifier="47622-6" identifier_type="1" preferred="1" location_id="1" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="86e0b6d5-0f4f-4acd-8c81-edeb985c0ab3"/>
<cohort cohort_id="1" name="Group A" description="Patients from Group A" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="dfb29c44-2e39-46c4-8cd7-18f21c6d47b1"/>
<cohort_member cohort_member_id="1" cohort_id="1" patient_id="2" start_date="2000-04-16" end_date="2030-04-16" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="1cf0dc57-ddc2-4f28-8e71-faf30bf2d923"/>
<cohort cohort_id="2" name="Group B" description="Patients from Group B" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="16a5538b-9463-41d7-a1d7-0ad041529479"/>
<cohort cohort_id="3" name="Group C" description="Patients from Group C" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="6f4816fb-0b75-4e25-aac0-4944a6d3b697"/>
<cohort_member cohort_member_id="1" cohort_id="1" patient_id="4" start_date="2000-04-16" end_date="2030-04-16" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="1cf0dc57-ddc2-4f28-8e71-faf30bf2d923"/>
<cohort_member cohort_member_id="2" cohort_id="1" patient_id="5" start_date="2000-04-16" end_date="2030-04-16" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="2a2f21e5-b34e-42c8-8bc8-69c482fcd449"/>
<cohort_member cohort_member_id="3" cohort_id="1" patient_id="6" start_date="2000-04-16" end_date="2030-04-16" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="bfd66f72-d058-4493-9e73-e021f89bd2aa"/>
<cohort_member cohort_member_id="4" cohort_id="1" patient_id="7" start_date="2000-04-16" end_date="2030-04-16" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="c9cb5447-1195-42f5-9dd7-421bf5d6ae82"/>
<cohort_member cohort_member_id="5" cohort_id="3" patient_id="8" start_date="2000-04-16" end_date="2030-04-16" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="7c650865-59e0-4e8e-8334-ba22ff539c08"/>
</dataset>

0 comments on commit 827ccdb

Please sign in to comment.