From a8585c881dfdd4485d480c584d7754a3f7bf83e3 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 4 Jul 2016 13:48:05 -0400 Subject: [PATCH] Fix #396 - Make tests timezone independent --- .../fhir/rest/gclient/DateClientParam.java | 15 ++++--- .../main/java/ca/uhn/fhir/util/TestUtil.java | 13 ++++--- .../rest/client/GenericClientDstu3Test.java | 39 ++++++++++--------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/DateClientParam.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/DateClientParam.java index b3447735b83..e6c758fa527 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/DateClientParam.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/DateClientParam.java @@ -101,35 +101,40 @@ public class DateClientParam extends BaseClientParam implements IParam { public ICriterion day(Date theValue) { DateTimeDt dt = new DateTimeDt(theValue); dt.setPrecision(TemporalPrecisionEnum.DAY); - return new Criterion(myPrefix, dt.getValueAsString()); + String valueAsString = dt.getValueAsString(); + return new Criterion(myPrefix, valueAsString); } @Override public ICriterion day(String theValue) { DateTimeDt dt = new DateTimeDt(theValue); dt.setPrecision(TemporalPrecisionEnum.DAY); - return new Criterion(myPrefix , dt.getValueAsString()); + String valueAsString = dt.getValueAsString(); + return new Criterion(myPrefix , valueAsString); } @Override public ICriterion now() { DateTimeDt dt = DateTimeDt.withCurrentTime(); dt.setPrecision(TemporalPrecisionEnum.SECOND); - return new Criterion(myPrefix , dt.getValueAsString()); + String valueAsString = dt.getValueAsString(); + return new Criterion(myPrefix , valueAsString); } @Override public ICriterion second(Date theValue) { DateTimeDt dt = new DateTimeDt(theValue); dt.setPrecision(TemporalPrecisionEnum.SECOND); - return new Criterion(myPrefix , dt.getValueAsString()); + String valueAsString = dt.getValueAsString(); + return new Criterion(myPrefix , valueAsString); } @Override public ICriterion second(String theValue) { DateTimeDt dt = new DateTimeDt(theValue); dt.setPrecision(TemporalPrecisionEnum.SECOND); - return new Criterion(myPrefix , dt.getValueAsString()); + String valueAsString = dt.getValueAsString(); + return new Criterion(myPrefix , valueAsString); } } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TestUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TestUtil.java index 3fcae4ede7d..01a6c913e67 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TestUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TestUtil.java @@ -24,6 +24,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Locale; +import java.util.TimeZone; public class TestUtil { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(TestUtil.class); @@ -70,10 +71,9 @@ public class TestUtil { * but it helps us make sure we don't have any tests that depend on a particular * environment */ - Locale[] available = { Locale.CANADA, Locale.GERMANY, Locale.TAIWAN }; - Locale newLocale = available[(int) (Math.random() * available.length)]; - Locale.setDefault(newLocale); - ourLog.info("Tests are running in locale: " + newLocale.getDisplayName()); + Locale[] availableLocales = { Locale.CANADA, Locale.GERMANY, Locale.TAIWAN }; + Locale.setDefault(availableLocales[(int) (Math.random() * availableLocales.length)]); + ourLog.info("Tests are running in locale: " + Locale.getDefault().getDisplayName()); if (Math.random() < 0.5) { ourLog.info("Tests are using WINDOWS line endings and ISO-8851-1"); System.setProperty("file.encoding", "ISO-8859-1"); @@ -83,7 +83,10 @@ public class TestUtil { System.setProperty("file.encoding", "UTF-8"); System.setProperty("line.separator", "\n"); } - + String availableTimeZones[] = { "GMT+08:00", "GMT-05:00", "GMT+00:00", "GMT+03:30" }; + String timeZone = availableTimeZones[(int)(Math.random() * availableTimeZones.length)]; + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + ourLog.info("Tests are using time zone: {}", TimeZone.getDefault().getID()); } diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu3Test.java index 308e52d970b..76ebc33c554 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu3Test.java @@ -19,6 +19,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.Locale; +import java.util.TimeZone; import org.apache.commons.io.IOUtils; import org.apache.commons.io.input.ReaderInputStream; @@ -685,7 +687,7 @@ public class GenericClientDstu3Test { } @Test - public void testSearchByDate() throws Exception { + public void testSearchByDate() throws Exception { final String msg = "{\"resourceType\":\"Bundle\",\"id\":null,\"base\":\"http://localhost:57931/fhir/contextDev\",\"total\":1,\"link\":[{\"relation\":\"self\",\"url\":\"http://localhost:57931/fhir/contextDev/Patient?identifier=urn%3AMultiFhirVersionTest%7CtestSubmitPatient01&_format=json\"}],\"entry\":[{\"resource\":{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}]}"; ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); @@ -702,66 +704,67 @@ public class GenericClientDstu3Test { IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); int idx = 0; - Date date = new DateTimeDt("2011-01-02T22:33:01Z").getValue(); + DateTimeDt now = DateTimeDt.withCurrentTime(); + String dateString = now.getValueAsString().substring(0, 10); //@formatter:off client.search() .forResource("Patient") - .where(Patient.BIRTHDATE.after().day("2011-01-02")) + .where(Patient.BIRTHDATE.after().day(dateString)) .returnBundle(Bundle.class) .execute(); //@formatter:on - assertEquals("http://example.com/fhir/Patient?birthdate=gt2011-01-02", capt.getAllValues().get(idx).getURI().toString()); + assertEquals("http://example.com/fhir/Patient?birthdate=gt"+dateString, capt.getAllValues().get(idx).getURI().toString()); idx++; //@formatter:off client.search() .forResource("Patient") - .where(Patient.BIRTHDATE.after().day(date)) + .where(Patient.BIRTHDATE.after().day(now.getValue())) .returnBundle(Bundle.class) .execute(); //@formatter:on - assertEquals("http://example.com/fhir/Patient?birthdate=gt2011-01-02", capt.getAllValues().get(idx).getURI().toString()); + assertEquals("http://example.com/fhir/Patient?birthdate=gt"+dateString, capt.getAllValues().get(idx).getURI().toString()); idx++; //@formatter:off client.search() .forResource("Patient") - .where(Patient.BIRTHDATE.afterOrEquals().day("2011-01-02")) + .where(Patient.BIRTHDATE.afterOrEquals().day(dateString)) .returnBundle(Bundle.class) .execute(); //@formatter:on - assertEquals("http://example.com/fhir/Patient?birthdate=ge2011-01-02", capt.getAllValues().get(idx).getURI().toString()); + assertEquals("http://example.com/fhir/Patient?birthdate=ge"+dateString, capt.getAllValues().get(idx).getURI().toString()); idx++; //@formatter:off client.search() .forResource("Patient") - .where(Patient.BIRTHDATE.before().day("2011-01-02")) + .where(Patient.BIRTHDATE.before().day(dateString)) .returnBundle(Bundle.class) .execute(); //@formatter:on - assertEquals("http://example.com/fhir/Patient?birthdate=lt2011-01-02", capt.getAllValues().get(idx).getURI().toString()); + assertEquals("http://example.com/fhir/Patient?birthdate=lt"+dateString, capt.getAllValues().get(idx).getURI().toString()); idx++; //@formatter:off client.search() .forResource("Patient") - .where(Patient.BIRTHDATE.beforeOrEquals().day("2011-01-02")) + .where(Patient.BIRTHDATE.beforeOrEquals().day(dateString)) .returnBundle(Bundle.class) .execute(); //@formatter:on - assertEquals("http://example.com/fhir/Patient?birthdate=le2011-01-02", capt.getAllValues().get(idx).getURI().toString()); + assertEquals("http://example.com/fhir/Patient?birthdate=le"+dateString, capt.getAllValues().get(idx).getURI().toString()); idx++; //@formatter:off client.search() .forResource("Patient") - .where(Patient.BIRTHDATE.exactly().day("2011-01-02")) + .where(Patient.BIRTHDATE.exactly().day(dateString)) .returnBundle(Bundle.class) .execute(); //@formatter:on - assertEquals("http://example.com/fhir/Patient?birthdate=2011-01-02", capt.getAllValues().get(idx).getURI().toString()); + assertEquals("http://example.com/fhir/Patient?birthdate="+dateString, capt.getAllValues().get(idx).getURI().toString()); idx++; //@formatter:off @@ -774,16 +777,14 @@ public class GenericClientDstu3Test { assertEquals("http://example.com/fhir/Patient?birthdate=gt2011-01-02T22:33:01Z", UrlUtil.unescape(capt.getAllValues().get(idx).getURI().toString())); idx++; - String expDate = new DateTimeDt(date).getValueAsString(); - //@formatter:off client.search() .forResource("Patient") - .where(Patient.BIRTHDATE.after().second(date)) + .where(Patient.BIRTHDATE.after().second(now.getValueAsString())) .returnBundle(Bundle.class) .execute(); //@formatter:on - assertEquals("http://example.com/fhir/Patient?birthdate=gt" + expDate, UrlUtil.unescape(capt.getAllValues().get(idx).getURI().toString())); + assertEquals("http://example.com/fhir/Patient?birthdate=gt" + now.getValueAsString(), UrlUtil.unescape(capt.getAllValues().get(idx).getURI().toString())); idx++; //@formatter:off @@ -794,7 +795,7 @@ public class GenericClientDstu3Test { .execute(); //@formatter:on assertThat(capt.getAllValues().get(idx).getURI().toString(), startsWith("http://example.com/fhir/Patient?birthdate=gt2")); - String dateString = UrlUtil.unescape(capt.getAllValues().get(idx).getURI().toString()).substring(44); + dateString = UrlUtil.unescape(capt.getAllValues().get(idx).getURI().toString()).substring(44); ourLog.info(dateString); assertEquals(TemporalPrecisionEnum.SECOND, new DateTimeDt(dateString).getPrecision()); idx++;