Fix NPE in tests

This commit is contained in:
James Agnew 2018-10-04 08:33:40 -04:00
parent b265c0281b
commit 95f00e1c94
2 changed files with 71 additions and 62 deletions

View File

@ -316,7 +316,7 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
Date retVal = myUpperBound.getValue();
if (myLowerBound.getPrecision().ordinal() <= TemporalPrecisionEnum.DAY.ordinal()) {
if (myUpperBound.getPrecision().ordinal() <= TemporalPrecisionEnum.DAY.ordinal()) {
Calendar cal = DateUtils.toCalendar(retVal);
cal.setTimeZone(TimeZone.getTimeZone("GMT+11:30"));
cal = DateUtils.truncate(cal, Calendar.DATE);

View File

@ -1,26 +1,5 @@
package ca.uhn.fhir.rest.server;
import static org.junit.Assert.assertEquals;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
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.r4.model.Patient;
import org.junit.*;
import com.google.common.base.Charsets;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.rest.annotation.RequiredParam;
@ -29,18 +8,52 @@ 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;
import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.DateUtils;
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.r4.model.Patient;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
public class DateRangeParamSearchR4Test {
private static final SimpleDateFormat ourFmtLower;
private static final SimpleDateFormat ourFmtUpper;
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forR4();
private static DateRangeParam ourLastDateRange;
private static int ourPort;
private static Server ourServer;
private static SimpleDateFormat ourFmt;
private static String ourBaseUrl;
static {
ourFmtLower = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
ourFmtLower.setTimeZone(TimeZone.getTimeZone("GMT-11:30"));
ourFmtUpper = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
ourFmtUpper.setTimeZone(TimeZone.getTimeZone("GMT+11:30"));
}
@Before
public void before() {
ourLastDateRange = null;
@ -57,7 +70,6 @@ public class DateRangeParamSearchR4Test {
}
@Test
public void testSearchForOneUnqualifiedDate() throws Exception {
HttpGet httpGet = new HttpGet(ourBaseUrl + "?birthdate=2012-01-01");
@ -68,13 +80,13 @@ public class DateRangeParamSearchR4Test {
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");
@ -85,7 +97,7 @@ public class DateRangeParamSearchR4Test {
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());
@ -102,7 +114,7 @@ public class DateRangeParamSearchR4Test {
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());
@ -119,7 +131,7 @@ public class DateRangeParamSearchR4Test {
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());
@ -136,7 +148,7 @@ public class DateRangeParamSearchR4Test {
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());
@ -153,23 +165,44 @@ public class DateRangeParamSearchR4Test {
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 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;
}
}
public static Date parse(String theString) throws ParseException {
return ourFmt.parse(theString);
Date retVal = ourFmtLower.parse(theString);
retVal = DateUtils.addDays(retVal, -1);
return retVal;
}
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");
return new Date(ourFmtUpper.parse(theString).getTime() - 1L);
}
@AfterClass
@ -177,7 +210,7 @@ public class DateRangeParamSearchR4Test {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
@ -203,28 +236,4 @@ public class DateRangeParamSearchR4Test {
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;
}
}
}