Merge pull request #2483 from hapifhir/2478_add_extension_mdm_matcher

2478 add extension mdm matcher
This commit is contained in:
longma1 2021-03-29 08:13:48 -06:00 committed by GitHub
commit 494c8e9593
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 589 additions and 53 deletions

View File

@ -28,6 +28,7 @@ import org.hl7.fhir.instance.model.api.IBaseExtension;
import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
@ -45,7 +46,7 @@ public class ExtensionUtil {
*/
public static IBaseExtension<?, ?> getOrCreateExtension(IBase theBase, String theUrl) {
IBaseHasExtensions baseHasExtensions = validateExtensionSupport(theBase);
IBaseExtension extension = getExtension(baseHasExtensions, theUrl);
IBaseExtension extension = getExtensionByUrl(baseHasExtensions, theUrl);
if (extension == null) {
extension = baseHasExtensions.addExtension();
extension.setUrl(theUrl);
@ -53,6 +54,34 @@ public class ExtensionUtil {
return extension;
}
/**
* Returns an new empty extension.
*
* @param theBase Base resource to add the extension to
* @return Returns a new extension
* @throws IllegalArgumentException IllegalArgumentException is thrown in case resource doesn't support extensions
*/
public static IBaseExtension<?, ?> addExtension(IBase theBase) {
return addExtension(theBase, null);
}
/**
* Returns an extension with the specified URL
*
* @param theBase Base resource to add the extension to
* @param theUrl URL for the extension
* @return Returns a new extension with the specified URL.
* @throws IllegalArgumentException IllegalArgumentException is thrown in case resource doesn't support extensions
*/
public static IBaseExtension<?, ?> addExtension(IBase theBase, String theUrl) {
IBaseHasExtensions baseHasExtensions = validateExtensionSupport(theBase);
IBaseExtension extension = baseHasExtensions.addExtension();
if (theUrl != null) {
extension.setUrl(theUrl);
}
return extension;
}
private static IBaseHasExtensions validateExtensionSupport(IBase theBase) {
if (!(theBase instanceof IBaseHasExtensions)) {
throw new IllegalArgumentException(String.format("Expected instance that supports extensions, but got %s", theBase));
@ -75,7 +104,7 @@ public class ExtensionUtil {
return false;
}
return getExtension(baseHasExtensions, theExtensionUrl) != null;
return getExtensionByUrl(baseHasExtensions, theExtensionUrl) != null;
}
/**
@ -89,7 +118,7 @@ public class ExtensionUtil {
if (!hasExtension(theBase, theExtensionUrl)) {
return false;
}
IBaseDatatype value = getExtension((IBaseHasExtensions) theBase, theExtensionUrl).getValue();
IBaseDatatype value = getExtensionByUrl((IBaseHasExtensions) theBase, theExtensionUrl).getValue();
if (value == null) {
return theExtensionValue == null;
}
@ -103,14 +132,71 @@ public class ExtensionUtil {
* @param theExtensionUrl URL of the extension to get. Must be non-null
* @return Returns the first available extension with the specified URL, or null if such extension doesn't exist
*/
public static IBaseExtension<?, ?> getExtension(IBaseHasExtensions theBase, String theExtensionUrl) {
return theBase.getExtension()
public static IBaseExtension<?, ?> getExtensionByUrl(IBase theBase, String theExtensionUrl) {
Predicate<IBaseExtension> filter;
if (theExtensionUrl == null) {
filter = (e -> true);
} else {
filter = (e -> theExtensionUrl.equals(e.getUrl()));
}
return getExtensionsMatchingPredicate(theBase, filter)
.stream()
.filter(e -> theExtensionUrl.equals(e.getUrl()))
.findFirst()
.orElse(null);
}
/**
* Gets all extensions that match the specified filter predicate
*
* @param theBase The resource to get the extension for
* @param theFilter Predicate to match the extension against
* @return Returns all extension with the specified URL, or an empty list if such extensions do not exist
*/
public static List<IBaseExtension<?, ?>> getExtensionsMatchingPredicate(IBase theBase, Predicate<? super IBaseExtension> theFilter) {
return validateExtensionSupport(theBase)
.getExtension()
.stream()
.filter(theFilter)
.collect(Collectors.toList());
}
/**
* Removes all extensions.
*
* @param theBase The resource to clear the extension for
* @return Returns all extension that were removed
*/
public static List<IBaseExtension<?, ?>> clearAllExtensions(IBase theBase) {
return clearExtensionsMatchingPredicate(theBase, (e -> true));
}
/**
* Removes all extensions by URL.
*
* @param theBase The resource to clear the extension for
* @param theUrl The url to clear extensions for
* @return Returns all extension that were removed
*/
public static List<IBaseExtension<?, ?>> clearExtensionsByUrl(IBase theBase, String theUrl) {
return clearExtensionsMatchingPredicate(theBase, (e -> theUrl.equals(e.getUrl())));
}
/**
* Removes all extensions that match the specified predicate
*
* @param theBase The base object to clear the extension for
* @param theFilter Defines which extensions should be cleared
* @return Returns all extension that were removed
*/
private static List<IBaseExtension<?, ?>> clearExtensionsMatchingPredicate(IBase theBase, Predicate<? super IBaseExtension> theFilter) {
List<IBaseExtension<?, ?>> retVal = getExtensionsMatchingPredicate(theBase, theFilter);
validateExtensionSupport(theBase)
.getExtension()
.removeIf(theFilter);
return retVal;
}
/**
* Gets all extensions with the specified URL
*
@ -118,11 +204,9 @@ public class ExtensionUtil {
* @param theExtensionUrl URL of the extension to get. Must be non-null
* @return Returns all extension with the specified URL, or an empty list if such extensions do not exist
*/
public static List<IBaseExtension<?, ?>> getExtensions(IBaseHasExtensions theBase, String theExtensionUrl) {
return theBase.getExtension()
.stream()
.filter(e -> theExtensionUrl.equals(e.getUrl()))
.collect(Collectors.toList());
public static List<IBaseExtension<?, ?>> getExtensionsByUrl(IBaseHasExtensions theBase, String theExtensionUrl) {
Predicate<IBaseExtension> urlEqualityPredicate = e -> theExtensionUrl.equals(e.getUrl());
return getExtensionsMatchingPredicate(theBase, urlEqualityPredicate);
}
/**
@ -133,7 +217,7 @@ public class ExtensionUtil {
* @param theFhirContext The context containing FHIR resource definitions
*/
public static void setExtension(FhirContext theFhirContext, IBaseExtension theExtension, String theValue) {
setExtension(theFhirContext, theExtension, "string", theValue);
setExtension(theFhirContext, theExtension, "string", (Object) theValue);
}
/**
@ -144,7 +228,7 @@ public class ExtensionUtil {
* @param theValue The value to set
* @param theFhirContext The context containing FHIR resource definitions
*/
public static void setExtension(FhirContext theFhirContext, IBaseExtension theExtension, String theExtensionType, String theValue) {
public static void setExtension(FhirContext theFhirContext, IBaseExtension theExtension, String theExtensionType, Object theValue) {
theExtension.setValue(TerserUtil.newElement(theFhirContext, theExtensionType, theValue));
}
@ -156,7 +240,7 @@ public class ExtensionUtil {
* @param theValue Extension value
* @param theFhirContext The context containing FHIR resource definitions
*/
public static void setExtension(FhirContext theFhirContext, IBase theBase, String theUrl, String theValue) {
public static void setExtensionAsString(FhirContext theFhirContext, IBase theBase, String theUrl, String theValue) {
IBaseExtension ext = getOrCreateExtension(theBase, theUrl);
setExtension(theFhirContext, ext, theValue);
}
@ -170,9 +254,19 @@ public class ExtensionUtil {
* @param theValue Extension value
* @param theFhirContext The context containing FHIR resource definitions
*/
public static void setExtension(FhirContext theFhirContext, IBase theBase, String theUrl, String theValueType, String theValue) {
public static void setExtension(FhirContext theFhirContext, IBase theBase, String theUrl, String theValueType, Object theValue) {
IBaseExtension ext = getOrCreateExtension(theBase, theUrl);
setExtension(theFhirContext, ext, theValue);
setExtension(theFhirContext, ext, theValueType, theValue);
}
/**
* Compares two extensions, returns true if they have the same value and url
*
* @param theLeftExtension : Extension to be evaluated #1
* @param theRightExtension : Extension to be evaluated #2
* @return Result of the comparison
*/
public static boolean equals(IBaseExtension theLeftExtension, IBaseExtension theRightExtension) {
return TerserUtil.equals(theLeftExtension, theRightExtension);
}
}

View File

@ -46,6 +46,8 @@ public final class TerserUtil {
public static final String FIELD_NAME_IDENTIFIER = "identifier";
private static final String EQUALS_DEEP = "equalsDeep";
public static final Collection<String> IDS_AND_META_EXCLUDES =
Collections.unmodifiableSet(Stream.of("id", "identifier", "meta").collect(Collectors.toSet()));
@ -97,12 +99,12 @@ public final class TerserUtil {
}
/**
* get the Values of a specified field.
* Gets all values of the specified field.
*
* @param theFhirContext Context holding resource definition
* @param theResource Resource to check if the specified field is set
* @param theFieldName name of the field to check
* @return Returns true if field exists and has any values set, and false otherwise
* @return Returns all values for the specified field or null if field with the provided name doesn't exist
*/
public static List<IBase> getValues(FhirContext theFhirContext, IBaseResource theResource, String theFieldName) {
RuntimeResourceDefinition resourceDefinition = theFhirContext.getResourceDefinition(theResource);
@ -114,6 +116,23 @@ public final class TerserUtil {
return resourceIdentifier.getAccessor().getValues(theResource);
}
/**
* Gets the first available value for the specified field.
*
* @param theFhirContext Context holding resource definition
* @param theResource Resource to check if the specified field is set
* @param theFieldName name of the field to check
* @return Returns the first value for the specified field or null if field with the provided name doesn't exist or
* has no values
*/
public static IBase getValueFirstRep(FhirContext theFhirContext, IBaseResource theResource, String theFieldName) {
List<IBase> values = getValues(theFhirContext, theResource, theFieldName);
if (values == null || values.isEmpty()) {
return null;
}
return values.get(0);
}
/**
* Clones specified composite field (collection). Composite field values must conform to the collections
* contract.
@ -157,26 +176,50 @@ public final class TerserUtil {
});
}
private static boolean contains(IBase theItem, List<IBase> theItems) {
private static Method getMethod(IBase theBase, String theMethodName) {
Method method = null;
for (Method m : theItem.getClass().getDeclaredMethods()) {
if (m.getName().equals("equalsDeep")) {
for (Method m : theBase.getClass().getDeclaredMethods()) {
if (m.getName().equals(theMethodName)) {
method = m;
break;
}
}
return method;
}
final Method m = method;
return theItems.stream().anyMatch(i -> {
if (m != null) {
try {
return (Boolean) m.invoke(theItem, i);
} catch (Exception e) {
throw new RuntimeException("Unable to compare equality via equalsDeep", e);
}
/**
* Checks if two items are equal via {@link #EQUALS_DEEP} method
*
* @param theItem1 First item to compare
* @param theItem2 Second item to compare
* @return Returns true if they are equal and false otherwise
*/
public static boolean equals(IBase theItem1, IBase theItem2) {
if (theItem1 == null) {
return theItem2 == null;
}
final Method method = getMethod(theItem1, EQUALS_DEEP);
if (method == null) {
throw new IllegalArgumentException(String.format("Instance %s do not provide %s method", theItem1, EQUALS_DEEP));
}
return equals(theItem1, theItem2, method);
}
private static boolean equals(IBase theItem1, IBase theItem2, Method theMethod) {
if (theMethod != null) {
try {
return (Boolean) theMethod.invoke(theItem1, theItem2);
} catch (Exception e) {
throw new RuntimeException(String.format("Unable to compare equality via %s", EQUALS_DEEP), e);
}
return theItem.equals(i);
});
}
return theItem1.equals(theItem2);
}
private static boolean contains(IBase theItem, List<IBase> theItems) {
final Method method = getMethod(theItem, EQUALS_DEEP);
return theItems.stream().anyMatch(i -> equals(i, theItem, method));
}
/**
@ -256,6 +299,20 @@ public final class TerserUtil {
childDefinition.getAccessor().getValues(theResource).clear();
}
/**
* Sets the provided field with the given values. This method will add to the collection of existing field values
* in case of multiple cardinality. Use {@link #clearField(FhirContext, FhirTerser, String, IBaseResource, IBase...)}
* to remove values before setting
*
* @param theFhirContext Context holding resource definition
* @param theFieldName Child field name of the resource to set
* @param theResource The resource to set the values on
* @param theValues The values to set on the resource child field name
*/
public static void setField(FhirContext theFhirContext, String theFieldName, IBaseResource theResource, IBase... theValues) {
setField(theFhirContext, theFhirContext.newTerser(), theFieldName, theResource, theValues);
}
/**
* Sets the provided field with the given values. This method will add to the collection of existing field values
* in case of multiple cardinality. Use {@link #clearField(FhirContext, FhirTerser, String, IBaseResource, IBase...)}
@ -269,10 +326,20 @@ public final class TerserUtil {
*/
public static void setField(FhirContext theFhirContext, FhirTerser theTerser, String theFieldName, IBaseResource theResource, IBase... theValues) {
BaseRuntimeChildDefinition childDefinition = getBaseRuntimeChildDefinition(theFhirContext, theFieldName, theResource);
List<IBase> theFromFieldValues = childDefinition.getAccessor().getValues(theResource);
if (theFromFieldValues.isEmpty()) {
for (IBase value : theValues) {
try {
childDefinition.getMutator().addValue(theResource, value);
} catch (UnsupportedOperationException e) {
ourLog.warn("Resource {} does not support multiple values, but an attempt to set {} was made. Setting the first item only", theResource, theValues);
childDefinition.getMutator().setValue(theResource, value);
break;
}
}
return;
}
List<IBase> theToFieldValues = Arrays.asList(theValues);
mergeFields(theTerser, theResource, childDefinition, theFromFieldValues, theToFieldValues);
}
@ -303,6 +370,18 @@ public final class TerserUtil {
setFieldByFhirPath(theFhirContext.newTerser(), theFhirPath, theResource, theValue);
}
public static List<IBase> getFieldByFhirPath(FhirContext theFhirContext, String theFhirPath, IBase theResource) {
return theFhirContext.newTerser().getValues(theResource, theFhirPath, false, false);
}
public static IBase getFirstFieldByFhirPath(FhirContext theFhirContext, String theFhirPath, IBase theResource) {
List<IBase> values = getFieldByFhirPath(theFhirContext, theFhirPath, theResource);
if (values == null || values.isEmpty()) {
return null;
}
return values.get(0);
}
private static void replaceField(IBaseResource theFrom, IBaseResource theTo, BaseRuntimeChildDefinition childDefinition) {
childDefinition.getAccessor().getFirstValueOrNull(theFrom).ifPresent(v -> {
childDefinition.getMutator().setValue(theTo, v);
@ -448,6 +527,9 @@ public final class TerserUtil {
*/
public static <T extends IBase> T newElement(FhirContext theFhirContext, String theElementType, Object theConstructorParam) {
BaseRuntimeElementDefinition def = theFhirContext.getElementDefinition(theElementType);
if (def == null) {
throw new IllegalArgumentException(String.format("Unable to find element type definition for %s", theElementType));
}
return (T) def.newInstance(theConstructorParam);
}

View File

@ -108,15 +108,50 @@ public class TerserUtilHelper {
}
/**
* Gets values of the specified field.
* Gets values for the specified child field.
*
* @param theField The field to get values from
* @return Returns a collection of values containing values or null if the spefied field doesn't exist
* @return Returns a list of retrieved values or null if the specified field doesn't exist
*/
public List<IBase> getFieldValues(String theField) {
return TerserUtil.getValues(myContext, myResource, theField);
}
/**
* Gets values for the specified field values by FHIRPath.
*
* @param theFhirPath The FHIR path expression to get the values from
* @return Returns a collection of values or null if the specified field doesn't exist
*/
public List<IBase> getFieldValuesByFhirPath(String theFhirPath) {
return TerserUtil.getFieldByFhirPath(myContext, theFhirPath, myResource);
}
/**
* Gets first available value for the specified field values by FHIRPath.
*
* @param theFhirPath The FHIR path expression to get the values from
* @return Returns the value or null if the specified field doesn't exist or is empty
*/
public IBase getFieldValueByFhirPath(String theFhirPath) {
return TerserUtil.getFirstFieldByFhirPath(myContext, theFhirPath, myResource);
}
/**
* Gets first available values of the specified field.
*
* @param theField The field to get values from
* @return Returns the first available value for the field name or null if the
* specified field doesn't exist or has no values
*/
public IBase getFieldValue(String theField) {
List<IBase> values = getFieldValues(theField);
if (values == null || values.isEmpty()) {
return null;
}
return values.get(0);
}
/**
* Gets the terser instance, creating one if necessary.
*

View File

@ -0,0 +1,4 @@
---
type: add
issue: 2478
title: "Added matching based on extension, when given the path to a fhir resource the matcher will take the extensions and match if the url and string value are the same"

View File

@ -403,6 +403,14 @@ The following algorithms are currently supported:
</td>
<td>If an optional "identifierSystem" is provided, then the identifiers only match when they belong to that system</td>
</tr>
<tr>
<td>EXTENSION_ANY_ORDER</td>
<td>matcher</td>
<td>
Matches extensions of resources in any order. Matches are made if both resources share at least one extensions that have the same URL and value.
</td>
<td></td>
</tr>
<tr>
<td>EMPTY_FIELD</td>
<td>matcher</td>

View File

@ -0,0 +1,29 @@
package ca.uhn.fhir.mdm.rules.matcher;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.util.ExtensionUtil;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseExtension;
import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
import java.util.List;
public class ExtensionMatcher implements IMdmFieldMatcher {
@Override
public boolean matches(FhirContext theFhirContext, IBase theLeftBase, IBase theRightBase, boolean theExact, String theIdentifierSystem) {
if (!(theLeftBase instanceof IBaseHasExtensions && theRightBase instanceof IBaseHasExtensions)) {
return false;
}
List<? extends IBaseExtension<?, ?>> leftExtension = ((IBaseHasExtensions) theLeftBase).getExtension();
List<? extends IBaseExtension<?, ?>> rightExtension = ((IBaseHasExtensions) theRightBase).getExtension();
boolean match = false;
for (IBaseExtension leftExtensionValue : leftExtension) {
for (IBaseExtension rightExtensionValue : rightExtension) {
match |= ExtensionUtil.equals(leftExtensionValue, rightExtensionValue);
}
}
return match;
}
}

View File

@ -50,7 +50,8 @@ public enum MdmMatcherEnum {
IDENTIFIER(new IdentifierMatcher()),
EMPTY_FIELD(new EmptyFieldMatcher());
EMPTY_FIELD(new EmptyFieldMatcher()),
EXTENSION_ANY_ORDER(new ExtensionMatcher());
private final IMdmFieldMatcher myMdmFieldMatcher;

View File

@ -151,6 +151,16 @@ public class MdmRuleValidatorTest extends BaseR4Test {
}
}
@Test
public void testMatcherExtensionJson() throws IOException {
try {
setMdmRuleJson("rules-extension-search.json");
}
catch (ConfigurationException e){
fail("Unable to validate extension matcher");
}
}
private void setMdmRuleJson(String theTheS) throws IOException {
MdmRuleValidator mdmRuleValidator = new MdmRuleValidator(ourFhirContext, mySearchParamRetriever);
MdmSettings mdmSettings = new MdmSettings(mdmRuleValidator);

View File

@ -0,0 +1,79 @@
package ca.uhn.fhir.mdm.rules.matcher;
import org.hl7.fhir.r4.model.IntegerType;
import org.hl7.fhir.r4.model.Organization;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.StringType;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ExtensionMatcherR4Test extends BaseMatcherR4Test {
@Test
public void testPatientWithMatchingExtension(){
Patient patient1 = new Patient();
Patient patient2 = new Patient();
patient1.addExtension("asd",new StringType("Patient1"));
patient2.addExtension("asd",new StringType("Patient1"));
assertTrue(MdmMatcherEnum.EXTENSION_ANY_ORDER.match(ourFhirContext, patient1, patient2, false, null));
}
@Test
public void testPatientWithoutMatchingExtension(){
Patient patient1 = new Patient();
Patient patient2 = new Patient();
patient1.addExtension("asd",new StringType("Patient1"));
patient2.addExtension("asd",new StringType("Patient2"));
assertFalse(MdmMatcherEnum.EXTENSION_ANY_ORDER.match(ourFhirContext, patient1, patient2, false, null));
}
@Test
public void testPatientSameValueDifferentUrl(){
Patient patient1 = new Patient();
Patient patient2 = new Patient();
patient1.addExtension("asd",new StringType("Patient1"));
patient2.addExtension("asd1",new StringType("Patient1"));
assertFalse(MdmMatcherEnum.EXTENSION_ANY_ORDER.match(ourFhirContext, patient1, patient2, false, null));
}
@Test
public void testPatientWithMultipleExtensionOneMatching(){
Patient patient1 = new Patient();
Patient patient2 = new Patient();
patient1.addExtension("asd",new StringType("Patient1"));
patient1.addExtension("url1", new StringType("asd"));
patient2.addExtension("asd",new StringType("Patient1"));
patient2.addExtension("asdasd", new StringType("some value"));
assertTrue(MdmMatcherEnum.EXTENSION_ANY_ORDER.match(ourFhirContext, patient1, patient2, false, null));
}
@Test
public void testPatientWithoutIntExtension(){
Patient patient1 = new Patient();
Patient patient2 = new Patient();
patient1.addExtension("asd", new IntegerType(123));
patient2.addExtension("asd", new IntegerType(123));
assertTrue(MdmMatcherEnum.EXTENSION_ANY_ORDER.match(ourFhirContext, patient1, patient2, false, null));
}
@Test
public void testPatientWithNoExtension(){
Patient patient1 = new Patient();
Patient patient2 = new Patient();
assertFalse(MdmMatcherEnum.EXTENSION_ANY_ORDER.match(ourFhirContext, patient1, patient2, false, null));
}
}

View File

@ -0,0 +1,17 @@
{
"version": "1",
"mdmTypes": ["Organization"],
"candidateSearchParams" : [],
"candidateFilterSearchParams" : [],
"matchFields" : [ {
"name" : "Organization-extension",
"resourceType" : "Organization",
"fhirPath" : "identifier[1]",
"matcher" : {
"algorithm": "EXTENSION_ANY_ORDER"
}
}],
"matchResultMap" : {
"Organization-extension" : "MATCH"
}
}

View File

@ -101,6 +101,11 @@ public class StandardizingInterceptor {
}
private void standardize(RequestDetails theRequest, IBaseResource theResource) {
if (theRequest == null) {
ourLog.debug("RequestDetails is null - unable to standardize {}", theResource);
return;
}
if (!theRequest.getHeaders(STANDARDIZATION_DISABLED_HEADER).isEmpty()) {
ourLog.debug("Standardization for {} is disabled via header {}", theResource, STANDARDIZATION_DISABLED_HEADER);
return;

View File

@ -107,6 +107,12 @@ public class AddressValidatingInterceptor {
protected void handleRequest(RequestDetails theRequest, IBaseResource theResource) {
if (getAddressValidator() == null) {
ourLog.debug("Address validator is not provided - validation disabled");
return;
}
if (theRequest == null) {
ourLog.debug("RequestDetails is null - unable to validate address for {}", theResource);
return;
}
@ -129,11 +135,11 @@ public class AddressValidatingInterceptor {
AddressValidationResult validationResult = getAddressValidator().isValid(theAddress, theFhirContext);
ourLog.debug("Validated address {}", validationResult);
ExtensionUtil.setExtension(theFhirContext, theAddress, IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL,
ExtensionUtil.setExtensionAsString(theFhirContext, theAddress, IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL,
validationResult.isValid() ? IAddressValidator.EXT_VALUE_VALID : IAddressValidator.EXT_VALUE_INVALID);
} catch (Exception ex) {
ourLog.warn("Unable to validate address", ex);
ExtensionUtil.setExtension(theFhirContext, theAddress, IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, IAddressValidator.EXT_UNABLE_TO_VALIDATE);
ExtensionUtil.setExtensionAsString(theFhirContext, theAddress, IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, IAddressValidator.EXT_UNABLE_TO_VALIDATE);
}
}

View File

@ -27,7 +27,6 @@ import ca.uhn.fhir.interceptor.api.Interceptor;
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.rest.api.server.RequestDetails;
import ca.uhn.fhir.rest.server.interceptor.ConfigLoader;
import ca.uhn.fhir.rest.server.interceptor.ServerOperationInterceptorAdapter;
import ca.uhn.fhir.rest.server.interceptor.validation.address.IAddressValidator;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
@ -73,6 +72,11 @@ public class FieldValidatingInterceptor {
}
protected void handleRequest(RequestDetails theRequest, IBaseResource theResource) {
if (theRequest == null) {
ourLog.debug("RequestDetails is null - unable to validate {}", theResource);
return;
}
if (!theRequest.getHeaders(VALIDATION_DISABLED_HEADER).isEmpty()) {
ourLog.debug("Address validation is disabled for this request via header");
return;

View File

@ -3,11 +3,14 @@ package ca.uhn.fhir.util;
import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.instance.model.api.IBaseDatatype;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.PrimitiveType;
import org.hl7.fhir.r4.model.StringType;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ExtensionUtilTest {
@ -19,7 +22,7 @@ class ExtensionUtilTest {
void testExtensionsWork() {
Patient p1 = new Patient();
assertFalse(ExtensionUtil.hasExtension(p1, EXT_URL));
ExtensionUtil.setExtension(ourFhirContext, p1, EXT_URL, "value");
ExtensionUtil.setExtensionAsString(ourFhirContext, p1, EXT_URL, "value");
assertTrue(ExtensionUtil.hasExtension(p1, EXT_URL));
}
@ -30,9 +33,46 @@ class ExtensionUtilTest {
ExtensionUtil.setExtension(ourFhirContext, p1, EXT_URL, "integer", "1");
assertTrue(ExtensionUtil.hasExtension(p1, EXT_URL));
assertEquals(1, ExtensionUtil.getExtensions(p1, EXT_URL).size());
assertEquals(1, ExtensionUtil.getExtensionsByUrl(p1, EXT_URL).size());
IBaseDatatype ext = ExtensionUtil.getExtension(p1, EXT_URL).getValue();
assertEquals("1", ext.toString());
IBaseDatatype ext = ExtensionUtil.getExtensionByUrl(p1, EXT_URL).getValue();
assertEquals("integer", ext.fhirType());
assertEquals("1", ((PrimitiveType) ext).asStringValue());
}
@Test
void testAddExtension() {
Patient p = new Patient();
assertNotNull(ExtensionUtil.addExtension(p));
assertNotNull(ExtensionUtil.addExtension(p, "myUrl"));
assertEquals(2, p.getExtension().size());
assertEquals("myUrl", p.getExtension().get(1).getUrl());
}
@Test
void testHasExtension() {
Patient p = new Patient();
p.addExtension("URL", new StringType("VALUE"));
assertTrue(ExtensionUtil.hasExtension(p, "URL"));
assertTrue(ExtensionUtil.hasExtension(p, "URL", "VALUE"));
}
@Test
void testClearExtension() {
Patient p = new Patient();
p.addExtension("URL", new StringType("VALUE"));
p.addExtension("URL2", new StringType("VALUE2"));
ExtensionUtil.clearExtensionsByUrl(p, "URL");
assertEquals(1, p.getExtension().size());
assertFalse(ExtensionUtil.hasExtension(p, "URL"));
assertTrue(ExtensionUtil.hasExtension(p, "URL2"));
ExtensionUtil.clearAllExtensions(p);
assertEquals(0, p.getExtension().size());
}
}

View File

@ -2,22 +2,20 @@ package ca.uhn.fhir.util;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeResourceDefinition;
import org.hl7.fhir.r4.model.Address;
import org.hl7.fhir.r4.model.DateTimeType;
import org.hl7.fhir.r4.model.DateType;
import org.hl7.fhir.r4.model.Enumerations;
import org.hl7.fhir.r4.model.Extension;
import org.hl7.fhir.r4.model.HumanName;
import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.PrimitiveType;
import org.junit.jupiter.api.Test;
import java.util.GregorianCalendar;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
class TerserUtilTest {
@ -57,9 +55,11 @@ class TerserUtilTest {
assertEquals(1, p2Helper.getFieldValues("identifier").size());
Identifier id1 = (Identifier) p1Helper.getFieldValues("identifier").get(0);
Identifier id2 = (Identifier) p2Helper.getFieldValues("identifier").get(0);
Identifier id2 = (Identifier) p2Helper.getFieldValue("identifier");
assertTrue(id1.equalsDeep(id2));
assertFalse(id1.equals(id2));
assertNull(p2Helper.getFieldValue("address"));
}
@Test
@ -237,4 +237,126 @@ class TerserUtilTest {
assertThat(p2.getName(), hasSize(1));
assertThat(p2.getName().get(0).getGiven(), hasSize(2));
}
@Test
void testEqualsFunction() {
Patient p1 = new Patient();
Patient p2 = new Patient();
p1.addName(new HumanName().setFamily("family").addGiven("asd"));
p2.addName(new HumanName().setFamily("family").addGiven("asd"));
assertTrue(TerserUtil.equals(p1, p2));
}
@Test
void testEqualsFunctionNotEqual() {
Patient p1 = new Patient();
Patient p2 = new Patient();
p1.addName(new HumanName().setFamily("family").addGiven("asd"));
p2.addName(new HumanName().setFamily("family").addGiven("asd1"));
assertFalse(TerserUtil.equals(p1, p2));
}
@Test
void testHasValues() {
Patient p1 = new Patient();
p1.addName().setFamily("Doe");
assertTrue(TerserUtil.hasValues(ourFhirContext, p1, "name"));
assertFalse(TerserUtil.hasValues(ourFhirContext, p1, "address"));
}
@Test
void testGetValues() {
Patient p1 = new Patient();
p1.addName().setFamily("Doe");
assertEquals("Doe", ((HumanName) TerserUtil.getValueFirstRep(ourFhirContext, p1, "name")).getFamily());
assertFalse(TerserUtil.getValues(ourFhirContext, p1, "name").isEmpty());
assertNull(TerserUtil.getValues(ourFhirContext, p1, "whoaIsThatReal"));
assertNull(TerserUtil.getValueFirstRep(ourFhirContext, p1, "whoaIsThatReal"));
}
@Test
public void testReplaceFields() {
Patient p1 = new Patient();
p1.addName().setFamily("Doe");
Patient p2 = new Patient();
p2.addName().setFamily("Smith");
TerserUtil.replaceField(ourFhirContext, "name", p1, p2);
assertEquals(1, p2.getName().size());
assertEquals("Doe", p2.getName().get(0).getFamily());
}
@Test
public void testClearFields() {
Patient p1 = new Patient();
p1.addName().setFamily("Doe");
TerserUtil.clearField(ourFhirContext, "name", p1);
assertEquals(0, p1.getName().size());
}
@Test
public void testSetField() {
Patient p1 = new Patient();
Address address = new Address();
address.setCity("CITY");
TerserUtil.setField(ourFhirContext, "address", p1, address);
assertEquals(1, p1.getAddress().size());
assertEquals("CITY", p1.getAddress().get(0).getCity());
}
@Test
public void testSetFieldByFhirPath() {
Patient p1 = new Patient();
Address address = new Address();
address.setCity("CITY");
TerserUtil.setFieldByFhirPath(ourFhirContext, "address", p1, address);
assertEquals(1, p1.getAddress().size());
assertEquals("CITY", p1.getAddress().get(0).getCity());
}
@Test
public void testClone() {
Patient p1 = new Patient();
p1.addName().setFamily("Doe").addGiven("Joe");
Patient p2 = TerserUtil.clone(ourFhirContext, p1);
assertEquals(p1.getName().get(0).getNameAsSingleString(), p2.getName().get(0).getNameAsSingleString());
assertTrue(p1.equalsDeep(p2));
}
@Test
public void testNewElement() {
assertNotNull(TerserUtil.newElement(ourFhirContext, "string"));
assertEquals(1, ((PrimitiveType) TerserUtil.newElement(ourFhirContext, "integer", "1")).getValue());
assertNotNull(TerserUtil.newElement(ourFhirContext, "string"));
assertNull(((PrimitiveType) TerserUtil.newElement(ourFhirContext, "integer")).getValue());
assertNotNull(TerserUtil.newElement(ourFhirContext, "string", null));
assertNull(((PrimitiveType) TerserUtil.newElement(ourFhirContext, "integer", null)).getValue());
}
@Test
public void testNewResource() {
assertNotNull(TerserUtil.newResource(ourFhirContext, "Patient"));
assertNotNull(TerserUtil.newResource(ourFhirContext, "Patient", null));
}
}