From 0411967e26add7dd1c6a2feae4f03b6742e9abfc Mon Sep 17 00:00:00 2001 From: Frank Tao Date: Fri, 11 Dec 2020 22:25:12 -0500 Subject: [PATCH] Added composite sort test case for R5 --- .../provider/r5/ResourceProviderR5Test.java | 144 ++++++++++++++++-- 1 file changed, 128 insertions(+), 16 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r5/ResourceProviderR5Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r5/ResourceProviderR5Test.java index d91fba41fde..ac1a6676c56 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r5/ResourceProviderR5Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r5/ResourceProviderR5Test.java @@ -1,13 +1,16 @@ package ca.uhn.fhir.jpa.provider.r5; -import ca.uhn.fhir.jpa.api.config.DaoConfig; -import ca.uhn.fhir.jpa.entity.Search; -import ca.uhn.fhir.jpa.model.search.SearchStatusEnum; -import ca.uhn.fhir.parser.StrictErrorHandler; -import ca.uhn.fhir.rest.client.interceptor.CapturingInterceptor; -import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException; -import ca.uhn.fhir.util.UrlUtil; -import com.google.common.base.Charsets; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + import org.apache.commons.io.IOUtils; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; @@ -16,24 +19,29 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.r5.model.Bundle; +import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent; +import org.hl7.fhir.r5.model.CodeableConcept; import org.hl7.fhir.r5.model.DateTimeType; +import org.hl7.fhir.r5.model.IdType; import org.hl7.fhir.r5.model.Observation; +import org.hl7.fhir.r5.model.Observation.ObservationComponentComponent; import org.hl7.fhir.r5.model.Patient; +import org.hl7.fhir.r5.model.Quantity; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.util.List; -import java.util.stream.Collectors; +import com.google.common.base.Charsets; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.containsString; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.hamcrest.MatcherAssert.assertThat; +import ca.uhn.fhir.jpa.api.config.DaoConfig; +import ca.uhn.fhir.jpa.entity.Search; +import ca.uhn.fhir.jpa.model.search.SearchStatusEnum; +import ca.uhn.fhir.parser.StrictErrorHandler; +import ca.uhn.fhir.rest.client.interceptor.CapturingInterceptor; +import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException; +import ca.uhn.fhir.util.UrlUtil; @SuppressWarnings("Duplicates") public class ResourceProviderR5Test extends BaseResourceProviderR5Test { @@ -246,5 +254,109 @@ public class ResourceProviderR5Test extends BaseResourceProviderR5Test { assertEquals(0, output.getEntry().size()); } + @Test + public void testSearchWithCompositeSort() throws IOException { + + IIdType pid0; + IIdType oid1; + IIdType oid2; + IIdType oid3; + IIdType oid4; + { + Patient patient = new Patient(); + patient.addIdentifier().setSystem("urn:system").setValue("001"); + patient.addName().setFamily("Tester").addGiven("Joe"); + pid0 = myPatientDao.create(patient, mySrd).getId().toUnqualifiedVersionless(); + } + { + Observation obs = new Observation(); + obs.addIdentifier().setSystem("urn:system").setValue("FOO"); + obs.getSubject().setReferenceElement(pid0); + + ObservationComponentComponent comp = obs.addComponent(); + CodeableConcept cc = new CodeableConcept(); + cc.addCoding().setCode("2345-7").setSystem("http://loinc.org"); + comp.setCode(cc); + comp.setValue(new Quantity().setValue(200)); + + oid1 = myObservationDao.create(obs, mySrd).getId().toUnqualifiedVersionless(); + + ourLog.info("Observation: \n" + myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(obs)); + } + + { + Observation obs = new Observation(); + obs.addIdentifier().setSystem("urn:system").setValue("FOO"); + obs.getSubject().setReferenceElement(pid0); + + ObservationComponentComponent comp = obs.addComponent(); + CodeableConcept cc = new CodeableConcept(); + cc.addCoding().setCode("2345-7").setSystem("http://loinc.org"); + comp.setCode(cc); + comp.setValue(new Quantity().setValue(300)); + + oid2 = myObservationDao.create(obs, mySrd).getId().toUnqualifiedVersionless(); + + ourLog.info("Observation: \n" + myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(obs)); + } + + { + Observation obs = new Observation(); + obs.addIdentifier().setSystem("urn:system").setValue("FOO"); + obs.getSubject().setReferenceElement(pid0); + + ObservationComponentComponent comp = obs.addComponent(); + CodeableConcept cc = new CodeableConcept(); + cc.addCoding().setCode("2345-7").setSystem("http://loinc.org"); + comp.setCode(cc); + comp.setValue(new Quantity().setValue(150)); + + oid3 = myObservationDao.create(obs, mySrd).getId().toUnqualifiedVersionless(); + + ourLog.info("Observation: \n" + myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(obs)); + } + + { + Observation obs = new Observation(); + obs.addIdentifier().setSystem("urn:system").setValue("FOO"); + obs.getSubject().setReferenceElement(pid0); + + ObservationComponentComponent comp = obs.addComponent(); + CodeableConcept cc = new CodeableConcept(); + cc.addCoding().setCode("2345-7").setSystem("http://loinc.org"); + comp.setCode(cc); + comp.setValue(new Quantity().setValue(250)); + oid4 = myObservationDao.create(obs, mySrd).getId().toUnqualifiedVersionless(); + + ourLog.info("Observation: \n" + myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(obs)); + } + + String uri = ourServerBase + "/Observation?_sort=combo-code-value-quantity"; + Bundle found; + + HttpGet get = new HttpGet(uri); + try (CloseableHttpResponse resp = ourHttpClient.execute(get)) { + String output = IOUtils.toString(resp.getEntity().getContent(), Charsets.UTF_8); + found = myFhirCtx.newXmlParser().parseResource(Bundle.class, output); + } + + ourLog.info("Bundle: \n" + myFhirCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(found)); + + List list = toUnqualifiedVersionlessIds(found); + assertEquals(4, found.getEntry().size()); + assertEquals(oid3, list.get(0)); + assertEquals(oid1, list.get(1)); + assertEquals(oid4, list.get(2)); + assertEquals(oid2, list.get(3)); + } + protected List toUnqualifiedVersionlessIds(Bundle theFound) { + List retVal = new ArrayList<>(); + for (BundleEntryComponent next : theFound.getEntry()) { + if (next.getResource()!= null) { + retVal.add(next.getResource().getIdElement().toUnqualifiedVersionless()); + } + } + return retVal; + } }