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(); Date retVal = myUpperBound.getValue();
if (myLowerBound.getPrecision().ordinal() <= TemporalPrecisionEnum.DAY.ordinal()) { if (myUpperBound.getPrecision().ordinal() <= TemporalPrecisionEnum.DAY.ordinal()) {
Calendar cal = DateUtils.toCalendar(retVal); Calendar cal = DateUtils.toCalendar(retVal);
cal.setTimeZone(TimeZone.getTimeZone("GMT+11:30")); cal.setTimeZone(TimeZone.getTimeZone("GMT+11:30"));
cal = DateUtils.truncate(cal, Calendar.DATE); cal = DateUtils.truncate(cal, Calendar.DATE);

View File

@ -1,26 +1,5 @@
package ca.uhn.fhir.rest.server; 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.context.FhirContext;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator; import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.rest.annotation.RequiredParam; 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.rest.param.ParamPrefixEnum;
import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil; 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 { public class DateRangeParamSearchR4Test {
private static final SimpleDateFormat ourFmtLower;
private static final SimpleDateFormat ourFmtUpper;
private static CloseableHttpClient ourClient; private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forR4(); private static FhirContext ourCtx = FhirContext.forR4();
private static DateRangeParam ourLastDateRange; private static DateRangeParam ourLastDateRange;
private static int ourPort; private static int ourPort;
private static Server ourServer; private static Server ourServer;
private static SimpleDateFormat ourFmt;
private static String ourBaseUrl; 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 @Before
public void before() { public void before() {
ourLastDateRange = null; ourLastDateRange = null;
@ -57,7 +70,6 @@ public class DateRangeParamSearchR4Test {
} }
@Test @Test
public void testSearchForOneUnqualifiedDate() throws Exception { public void testSearchForOneUnqualifiedDate() throws Exception {
HttpGet httpGet = new HttpGet(ourBaseUrl + "?birthdate=2012-01-01"); HttpGet httpGet = new HttpGet(ourBaseUrl + "?birthdate=2012-01-01");
@ -160,16 +172,37 @@ public class DateRangeParamSearchR4Test {
assertEquals(ParamPrefixEnum.LESSTHAN_OR_EQUALS, ourLastDateRange.getUpperBound().getPrefix()); 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 { 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 { public static Date parseM1(String theString) throws ParseException {
return new Date(ourFmt.parse(theString).getTime() - 1L); return new Date(ourFmtUpper.parse(theString).getTime() - 1L);
}
static {
ourFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
} }
@AfterClass @AfterClass
@ -203,28 +236,4 @@ public class DateRangeParamSearchR4Test {
ourBaseUrl = "http://localhost:" + ourPort + "/Patient"; 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;
}
}
} }