Treat date eq params correctly

This commit is contained in:
James Agnew 2016-05-13 18:49:41 -04:00
parent 84a7856c19
commit 563f4e4c46
7 changed files with 282 additions and 160 deletions

View File

@ -1,5 +1,7 @@
package ca.uhn.fhir.rest.param;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
/*
* #%L
* HAPI FHIR - Core Library
@ -42,7 +44,7 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
* {@link #setUpperBound(DateParam)}
*/
public DateRangeParam() {
// nothing
super();
}
/**
@ -149,13 +151,13 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
}
private void addParam(DateParam theParsed) throws InvalidRequestException {
if (theParsed.getPrefix() == null) {
if (theParsed.getPrefix() == null || theParsed.getPrefix() == ParamPrefixEnum.EQUAL) {
if (myLowerBound != null || myUpperBound != null) {
throw new InvalidRequestException("Can not have multiple date range parameters for the same param without a qualifier");
}
myLowerBound = new DateParam(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, theParsed.getValueAsString());
myUpperBound = new DateParam(ParamPrefixEnum.LESSTHAN_OR_EQUALS, theParsed.getValueAsString());
myLowerBound = new DateParam(ParamPrefixEnum.EQUAL, theParsed.getValueAsString());
myUpperBound = new DateParam(ParamPrefixEnum.EQUAL, theParsed.getValueAsString());
} else {
@ -195,6 +197,7 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
case GREATERTHAN:
retVal = myLowerBound.getPrecision().add(retVal, 1);
break;
case EQUAL:
case GREATERTHAN_OR_EQUALS:
break;
case LESSTHAN:
@ -219,6 +222,7 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
case LESSTHAN:
retVal = new Date(retVal.getTime() - 1L);
break;
case EQUAL:
case LESSTHAN_OR_EQUALS:
retVal = myUpperBound.getPrecision().add(retVal, 1);
retVal = new Date(retVal.getTime() - 1L);
@ -329,6 +333,10 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
public void setRangeFromDatesInclusive(String theLowerBound, String theUpperBound) {
myLowerBound = theLowerBound != null ? new DateParam(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, theLowerBound) : null;
myUpperBound = theUpperBound != null ? new DateParam(ParamPrefixEnum.LESSTHAN_OR_EQUALS, theUpperBound) : null;
if (isNotBlank(theLowerBound) && isNotBlank(theUpperBound) && theLowerBound.equals(theUpperBound)) {
myLowerBound.setPrefix(ParamPrefixEnum.EQUAL);
myUpperBound.setPrefix(ParamPrefixEnum.EQUAL);
}
validateAndThrowDataFormatExceptionIfInvalid();
}

View File

@ -1103,7 +1103,7 @@ public class SearchBuilder {
if (lowerBound != null) {
Predicate gt = theBuilder.greaterThanOrEqualTo(theFrom.<Date> get("myValueLow"), lowerBound);
Predicate lt = theBuilder.greaterThanOrEqualTo(theFrom.<Date> get("myValueHigh"), lowerBound);
if (theRange.getLowerBound().getPrefix() == ParamPrefixEnum.STARTS_AFTER) {
if (theRange.getLowerBound().getPrefix() == ParamPrefixEnum.STARTS_AFTER || theRange.getLowerBound().getPrefix() == ParamPrefixEnum.EQUAL) {
lb = gt;
} else {
lb = theBuilder.or(gt, lt);
@ -1114,7 +1114,7 @@ public class SearchBuilder {
if (upperBound != null) {
Predicate gt = theBuilder.lessThanOrEqualTo(theFrom.<Date> get("myValueLow"), upperBound);
Predicate lt = theBuilder.lessThanOrEqualTo(theFrom.<Date> get("myValueHigh"), upperBound);
if (theRange.getUpperBound().getPrefix() == ParamPrefixEnum.ENDS_BEFORE) {
if (theRange.getUpperBound().getPrefix() == ParamPrefixEnum.ENDS_BEFORE || theRange.getUpperBound().getPrefix() == ParamPrefixEnum.EQUAL) {
ub = lt;
} else {
ub = theBuilder.or(gt, lt);

View File

@ -116,22 +116,6 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test {
map.add(Subscription.SP_STATUS, new TokenParam(null, SubscriptionStatus.ACTIVE.toCode()));
assertThat(toUnqualifiedVersionlessIds(mySubscriptionDao.search(map)), contains(id));
}
@Test
public void testDatePeriod() {
Encounter enc = new Encounter();
enc.getPeriod().setStartElement(new DateTimeType("2016-05-10")).setEndElement(new DateTimeType("2016-05-20"));
String id = myEncounterDao.create(enc, mySrd).getId().toUnqualifiedVersionless().getValue();
List<String> ids;
ids = toUnqualifiedVersionlessIdValues(myEncounterDao.search(Encounter.SP_DATE, new DateParam("2016-05-15")));
assertThat(ids, contains(id));
ids = toUnqualifiedVersionlessIdValues(myEncounterDao.search(Encounter.SP_DATE, new DateParam("eq2016-05-15")));
assertThat(ids, contains(id));
}
@Test

View File

@ -326,6 +326,35 @@ public class FhirResourceDaoDstu3Test extends BaseJpaDstu3Test {
}
}
@Test
public void testChoiceParamDateEquals() {
Encounter enc = new Encounter();
enc.getPeriod().setStartElement(new DateTimeType("2016-05-10")).setEndElement(new DateTimeType("2016-05-20"));
String id = myEncounterDao.create(enc, mySrd).getId().toUnqualifiedVersionless().getValue();
List<String> ids;
/*
* This should not match, per the definition of eq
*/
ids = toUnqualifiedVersionlessIdValues(myEncounterDao.search(Encounter.SP_DATE, new DateParam("2016-05-15")));
assertThat(ids, empty());
ids = toUnqualifiedVersionlessIdValues(myEncounterDao.search(Encounter.SP_DATE, new DateParam("eq2016-05-15")));
assertThat(ids, empty());
// Should match
ids = toUnqualifiedVersionlessIdValues(myEncounterDao.search(Encounter.SP_DATE, new DateParam("eq2016")));
assertThat(ids, contains(id));
ids = toUnqualifiedVersionlessIdValues(myEncounterDao.search(Encounter.SP_DATE, new DateParam("2016")));
assertThat(ids, contains(id));
}
@Test
public void testChoiceParamDateRange() {
Observation o1 = new Observation();

View File

@ -1,138 +0,0 @@
package ca.uhn.fhir.rest.server;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.rest.annotation.RequiredParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.DateRangeParamTest;
import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil;
/**
* Created by dsotnikov on 2/25/2014.
*/
public class DateRangeParamSearchTest {
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu1();
private static DateRangeParam ourLastDateRange;
private static int ourPort;
private static Server ourServer;
@Before
public void before() {
ourLastDateRange = null;
}
@Test
public void testSearchForMultipleUnqualifiedDate() throws Exception {
String baseUrl = "http://localhost:" + ourPort + "/Patient?" + Patient.SP_BIRTHDATE + "=";
HttpGet httpGet = new HttpGet(baseUrl + "2012-01-01&" + Patient.SP_BIRTHDATE + "=2012-02-03");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(400, status.getStatusLine().getStatusCode());
}
@Test
public void testSearchForOneUnqualifiedDate() throws Exception {
String baseUrl = "http://localhost:" + ourPort + "/Patient?" + Patient.SP_BIRTHDATE + "=";
HttpGet httpGet = new HttpGet(baseUrl + "2012-01-01");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("2012-01-01", ourLastDateRange.getLowerBound().getValueAsString());
assertEquals("2012-01-01", ourLastDateRange.getUpperBound().getValueAsString());
assertEquals(DateRangeParamTest.parse("2012-01-01 00:00:00.0000"), ourLastDateRange.getLowerBoundAsInstant());
assertEquals(DateRangeParamTest.parseM1("2012-01-02 00:00:00.0000"), ourLastDateRange.getUpperBoundAsInstant());
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider();
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer(ourCtx);
servlet.getFhirContext().setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator());
servlet.setResourceProviders(patientProvider);
ServletHolder servletHolder = new ServletHolder(servlet);
proxyHandler.addServletWithMapping(servletHolder, "/*");
ourServer.setHandler(proxyHandler);
ourServer.start();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(connectionManager);
ourClient = builder.build();
}
/**
* Created by dsotnikov on 2/25/2014.
*/
public static class DummyPatientResourceProvider implements IResourceProvider {
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
@Search()
public List<Patient> search(@RequiredParam(name=Patient.SP_BIRTHDATE) DateRangeParam theDateRange) {
ourLastDateRange = theDateRange;
ArrayList<Patient> retVal = new ArrayList<Patient>();
Patient patient = new Patient();
patient.setId("1");
patient.addIdentifier("system", "hello");
retVal.add(patient);
return retVal;
}
}
}

View File

@ -0,0 +1,233 @@
package ca.uhn.fhir.rest.server;
import static org.junit.Assert.assertEquals;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.hl7.fhir.dstu3.model.Patient;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.rest.annotation.RequiredParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.ParamPrefixEnum;
import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil;
public class DateRangeParamSearchDstu3Test {
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu3();
private static DateRangeParam ourLastDateRange;
private static int ourPort;
private static Server ourServer;
private static SimpleDateFormat ourFmt;
private static String ourBaseUrl;
@Before
public void before() {
ourLastDateRange = null;
}
@Test
public void testSearchForMultipleUnqualifiedDate() throws Exception {
String baseUrl = "http://localhost:" + ourPort + "/Patient?" + Patient.SP_BIRTHDATE + "=";
HttpGet httpGet = new HttpGet(baseUrl + "2012-01-01&" + Patient.SP_BIRTHDATE + "=2012-02-03");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(400, status.getStatusLine().getStatusCode());
}
@Test
public void testSearchForOneUnqualifiedDate() throws Exception {
HttpGet httpGet = new HttpGet(ourBaseUrl + "?birthdate=2012-01-01");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("2012-01-01", ourLastDateRange.getLowerBound().getValueAsString());
assertEquals("2012-01-01", ourLastDateRange.getUpperBound().getValueAsString());
assertEquals(parse("2012-01-01 00:00:00.0000"), ourLastDateRange.getLowerBoundAsInstant());
assertEquals(parseM1("2012-01-02 00:00:00.0000"), ourLastDateRange.getUpperBoundAsInstant());
assertEquals(ParamPrefixEnum.EQUAL, ourLastDateRange.getLowerBound().getPrefix());
assertEquals(ParamPrefixEnum.EQUAL, ourLastDateRange.getUpperBound().getPrefix());
}
@Test
public void testSearchForOneQualifiedDateEq() throws Exception {
HttpGet httpGet = new HttpGet(ourBaseUrl + "?birthdate=eq2012-01-01");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("2012-01-01", ourLastDateRange.getLowerBound().getValueAsString());
assertEquals("2012-01-01", ourLastDateRange.getUpperBound().getValueAsString());
assertEquals(parse("2012-01-01 00:00:00.0000"), ourLastDateRange.getLowerBoundAsInstant());
assertEquals(parseM1("2012-01-02 00:00:00.0000"), ourLastDateRange.getUpperBoundAsInstant());
assertEquals(ParamPrefixEnum.EQUAL, ourLastDateRange.getLowerBound().getPrefix());
assertEquals(ParamPrefixEnum.EQUAL, ourLastDateRange.getUpperBound().getPrefix());
}
@Test
public void testSearchForOneQualifiedDateGt() throws Exception {
HttpGet httpGet = new HttpGet(ourBaseUrl + "?birthdate=gt2012-01-01");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("2012-01-01", ourLastDateRange.getLowerBound().getValueAsString());
assertEquals(null, ourLastDateRange.getUpperBound());
assertEquals(parse("2012-01-02 00:00:00.0000"), ourLastDateRange.getLowerBoundAsInstant());
assertEquals(null, ourLastDateRange.getUpperBoundAsInstant());
assertEquals(ParamPrefixEnum.GREATERTHAN, ourLastDateRange.getLowerBound().getPrefix());
assertEquals(null, ourLastDateRange.getUpperBound());
}
@Test
public void testSearchForOneQualifiedDateLt() throws Exception {
HttpGet httpGet = new HttpGet(ourBaseUrl + "?birthdate=lt2012-01-01");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(null, ourLastDateRange.getLowerBound());
assertEquals("2012-01-01", ourLastDateRange.getUpperBound().getValueAsString());
assertEquals(null, ourLastDateRange.getLowerBoundAsInstant());
assertEquals(parseM1("2012-01-01 00:00:00.0000"), ourLastDateRange.getUpperBoundAsInstant());
assertEquals(null, ourLastDateRange.getLowerBound());
assertEquals(ParamPrefixEnum.LESSTHAN, ourLastDateRange.getUpperBound().getPrefix());
}
@Test
public void testSearchForOneQualifiedDateGe() throws Exception {
HttpGet httpGet = new HttpGet(ourBaseUrl + "?birthdate=ge2012-01-01");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals("2012-01-01", ourLastDateRange.getLowerBound().getValueAsString());
assertEquals(null, ourLastDateRange.getUpperBound());
assertEquals(parse("2012-01-01 00:00:00.0000"), ourLastDateRange.getLowerBoundAsInstant());
assertEquals(null, ourLastDateRange.getUpperBoundAsInstant());
assertEquals(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, ourLastDateRange.getLowerBound().getPrefix());
assertEquals(null, ourLastDateRange.getUpperBound());
}
@Test
public void testSearchForOneQualifiedDateLe() throws Exception {
HttpGet httpGet = new HttpGet(ourBaseUrl + "?birthdate=le2012-01-01");
HttpResponse status = ourClient.execute(httpGet);
IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertEquals(null, ourLastDateRange.getLowerBound());
assertEquals("2012-01-01", ourLastDateRange.getUpperBound().getValueAsString());
assertEquals(null, ourLastDateRange.getLowerBoundAsInstant());
assertEquals(parseM1("2012-01-02 00:00:00.0000"), ourLastDateRange.getUpperBoundAsInstant());
assertEquals(null, ourLastDateRange.getLowerBound());
assertEquals(ParamPrefixEnum.LESSTHAN_OR_EQUALS, ourLastDateRange.getUpperBound().getPrefix());
}
public static Date parse(String theString) throws ParseException {
return ourFmt.parse(theString);
}
public static Date parseM1(String theString) throws ParseException {
return new Date(ourFmt.parse(theString).getTime() - 1L);
}
static {
ourFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider();
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer(ourCtx);
servlet.getFhirContext().setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator());
servlet.setResourceProviders(patientProvider);
ServletHolder servletHolder = new ServletHolder(servlet);
proxyHandler.addServletWithMapping(servletHolder, "/*");
ourServer.setHandler(proxyHandler);
ourServer.start();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(connectionManager);
ourClient = builder.build();
ourBaseUrl = "http://localhost:" + ourPort + "/Patient";
}
public static class DummyPatientResourceProvider implements IResourceProvider {
@Override
public Class<Patient> getResourceType() {
return Patient.class;
}
@Search()
public List<Patient> search(@RequiredParam(name=Patient.SP_BIRTHDATE) DateRangeParam theDateRange) {
ourLastDateRange = theDateRange;
ArrayList<Patient> retVal = new ArrayList<Patient>();
Patient patient = new Patient();
patient.setId("1");
patient.addIdentifier().setSystem("system").setValue("hello");
retVal.add(patient);
return retVal;
}
}
}

View File

@ -170,6 +170,12 @@
tolerant of errors. If the ID is present in the body but does not agree with the
ID in the URL this remains an error.
</action>
<action type="fix">
Server / JPA server date range search params (e.g. Encounter:date) now treat
a single date with no comparator (or the eq comparator) as requiring that the
value be completely contained by the range specified. Thanks to Chris Moesel
for the suggestion.
</action>
</release>
<release version="1.5" date="2016-04-20">
<action type="fix" issue="339">