Removed obsolete test and added more control to replace fields in TU

This commit is contained in:
Nick Goupinets 2021-04-01 15:52:52 -04:00
parent 0cd14b971a
commit 9629aceff0
3 changed files with 51 additions and 29 deletions

View File

@ -25,6 +25,7 @@ import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition; import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.context.RuntimeResourceDefinition;
import org.apache.commons.lang3.tuple.Triple;
import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -58,6 +59,17 @@ public final class TerserUtil {
} }
}; };
public static final Predicate<Triple<BaseRuntimeChildDefinition, IBase, IBase>> EXCLUDE_IDS_META_AND_EMPTY = new Predicate<Triple<BaseRuntimeChildDefinition, IBase, IBase>>() {
@Override
public boolean test(Triple<BaseRuntimeChildDefinition, IBase, IBase> theTriple) {
if (!EXCLUDE_IDS_AND_META.test(theTriple.getLeft().getElementName())) {
return false;
}
return theTriple.getLeft().getAccessor().getValues(theTriple.getRight()).isEmpty();
}
};
public static final Predicate<String> INCLUDE_ALL = new Predicate<String>() { public static final Predicate<String> INCLUDE_ALL = new Predicate<String>() {
@Override @Override
public boolean test(String s) { public boolean test(String s) {
@ -235,20 +247,35 @@ public final class TerserUtil {
} }
/** /**
* Replaces all fields that test positive by the given inclusion strategy. <code>theTo</code> will contain a copy of the * Replaces all fields that have matching field names by the given inclusion strategy. <code>theTo</code> will contain a copy of the
* values from <code>theFrom</code> instance. * values from <code>theFrom</code> instance.
* *
* @param theFhirContext Context holding resource definition * @param theFhirContext Context holding resource definition
* @param theFrom The resource to merge the fields from * @param theFrom The resource to merge the fields from
* @param theTo The resource to merge the fields into * @param theTo The resource to merge the fields into
* @param inclusionStrategy Inclusion strategy that checks if a given field should be replaced by checking {@link Predicate#test(Object)} * @param theFieldNameInclusion Inclusion strategy that checks if a given field should be replaced
*/ */
public static void replaceFields(FhirContext theFhirContext, IBaseResource theFrom, IBaseResource theTo, Predicate<String> inclusionStrategy) { public static void replaceFields(FhirContext theFhirContext, IBaseResource theFrom, IBaseResource theTo, Predicate<String> theFieldNameInclusion) {
Predicate<Triple<BaseRuntimeChildDefinition, IBase, IBase>> predicate
= (t) -> theFieldNameInclusion.test(t.getLeft().getElementName());
replaceFieldsByPredicate(theFhirContext, theFrom, theTo, predicate);
}
/**
* Replaces empty fields on theTo resource that test positive by the given predicate. <code>theTo</code> will contain a copy of the
* values from <code>theFrom</code> for which predicate tests positive.
*
* @param theFhirContext Context holding resource definition
* @param theFrom The resource to merge the fields from
* @param theTo The resource to merge the fields into
* @param thePredicate Predicate that checks if a given field should be replaced
*/
public static void replaceFieldsByPredicate(FhirContext theFhirContext, IBaseResource theFrom, IBaseResource theTo, Predicate<Triple<BaseRuntimeChildDefinition, IBase, IBase>> thePredicate) {
FhirTerser terser = theFhirContext.newTerser(); FhirTerser terser = theFhirContext.newTerser();
RuntimeResourceDefinition definition = theFhirContext.getResourceDefinition(theFrom); RuntimeResourceDefinition definition = theFhirContext.getResourceDefinition(theFrom);
for (BaseRuntimeChildDefinition childDefinition : definition.getChildrenAndExtension()) { for (BaseRuntimeChildDefinition childDefinition : definition.getChildrenAndExtension()) {
if (!inclusionStrategy.test(childDefinition.getElementName())) { if (!thePredicate.test(Triple.of(childDefinition, theFrom, theTo))) {
continue; continue;
} }

View File

@ -476,28 +476,6 @@ public class MdmMatchLinkSvcTest extends BaseMdmR4Test {
assertThat(nameFirstRep.getGivenAsSingleString(), is(equalToIgnoringCase("paul"))); assertThat(nameFirstRep.getGivenAsSingleString(), is(equalToIgnoringCase("paul")));
} }
@Test
public void testPatientCreateDoesNotOverwriteGoldenResourceAttributesThatAreInvolvedInLinking() {
Patient paul = buildPaulPatient();
paul.setGender(Enumerations.AdministrativeGender.MALE);
paul = createPatientAndUpdateLinks(paul);
Patient sourcePatientFromTarget = (Patient) getGoldenResourceFromTargetResource(paul);
assertThat(sourcePatientFromTarget.getGender(), is(equalTo(Enumerations.AdministrativeGender.MALE)));
Patient paul2 = buildPaulPatient();
paul2.setGender(Enumerations.AdministrativeGender.FEMALE);
paul2 = createPatientAndUpdateLinks(paul2);
assertThat(paul2, is(sameGoldenResourceAs(paul)));
//Newly matched patients aren't allowed to overwrite GoldenResource Attributes unless they are empty,
// so gender should still be set to male.
Patient paul2GoldenResource = (Patient) getGoldenResourceFromTargetResource(paul2);
assertThat(paul2GoldenResource.getGender(), is(equalTo(Enumerations.AdministrativeGender.MALE)));
}
@Test @Test
//Test Case #1 //Test Case #1
public void testPatientUpdatesOverwriteGoldenResourceData() { public void testPatientUpdatesOverwriteGoldenResourceData() {

View File

@ -77,6 +77,7 @@ class TerserUtilTest {
assertEquals(check.getValue(), p.getBirthDate()); assertEquals(check.getValue(), p.getBirthDate());
} }
@Test @Test
void testFieldExists() { void testFieldExists() {
assertTrue(TerserUtil.fieldExists(ourFhirContext, "identifier", TerserUtil.newResource(ourFhirContext, "Patient"))); assertTrue(TerserUtil.fieldExists(ourFhirContext, "identifier", TerserUtil.newResource(ourFhirContext, "Patient")));
@ -294,6 +295,22 @@ class TerserUtilTest {
assertEquals("Doe", p2.getName().get(0).getFamily()); assertEquals("Doe", p2.getName().get(0).getFamily());
} }
@Test
public void testReplaceFieldsByPredicate() {
Patient p1 = new Patient();
p1.addName().setFamily("Doe");
p1.setGender(Enumerations.AdministrativeGender.MALE);
Patient p2 = new Patient();
p2.addName().setFamily("Smith");
TerserUtil.replaceFieldsByPredicate(ourFhirContext, p1, p2, TerserUtil.EXCLUDE_IDS_META_AND_EMPTY);
assertEquals(1, p2.getName().size());
assertEquals("Smith", p2.getName().get(0).getFamily());
assertEquals(Enumerations.AdministrativeGender.MALE, p2.getGender());
}
@Test @Test
public void testClearFields() { public void testClearFields() {
Patient p1 = new Patient(); Patient p1 = new Patient();