Added _list as a valid parameter in MatchUrlService

Wrote tests for and/or/both processing in FhirResourceDaoSearchListTest.
This commit is contained in:
Kevin Hartmann 2021-02-24 10:27:22 -05:00
parent 62bfa2cdbc
commit 1eab2caf9f
3 changed files with 102 additions and 32 deletions

View File

@ -1336,8 +1336,6 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
String key = keyIterator.next();
if (Constants.PARAM_LIST.equals((key))) {
List<List<IQueryParameterType>> andOrValues = theParams.get(key);
// FIXME KH generalize this to properly support and and or and write tests for both
// FIXME KH this is a hack to test it out in theory
theParams.remove(key);
List<List<IQueryParameterType>> hasParamValues = new ArrayList<>();
for (List<IQueryParameterType> orValues : andOrValues) {

View File

@ -14,10 +14,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static ca.uhn.fhir.rest.api.Constants.PARAM_HAS;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class FhirResourceDaoSearchListTest extends BaseJpaR4Test {
@Autowired
@ -30,37 +33,103 @@ public class FhirResourceDaoSearchListTest extends BaseJpaR4Test {
* See https://www.hl7.org/fhir/search.html#list
*/
@Test
public void testList() {
Patient patient1 = new Patient();
Patient patient2 = new Patient();
IIdType pid1 = myPatientDao.create(patient1).getId();
IIdType pid2 = myPatientDao.create(patient2).getId();
ListResource list = new ListResource();
list.addEntry().getItem().setReferenceElement(pid1);
list.addEntry().getItem().setReferenceElement(pid2);
String listIdString = myListResourceDao.create(list).getId().getValue();
public void testBasicListQuery() {
IIdType[] patientIds = createPatients(2);
String listIdString = createList(patientIds);
{
// What we need to emulate
// /Patient?_has=List:item:_id=123
SearchParameterMap map = SearchParameterMap.newSynchronous();
// public HasParam(String theTargetResourceType, String theReferenceFieldName, String theParameterName, String theParameterValue) {
map.add(PARAM_HAS, new HasParam("List", "item", "_id", listIdString));
IBundleProvider bundle = myPatientDao.search(map);
List<IBaseResource> resources = bundle.getResources(0, 2);
assertThat(resources, hasSize(2));
}
{
// The new syntax
String queryString = "_list=" + listIdString;
SearchParameterMap map = myMatchUrlService.translateMatchUrl(queryString, myFhirCtx.getResourceDefinition("List"));
IBundleProvider bundle = myPatientDao.search(map);
List<IBaseResource> resources = bundle.getResources(0, 2);
assertThat(resources, hasSize(2));
// assert ids equal pid1 and pid2
}
String queryString = "_list=" + listIdString;
testQuery(queryString, patientIds);
}
// FIXME KH test other cases like _list=12,13&_list=14
@Test
public void testBigListQuery() {
IIdType[] patientIds = createPatients(100);
String listIdString = createList(patientIds);
String queryString = "_list=" + listIdString;
testQuery(queryString, patientIds);
}
private void testQuery(String theQueryString, IIdType... theExpectedPatientIds) {
SearchParameterMap map = myMatchUrlService.translateMatchUrl(theQueryString, myFhirCtx.getResourceDefinition("List"));
IBundleProvider bundle = myPatientDao.search(map);
List<IBaseResource> resources = bundle.getResources(0, theExpectedPatientIds.length);
assertThat(resources, hasSize(theExpectedPatientIds.length));
Set<IIdType> ids = resources.stream().map(IBaseResource::getIdElement).collect(Collectors.toSet());
assertThat(ids, hasSize(theExpectedPatientIds.length));
for(IIdType patientId: theExpectedPatientIds) {
assertTrue(ids.contains(patientId));
//assertThat(patientId, contains(ids));
}
// assert ids equal pid1 and pid2
}
@Test
public void testAnd() {
IIdType[] patientIds = createPatients(3);
String listIdString1 = createList(patientIds[0], patientIds[1]);
String listIdString2 = createList(patientIds[1], patientIds[2]);
String queryString = "_list=" + listIdString1 + "&_list=" + listIdString2;
testQuery(queryString, patientIds[1]);
}
@Test
public void testOr() {
IIdType[] patientIds = createPatients(3);
String listIdString1 = createList(patientIds[0], patientIds[1]);
String listIdString2 = createList(patientIds[1], patientIds[2]);
String queryString = "_list=" + listIdString1 + "," + listIdString2;
testQuery(queryString, patientIds);
}
@Test
public void testBoth() {
IIdType[] patientIds = createPatients(5);
String listIdString1 = createList(patientIds[0], patientIds[1]);
String listIdString2 = createList(patientIds[3], patientIds[4]);
String listIdString3 = createList(patientIds[2], patientIds[3]);
String queryString = "_list=" + listIdString1 + "," + listIdString2 + "&_list=" + listIdString3;
testQuery(queryString, patientIds[3]);
}
@Test
public void testAlternateSyntax() {
IIdType[] patientIds = createPatients(2);
String listIdString = createList(patientIds);
// What we need to emulate
// /Patient?_has=List:item:_id=123
SearchParameterMap map = SearchParameterMap.newSynchronous();
// public HasParam(String theTargetResourceType, String theReferenceFieldName, String theParameterName, String theParameterValue) {
map.add(PARAM_HAS, new HasParam("List", "item", "_id", listIdString));
IBundleProvider bundle = myPatientDao.search(map);
List<IBaseResource> resources = bundle.getResources(0, 2);
assertThat(resources, hasSize(2));
}
private IIdType[] createPatients(int theNumberOfPatientsToCreate) {
IIdType[] patientIds = new IIdType[theNumberOfPatientsToCreate];
for(int i=0; i < theNumberOfPatientsToCreate; i++) {
patientIds[i] = myPatientDao.create(new Patient()).getId();
}
return patientIds;
}
private String createList(IIdType... thePatientIds) {
ListResource list = new ListResource();
for(IIdType patientId: thePatientIds) {
list.addEntry().getItem().setReferenceElement(patientId);
}
return myListResourceDao.create(list).getId().getIdPart();
}
}

View File

@ -131,6 +131,9 @@ public class MatchUrlService {
paramMap.add(nextParamName, param);
} else if (JpaConstants.PARAM_DELETE_EXPUNGE.equals(nextParamName)) {
paramMap.setDeleteExpunge(true);
} else if (Constants.PARAM_LIST.equals(nextParamName)) {
IQueryParameterAnd<?> param = ParameterUtil.parseQueryParams(myContext, RestSearchParameterTypeEnum.TOKEN, nextParamName, paramList);
paramMap.add(nextParamName, param);
} else if (nextParamName.startsWith("_")) {
// ignore these since they aren't search params (e.g. _sort)
} else {