From abd8bc2f140608bddab078b9d82da95f710dd5e3 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Fri, 13 May 2016 17:40:56 -0400 Subject: [PATCH] Work on term svc --- .../uhn/fhir/rest/method/RequestDetails.java | 3 ++ .../uhn/fhir/rest/param/DateRangeParam.java | 3 ++ .../ResponseHighlighterInterceptor.java | 8 ++-- .../jpa/entity/TermCodeSystemVersion.java | 3 +- .../java/ca/uhn/fhir/jpa/dao/BaseJpaTest.java | 10 +++++ .../FhirResourceDaoDstu3SearchNoFtTest.java | 16 ++++++++ .../fhir/jpa/term/TerminologySvcImplTest.java | 3 +- .../ca/uhn/fhirtest/config/CommonConfig.java | 11 ++++++ .../fhirtest/joke/HolyFooCowInterceptor.java | 22 +++++++++++ .../ResponseHighlightingInterceptorTest.java | 5 ++- .../rest/client/GenericClientDstu3Test.java | 37 +++++++++++++++++++ .../FhirInstanceValidatorDstu3Test.java | 32 ---------------- 12 files changed, 115 insertions(+), 38 deletions(-) create mode 100644 hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/joke/HolyFooCowInterceptor.java diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/RequestDetails.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/RequestDetails.java index fce4aa5a56f..0087d56c442 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/RequestDetails.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/RequestDetails.java @@ -112,6 +112,9 @@ public abstract class RequestDetails { } public Map getParameters() { + if (myParameters == null) { + return Collections.emptyMap(); + } return myParameters; } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/DateRangeParam.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/DateRangeParam.java index 5b41805a04b..ce9cdf04b22 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/DateRangeParam.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/DateRangeParam.java @@ -77,6 +77,9 @@ public class DateRangeParam implements IQueryParameterAnd { setRangeFromDatesInclusive(theDateParam.getValueAsString(), theDateParam.getValueAsString()); } else { switch (theDateParam.getPrefix()) { + case EQUAL: + setRangeFromDatesInclusive(theDateParam.getValueAsString(), theDateParam.getValueAsString()); + break; case STARTS_AFTER: case GREATERTHAN: case GREATERTHAN_OR_EQUALS: diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlighterInterceptor.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlighterInterceptor.java index b94bffe5db2..3be5c0512e1 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlighterInterceptor.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlighterInterceptor.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Enumeration; import java.util.List; +import java.util.Map; import java.util.Set; import javax.servlet.ServletException; @@ -249,7 +250,8 @@ private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger private void streamResponse(RequestDetails theRequestDetails, HttpServletResponse theServletResponse, IBaseResource resource) { IParser p; - if (theRequestDetails.getParameters().containsKey(Constants.PARAM_FORMAT)) { + Map parameters = theRequestDetails.getParameters(); + if (parameters.containsKey(Constants.PARAM_FORMAT)) { p = RestfulServerUtils.getNewParser(theRequestDetails.getServer().getFhirContext(), theRequestDetails); } else { EncodingEnum defaultResponseEncoding = theRequestDetails.getServer().getDefaultResponseEncoding(); @@ -263,11 +265,11 @@ private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger theServletResponse.setContentType(Constants.CT_HTML_WITH_UTF8); StringBuilder rawB = new StringBuilder(); - for (String next : theRequestDetails.getParameters().keySet()) { + for (String next : parameters.keySet()) { if (next.equals(PARAM_RAW)) { continue; } - for (String nextValue : theRequestDetails.getParameters().get(next)) { + for (String nextValue : parameters.get(next)) { if (isBlank(nextValue)) { continue; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TermCodeSystemVersion.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TermCodeSystemVersion.java index 4d1f35de323..31897237681 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TermCodeSystemVersion.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TermCodeSystemVersion.java @@ -27,6 +27,7 @@ import java.util.Collection; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; +import javax.persistence.ForeignKey; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @@ -56,7 +57,7 @@ public class TermCodeSystemVersion implements Serializable { private Long myPid; @OneToOne() - @JoinColumn(name = "RES_ID", referencedColumnName = "RES_ID", nullable = false, updatable = false) + @JoinColumn(name = "RES_ID", referencedColumnName = "RES_ID", nullable = false, updatable = false, foreignKey=@ForeignKey(name="FK_CODESYSVER_RES_ID")) private ResourceTable myResource; @Column(name = "RES_VERSION_ID", nullable = false, updatable = false) diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaTest.java index ba3e76e3112..954f7d17f9c 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaTest.java @@ -190,6 +190,16 @@ public class BaseJpaTest { @Override public Void doInTransaction(TransactionStatus theStatus) { entityManager.createQuery("DELETE from " + TermConcept.class.getSimpleName() + " d").executeUpdate(); + for (TermCodeSystem next : entityManager.createQuery("SELECT c FROM " + TermCodeSystem.class.getName() + " c", TermCodeSystem.class).getResultList()) { + next.setCurrentVersion(null); + entityManager.merge(next); + } + return null; + } + }); + txTemplate.execute(new TransactionCallback() { + @Override + public Void doInTransaction(TransactionStatus theStatus) { entityManager.createQuery("DELETE from " + TermCodeSystemVersion.class.getSimpleName() + " d").executeUpdate(); entityManager.createQuery("DELETE from " + TermCodeSystem.class.getSimpleName() + " d").executeUpdate(); return null; diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java index dde4d320626..d18239c3dd0 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java @@ -116,6 +116,22 @@ 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 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 diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplTest.java index 859605506ae..94cc26e6732 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplTest.java @@ -20,6 +20,7 @@ import ca.uhn.fhir.jpa.entity.ResourceTable; import ca.uhn.fhir.jpa.entity.TermCodeSystemVersion; import ca.uhn.fhir.jpa.entity.TermConcept; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; import ca.uhn.fhir.util.TestUtil; @@ -202,7 +203,7 @@ public class TerminologySvcImplTest extends BaseJpaDstu3Test { try { myTermSvc.storeNewCodeSystemVersion(table.getId(), "http://example.com/my_code_system", cs); fail(); - } catch (InvalidRequestException e) { + } catch (UnprocessableEntityException e) { assertThat(e.getMessage(), containsString("Can not create multiple code systems with URI \"http://example.com/my_code_system\", already have one with resource ID: CodeSystem/")); } diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java index 0d1b9620cc3..129034a91ba 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/CommonConfig.java @@ -5,6 +5,7 @@ import org.springframework.context.annotation.Configuration; import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; import ca.uhn.fhir.rest.server.interceptor.LoggingInterceptor; +import ca.uhn.fhirtest.joke.HolyFooCowInterceptor; @Configuration public class CommonConfig { @@ -34,5 +35,15 @@ public class CommonConfig { retVal.setLogExceptions(false); return retVal; } + + /** + * This is a joke + * + * https://chat.fhir.org/#narrow/stream/implementers/topic/Unsupported.20search.20parameters + */ + @Bean + public IServerInterceptor holyFooCowInterceptor() { + return new HolyFooCowInterceptor(); + } } diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/joke/HolyFooCowInterceptor.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/joke/HolyFooCowInterceptor.java new file mode 100644 index 00000000000..550b37781ab --- /dev/null +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/joke/HolyFooCowInterceptor.java @@ -0,0 +1,22 @@ +package ca.uhn.fhirtest.joke; + +import static org.apache.commons.lang3.StringUtils.isNotBlank; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import ca.uhn.fhir.rest.server.exceptions.UnclassifiedServerFailureException; +import ca.uhn.fhir.rest.server.interceptor.InterceptorAdapter; + +public class HolyFooCowInterceptor extends InterceptorAdapter { + + @Override + public boolean incomingRequestPreProcessed(HttpServletRequest theRequest, HttpServletResponse theResponse) { + if (isNotBlank(theRequest.getParameter("holyfoocow"))) { + throw new UnclassifiedServerFailureException(418, "HTTP 418 IM A TEAPOT - Jenni, please do not hack with the server, it's very fragile today."); + } + + return true; + } + +} diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlightingInterceptorTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlightingInterceptorTest.java index 4bb8561fc79..43cb185e07e 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlightingInterceptorTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlightingInterceptorTest.java @@ -154,10 +154,12 @@ public class ResponseHighlightingInterceptorTest { ServletRequestDetails reqDetails = new ServletRequestDetails(); reqDetails.setRequestType(RequestTypeEnum.GET); - reqDetails.setParameters(new HashMap()); reqDetails.setServer(new RestfulServer(ourCtx)); reqDetails.setServletRequest(req); + // This can be null depending on the exception type + // reqDetails.setParameters(null); + ResourceNotFoundException exception = new ResourceNotFoundException("Not found"); exception.setOperationOutcome(new OperationOutcome().addIssue(new Issue().setDiagnostics("Hello"))); @@ -168,6 +170,7 @@ public class ResponseHighlightingInterceptorTest { assertThat(output, containsString("OperationOutcome")); } + @Test public void testHighlightNormalResponseForcePrettyPrint() throws Exception { ResponseHighlighterInterceptor ic = new ResponseHighlighterInterceptor(); 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 00cc068732b..09ff04dc1ce 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 @@ -489,6 +489,43 @@ public class GenericClientDstu3Test { assertEquals("http://foo.com/base/Patient/222/_history/3", capt.getAllValues().get(1).getURI().toASCIIString()); } + @Test + public void testValidate() throws Exception { + final IParser p = ourCtx.newXmlParser(); + + final OperationOutcome resp0 = new OperationOutcome(); + resp0.getText().setDivAsString("OK!"); + + ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); + when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse); + when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); + when(myHttpResponse.getAllHeaders()).thenAnswer(new Answer() { + @Override + public Header[] answer(InvocationOnMock theInvocation) throws Throwable { + return new Header[] { }; + } + }); + when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8")); + when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer() { + @Override + public ReaderInputStream answer(InvocationOnMock theInvocation) throws Throwable { + return new ReaderInputStream(new StringReader(p.encodeResourceToString(resp0)), Charset.forName("UTF-8")); + } + }); + + IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); + + Patient pt = new Patient(); + pt.setId("Patient/222"); + pt.getText().setDivAsString("A PATIENT"); + + MethodOutcome outcome = client.validate().resource(pt).execute(); + + assertNotNull(outcome.getOperationOutcome()); + assertEquals("
OK!
", ((OperationOutcome) outcome.getOperationOutcome()).getText().getDivAsString()); + + } + @Test public void testUpdateWithPreferRepresentationServerReturnsResource() throws Exception { final IParser p = ourCtx.newXmlParser(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorDstu3Test.java index 1c7b3d743a8..f7c0f0bfe26 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorDstu3Test.java @@ -83,38 +83,6 @@ public class FhirInstanceValidatorDstu3Test { myValidConcepts.add(theSystem + "___" + theCode); } - @Test - public void testValidateCarePlan() { - //@formatter:off - String input = "{\n" + - " \"resourceType\": \"Basic\",\n" + - " \"meta\": {\n" + - " \"profile\": [\n" + - " \"http://localhost:8080/baseDstu3/StructureDefinition/testBasic\"\n" + - " ]\n" + - " },\n" + - " \"code\": {\n" + - " \"coding\": [\n" + - " {\n" + - " \"system\": \"http://hl7.org/fhir/basic-resource-type\",\n" + - " \"code\": \"consent\",\n" + - " \"display\": \"Consent\"\n" + - " }\n" + - " ],\n" + - " \"text\": \"Consent\"\n" + - " },\n" + - " \"subject\": {\n" + - " \"reference\": \"Patient/2676\",\n" + - " \"display\": \"Lucy Turner\"\n" + - " }\n" + - "}"; - //@formatter:on - - ValidationResult output = myVal.validateWithResult(input); - List errors = logResultsAndReturnNonInformationalOnes(output); - assertThat(errors, empty()); - - } @Test // @Ignore