From 481698cf240b7ced283b2c4ed48e09462f05e0f9 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Mon, 12 Jun 2023 17:17:30 +0100 Subject: [PATCH] Fix up terminology tests to ignore irrelevant extensions --- .../org/hl7/fhir/r5/utils/ElementVisitor.java | 45 +++++++++++ .../org/hl7/fhir/validation/ValidatorCli.java | 2 +- .../hl7/fhir/validation/special/TxTester.java | 12 +-- .../validation/special/TxTesterScrubbers.java | 75 +++++++++++++++++-- .../ExternalTerminologyServiceTests.java | 2 +- .../tests/TerminologyServiceTests.java | 6 +- pom.xml | 2 +- 7 files changed, 128 insertions(+), 16 deletions(-) create mode 100644 org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ElementVisitor.java diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ElementVisitor.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ElementVisitor.java new file mode 100644 index 000000000..0ae4b1a56 --- /dev/null +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ElementVisitor.java @@ -0,0 +1,45 @@ +package org.hl7.fhir.r5.utils; + +import org.hl7.fhir.r5.model.Base; +import org.hl7.fhir.r5.model.Element; +import org.hl7.fhir.r5.model.Property; +import org.hl7.fhir.r5.model.Resource; + +public class ElementVisitor { + + public interface IElementVisitor { + public void visit(Resource resource); + public void visit(Element element); + } + + private IElementVisitor visitor; + + public ElementVisitor(IElementVisitor visitor) { + this.visitor = visitor; + } + + private void visitBase(Base base) { + for (Property p : base.children()) { + if (p.hasValues()) { + for (Base b : p.getValues()) { + if (b instanceof Resource) { + visit((Resource) b); + } else { + visit((Element) b); + } + } + } + } + } + + public void visit(Resource res) { + visitor.visit(res); + visitBase(res); + } + + public void visit(Element e) { + visitor.visit(e); + visitBase(e); + } + +} diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidatorCli.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidatorCli.java index 76d019efc..4057eedd9 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidatorCli.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidatorCli.java @@ -224,7 +224,7 @@ public class ValidatorCli { final String version = Params.getParam(args, Params.VERSION); final String tx = Params.getParam(args, Params.TERMINOLOGY); final String filter = Params.getParam(args, Params.FILTER); - boolean ok = new TxTester(new InternalTxLoader(source, output), tx).setOutput(output).execute(version, filter); + boolean ok = new TxTester(new InternalTxLoader(source, output), tx, false).setOutput(output).execute(version, filter); System.exit(ok ? 1 : 0); } else { final String testModuleParam = Params.getParam(args, Params.TEST_MODULES); diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTester.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTester.java index 0b7ffcdb9..eb31c7727 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTester.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTester.java @@ -58,16 +58,18 @@ public class TxTester { private String error; private String output; private ITerminologyClient tx; + private boolean tight; - public TxTester(ITxTesterLoader loader, String server) { + public TxTester(ITxTesterLoader loader, String server, boolean tight) { super(); this.server = server; this.loader = loader; + this.tight = tight; } public static void main(String[] args) throws Exception { - new TxTester(new InternalTxLoader(args[0]), args[1]).execute(args[2], args[3]); + new TxTester(new InternalTxLoader(args[0]), args[1], "true".equals(args[2])).execute(args[2], args[3]); } public boolean execute(String version, String filter) throws IOException, URISyntaxException { @@ -239,12 +241,12 @@ public class TxTester { String vsj; try { ValueSet vs = tx.expandValueset(null, p, null); - TxTesterScrubbers.scrub(vs); + TxTesterScrubbers.scrubVS(vs, tight); TxTesterSorters.sortValueSet(vs); vsj = new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).composeString(vs); } catch (EFhirClientException e) { OperationOutcome oo = e.getServerErrors().get(0); - TxTesterScrubbers.scrub(oo); + TxTesterScrubbers.scrubOO(oo, tight); vsj = new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).composeString(oo); } String diff = CompareUtilities.checkJsonSrcIsSame(resp, vsj); @@ -263,7 +265,7 @@ public class TxTester { String pj; try { Parameters po = tx.validateVS(p); - TxTesterScrubbers.scrub(po); + TxTesterScrubbers.scrubParams(po); TxTesterSorters.sortParameters(po); pj = new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).composeString(po); } catch (EFhirClientException e) { diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTesterScrubbers.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTesterScrubbers.java index cf9920040..5d565f958 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTesterScrubbers.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTesterScrubbers.java @@ -1,20 +1,85 @@ package org.hl7.fhir.validation.special; import org.hl7.fhir.r5.model.DomainResource; +import org.hl7.fhir.r5.model.Element; +import org.hl7.fhir.r5.model.Extension; +import org.hl7.fhir.r5.model.OperationOutcome; import org.hl7.fhir.r5.model.Parameters; +import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.model.ValueSet; +import org.hl7.fhir.r5.utils.ElementVisitor; +import org.hl7.fhir.r5.utils.ElementVisitor.IElementVisitor; +import org.hl7.fhir.utilities.Utilities; public class TxTesterScrubbers { - public static void scrub(DomainResource dr) { - dr.setText(null); - dr.setMeta(null); + + public static class TxTesterScrubberVisitor implements IElementVisitor { + + private boolean tight; + protected TxTesterScrubberVisitor(boolean tight) { + super(); + this.tight = tight; + } + + private boolean isManagedExtension(Extension extension) { + return !tight || !Utilities.isAbsoluteUrl(extension.getUrl()) || Utilities.existsInList(extension.getUrl(), + "http://hl7.org/fhir/StructureDefinition/codesystem-alternate", + "http://hl7.org/fhir/StructureDefinition/codesystem-conceptOrder", + "http://hl7.org/fhir/StructureDefinition/codesystem-label", + "http://hl7.org/fhir/StructureDefinition/coding-sctdescid", + "http://hl7.org/fhir/StructureDefinition/itemWeight", + "http://hl7.org/fhir/StructureDefinition/rendering-style", + "http://hl7.org/fhir/StructureDefinition/rendering-xhtml", + "http://hl7.org/fhir/StructureDefinition/translation", + "http://hl7.org/fhir/StructureDefinition/valueset-concept-definition", + "http://hl7.org/fhir/StructureDefinition/valueset-conceptOrder", + "http://hl7.org/fhir/StructureDefinition/valueset-deprecated", + "http://hl7.org/fhir/StructureDefinition/valueset-label", + "http://hl7.org/fhir/StructureDefinition/valueset-supplement", + "http://hl7.org/fhir/test/CodeSystem/de-multi", + "http://hl7.org/fhir/test/CodeSystem/en-multi", + "http://hl7.org/fhir/test/StructureDefinition/unknown-extension-1", + "http://hl7.org/fhir/test/StructureDefinition/unknown-extension-3", + "http://hl7.org/fhir/test/StructureDefinition/unknown-extension-4", + "http://hl7.org/fhir/test/StructureDefinition/unknown-extension-5", + "http://hl7.org/fhir/test/ValueSet/extensions-bad-supplement", + "http://hl7.org/fhir/test/ValueSet/simple-all", + "http://hl7.org/fhir/test/ValueSet/simple-enumerated", + "http://hl7.org/fhir/test/ValueSet/simple-filter-isa"); + } + + @Override + public void visit(Resource resource) { + if (resource instanceof DomainResource) { + DomainResource dr = (DomainResource) resource; + dr.getExtension().removeIf(ext -> !isManagedExtension(ext)); + } + } + + @Override + public void visit(Element element) { + element.getExtension().removeIf(ext -> !isManagedExtension(ext)); + } + } + + public static void scrubDR(DomainResource dr, boolean tight) { + dr.setText(null); + dr.setMeta(null); + new ElementVisitor(new TxTesterScrubberVisitor(tight)).visit(dr); } - public static void scrub(Parameters po) { + public static void scrubVS(ValueSet vs, boolean tight) { + scrubDR(vs, tight); + } + + public static void scrubParams(Parameters po) { po.setMeta(null); - + } + + public static void scrubOO(OperationOutcome po, boolean tight) { + scrubDR(po, tight); } } diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/terminology/tests/ExternalTerminologyServiceTests.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/terminology/tests/ExternalTerminologyServiceTests.java index 744a08f0c..b4225c1d8 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/terminology/tests/ExternalTerminologyServiceTests.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/terminology/tests/ExternalTerminologyServiceTests.java @@ -93,7 +93,7 @@ public class ExternalTerminologyServiceTests implements ITxTesterLoader { public void test() throws Exception { if (SERVER != null) { if (tester == null) { - tester = new TxTester(this, SERVER); + tester = new TxTester(this, SERVER, true); } String err = tester.executeTest(setup.suite, setup.test); Assertions.assertTrue(err == null, err); diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/terminology/tests/TerminologyServiceTests.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/terminology/tests/TerminologyServiceTests.java index 6a7bb24ec..dd7959496 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/terminology/tests/TerminologyServiceTests.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/terminology/tests/TerminologyServiceTests.java @@ -161,7 +161,7 @@ public class TerminologyServiceTests { removeParameter(vse.getValueset(), "excludeNested"); } TxTesterSorters.sortValueSet(vse.getValueset()); - TxTesterScrubbers.scrub(vse.getValueset()); + TxTesterScrubbers.scrubVS(vse.getValueset(), false); String vsj = new JsonParser().setOutputStyle(OutputStyle.PRETTY).composeString(vse.getValueset()); String diff = CompareUtilities.checkJsonSrcIsSame(resp, vsj); if (diff != null) { @@ -205,7 +205,7 @@ public class TerminologyServiceTests { } e.getDetails().setText(vse.getError()); oo.addIssue(e); - TxTesterScrubbers.scrub(oo); + TxTesterScrubbers.scrubOO(oo, false); String ooj = new JsonParser().setOutputStyle(OutputStyle.PRETTY).composeString(oo); String diff = CompareUtilities.checkJsonSrcIsSame(resp, ooj); @@ -291,7 +291,7 @@ public class TerminologyServiceTests { res.addParameter().setName("issues").setResource(oo); } TxTesterSorters.sortParameters(res); - TxTesterScrubbers.scrub(res); + TxTesterScrubbers.scrubParams(res); String pj = new JsonParser().setOutputStyle(OutputStyle.PRETTY).composeString(res); String diff = CompareUtilities.checkJsonSrcIsSame(resp, pj); diff --git a/pom.xml b/pom.xml index 018cd5dda..2e9dc5843 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ 6.4.1 - 1.3.9 + 1.3.10-SNAPSHOT 2.14.0 5.9.2 1.8.2