diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java index fc2dbae1c4b..f3fb2e366bd 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java @@ -42,6 +42,7 @@ import org.apache.http.client.methods.HttpRequestBase; import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseConformance; +import org.hl7.fhir.instance.model.api.IBaseDatatype; import org.hl7.fhir.instance.model.api.IBaseMetaType; import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -100,6 +101,7 @@ import ca.uhn.fhir.rest.gclient.IOperation; import ca.uhn.fhir.rest.gclient.IOperationUnnamed; import ca.uhn.fhir.rest.gclient.IOperationUntyped; import ca.uhn.fhir.rest.gclient.IOperationUntypedWithInput; +import ca.uhn.fhir.rest.gclient.IOperationUntypedWithInputAndPartialOutput; import ca.uhn.fhir.rest.gclient.IParam; import ca.uhn.fhir.rest.gclient.IQuery; import ca.uhn.fhir.rest.gclient.IRead; @@ -229,7 +231,8 @@ public class GenericClient extends BaseClient implements IGenericClient { return delete(theType, new IdDt(theId)); } - private T doReadOrVRead(final Class theType, IIdType theId, boolean theVRead, ICallable theNotModifiedHandler, String theIfVersionMatches, Boolean thePrettyPrint, SummaryEnum theSummary, EncodingEnum theEncoding, Set theSubsetElements) { + private T doReadOrVRead(final Class theType, IIdType theId, boolean theVRead, ICallable theNotModifiedHandler, String theIfVersionMatches, Boolean thePrettyPrint, + SummaryEnum theSummary, EncodingEnum theEncoding, Set theSubsetElements) { String resName = toResourceName(theType); IIdType id = theId; if (!id.hasBaseUrl()) { @@ -1353,13 +1356,14 @@ public class GenericClient extends BaseClient implements IGenericClient { } @SuppressWarnings("rawtypes") - private class OperationInternal extends BaseClientExecutable implements IOperation, IOperationUnnamed, IOperationUntyped, IOperationUntypedWithInput { + private class OperationInternal extends BaseClientExecutable implements IOperation, IOperationUnnamed, IOperationUntyped, IOperationUntypedWithInput, IOperationUntypedWithInputAndPartialOutput { private IIdType myId; private String myOperationName; private IBaseParameters myParameters; private Class myType; private boolean myUseHttpGet; + private RuntimeResourceDefinition myParametersDef; @SuppressWarnings("unchecked") @Override @@ -1455,6 +1459,51 @@ public class GenericClient extends BaseClient implements IGenericClient { return this; } + @SuppressWarnings("unchecked") + @Override + public IOperationUntypedWithInputAndPartialOutput withParameter(Class theParameterType, String theName, IBase theValue) { + Validate.notNull(theParameterType, "theParameterType must not be null"); + Validate.notEmpty(theName, "theName must not be null"); + Validate.notNull(theValue, "theValue must not be null"); + + myParametersDef = myContext.getResourceDefinition(theParameterType); + myParameters = (IBaseParameters) myParametersDef.newInstance(); + + addParam(theName, theValue); + + return this; + } + + @SuppressWarnings("unchecked") + private void addParam(String theName, IBase theValue) { + BaseRuntimeChildDefinition parameterChild = myParametersDef.getChildByName("parameter"); + BaseRuntimeElementCompositeDefinition parameterElem = (BaseRuntimeElementCompositeDefinition) parameterChild.getChildByName("parameter"); + + IBase parameter = parameterElem.newInstance(); + parameterChild.getMutator().addValue(myParameters, parameter); + + IPrimitiveType name = (IPrimitiveType) myContext.getElementDefinition("string").newInstance(); + name.setValue(theName); + parameterElem.getChildByName("name").getMutator().setValue(parameter, name); + + if (theValue instanceof IBaseDatatype) { + String childElementName = "value" + StringUtils.capitalize(myContext.getElementDefinition(theValue.getClass()).getName()); + parameterElem.getChildByName(childElementName).getMutator().setValue(parameter, theValue); + } else if (theValue instanceof IBaseResource) { + parameterElem.getChildByName("resource").getMutator().setValue(parameter, theValue); + } else { + throw new IllegalArgumentException("Don't know how to handle parameter of type " + theValue.getClass()); + } + } + + @Override + public IOperationUntypedWithInputAndPartialOutput andParameter(String theName, IBase theValue) { + Validate.notEmpty(theName, "theName must not be null"); + Validate.notNull(theValue, "theValue must not be null"); + addParam(theName, theValue); + return this; + } + } private final class OperationOutcomeResponseHandler implements IClientResponseHandler { @@ -1506,7 +1555,7 @@ public class GenericClient extends BaseClient implements IGenericClient { private RuntimeResourceDefinition myType; @Override - public Object execute() {//AAA + public Object execute() {// AAA if (myId.hasVersionIdPart()) { return doReadOrVRead(myType.getImplementingClass(), myId, true, myNotModifiedHandler, myIfVersionMatches, myPrettyPrint, mySummaryMode, myParamEncoding, getSubsetElements()); } else { @@ -1693,7 +1742,7 @@ public class GenericClient extends BaseClient implements IGenericClient { BaseRuntimeElementCompositeDefinition textElement = (BaseRuntimeElementCompositeDefinition) textChild.getChildByName("text"); IBase textInstance = textElement.newInstance(); textChild.getMutator().addValue(instance, textInstance); - + BaseRuntimeChildDefinition divChild = textElement.getChildByName("div"); BaseRuntimeElementDefinition divElement = divChild.getChildByName("div"); IPrimitiveType divInstance = (IPrimitiveType) divElement.newInstance(); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntyped.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntyped.java index 2a8edd07228..f8f40d67020 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntyped.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntyped.java @@ -1,5 +1,7 @@ package ca.uhn.fhir.rest.gclient; +import org.hl7.fhir.instance.model.api.IBase; + /* * #%L * HAPI FHIR - Core Library @@ -40,4 +42,16 @@ public interface IOperationUntyped { */ IOperationUntypedWithInput withNoParameters(Class theOutputParameterType); + /** + * Use chained method calls to construct a Parameters input. This form is a convenience + * in order to allow simple method chaining to be used to build up a parameters + * resource for the input of an operation without needing to manually construct one. + * + * @param theParameterType The type to use for the output parameters (this should be set to + * Parameters.class drawn from the version of the FHIR structures you are using) + * @param theName The first parameter name + * @param theValue The first parameter value + */ + IOperationUntypedWithInputAndPartialOutput withParameter(Class theParameterType, String theName, IBase theValue); + } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntypedWithInputAndPartialOutput.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntypedWithInputAndPartialOutput.java new file mode 100644 index 00000000000..282567dd16c --- /dev/null +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IOperationUntypedWithInputAndPartialOutput.java @@ -0,0 +1,18 @@ +package ca.uhn.fhir.rest.gclient; + +import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseParameters; + +public interface IOperationUntypedWithInputAndPartialOutput extends IOperationUntypedWithInput { + + /** + * Use chained method calls to construct a Parameters input. This form is a convenience + * in order to allow simple method chaining to be used to build up a parameters + * resource for the input of an operation without needing to manually construct one. + * + * @param theName The first parameter name + * @param theValue The first parameter value + */ + IOperationUntypedWithInputAndPartialOutput andParameter(String theName, IBase theValue); + +} diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoQuestionnaireResponseDstu2.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoQuestionnaireResponseDstu2.java index 2fa92c6f9d6..c8ad24c8cae 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoQuestionnaireResponseDstu2.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoQuestionnaireResponseDstu2.java @@ -25,6 +25,8 @@ import javax.annotation.PostConstruct; import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IIdType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jpa.entity.ResourceTable; @@ -41,7 +43,10 @@ import ca.uhn.fhir.validation.ValidationResult; public class FhirResourceDaoQuestionnaireResponseDstu2 extends FhirResourceDaoDstu2 { + @Autowired + @Qualifier("myFhirContextDstu2Hl7Org") private FhirContext myRefImplCtx; + private Boolean myValidateResponses; /** @@ -52,7 +57,6 @@ public class FhirResourceDaoQuestionnaireResponseDstu2 extends FhirResourceDaoDs try { Class.forName("org.hl7.fhir.instance.model.QuestionnaireResponse"); myValidateResponses = true; - myRefImplCtx = FhirContext.forDstu2Hl7Org(); } catch (ClassNotFoundException e) { myValidateResponses = Boolean.FALSE; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoValueSetDstu2.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoValueSetDstu2.java index 48a5374251a..f4d5eed65da 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoValueSetDstu2.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoValueSetDstu2.java @@ -28,10 +28,15 @@ import java.util.Collections; import java.util.List; import java.util.Set; +import javax.annotation.PostConstruct; + import org.apache.commons.codec.binary.StringUtils; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IPrimitiveType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jpa.entity.BaseHasResource; import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt; import ca.uhn.fhir.model.dstu2.composite.CodingDt; @@ -46,32 +51,83 @@ import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.model.primitive.UriDt; import ca.uhn.fhir.rest.param.TokenParam; +import ca.uhn.fhir.rest.param.UriParam; +import ca.uhn.fhir.rest.server.IBundleProvider; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; +import ca.uhn.fhir.validation.DefaultProfileValidationSupport; +import ca.uhn.fhir.validation.ValidationSupportChain; public class FhirResourceDaoValueSetDstu2 extends FhirResourceDaoDstu2implements IFhirResourceDaoValueSet { - @Override - public ValueSet expand(IIdType theId, StringDt theFilter) { - ValueSet retVal = new ValueSet(); - retVal.setDate(DateTimeDt.withCurrentTime()); + @Autowired + private IJpaValidationSupport myJpaValidationSupport; + + private ValidationSupportChain myValidationSupport; + + @Autowired + @Qualifier("myFhirContextDstu2Hl7Org") + private FhirContext myRiCtx; + private DefaultProfileValidationSupport myDefaultProfileValidationSupport; + + @Override + @PostConstruct + public void postConstruct() { + super.postConstruct(); + myDefaultProfileValidationSupport = new DefaultProfileValidationSupport(); + myValidationSupport = new ValidationSupportChain(myDefaultProfileValidationSupport, myJpaValidationSupport); + } + + @Override + public ValueSet expand(IIdType theId, String theFilter) { BaseHasResource sourceEntity = readEntity(theId); if (sourceEntity == null) { throw new ResourceNotFoundException(theId); } ValueSet source = (ValueSet) toResource(sourceEntity, false); + return expand(source, theFilter); + + } + + @Override + public ValueSet expandByIdentifier(String theUri, String theFilter) { + if (isBlank(theUri)) { + throw new InvalidRequestException("URI must not be blank or missing"); + } + ValueSet source; + + org.hl7.fhir.instance.model.ValueSet defaultValueSet = myDefaultProfileValidationSupport.fetchResource(myRiCtx, org.hl7.fhir.instance.model.ValueSet.class, theUri); + if (defaultValueSet != null) { + source = getContext().newJsonParser().parseResource(ValueSet.class, myRiCtx.newJsonParser().encodeResourceToString(defaultValueSet)); + } else { + IBundleProvider ids = search(ValueSet.SP_URL, new UriParam(theUri)); + if (ids.size() == 0) { + throw new InvalidRequestException("Unknown ValueSet URI: " + theUri); + } + source = (ValueSet) ids.getResources(0, 1).get(0); + } + + return expand(source, theFilter); + + } + + @Override + public ValueSet expand(ValueSet source, String theFilter) { + ValueSet retVal = new ValueSet(); + retVal.setDate(DateTimeDt.withCurrentTime()); + /* * Add composed concepts */ for (ComposeInclude nextInclude : source.getCompose().getInclude()) { for (ComposeIncludeConcept next : nextInclude.getConcept()) { - if (theFilter == null || theFilter.isEmpty()) { + if (isBlank(theFilter)) { addCompose(retVal, nextInclude.getSystem(), next.getCode(), next.getDisplay()); } else { - String filter = theFilter.getValue().toLowerCase(); + String filter = theFilter.toLowerCase(); if (next.getDisplay().toLowerCase().contains(filter) || next.getCode().toLowerCase().contains(filter)) { addCompose(retVal, nextInclude.getSystem(), next.getCode(), next.getDisplay()); } @@ -88,14 +144,13 @@ public class FhirResourceDaoValueSetDstu2 extends FhirResourceDaoDstu2 } return retVal; - } - private void addCompose(StringDt theFilter, ValueSet theValueSetToPopulate, ValueSet theSourceValueSet, CodeSystemConcept theConcept) { - if (theFilter == null || theFilter.isEmpty()) { + private void addCompose(String theFilter, ValueSet theValueSetToPopulate, ValueSet theSourceValueSet, CodeSystemConcept theConcept) { + if (isBlank(theFilter)) { addCompose(theValueSetToPopulate, theSourceValueSet.getCodeSystem().getSystem(), theConcept.getCode(), theConcept.getDisplay()); } else { - String filter = theFilter.getValue().toLowerCase(); + String filter = theFilter.toLowerCase(); if (theConcept.getDisplay().toLowerCase().contains(filter) || theConcept.getCode().toLowerCase().contains(filter)) { addCompose(theValueSetToPopulate, theSourceValueSet.getCodeSystem().getSystem(), theConcept.getCode(), theConcept.getDisplay()); } @@ -133,7 +188,7 @@ public class FhirResourceDaoValueSetDstu2 extends FhirResourceDaoDstu2 boolean haveIdentifierParam = theValueSetIdentifier != null && theValueSetIdentifier.isEmpty() == false; if (theId != null) { - valueSetIds = Collections.singletonList((IIdType) theId); + valueSetIds = Collections.singletonList(theId); } else if (haveIdentifierParam) { Set ids = searchForIds(ValueSet.SP_IDENTIFIER, new TokenParam(null, theValueSetIdentifier.getValue())); valueSetIds = new ArrayList(); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFhirResourceDaoValueSet.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFhirResourceDaoValueSet.java index f4a3359d1a8..488dc99fc0f 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFhirResourceDaoValueSet.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IFhirResourceDaoValueSet.java @@ -32,8 +32,12 @@ import ca.uhn.fhir.model.primitive.UriDt; public interface IFhirResourceDaoValueSet extends IFhirResourceDao { - ValueSet expand(IIdType theId, StringDt theFilter); + ValueSet expand(IIdType theId, String theFilter); + ValueSet expand(ValueSet theSource, String theFilter); + + ValueSet expandByIdentifier(String theUri, String theFilter); + ValidateCodeResult validateCode(UriDt theValueSetIdentifier, IIdType theId, CodeDt theCode, UriDt theSystem, StringDt theDisplay, CodingDt theCoding, CodeableConceptDt theCodeableConcept); public class ValidateCodeResult { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IJpaValidationSupport.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IJpaValidationSupport.java new file mode 100644 index 00000000000..57d544fcbbd --- /dev/null +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/IJpaValidationSupport.java @@ -0,0 +1,7 @@ +package ca.uhn.fhir.jpa.dao; + +import ca.uhn.fhir.validation.IValidationSupport; + +public interface IJpaValidationSupport extends IValidationSupport { + +} diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/JpaValidationSupportDstu2.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/JpaValidationSupportDstu2.java index c12f0d41139..5a10218a7fc 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/JpaValidationSupportDstu2.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/JpaValidationSupportDstu2.java @@ -30,13 +30,14 @@ import org.springframework.beans.factory.annotation.Qualifier; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.param.UriParam; import ca.uhn.fhir.rest.server.IBundleProvider; -import ca.uhn.fhir.validation.IValidationSupport; -public class JpaValidationSupportDstu2 implements IValidationSupport { +public class JpaValidationSupportDstu2 implements IJpaValidationSupport { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JpaValidationSupportDstu2.class); - private FhirContext myRiCtx = FhirContext.forDstu2Hl7Org(); + @Autowired + @Qualifier("myFhirContextDstu2Hl7Org") + private FhirContext myRiCtx; @Autowired @Qualifier("myStructureDefinitionDaoDstu2") diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/provider/BaseJpaResourceProviderValueSetDstu2.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/provider/BaseJpaResourceProviderValueSetDstu2.java index fd5ea50e45d..dfaf108450d 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/provider/BaseJpaResourceProviderValueSetDstu2.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/provider/BaseJpaResourceProviderValueSetDstu2.java @@ -45,19 +45,63 @@ public class BaseJpaResourceProviderValueSetDstu2 extends JpaResourceProviderDst @Operation(name = "$expand", idempotent = true) public ValueSet everything( HttpServletRequest theServletRequest, - @IdParam IdDt theId, - @OperationParam(name = "filter") StringDt theFilter) { + + @IdParam IdDt theId, + + @OperationParam(name = "filter", min=0, max=1) StringDt theFilter) { //@formatter:on startRequest(theServletRequest); try { IFhirResourceDaoValueSet dao = (IFhirResourceDaoValueSet) getDao(); - return dao.expand(theId, theFilter); + return dao.expand(theId, toFilterString(theFilter)); } finally { endRequest(theServletRequest); } } + //@formatter:off + @Operation(name = "$expand", idempotent = true) + public ValueSet everything( + HttpServletRequest theServletRequest, + + @OperationParam(name="identifier", min=1, max=1) UriDt theIdentifier, + + @OperationParam(name = "filter", min=0, max=1) StringDt theFilter) { + //@formatter:on + + startRequest(theServletRequest); + try { + IFhirResourceDaoValueSet dao = (IFhirResourceDaoValueSet) getDao(); + return dao.expandByIdentifier(theIdentifier.getValue(), toFilterString(theFilter)); + } finally { + endRequest(theServletRequest); + } + } + + //@formatter:off + @Operation(name = "$expand", idempotent = true) + public ValueSet everything( + HttpServletRequest theServletRequest, + + @OperationParam(name="valueSet", min=1, max=1) ValueSet theValueSet, + + @OperationParam(name = "filter", min=0, max=1) StringDt theFilter) { + //@formatter:on + + startRequest(theServletRequest); + try { + IFhirResourceDaoValueSet dao = (IFhirResourceDaoValueSet) getDao(); + return dao.expand(theValueSet, toFilterString(theFilter)); + } finally { + endRequest(theServletRequest); + } + } + + private String toFilterString(StringDt theFilter) { + return theFilter != null ? theFilter.getValue() : null; + } + //@formatter:off @Operation(name = "$validate-code", idempotent = true, returnParameters= { @OperationParam(name="result", type=BooleanDt.class, min=1), diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaDstu2Test.java index 4b9f8eb0bfe..c702ba79e0c 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaDstu2Test.java @@ -103,6 +103,7 @@ public abstract class BaseJpaDstu2Test extends BaseJpaTest { @PersistenceContext() protected EntityManager myEntityManager; @Autowired + @Qualifier("myFhirContextDstu2") protected FhirContext myFhirCtx; @Autowired @Qualifier("myImmunizationDaoDstu2") @@ -151,7 +152,7 @@ public abstract class BaseJpaDstu2Test extends BaseJpaTest { protected PlatformTransactionManager myTxManager; @Autowired @Qualifier("myValueSetDaoDstu2") - protected IFhirResourceDao myValueSetDao; + protected IFhirResourceDaoValueSet myValueSetDao; @Before public void beforeCreateInterceptor() { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoValueSetDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoValueSetDstu2Test.java index b21849ce836..fc10780cf1c 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoValueSetDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoValueSetDstu2Test.java @@ -1,68 +1,42 @@ package ca.uhn.fhir.jpa.dao; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.stringContainsInOrder; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import java.io.IOException; -import java.io.InputStreamReader; import org.hl7.fhir.instance.model.api.IIdType; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; -import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult; -import ca.uhn.fhir.jpa.provider.ResourceProviderDstu2Test; import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt; import ca.uhn.fhir.model.dstu2.composite.CodingDt; -import ca.uhn.fhir.model.dstu2.resource.Bundle; import ca.uhn.fhir.model.dstu2.resource.ValueSet; import ca.uhn.fhir.model.primitive.CodeDt; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.model.primitive.UriDt; -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration({ "/hapi-fhir-server-resourceproviders-dstu2.xml", "/fhir-jpabase-spring-test-config.xml" }) -public class FhirResourceDaoValueSetDstu2Test { +public class FhirResourceDaoValueSetDstu2Test extends BaseJpaDstu2Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirResourceDaoValueSetDstu2Test.class); - private static IIdType vsid; + private IIdType myExtensionalVsId; - @Autowired - private FhirContext myCtx; - - @Autowired - @Qualifier("mySystemDaoDstu2") - private IFhirSystemDao mySystemDao; - - @Autowired - @Qualifier("myValueSetDaoDstu2") - private IFhirResourceDaoValueSet myValueSetDao; @Before @Transactional - public void before01() { - if (vsid == null) { - FhirSystemDaoDstu2Test.doDeleteEverything(mySystemDao); - } - } - - @Before - @Transactional - public void before02() { - if (vsid == null) { - ValueSet upload = myCtx.newXmlParser().parseResource(ValueSet.class, new InputStreamReader(ResourceProviderDstu2Test.class.getResourceAsStream("/extensional-case-2.xml"))); - upload.setId(""); - vsid = myValueSetDao.create(upload).getId().toUnqualifiedVersionless(); - } + public void before02() throws IOException { + ValueSet upload = loadResourceFromClasspath(ValueSet.class, "/extensional-case-2.xml"); + upload.setId(""); + myExtensionalVsId = myValueSetDao.create(upload).getId().toUnqualifiedVersionless(); } @Test @@ -137,7 +111,7 @@ public class FhirResourceDaoValueSetDstu2Test { @Test public void testValidateCodeOperationByResourceIdAndCodeableConcept() { UriDt valueSetIdentifier = null; - IIdType id = vsid; + IIdType id = myExtensionalVsId; CodeDt code = null; UriDt system = null; StringDt display = null; @@ -151,7 +125,7 @@ public class FhirResourceDaoValueSetDstu2Test { @Test public void testValidateCodeOperationByResourceIdAndCodeAndSystem() { UriDt valueSetIdentifier = null; - IIdType id = vsid; + IIdType id = myExtensionalVsId; CodeDt code = new CodeDt("11378-7"); UriDt system = new UriDt("http://loinc.org"); StringDt display = null; @@ -163,11 +137,11 @@ public class FhirResourceDaoValueSetDstu2Test { } @Test - public void testValueSetExpandOperation() throws IOException { + public void testExpandById() throws IOException { String resp; - ValueSet expanded = myValueSetDao.expand(vsid, null); - resp = myCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); + ValueSet expanded = myValueSetDao.expand(myExtensionalVsId, null); + resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); ourLog.info(resp); // @formatter:off assertThat(resp, @@ -191,8 +165,8 @@ public class FhirResourceDaoValueSetDstu2Test { * Filter with display name */ - expanded = myValueSetDao.expand(vsid, new StringDt("systolic")); - resp = myCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); + expanded = myValueSetDao.expand(myExtensionalVsId, ("systolic")); + resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); ourLog.info(resp); //@formatter:off assertThat(resp, stringContainsInOrder( @@ -204,8 +178,8 @@ public class FhirResourceDaoValueSetDstu2Test { * Filter with code */ - expanded = myValueSetDao.expand(vsid, new StringDt("11378")); - resp = myCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); + expanded = myValueSetDao.expand(myExtensionalVsId, ("11378")); + resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); ourLog.info(resp); //@formatter:off assertThat(resp, stringContainsInOrder( @@ -213,5 +187,34 @@ public class FhirResourceDaoValueSetDstu2Test { "")); //@formatter:on } + + @Test + public void testExpandByIdentifier() { + ValueSet expanded = myValueSetDao.expandByIdentifier("http://www.healthintersections.com.au/fhir/ValueSet/extensional-case-2", "11378"); + String resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); + ourLog.info(resp); + //@formatter:off + assertThat(resp, stringContainsInOrder( + "", + "")); + //@formatter:on + + assertThat(resp, not(containsString(""))); + } + + @Test + public void testExpandByValueSet() throws IOException { + ValueSet toExpand = loadResourceFromClasspath(ValueSet.class, "/extensional-case-2.xml"); + ValueSet expanded = myValueSetDao.expand(toExpand, "11378"); + String resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); + ourLog.info(resp); + //@formatter:off + assertThat(resp, stringContainsInOrder( + "", + "")); + //@formatter:on + + assertThat(resp, not(containsString(""))); + } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/ResourceProviderDstu2ValueSetTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/ResourceProviderDstu2ValueSetTest.java new file mode 100644 index 00000000000..e89eeeadd98 --- /dev/null +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/ResourceProviderDstu2ValueSetTest.java @@ -0,0 +1,161 @@ +package ca.uhn.fhir.jpa.provider; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.stringContainsInOrder; +import static org.junit.Assert.assertThat; + +import java.io.IOException; + +import org.hl7.fhir.instance.model.api.IIdType; +import org.junit.Before; +import org.junit.Test; +import org.springframework.transaction.annotation.Transactional; + +import ca.uhn.fhir.model.dstu2.resource.Parameters; +import ca.uhn.fhir.model.dstu2.resource.ValueSet; +import ca.uhn.fhir.model.primitive.StringDt; +import ca.uhn.fhir.model.primitive.UriDt; + +public class ResourceProviderDstu2ValueSetTest extends BaseResourceProviderDstu2Test { + + private IIdType myExtensionalVsId; + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResourceProviderDstu2ValueSetTest.class); + + @Before + @Transactional + public void before02() throws IOException { + ValueSet upload = loadResourceFromClasspath(ValueSet.class, "/extensional-case-2.xml"); + upload.setId(""); + myExtensionalVsId = myValueSetDao.create(upload).getId().toUnqualifiedVersionless(); + } + + @Test + public void testExpandById() throws IOException { + //@formatter:off + Parameters respParam = ourClient + .operation() + .onInstance(myExtensionalVsId) + .named("expand") + .withNoParameters(Parameters.class) + .execute(); + ValueSet expanded = (ValueSet) respParam.getParameter().get(0).getResource(); + //@formatter:on + + String resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); + ourLog.info(resp); + // @formatter:off + assertThat(resp, + stringContainsInOrder("", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" + )); + //@formatter:on + + /* + * Filter with display name + */ + + //@formatter:off + respParam = ourClient + .operation() + .onInstance(myExtensionalVsId) + .named("expand") + .withParameter(Parameters.class, "filter", new StringDt("systolic")) + .execute(); + expanded = (ValueSet) respParam.getParameter().get(0).getResource(); + //@formatter:on + + expanded = myValueSetDao.expand(myExtensionalVsId, ("systolic")); + resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); + ourLog.info(resp); + //@formatter:off + assertThat(resp, stringContainsInOrder( + "", + "")); + //@formatter:on + + /* + * Filter with code + */ + + //@formatter:off + respParam = ourClient + .operation() + .onInstance(myExtensionalVsId) + .named("expand") + .withParameter(Parameters.class, "filter", new StringDt("11378")) + .execute(); + expanded = (ValueSet) respParam.getParameter().get(0).getResource(); + //@formatter:on + resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); + ourLog.info(resp); + //@formatter:off + assertThat(resp, stringContainsInOrder( + "", + "")); + //@formatter:on + } + + @Test + public void testExpandByIdentifier() { + //@formatter:off + Parameters respParam = ourClient + .operation() + .onInstance(myExtensionalVsId) + .named("expand") + .withParameter(Parameters.class, "identifier", new UriDt("http://www.healthintersections.com.au/fhir/ValueSet/extensional-case-2")) + .andParameter("filter", new StringDt("11378")) + .execute(); + ValueSet expanded = (ValueSet) respParam.getParameter().get(0).getResource(); + //@formatter:on + + String resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); + ourLog.info(resp); + //@formatter:off + assertThat(resp, stringContainsInOrder( + "", + "")); + //@formatter:on + + assertThat(resp, not(containsString(""))); + } + + @Test + public void testExpandByValueSet() throws IOException { + ValueSet toExpand = loadResourceFromClasspath(ValueSet.class, "/extensional-case-2.xml"); + + //@formatter:off + Parameters respParam = ourClient + .operation() + .onInstance(myExtensionalVsId) + .named("expand") + .withParameter(Parameters.class, "valueSet", toExpand) + .andParameter("filter", new StringDt("11378")) + .execute(); + ValueSet expanded = (ValueSet) respParam.getParameter().get(0).getResource(); + //@formatter:on + + String resp = myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(expanded); + ourLog.info(resp); + //@formatter:off + assertThat(resp, stringContainsInOrder( + "", + "")); + //@formatter:on + + assertThat(resp, not(containsString(""))); + } + + +} diff --git a/hapi-fhir-jpaserver-base/src/test/resources/extensional-case-2.xml b/hapi-fhir-jpaserver-base/src/test/resources/extensional-case-2.xml index 7d2aa6e49a0..fb5ac110c50 100644 --- a/hapi-fhir-jpaserver-base/src/test/resources/extensional-case-2.xml +++ b/hapi-fhir-jpaserver-base/src/test/resources/extensional-case-2.xml @@ -4,6 +4,7 @@
A selection of codes from http://loinc.org
+ diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu2Test.java index 570e220d304..2375a7c116f 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/GenericClientDstu2Test.java @@ -27,6 +27,7 @@ import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicStatusLine; +import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; import org.hl7.fhir.instance.model.api.IBaseResource; @@ -42,6 +43,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; +import ca.uhn.fhir.model.dstu2.composite.IdentifierDt; import ca.uhn.fhir.model.dstu2.composite.MetaDt; import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry; import ca.uhn.fhir.model.dstu2.resource.Bundle.Link; @@ -69,8 +71,11 @@ import ca.uhn.fhir.rest.server.EncodingEnum; public class GenericClientDstu2Test { private static FhirContext ourCtx; private HttpClient myHttpClient; + private HttpResponse myHttpResponse; + private int myResponseCount = 0; + @Before public void before() { myHttpClient = mock(HttpClient.class, new ReturnsDeepStubs()); @@ -106,6 +111,133 @@ public class GenericClientDstu2Test { return msg; } + @Test + public void testAcceptHeaderFetchConformance() throws Exception { + IParser p = ourCtx.newXmlParser(); + + Conformance conf = new Conformance(); + conf.setCopyright("COPY"); + + final String respString = p.encodeResourceToString(conf); + 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.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(respString), Charset.forName("UTF-8")); + } + }); + + IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); + + int idx = 0; + + client.fetchConformance().ofType(Conformance.class).execute(); + assertEquals("http://example.com/fhir/metadata", capt.getAllValues().get(idx).getURI().toASCIIString()); + assertEquals(1, capt.getAllValues().get(idx).getHeaders("Accept").length); + assertThat(capt.getAllValues().get(idx).getHeaders("Accept")[0].getValue(), containsString(Constants.HEADER_ACCEPT_VALUE_ALL)); + idx++; + + client.fetchConformance().ofType(Conformance.class).encodedJson().execute(); + assertEquals("http://example.com/fhir/metadata?_format=json", capt.getAllValues().get(idx).getURI().toASCIIString()); + assertEquals(1, capt.getAllValues().get(idx).getHeaders("Accept").length); + assertThat(capt.getAllValues().get(idx).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_JSON)); + idx++; + + client.fetchConformance().ofType(Conformance.class).encodedXml().execute(); + assertEquals("http://example.com/fhir/metadata?_format=xml", capt.getAllValues().get(idx).getURI().toASCIIString()); + assertEquals(1, capt.getAllValues().get(idx).getHeaders("Accept").length); + assertThat(capt.getAllValues().get(idx).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_XML)); + idx++; + } + + @Test + public void testAcceptHeaderPreflightConformance() throws Exception { + String methodName = "testAcceptHeaderPreflightConformance"; + final IParser p = ourCtx.newXmlParser(); + + final Conformance conf = new Conformance(); + conf.setCopyright("COPY"); + + final Patient patient = new Patient(); + patient.addName().addFamily("FAMILY"); + + 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.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 { + if (myResponseCount++ == 0) { + return new ReaderInputStream(new StringReader(p.encodeResourceToString(conf)), Charset.forName("UTF-8")); + } else { + return new ReaderInputStream(new StringReader(p.encodeResourceToString(patient)), Charset.forName("UTF-8")); + } + } + }); + + ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.ONCE); + IGenericClient client = ourCtx.newRestfulGenericClient("http://" + methodName + ".example.com/fhir"); + + Patient resp = client.read(Patient.class, new IdDt("123")); + assertEquals("FAMILY", resp.getName().get(0).getFamily().get(0).getValue()); + assertEquals("http://" + methodName + ".example.com/fhir/metadata", capt.getAllValues().get(0).getURI().toASCIIString()); + assertEquals(1, capt.getAllValues().get(0).getHeaders("Accept").length); + assertThat(capt.getAllValues().get(0).getHeaders("Accept")[0].getValue(), containsString(Constants.HEADER_ACCEPT_VALUE_ALL)); + assertThat(capt.getAllValues().get(0).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_XML)); + assertThat(capt.getAllValues().get(0).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_JSON)); + assertEquals("http://" + methodName + ".example.com/fhir/Patient/123", capt.getAllValues().get(1).getURI().toASCIIString()); + assertEquals(1, capt.getAllValues().get(1).getHeaders("Accept").length); + assertThat(capt.getAllValues().get(1).getHeaders("Accept")[0].getValue(), containsString(Constants.HEADER_ACCEPT_VALUE_ALL)); + assertThat(capt.getAllValues().get(1).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_XML)); + assertThat(capt.getAllValues().get(1).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_JSON)); + } + + @Test + public void testAcceptHeaderPreflightConformancePreferJson() throws Exception { + String methodName = "testAcceptHeaderPreflightConformancePreferJson"; + final IParser p = ourCtx.newXmlParser(); + + final Conformance conf = new Conformance(); + conf.setCopyright("COPY"); + + final Patient patient = new Patient(); + patient.addName().addFamily("FAMILY"); + + 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.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 { + if (myResponseCount++ == 0) { + return new ReaderInputStream(new StringReader(p.encodeResourceToString(conf)), Charset.forName("UTF-8")); + } else { + return new ReaderInputStream(new StringReader(p.encodeResourceToString(patient)), Charset.forName("UTF-8")); + } + } + }); + + ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.ONCE); + IGenericClient client = ourCtx.newRestfulGenericClient("http://" + methodName + ".example.com/fhir"); + client.setEncoding(EncodingEnum.JSON); + + Patient resp = client.read(Patient.class, new IdDt("123")); + assertEquals("FAMILY", resp.getName().get(0).getFamily().get(0).getValue()); + assertEquals("http://" + methodName + ".example.com/fhir/metadata?_format=json", capt.getAllValues().get(0).getURI().toASCIIString()); + assertEquals(1, capt.getAllValues().get(0).getHeaders("Accept").length); + assertThat(capt.getAllValues().get(0).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_JSON)); + assertThat(capt.getAllValues().get(0).getHeaders("Accept")[0].getValue(), not(containsString(Constants.CT_FHIR_XML))); + assertEquals("http://" + methodName + ".example.com/fhir/Patient/123?_format=json", capt.getAllValues().get(1).getURI().toASCIIString()); + assertEquals(1, capt.getAllValues().get(1).getHeaders("Accept").length); + assertThat(capt.getAllValues().get(1).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_JSON)); + assertThat(capt.getAllValues().get(1).getHeaders("Accept")[0].getValue(), not(containsString(Constants.CT_FHIR_XML))); + } + @Test @SuppressWarnings("deprecation") public void testConformance() throws Exception { @@ -141,136 +273,6 @@ public class GenericClientDstu2Test { } - @Test - public void testAcceptHeaderFetchConformance() throws Exception { - IParser p = ourCtx.newXmlParser(); - - Conformance conf = new Conformance(); - conf.setCopyright("COPY"); - - final String respString = p.encodeResourceToString(conf); - 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.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(respString), Charset.forName("UTF-8")); - } - }); - - IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); - - int idx = 0; - - Conformance resp = (Conformance)client.fetchConformance().ofType(Conformance.class).execute(); - assertEquals("http://example.com/fhir/metadata", capt.getAllValues().get(idx).getURI().toASCIIString()); - assertEquals(1, capt.getAllValues().get(idx).getHeaders("Accept").length); - assertThat(capt.getAllValues().get(idx).getHeaders("Accept")[0].getValue(), containsString(Constants.HEADER_ACCEPT_VALUE_ALL)); - idx++; - - resp = (Conformance)client.fetchConformance().ofType(Conformance.class).encodedJson().execute(); - assertEquals("http://example.com/fhir/metadata?_format=json", capt.getAllValues().get(idx).getURI().toASCIIString()); - assertEquals(1, capt.getAllValues().get(idx).getHeaders("Accept").length); - assertThat(capt.getAllValues().get(idx).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_JSON)); - idx++; - - resp = (Conformance)client.fetchConformance().ofType(Conformance.class).encodedXml().execute(); - assertEquals("http://example.com/fhir/metadata?_format=xml", capt.getAllValues().get(idx).getURI().toASCIIString()); - assertEquals(1, capt.getAllValues().get(idx).getHeaders("Accept").length); - assertThat(capt.getAllValues().get(idx).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_XML)); - idx++; - } - - private int myResponseCount = 0; - - @Test - public void testAcceptHeaderPreflightConformancePreferJson() throws Exception { - String methodName = "testAcceptHeaderPreflightConformancePreferJson"; - final IParser p = ourCtx.newXmlParser(); - - final Conformance conf = new Conformance(); - conf.setCopyright("COPY"); - - final Patient patient = new Patient(); - patient.addName().addFamily("FAMILY"); - - 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.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 { - if (myResponseCount++ == 0) { - return new ReaderInputStream(new StringReader(p.encodeResourceToString(conf)), Charset.forName("UTF-8")); - } else { - return new ReaderInputStream(new StringReader(p.encodeResourceToString(patient)), Charset.forName("UTF-8")); - } - } - }); - - ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.ONCE); - IGenericClient client = ourCtx.newRestfulGenericClient("http://"+methodName+".example.com/fhir"); - client.setEncoding(EncodingEnum.JSON); - - Patient resp = client.read(Patient.class, new IdDt("123")); - assertEquals("FAMILY", resp.getName().get(0).getFamily().get(0).getValue()); - assertEquals("http://"+methodName+".example.com/fhir/metadata?_format=json", capt.getAllValues().get(0).getURI().toASCIIString()); - assertEquals(1, capt.getAllValues().get(0).getHeaders("Accept").length); - assertThat(capt.getAllValues().get(0).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_JSON)); - assertThat(capt.getAllValues().get(0).getHeaders("Accept")[0].getValue(), not(containsString(Constants.CT_FHIR_XML))); - assertEquals("http://"+methodName+".example.com/fhir/Patient/123?_format=json", capt.getAllValues().get(1).getURI().toASCIIString()); - assertEquals(1, capt.getAllValues().get(1).getHeaders("Accept").length); - assertThat(capt.getAllValues().get(1).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_JSON)); - assertThat(capt.getAllValues().get(1).getHeaders("Accept")[0].getValue(), not(containsString(Constants.CT_FHIR_XML))); - } - - @Test - public void testAcceptHeaderPreflightConformance() throws Exception { - String methodName = "testAcceptHeaderPreflightConformance"; - final IParser p = ourCtx.newXmlParser(); - - final Conformance conf = new Conformance(); - conf.setCopyright("COPY"); - - final Patient patient = new Patient(); - patient.addName().addFamily("FAMILY"); - - 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.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 { - if (myResponseCount++ == 0) { - return new ReaderInputStream(new StringReader(p.encodeResourceToString(conf)), Charset.forName("UTF-8")); - } else { - return new ReaderInputStream(new StringReader(p.encodeResourceToString(patient)), Charset.forName("UTF-8")); - } - } - }); - - ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.ONCE); - IGenericClient client = ourCtx.newRestfulGenericClient("http://"+methodName+".example.com/fhir"); - - Patient resp = client.read(Patient.class, new IdDt("123")); - assertEquals("FAMILY", resp.getName().get(0).getFamily().get(0).getValue()); - assertEquals("http://"+methodName+".example.com/fhir/metadata", capt.getAllValues().get(0).getURI().toASCIIString()); - assertEquals(1, capt.getAllValues().get(0).getHeaders("Accept").length); - assertThat(capt.getAllValues().get(0).getHeaders("Accept")[0].getValue(), containsString(Constants.HEADER_ACCEPT_VALUE_ALL)); - assertThat(capt.getAllValues().get(0).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_XML)); - assertThat(capt.getAllValues().get(0).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_JSON)); - assertEquals("http://"+methodName+".example.com/fhir/Patient/123", capt.getAllValues().get(1).getURI().toASCIIString()); - assertEquals(1, capt.getAllValues().get(1).getHeaders("Accept").length); - assertThat(capt.getAllValues().get(1).getHeaders("Accept")[0].getValue(), containsString(Constants.HEADER_ACCEPT_VALUE_ALL)); - assertThat(capt.getAllValues().get(1).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_XML)); - assertThat(capt.getAllValues().get(1).getHeaders("Accept")[0].getValue(), containsString(Constants.CT_FHIR_JSON)); - } - - @Test public void testCreate() throws Exception { ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); @@ -382,44 +384,6 @@ public class GenericClientDstu2Test { idx++; } - @SuppressWarnings("deprecation") - @Test - public void testUpdateNonFluent() throws Exception { - ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); - when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse); - when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), Constants.STATUS_HTTP_204_NO_CONTENT, "")); - when(myHttpResponse.getEntity().getContent()).then(new Answer() { - @Override - public ReaderInputStream answer(InvocationOnMock theInvocation) throws Throwable { - return new ReaderInputStream(new StringReader(""), Charset.forName("UTF-8")); - } - }); - - IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); - - int idx = 0; - - Patient p = new Patient(); - p.addName().addFamily("FOOFAMILY"); - - client.update(new IdDt("Patient/123"), p); - assertEquals(1, capt.getAllValues().get(idx).getHeaders(Constants.HEADER_CONTENT_TYPE).length); - assertEquals(EncodingEnum.XML.getResourceContentType() + Constants.HEADER_SUFFIX_CT_UTF_8, capt.getAllValues().get(idx).getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue()); - assertThat(extractBody(capt, idx), containsString("")); - assertEquals("http://example.com/fhir/Patient/123", capt.getAllValues().get(idx).getURI().toString()); - assertEquals("PUT", capt.getAllValues().get(idx).getRequestLine().getMethod()); - idx++; - - client.update("123", p); - assertEquals(1, capt.getAllValues().get(idx).getHeaders(Constants.HEADER_CONTENT_TYPE).length); - assertEquals(EncodingEnum.XML.getResourceContentType() + Constants.HEADER_SUFFIX_CT_UTF_8, capt.getAllValues().get(idx).getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue()); - assertThat(extractBody(capt, idx), containsString("")); - assertEquals("http://example.com/fhir/Patient/123", capt.getAllValues().get(idx).getURI().toString()); - assertEquals("PUT", capt.getAllValues().get(idx).getRequestLine().getMethod()); - idx++; -} - - @Test public void testCreatePrefer() throws Exception { ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); @@ -450,7 +414,7 @@ public class GenericClientDstu2Test { idx++; } - + @Test public void testCreateReturningResourceBody() throws Exception { Patient p = new Patient(); @@ -535,7 +499,7 @@ public class GenericClientDstu2Test { assertEquals("DELETE", capt.getAllValues().get(idx).getMethod()); assertEquals("http://example.com/fhir/Patient/123", capt.getAllValues().get(idx).getURI().toString()); idx++; - + client.delete(Patient.class, "123"); assertEquals("DELETE", capt.getAllValues().get(idx).getMethod()); assertEquals("http://example.com/fhir/Patient/123", capt.getAllValues().get(idx).getURI().toString()); @@ -631,7 +595,8 @@ public class GenericClientDstu2Test { .since(new InstantDt("2001-01-02T11:22:33Z")) .execute(); //@formatter:on - assertThat(capt.getAllValues().get(idx).getURI().toString(), either(equalTo("http://example.com/fhir/Patient/123/_history?_since=2001-01-02T11:22:33Z&_count=123")).or(equalTo("http://example.com/fhir/Patient/123/_history?_count=123&_since=2001-01-02T11:22:33Z"))); + assertThat(capt.getAllValues().get(idx).getURI().toString(), either(equalTo("http://example.com/fhir/Patient/123/_history?_since=2001-01-02T11:22:33Z&_count=123")) + .or(equalTo("http://example.com/fhir/Patient/123/_history?_count=123&_since=2001-01-02T11:22:33Z"))); assertEquals(1, response.getEntry().size()); idx++; @@ -646,7 +611,7 @@ public class GenericClientDstu2Test { assertThat(capt.getAllValues().get(idx).getURI().toString(), containsString("_since=2001-01")); assertEquals(1, response.getEntry().size()); idx++; -} + } @Test public void testMetaAdd() throws Exception { @@ -684,12 +649,12 @@ public class GenericClientDstu2Test { assertEquals("http://example.com/fhir/Patient/123/$meta-add", capt.getAllValues().get(idx).getURI().toASCIIString()); assertEquals("urn:profile:out", resp.getProfile().get(0).getValue()); assertEquals("POST", capt.getAllValues().get(idx).getRequestLine().getMethod()); - assertEquals("", extractBody(capt, idx)); + assertEquals("", + extractBody(capt, idx)); idx++; } - @Test public void testMetaGet() throws Exception { IParser p = ourCtx.newXmlParser(); @@ -946,9 +911,8 @@ public class GenericClientDstu2Test { Parameters outParams = client.operation().onInstance(new IdDt("Patient", "18066")).named("$everything").withParameters(inParams).execute(); /* - * Note that the $everything operation returns a Bundle instead of a Parameters resource. The client operation - * methods return a Parameters instance however, so HAPI creates a Parameters object with a single parameter - * containing the value. + * Note that the $everything operation returns a Bundle instead of a Parameters resource. The client operation methods return a Parameters instance however, so HAPI creates a Parameters object + * with a single parameter containing the value. */ ca.uhn.fhir.model.dstu2.resource.Bundle responseBundle = (ca.uhn.fhir.model.dstu2.resource.Bundle) outParams.getParameter().get(0).getResource(); @@ -1001,6 +965,121 @@ public class GenericClientDstu2Test { idx++; } + @Test + public void testOperationWithInlineParams() throws Exception { + IParser p = ourCtx.newXmlParser(); + + Parameters outParams = new Parameters(); + outParams.addParameter().setValue(new StringDt("STRINGVALOUT1")); + outParams.addParameter().setValue(new StringDt("STRINGVALOUT2")); + final String respString = p.encodeResourceToString(outParams); + + 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.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(respString), Charset.forName("UTF-8")); + } + }); + + IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); + + int idx = 0; + + //@formatter:off + Parameters resp = client + .operation() + .onServer() + .named("$SOMEOPERATION") + .withParameter(Parameters.class, "name1", new StringDt("value1")) + .andParameter("name2", new StringDt("value1")) + .execute(); + //@formatter:on + assertEquals("http://example.com/fhir/$SOMEOPERATION", capt.getAllValues().get(idx).getURI().toASCIIString()); + assertEquals(respString, p.encodeResourceToString(resp)); + assertEquals(1, capt.getAllValues().get(idx).getHeaders(Constants.HEADER_CONTENT_TYPE).length); + assertEquals(EncodingEnum.XML.getResourceContentType() + Constants.HEADER_SUFFIX_CT_UTF_8, capt.getAllValues().get(idx).getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue()); + assertEquals("POST", capt.getAllValues().get(idx).getRequestLine().getMethod()); + assertEquals( + "", + (extractBody(capt, idx))); + idx++; + + /* + * Composite type + */ + + //@formatter:off + resp = client + .operation() + .onServer() + .named("$SOMEOPERATION") + .withParameter(Parameters.class, "name1", new IdentifierDt("system1", "value1")) + .andParameter("name2", new StringDt("value1")) + .execute(); + //@formatter:on + assertEquals("http://example.com/fhir/$SOMEOPERATION", capt.getAllValues().get(idx).getURI().toASCIIString()); + assertEquals(respString, p.encodeResourceToString(resp)); + assertEquals(1, capt.getAllValues().get(idx).getHeaders(Constants.HEADER_CONTENT_TYPE).length); + assertEquals(EncodingEnum.XML.getResourceContentType() + Constants.HEADER_SUFFIX_CT_UTF_8, capt.getAllValues().get(idx).getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue()); + assertEquals("POST", capt.getAllValues().get(idx).getRequestLine().getMethod()); + assertEquals( + "", + (extractBody(capt, idx))); + idx++; + + /* + * Resource + */ + + //@formatter:off + resp = client + .operation() + .onServer() + .named("$SOMEOPERATION") + .withParameter(Parameters.class, "name1", new IdentifierDt("system1", "value1")) + .andParameter("name2", new Patient().setActive(true)) + .execute(); + //@formatter:on + assertEquals("http://example.com/fhir/$SOMEOPERATION", capt.getAllValues().get(idx).getURI().toASCIIString()); + assertEquals(respString, p.encodeResourceToString(resp)); + assertEquals(1, capt.getAllValues().get(idx).getHeaders(Constants.HEADER_CONTENT_TYPE).length); + assertEquals(EncodingEnum.XML.getResourceContentType() + Constants.HEADER_SUFFIX_CT_UTF_8, capt.getAllValues().get(idx).getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue()); + assertEquals("POST", capt.getAllValues().get(idx).getRequestLine().getMethod()); + assertEquals( + "", + (extractBody(capt, idx))); + idx++; + + } + + @Test(expected = IllegalArgumentException.class) + public void testOperationWithInvalidParam() { + IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); + + // Who knows what the heck this is! + IBase weirdBase = new IBase() { + private static final long serialVersionUID = 1L; + + @Override + public boolean isEmpty() { + return false; + } + }; + + //@formatter:off + client + .operation() + .onServer() + .named("$SOMEOPERATION") + .withParameter(Parameters.class, "name1", weirdBase) + .execute(); + //@formatter:on + } + @Test public void testOperationWithListOfParameterResponse() throws Exception { IParser p = ourCtx.newXmlParser(); @@ -1315,11 +1394,7 @@ public class GenericClientDstu2Test { Patient response; int idx = 0; - response = (Patient)client - .read() - .resource(Patient.class) - .withUrl(new IdDt("http://domain2.example.com/base/Patient/123")) - .execute(); + response = (Patient) client.read().resource(Patient.class).withUrl(new IdDt("http://domain2.example.com/base/Patient/123")).execute(); assertEquals("http://domain2.example.com/base/Patient/123", capt.getAllValues().get(idx).getURI().toASCIIString()); assertEquals("FAM", response.getName().get(0).getFamily().get(0).getValue()); } @@ -1369,7 +1444,7 @@ public class GenericClientDstu2Test { assertEquals("2015-06-22T15:48:57.554-04:00", ResourceMetadataKeyEnum.UPDATED.get(response).getValueAsString()); } - + @Test public void testReadWithElementsParam() throws Exception { String msg = "{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}"; @@ -1390,7 +1465,8 @@ public class GenericClientDstu2Test { .execute(); //@formatter:on - assertThat(capt.getValue().getURI().toString(), either(equalTo("http://example.com/fhir/Patient/123?_elements=name%2Cidentifier")).or(equalTo("http://example.com/fhir/Patient/123?_elements=identifier%2Cname"))); + assertThat(capt.getValue().getURI().toString(), + either(equalTo("http://example.com/fhir/Patient/123?_elements=name%2Cidentifier")).or(equalTo("http://example.com/fhir/Patient/123?_elements=identifier%2Cname"))); assertEquals(Patient.class, response.getClass()); } @@ -1420,8 +1496,7 @@ public class GenericClientDstu2Test { } //@formatter:on } - - + @Test public void testReadWithSummaryParamHtml() throws Exception { String msg = "
HELP IM A DIV
"; @@ -1524,7 +1599,8 @@ public class GenericClientDstu2Test { .execute(); //@formatter:on - assertThat(capt.getValue().getURI().toString(), either(equalTo("http://example.com/fhir/Patient?name=james&_elements=name%2Cidentifier")).or(equalTo("http://example.com/fhir/Patient?name=james&_elements=identifier%2Cname"))); + assertThat(capt.getValue().getURI().toString(), + either(equalTo("http://example.com/fhir/Patient?name=james&_elements=name%2Cidentifier")).or(equalTo("http://example.com/fhir/Patient?name=james&_elements=identifier%2Cname"))); assertEquals(Patient.class, response.getEntries().get(0).getResource().getClass()); } @@ -1835,6 +1911,43 @@ public class GenericClientDstu2Test { } + @SuppressWarnings("deprecation") + @Test + public void testUpdateNonFluent() throws Exception { + ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); + when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse); + when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), Constants.STATUS_HTTP_204_NO_CONTENT, "")); + when(myHttpResponse.getEntity().getContent()).then(new Answer() { + @Override + public ReaderInputStream answer(InvocationOnMock theInvocation) throws Throwable { + return new ReaderInputStream(new StringReader(""), Charset.forName("UTF-8")); + } + }); + + IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); + + int idx = 0; + + Patient p = new Patient(); + p.addName().addFamily("FOOFAMILY"); + + client.update(new IdDt("Patient/123"), p); + assertEquals(1, capt.getAllValues().get(idx).getHeaders(Constants.HEADER_CONTENT_TYPE).length); + assertEquals(EncodingEnum.XML.getResourceContentType() + Constants.HEADER_SUFFIX_CT_UTF_8, capt.getAllValues().get(idx).getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue()); + assertThat(extractBody(capt, idx), containsString("")); + assertEquals("http://example.com/fhir/Patient/123", capt.getAllValues().get(idx).getURI().toString()); + assertEquals("PUT", capt.getAllValues().get(idx).getRequestLine().getMethod()); + idx++; + + client.update("123", p); + assertEquals(1, capt.getAllValues().get(idx).getHeaders(Constants.HEADER_CONTENT_TYPE).length); + assertEquals(EncodingEnum.XML.getResourceContentType() + Constants.HEADER_SUFFIX_CT_UTF_8, capt.getAllValues().get(idx).getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue()); + assertThat(extractBody(capt, idx), containsString("")); + assertEquals("http://example.com/fhir/Patient/123", capt.getAllValues().get(idx).getURI().toString()); + assertEquals("PUT", capt.getAllValues().get(idx).getRequestLine().getMethod()); + idx++; + } + @Test public void testUpdatePrefer() throws Exception { ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); @@ -1924,7 +2037,9 @@ public class GenericClientDstu2Test { response = client.validate().resource(p).execute(); assertEquals("http://example.com/fhir/Patient/$validate", capt.getAllValues().get(idx).getURI().toASCIIString()); assertEquals("POST", capt.getAllValues().get(idx).getRequestLine().getMethod()); - assertEquals("", extractBody(capt, idx)); + assertEquals( + "", + extractBody(capt, idx)); assertNotNull(response.getOperationOutcome()); assertEquals("FOOBAR", toOo(response.getOperationOutcome()).getIssueFirstRep().getDiagnosticsElement().getValue()); idx++; @@ -1932,7 +2047,9 @@ public class GenericClientDstu2Test { response = client.validate().resource(ourCtx.newXmlParser().encodeResourceToString(p)).execute(); assertEquals("http://example.com/fhir/Patient/$validate?_format=xml", capt.getAllValues().get(idx).getURI().toASCIIString()); assertEquals("POST", capt.getAllValues().get(idx).getRequestLine().getMethod()); - assertEquals("", extractBody(capt, idx)); + assertEquals( + "", + extractBody(capt, idx)); assertNotNull(response.getOperationOutcome()); assertEquals("FOOBAR", toOo(response.getOperationOutcome()).getIssueFirstRep().getDiagnosticsElement().getValue()); idx++; @@ -1986,7 +2103,9 @@ public class GenericClientDstu2Test { assertEquals("http://example.com/fhir/Patient/$validate", capt.getAllValues().get(idx).getURI().toASCIIString()); assertEquals("POST", capt.getAllValues().get(idx).getRequestLine().getMethod()); - assertEquals("", extractBody(capt, idx)); + assertEquals( + "", + extractBody(capt, idx)); assertNotNull(response.getOperationOutcome()); assertEquals("FOOBAR", toOo(response.getOperationOutcome()).getIssueFirstRep().getDiagnosticsElement().getValue()); idx++; diff --git a/hapi-fhir-structures-hl7org-dstu2/src/main/java/ca/uhn/fhir/validation/FhirInstanceValidator.java b/hapi-fhir-structures-hl7org-dstu2/src/main/java/ca/uhn/fhir/validation/FhirInstanceValidator.java index 736ed447b1d..5c6b9d13320 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/main/java/ca/uhn/fhir/validation/FhirInstanceValidator.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/main/java/ca/uhn/fhir/validation/FhirInstanceValidator.java @@ -14,22 +14,8 @@ import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.Validate; -import org.hl7.fhir.instance.formats.IParser; -import org.hl7.fhir.instance.formats.ParserType; -import org.hl7.fhir.instance.model.ConceptMap; import org.hl7.fhir.instance.model.OperationOutcome.IssueSeverity; -import org.hl7.fhir.instance.model.Resource; import org.hl7.fhir.instance.model.StructureDefinition; -import org.hl7.fhir.instance.model.ValueSet; -import org.hl7.fhir.instance.model.ValueSet.ConceptSetComponent; -import org.hl7.fhir.instance.model.ValueSet.ValueSetExpansionComponent; -import org.hl7.fhir.instance.terminologies.ValueSetExpander; -import org.hl7.fhir.instance.terminologies.ValueSetExpanderFactory; -import org.hl7.fhir.instance.terminologies.ValueSetExpanderSimple; -import org.hl7.fhir.instance.utils.EOperationOutcome; -import org.hl7.fhir.instance.utils.INarrativeGenerator; -import org.hl7.fhir.instance.utils.IWorkerContext; -import org.hl7.fhir.instance.validation.IResourceValidator; import org.hl7.fhir.instance.validation.IResourceValidator.BestPracticeWarningLevel; import org.hl7.fhir.instance.validation.ValidationMessage; import org.w3c.dom.Document; @@ -46,7 +32,6 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.rest.server.EncodingEnum; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; -import ca.uhn.fhir.validation.IValidationSupport.CodeValidationResult; public class FhirInstanceValidator extends BaseValidatorBridge implements IValidatorModule { @@ -151,7 +136,7 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid } protected List validate(final FhirContext theCtx, String theInput, EncodingEnum theEncoding) { - HapiWorkerContext workerContext = new HapiWorkerContext(theCtx); + HapiWorkerContext workerContext = new HapiWorkerContext(theCtx, myValidationSupport); org.hl7.fhir.instance.validation.InstanceValidator v; try { @@ -260,125 +245,4 @@ public class FhirInstanceValidator extends BaseValidatorBridge implements IValid return profile; } - private final class HapiWorkerContext implements IWorkerContext, ValueSetExpanderFactory, ValueSetExpander { - private final FhirContext myCtx; - - private HapiWorkerContext(FhirContext theCtx) { - myCtx = theCtx; - } - - @Override - public ValueSetExpansionComponent expandVS(ConceptSetComponent theInc) { - return myValidationSupport.expandValueSet(myCtx, theInc); - } - - @Override - public ValueSetExpansionOutcome expandVS(ValueSet theSource) { - throw new UnsupportedOperationException(); - } - - @Override - public ValueSet fetchCodeSystem(String theSystem) { - if (myValidationSupport == null) { - return null; - } else { - return myValidationSupport.fetchCodeSystem(myCtx, theSystem); - } - } - - @Override - public T fetchResource(Class theClass, String theUri) throws EOperationOutcome, Exception { - if (myValidationSupport == null) { - return null; - } else { - return myValidationSupport.fetchResource(myCtx, theClass, theUri); - } - } - - @Override - public INarrativeGenerator getNarrativeGenerator(String thePrefix, String theBasePath) { - throw new UnsupportedOperationException(); - } - - @Override - public IParser getParser(ParserType theType) { - throw new UnsupportedOperationException(); - } - - @Override - public IParser getParser(String theType) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean hasResource(Class theClass_, String theUri) { - throw new UnsupportedOperationException(); - } - - @Override - public IParser newJsonParser() { - throw new UnsupportedOperationException(); - } - - @Override - public IResourceValidator newValidator() throws Exception { - throw new UnsupportedOperationException(); - } - - @Override - public IParser newXmlParser() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean supportsSystem(String theSystem) { - if (myValidationSupport == null) { - return false; - } else { - return myValidationSupport.isCodeSystemSupported(myCtx, theSystem); - } - } - - @Override - public ValidationResult validateCode(String theSystem, String theCode, String theDisplay) { - CodeValidationResult result = myValidationSupport.validateCode(myCtx, theSystem, theCode, theDisplay); - if (result == null) { - return null; - } - return new ValidationResult(result.getSeverity(), result.getMessage(), result.asConceptDefinition()); - } - - @Override - public ValidationResult validateCode(String theSystem, String theCode, String theDisplay, - ConceptSetComponent theVsi) { - throw new UnsupportedOperationException(); - } - - @Override - public ValidationResult validateCode(String theSystem, String theCode, String theDisplay, ValueSet theVs) { - throw new UnsupportedOperationException(); - } - - @Override - public ValueSetExpander getExpander() { - return this; - } - - @Override - public ValueSetExpansionOutcome expand(ValueSet theSource) throws ETooCostly, Exception { - ValueSetExpander vse = new ValueSetExpanderSimple(this, this); - ValueSetExpansionOutcome vso = vse.expand(theSource); - if (vso.getError() != null) { - return null; - } else { - return vso; - } - } - - @Override - public List findMapsForSource(String theUrl) { - throw new UnsupportedOperationException(); - } - } - } diff --git a/hapi-fhir-structures-hl7org-dstu2/src/main/java/ca/uhn/fhir/validation/HapiWorkerContext.java b/hapi-fhir-structures-hl7org-dstu2/src/main/java/ca/uhn/fhir/validation/HapiWorkerContext.java new file mode 100644 index 00000000000..5cc3210da27 --- /dev/null +++ b/hapi-fhir-structures-hl7org-dstu2/src/main/java/ca/uhn/fhir/validation/HapiWorkerContext.java @@ -0,0 +1,144 @@ +package ca.uhn.fhir.validation; + +import java.util.List; + +import org.hl7.fhir.instance.formats.IParser; +import org.hl7.fhir.instance.formats.ParserType; +import org.hl7.fhir.instance.model.ConceptMap; +import org.hl7.fhir.instance.model.Resource; +import org.hl7.fhir.instance.model.ValueSet; +import org.hl7.fhir.instance.model.ValueSet.ConceptSetComponent; +import org.hl7.fhir.instance.model.ValueSet.ValueSetExpansionComponent; +import org.hl7.fhir.instance.terminologies.ValueSetExpander; +import org.hl7.fhir.instance.terminologies.ValueSetExpanderFactory; +import org.hl7.fhir.instance.terminologies.ValueSetExpanderSimple; +import org.hl7.fhir.instance.utils.EOperationOutcome; +import org.hl7.fhir.instance.utils.INarrativeGenerator; +import org.hl7.fhir.instance.utils.IWorkerContext; +import org.hl7.fhir.instance.validation.IResourceValidator; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.validation.IValidationSupport.CodeValidationResult; + +public final class HapiWorkerContext implements IWorkerContext, ValueSetExpanderFactory, ValueSetExpander { + private final FhirContext myCtx; + private IValidationSupport myValidationSupport; + + public HapiWorkerContext(FhirContext theCtx, IValidationSupport theValidationSupport) { + myCtx = theCtx; + myValidationSupport = theValidationSupport; + } + + @Override + public ValueSetExpansionComponent expandVS(ConceptSetComponent theInc) { + return myValidationSupport.expandValueSet(myCtx, theInc); + } + + @Override + public ValueSetExpansionOutcome expandVS(ValueSet theSource) { + throw new UnsupportedOperationException(); + } + + @Override + public ValueSet fetchCodeSystem(String theSystem) { + if (myValidationSupport == null) { + return null; + } else { + return myValidationSupport.fetchCodeSystem(myCtx, theSystem); + } + } + + @Override + public T fetchResource(Class theClass, String theUri) throws EOperationOutcome, Exception { + if (myValidationSupport == null) { + return null; + } else { + return myValidationSupport.fetchResource(myCtx, theClass, theUri); + } + } + + @Override + public INarrativeGenerator getNarrativeGenerator(String thePrefix, String theBasePath) { + throw new UnsupportedOperationException(); + } + + @Override + public IParser getParser(ParserType theType) { + throw new UnsupportedOperationException(); + } + + @Override + public IParser getParser(String theType) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean hasResource(Class theClass_, String theUri) { + throw new UnsupportedOperationException(); + } + + @Override + public IParser newJsonParser() { + throw new UnsupportedOperationException(); + } + + @Override + public IResourceValidator newValidator() throws Exception { + throw new UnsupportedOperationException(); + } + + @Override + public IParser newXmlParser() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean supportsSystem(String theSystem) { + if (myValidationSupport == null) { + return false; + } else { + return myValidationSupport.isCodeSystemSupported(myCtx, theSystem); + } + } + + @Override + public ValidationResult validateCode(String theSystem, String theCode, String theDisplay) { + CodeValidationResult result = myValidationSupport.validateCode(myCtx, theSystem, theCode, theDisplay); + if (result == null) { + return null; + } + return new ValidationResult(result.getSeverity(), result.getMessage(), result.asConceptDefinition()); + } + + @Override + public ValidationResult validateCode(String theSystem, String theCode, String theDisplay, + ConceptSetComponent theVsi) { + throw new UnsupportedOperationException(); + } + + @Override + public ValidationResult validateCode(String theSystem, String theCode, String theDisplay, ValueSet theVs) { + throw new UnsupportedOperationException(); + } + + @Override + public ValueSetExpander getExpander() { + return this; + } + + @Override + public ValueSetExpansionOutcome expand(ValueSet theSource) throws ETooCostly, Exception { + ValueSetExpander vse = new ValueSetExpanderSimple(this, this); + ValueSetExpansionOutcome vso = vse.expand(theSource); + if (vso.getError() != null) { + return null; + } else { + return vso; + } + } + + @Override + public List findMapsForSource(String theUrl) { + throw new UnsupportedOperationException(); + } +} \ No newline at end of file diff --git a/src/changes/changes.xml b/src/changes/changes.xml index da7b971e886..2d3870c30d9 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -127,6 +127,12 @@ JPA server now supports $everything on Patient and Encounter types (patient and encounter instance was already supported) + + Generic client operation invocations now + have an additional inline method for generating the input + Parameters using chained method calls instead + of by passing a Parameters resource in +