From 82c36de65d9ef100959c99a50b8b533d60707dec Mon Sep 17 00:00:00 2001 From: "dotasek.dev" Date: Tue, 19 Mar 2024 17:53:45 -0400 Subject: [PATCH 1/9] WIP refactor terminology service tests for reuse outside core --- .../special/TxServiceTestHelper.java | 239 +++++++++++++++++ .../tests/TerminologyServiceTests.java | 241 ++---------------- 2 files changed, 267 insertions(+), 213 deletions(-) create mode 100644 org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java new file mode 100644 index 000000000..b05a0170c --- /dev/null +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java @@ -0,0 +1,239 @@ +package org.hl7.fhir.validation.special; + +import com.google.gson.JsonSyntaxException; +import lombok.Getter; +import org.hl7.fhir.r5.formats.JsonParser; +import org.hl7.fhir.r5.context.IWorkerContext; +import org.hl7.fhir.r5.formats.IParser; +import org.hl7.fhir.r5.model.*; +import org.hl7.fhir.r5.terminologies.utilities.TerminologyServiceErrorClass; +import org.hl7.fhir.r5.terminologies.utilities.ValidationResult; +import org.hl7.fhir.r5.test.utils.CompareUtilities; +import org.hl7.fhir.r5.test.utils.TestingUtilities; +import org.hl7.fhir.utilities.FhirPublication; +import org.hl7.fhir.utilities.TextFile; +import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.i18n.I18nConstants; +import org.hl7.fhir.utilities.json.model.JsonObject; +import org.hl7.fhir.utilities.validation.ValidationOptions; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.*; + +public class TxServiceTestHelper { + + + public static class TestSetup { + public TestSetup(JsonObject suite, JsonObject test) { + this.suite = suite; + this.test = test; + } + @Getter + private JsonObject suite; + @Getter + private JsonObject test; + + } + + public static class TestData { + + @Getter + private final JsonObject manifest; + + @Getter + private final JsonObject externals; + + @Getter + private final List testData; + + public TestData() throws IOException { + String contents = TestingUtilities.loadTestResource("tx", "test-cases.json"); + String externalSource = TestingUtilities.loadTestResource("tx", "messages-tx.fhir.org.json"); + externals = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(externalSource); + + Map examples = new HashMap(); + manifest = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(contents); + for (org.hl7.fhir.utilities.json.model.JsonObject suite : manifest.getJsonObjects("suites")) { + if (!"tx.fhir.org".equals(suite.asString("mode"))) { + String sn = suite.asString("name"); + for (org.hl7.fhir.utilities.json.model.JsonObject test : suite.getJsonObjects("tests")) { + String tn = test.asString("name"); + examples.put(sn+"."+tn, new TestSetup(suite, test)); + } + } + } + + List names = new ArrayList(examples.size()); + names.addAll(examples.keySet()); + Collections.sort(names); + + testData = new ArrayList(examples.size()); + for (String id : names) { + testData.add(new Object[]{id, examples.get(id)}); + } + } + } + + public static String getDiffForValidation(IWorkerContext context, String name, Resource req, String resp, String lang, String fp, JsonObject ext, boolean isCS) throws JsonSyntaxException, FileNotFoundException, IOException { + org.hl7.fhir.r5.model.Parameters p = (org.hl7.fhir.r5.model.Parameters) req; + ValueSet vs = null; + String vsurl = null; + if (!isCS) { + if (p.hasParameter("valueSetVersion")) { + vsurl = p.getParameterValue("url").primitiveValue()+"|"+p.getParameterValue("valueSetVersion").primitiveValue(); + vs = context.fetchResource(ValueSet.class, p.getParameterValue("url").primitiveValue(), p.getParameterValue("valueSetVersion").primitiveValue()); + } else { + vsurl = p.getParameterValue("url").primitiveValue(); + vs = context.fetchResource(ValueSet.class, p.getParameterValue("url").primitiveValue()); + } + } + ValidationResult vm = null; + String code = null; + String system = null; + String version = null; + String display = null; + CodeableConcept cc = null; + org.hl7.fhir.r5.model.Parameters res = null; + OperationOutcome oo = null; + + if (vs == null && vsurl != null) { + String msg = context.formatMessage(I18nConstants.UNABLE_TO_RESOLVE_VALUE_SET_, vsurl); + oo = new OperationOutcome(); + CodeableConcept cct = oo.addIssue().setSeverity(OperationOutcome.IssueSeverity.ERROR).setCode(OperationOutcome.IssueType.NOTFOUND).getDetails(); + cct.addCoding("http://hl7.org/fhir/tools/CodeSystem/tx-issue-type", "not-found", null); + cct.setText(msg); + } else { + ValidationOptions options = new ValidationOptions(FhirPublication.R5); + if (p.hasParameter("displayLanguage")) { + options = options.withLanguage(p.getParameterString("displayLanguage")); + } else if (lang != null ) { + options = options.withLanguage(lang); + } + if (p.hasParameter("valueset-membership-only") && "true".equals(p.getParameterString("valueset-membership-only"))) { + options = options.withCheckValueSetOnly(); + } + if (p.hasParameter("lenient-display-validation") && "true".equals(p.getParameterString("lenient-display-validation"))) { + options = options.setDisplayWarningMode(true); + } + if (p.hasParameter("activeOnly") && "true".equals(p.getParameterString("activeOnly"))) { + options = options.setActiveOnly(true); + } + context.getExpansionParameters().clearParameters("includeAlternateCodes"); + for (Parameters.ParametersParameterComponent pp : p.getParameter()) { + if ("includeAlternateCodes".equals(pp.getName())) { + context.getExpansionParameters().addParameter(pp.copy()); + } + } + if (p.hasParameter("code")) { + code = p.getParameterString("code"); + system = p.getParameterString(isCS ? "url" : "system"); + version = p.getParameterString(isCS ? "version" : "systemVersion"); + display = p.getParameterString("display"); + vm = context.validateCode(options.withGuessSystem(), + p.getParameterString(isCS ? "url" : "system"), p.getParameterString(isCS ? "version" : "systemVersion"), + p.getParameterString("code"), p.getParameterString("display"), vs); + } else if (p.hasParameter("coding")) { + Coding coding = (Coding) p.getParameterValue("coding"); + code = coding.getCode(); + system = coding.getSystem(); + version = coding.getVersion(); + display = coding.getDisplay(); + vm = context.validateCode(options, coding, vs); + } else if (p.hasParameter("codeableConcept")) { + cc = (CodeableConcept) p.getParameterValue("codeableConcept"); + vm = context.validateCode(options, cc, vs); + } else { + throw new Error("validate not done yet for this steup"); + } + } + if (oo == null && vm != null && vm.getSeverity() == org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity.FATAL) { + oo = new OperationOutcome(); + oo.getIssue().addAll(vm.getIssues()); + } + if (oo != null) { + TxTesterSorters.sortOperationOutcome(oo); + TxTesterScrubbers.scrubOO(oo, false); + + String pj = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(oo); + String diff = CompareUtilities.checkJsonSrcIsSame(resp, pj, ext); + if (diff != null) { + Utilities.createDirectory(Utilities.getDirectoryForFile(fp)); + TextFile.stringToFile(pj, fp); + System.out.println("Test "+name+"failed: "+diff); + } + return diff; + } else { + if (res == null) { + res = new org.hl7.fhir.r5.model.Parameters(); + if (vm.getSystem() != null) { + res.addParameter("system", new UriType(vm.getSystem())); + } else if (system != null) { + res.addParameter("system", new UriType(system)); + } + if (vm.getCode() != null) { + if (code != null && !code.equals(vm.getCode())) { + res.addParameter("code", new CodeType(code)); + res.addParameter("normalized-code", new CodeType(vm.getCode())); + } else { + res.addParameter("code", new CodeType(vm.getCode())); + } + } else if (code != null) { + res.addParameter("code", new CodeType(code)); + } + if (vm.getSeverity() == org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity.ERROR) { + res.addParameter("result", false); + } else { + res.addParameter("result", true); + } + if (vm.getMessage() != null) { + res.addParameter("message", vm.getMessage()); + } + if (vm.getVersion() != null) { + res.addParameter("version", vm.getVersion()); + } else if (version != null) { + res.addParameter("version", new StringType(version)); + } + if (vm.getDisplay() != null) { + res.addParameter("display", vm.getDisplay()); + } else if (display != null) { + res.addParameter("display", new StringType(display)); + } + // if (vm.getCodeableConcept() != null) { + // res.addParameter("codeableConcept", vm.getCodeableConcept()); + // } else + if (cc != null) { + res.addParameter("codeableConcept", cc); + } + if (vm.isInactive()) { + res.addParameter("inactive", true); + } + if (vm.getStatus() != null) { + res.addParameter("status", vm.getStatus()); + } + if (vm.getUnknownSystems() != null) { + for (String s : vm.getUnknownSystems()) { + res.addParameter(vm.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED ? "x-caused-by-unknown-system" : "x-unknown-system", new CanonicalType(s)); + } + } + if (vm.getIssues().size() > 0) { + oo = new OperationOutcome(); + oo.getIssue().addAll(vm.getIssues()); + res.addParameter().setName("issues").setResource(oo); + } + } + + TxTesterSorters.sortParameters(res); + TxTesterScrubbers.scrubParams(res); + + String pj = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(res); + String diff = CompareUtilities.checkJsonSrcIsSame(resp, pj, ext); + if (diff != null) { + Utilities.createDirectory(Utilities.getDirectoryForFile(fp)); + TextFile.stringToFile(pj, fp); + System.out.println("Test "+name+"failed: "+diff); + } + return diff; + } + } +} 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 a278efd85..9c6fbf71e 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 @@ -48,6 +48,8 @@ import org.hl7.fhir.utilities.i18n.I18nConstants; import org.hl7.fhir.utilities.json.model.JsonObject; import org.hl7.fhir.utilities.validation.ValidationOptions; import org.hl7.fhir.validation.ValidationEngine; +import org.hl7.fhir.validation.special.TxServiceTestHelper; +import org.hl7.fhir.validation.special.TxServiceTestHelper.TestSetup; import org.hl7.fhir.validation.special.TxTesterScrubbers; import org.hl7.fhir.validation.special.TxTesterSorters; import org.hl7.fhir.validation.tests.utilities.TestUtilities; @@ -61,60 +63,31 @@ import org.junit.runners.Parameterized.Parameters; import com.google.common.base.Charsets; import com.google.gson.JsonSyntaxException; +import org.hl7.fhir.r5.context.IWorkerContext; + +import static org.junit.Assert.assertNull; @RunWith(Parameterized.class) public class TerminologyServiceTests { - public static class JsonObjectPair { - public JsonObjectPair(JsonObject suite, JsonObject test) { - this.suite = suite; - this.test = test; - } - private JsonObject suite; - private JsonObject test; - } + +private static TxServiceTestHelper.TestData testData; @Parameters(name = "{index}: id {0}") public static Iterable data() throws IOException { - - String contents = TestingUtilities.loadTestResource("tx", "test-cases.json"); - String externalSource = TestingUtilities.loadTestResource("tx", "messages-tx.fhir.org.json"); - externals = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(externalSource); - - Map examples = new HashMap(); - manifest = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(contents); - for (org.hl7.fhir.utilities.json.model.JsonObject suite : manifest.getJsonObjects("suites")) { - if (!"tx.fhir.org".equals(suite.asString("mode"))) { - String sn = suite.asString("name"); - for (org.hl7.fhir.utilities.json.model.JsonObject test : suite.getJsonObjects("tests")) { - String tn = test.asString("name"); - examples.put(sn+"."+tn, new JsonObjectPair(suite, test)); - } - } - } - - List names = new ArrayList(examples.size()); - names.addAll(examples.keySet()); - Collections.sort(names); - - List objects = new ArrayList(examples.size()); - for (String id : names) { - objects.add(new Object[]{id, examples.get(id)}); - } - return objects; + testData = new TxServiceTestHelper.TestData(); + return testData.getTestData(); } - private static org.hl7.fhir.utilities.json.model.JsonObject manifest; - private static org.hl7.fhir.utilities.json.model.JsonObject externals; - private JsonObjectPair setup; - private String version; - private String name; + private final TestSetup setup; + private final String version; + private final String name; private static ValidationEngine baseEngine; - public TerminologyServiceTests(String name, JsonObjectPair setup) { + public TerminologyServiceTests(String name, TestSetup setup) { this.name = name; this.setup = setup; version = "5.0.0"; @@ -127,33 +100,35 @@ public class TerminologyServiceTests { baseEngine = TestUtilities.getValidationEngineNoTxServer("hl7.fhir.r5.core#5.0.0", FhirPublication.R5, "5.0.0"); } ValidationEngine engine = new ValidationEngine(this.baseEngine); - for (String s : setup.suite.forceArray("setup").asStrings()) { + for (String s : setup.getSuite().forceArray("setup").asStrings()) { // System.out.println(s); Resource res = loadResource(s); engine.seeResource(res); } - Resource req = loadResource(setup.test.asString("request")); - String fn = setup.test.has("response:tx.fhir.org") ? setup.test.asString("response:tx.fhir.org") : setup.test.asString("response"); + Resource req = loadResource(setup.getTest().asString("request")); + String fn = setup.getTest().has("response:tx.fhir.org") ? setup.getTest().asString("response:tx.fhir.org") : setup.getTest().asString("response"); String resp = TestingUtilities.loadTestResource("tx", fn); String fp = Utilities.path("[tmp]", "tx", fn); - JsonObject ext = externals == null ? null : externals.getJsonObject(fn); + JsonObject ext = testData.getExternals() == null ? null : testData.getExternals().getJsonObject(fn); File fo = new File(fp); if (fo.exists()) { fo.delete(); } - if (setup.test.has("profile")) { - engine.getContext().setExpansionParameters((org.hl7.fhir.r5.model.Parameters) loadResource(setup.test.asString("profile"))); + if (setup.getTest().has("profile")) { + engine.getContext().setExpansionParameters((org.hl7.fhir.r5.model.Parameters) loadResource(setup.getTest().asString("profile"))); } else { engine.getContext().setExpansionParameters((org.hl7.fhir.r5.model.Parameters) loadResource("parameters-default.json")); } - if (setup.test.asString("operation").equals("expand")) { - expand(engine, req, resp, setup.test.asString("Content-Language"), fp, ext); - } else if (setup.test.asString("operation").equals("validate-code")) { - validate(engine, setup.test.asString("name"), req, resp, setup.test.asString("Content-Language"), fp, ext, false); - } else if (setup.test.asString("operation").equals("cs-validate-code")) { - validate(engine, setup.test.asString("name"), req, resp, setup.test.asString("Content-Language"), fp, ext, true); + if (setup.getTest().asString("operation").equals("expand")) { + expand(engine, req, resp, setup.getTest().asString("Content-Language"), fp, ext); + } else if (setup.getTest().asString("operation").equals("validate-code")) { + String diff = TxServiceTestHelper.getDiffForValidation(engine.getContext(), setup.getTest().asString("name"), req, resp, setup.getTest().asString("Content-Language"), fp, ext, false); + assertNull(diff, diff); + } else if (setup.getTest().asString("operation").equals("cs-validate-code")) { + String diff = TxServiceTestHelper.getDiffForValidation(engine.getContext(), setup.getTest().asString("name"), req, resp, setup.getTest().asString("Content-Language"), fp, ext, true); + assertNull(diff, diff); } else { - Assertions.fail("Unknown Operation "+setup.test.asString("operation")); + Assertions.fail("Unknown Operation "+ setup.getTest().asString("operation")); } } @@ -243,167 +218,7 @@ public class TerminologyServiceTests { } } - private void validate(ValidationEngine engine, String name, Resource req, String resp, String lang, String fp, JsonObject ext, boolean isCS) throws JsonSyntaxException, FileNotFoundException, IOException { - org.hl7.fhir.r5.model.Parameters p = (org.hl7.fhir.r5.model.Parameters) req; - ValueSet vs = null; - String vsurl = null; - if (!isCS) { - if (p.hasParameter("valueSetVersion")) { - vsurl = p.getParameterValue("url").primitiveValue()+"|"+p.getParameterValue("valueSetVersion").primitiveValue(); - vs = engine.getContext().fetchResource(ValueSet.class, p.getParameterValue("url").primitiveValue(), p.getParameterValue("valueSetVersion").primitiveValue()); - } else { - vsurl = p.getParameterValue("url").primitiveValue(); - vs = engine.getContext().fetchResource(ValueSet.class, p.getParameterValue("url").primitiveValue()); - } - } - ValidationResult vm = null; - String code = null; - String system = null; - String version = null; - String display = null; - CodeableConcept cc = null; - org.hl7.fhir.r5.model.Parameters res = null; - OperationOutcome oo = null; - - if (vs == null && vsurl != null) { - String msg = engine.getContext().formatMessage(I18nConstants.UNABLE_TO_RESOLVE_VALUE_SET_, vsurl); - oo = new OperationOutcome(); - CodeableConcept cct = oo.addIssue().setSeverity(IssueSeverity.ERROR).setCode(IssueType.NOTFOUND).getDetails(); - cct.addCoding("http://hl7.org/fhir/tools/CodeSystem/tx-issue-type", "not-found", null); - cct.setText(msg); - } else { - ValidationOptions options = new ValidationOptions(FhirPublication.R5); - if (p.hasParameter("displayLanguage")) { - options = options.withLanguage(p.getParameterString("displayLanguage")); - } else if (lang != null ) { - options = options.withLanguage(lang); - } - if (p.hasParameter("valueset-membership-only") && "true".equals(p.getParameterString("valueset-membership-only"))) { - options = options.withCheckValueSetOnly(); - } - if (p.hasParameter("lenient-display-validation") && "true".equals(p.getParameterString("lenient-display-validation"))) { - options = options.setDisplayWarningMode(true); - } - if (p.hasParameter("activeOnly") && "true".equals(p.getParameterString("activeOnly"))) { - options = options.setActiveOnly(true); - } - engine.getContext().getExpansionParameters().clearParameters("includeAlternateCodes"); - for (ParametersParameterComponent pp : p.getParameter()) { - if ("includeAlternateCodes".equals(pp.getName())) { - engine.getContext().getExpansionParameters().addParameter(pp.copy()); - } - } - if (p.hasParameter("code")) { - code = p.getParameterString("code"); - system = p.getParameterString(isCS ? "url" : "system"); - version = p.getParameterString(isCS ? "version" : "systemVersion"); - display = p.getParameterString("display"); - vm = engine.getContext().validateCode(options.withGuessSystem(), - p.getParameterString(isCS ? "url" : "system"), p.getParameterString(isCS ? "version" : "systemVersion"), - p.getParameterString("code"), p.getParameterString("display"), vs); - } else if (p.hasParameter("coding")) { - Coding coding = (Coding) p.getParameterValue("coding"); - code = coding.getCode(); - system = coding.getSystem(); - version = coding.getVersion(); - display = coding.getDisplay(); - vm = engine.getContext().validateCode(options, coding, vs); - } else if (p.hasParameter("codeableConcept")) { - cc = (CodeableConcept) p.getParameterValue("codeableConcept"); - vm = engine.getContext().validateCode(options, cc, vs); - } else { - throw new Error("validate not done yet for this steup"); - } - } - if (oo == null && vm != null && vm.getSeverity() == org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity.FATAL) { - oo = new OperationOutcome(); - oo.getIssue().addAll(vm.getIssues()); - } - if (oo != null) { - TxTesterSorters.sortOperationOutcome(oo); - TxTesterScrubbers.scrubOO(oo, false); - - String pj = new JsonParser().setOutputStyle(OutputStyle.PRETTY).composeString(oo); - String diff = CompareUtilities.checkJsonSrcIsSame(resp, pj, ext); - if (diff != null) { - Utilities.createDirectory(Utilities.getDirectoryForFile(fp)); - TextFile.stringToFile(pj, fp); - System.out.println("Test "+name+"failed: "+diff); - } - Assertions.assertTrue(diff == null, diff); - } else { - if (res == null) { - res = new org.hl7.fhir.r5.model.Parameters(); - if (vm.getSystem() != null) { - res.addParameter("system", new UriType(vm.getSystem())); - } else if (system != null) { - res.addParameter("system", new UriType(system)); - } - if (vm.getCode() != null) { - if (code != null && !code.equals(vm.getCode())) { - res.addParameter("code", new CodeType(code)); - res.addParameter("normalized-code", new CodeType(vm.getCode())); - } else { - res.addParameter("code", new CodeType(vm.getCode())); - } - } else if (code != null) { - res.addParameter("code", new CodeType(code)); - } - if (vm.getSeverity() == org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity.ERROR) { - res.addParameter("result", false); - } else { - res.addParameter("result", true); - } - if (vm.getMessage() != null) { - res.addParameter("message", vm.getMessage()); - } - if (vm.getVersion() != null) { - res.addParameter("version", vm.getVersion()); - } else if (version != null) { - res.addParameter("version", new StringType(version)); - } - if (vm.getDisplay() != null) { - res.addParameter("display", vm.getDisplay()); - } else if (display != null) { - res.addParameter("display", new StringType(display)); - } - // if (vm.getCodeableConcept() != null) { - // res.addParameter("codeableConcept", vm.getCodeableConcept()); - // } else - if (cc != null) { - res.addParameter("codeableConcept", cc); - } - if (vm.isInactive()) { - res.addParameter("inactive", true); - } - if (vm.getStatus() != null) { - res.addParameter("status", vm.getStatus()); - } - if (vm.getUnknownSystems() != null) { - for (String s : vm.getUnknownSystems()) { - res.addParameter(vm.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED ? "x-caused-by-unknown-system" : "x-unknown-system", new CanonicalType(s)); - } - } - if (vm.getIssues().size() > 0) { - oo = new OperationOutcome(); - oo.getIssue().addAll(vm.getIssues()); - res.addParameter().setName("issues").setResource(oo); - } - } - TxTesterSorters.sortParameters(res); - TxTesterScrubbers.scrubParams(res); - - String pj = new JsonParser().setOutputStyle(OutputStyle.PRETTY).composeString(res); - String diff = CompareUtilities.checkJsonSrcIsSame(resp, pj, ext); - if (diff != null) { - Utilities.createDirectory(Utilities.getDirectoryForFile(fp)); - TextFile.stringToFile(pj, fp); - System.out.println("Test "+name+"failed: "+diff); - } - Assertions.assertTrue(diff == null, diff); - } - } public Resource loadResource(String filename) throws IOException, FHIRFormatError, FileNotFoundException, FHIRException, DefinitionException { String contents = TestingUtilities.loadTestResource("tx", filename); From 890776604d7d37c99a2a763f65b85847c29776ba Mon Sep 17 00:00:00 2001 From: "dotasek.dev" Date: Wed, 20 Mar 2024 18:26:10 -0400 Subject: [PATCH 2/9] variable renaming --- .../special/TxServiceTestHelper.java | 100 +++++++++--------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java index b05a0170c..b23a2f80d 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java @@ -75,11 +75,11 @@ public class TxServiceTestHelper { } } - public static String getDiffForValidation(IWorkerContext context, String name, Resource req, String resp, String lang, String fp, JsonObject ext, boolean isCS) throws JsonSyntaxException, FileNotFoundException, IOException { - org.hl7.fhir.r5.model.Parameters p = (org.hl7.fhir.r5.model.Parameters) req; + public static String getDiffForValidation(IWorkerContext context, String name, Resource requestParameters, String expectedResponse, String lang, String fp, JsonObject externals, boolean isCodeSystem) throws JsonSyntaxException, FileNotFoundException, IOException { + org.hl7.fhir.r5.model.Parameters p = (org.hl7.fhir.r5.model.Parameters) requestParameters; ValueSet vs = null; String vsurl = null; - if (!isCS) { + if (!isCodeSystem) { if (p.hasParameter("valueSetVersion")) { vsurl = p.getParameterValue("url").primitiveValue()+"|"+p.getParameterValue("valueSetVersion").primitiveValue(); vs = context.fetchResource(ValueSet.class, p.getParameterValue("url").primitiveValue(), p.getParameterValue("valueSetVersion").primitiveValue()); @@ -88,21 +88,21 @@ public class TxServiceTestHelper { vs = context.fetchResource(ValueSet.class, p.getParameterValue("url").primitiveValue()); } } - ValidationResult vm = null; + ValidationResult validationResult = null; String code = null; String system = null; String version = null; String display = null; CodeableConcept cc = null; org.hl7.fhir.r5.model.Parameters res = null; - OperationOutcome oo = null; + OperationOutcome operationOutcome = null; if (vs == null && vsurl != null) { String msg = context.formatMessage(I18nConstants.UNABLE_TO_RESOLVE_VALUE_SET_, vsurl); - oo = new OperationOutcome(); - CodeableConcept cct = oo.addIssue().setSeverity(OperationOutcome.IssueSeverity.ERROR).setCode(OperationOutcome.IssueType.NOTFOUND).getDetails(); - cct.addCoding("http://hl7.org/fhir/tools/CodeSystem/tx-issue-type", "not-found", null); - cct.setText(msg); + operationOutcome = new OperationOutcome(); + CodeableConcept codeableConcept = operationOutcome.addIssue().setSeverity(OperationOutcome.IssueSeverity.ERROR).setCode(OperationOutcome.IssueType.NOTFOUND).getDetails(); + codeableConcept.addCoding("http://hl7.org/fhir/tools/CodeSystem/tx-issue-type", "not-found", null); + codeableConcept.setText(msg); } else { ValidationOptions options = new ValidationOptions(FhirPublication.R5); if (p.hasParameter("displayLanguage")) { @@ -127,11 +127,11 @@ public class TxServiceTestHelper { } if (p.hasParameter("code")) { code = p.getParameterString("code"); - system = p.getParameterString(isCS ? "url" : "system"); - version = p.getParameterString(isCS ? "version" : "systemVersion"); + system = p.getParameterString(isCodeSystem ? "url" : "system"); + version = p.getParameterString(isCodeSystem ? "version" : "systemVersion"); display = p.getParameterString("display"); - vm = context.validateCode(options.withGuessSystem(), - p.getParameterString(isCS ? "url" : "system"), p.getParameterString(isCS ? "version" : "systemVersion"), + validationResult = context.validateCode(options.withGuessSystem(), + p.getParameterString(isCodeSystem ? "url" : "system"), p.getParameterString(isCodeSystem ? "version" : "systemVersion"), p.getParameterString("code"), p.getParameterString("display"), vs); } else if (p.hasParameter("coding")) { Coding coding = (Coding) p.getParameterValue("coding"); @@ -139,63 +139,63 @@ public class TxServiceTestHelper { system = coding.getSystem(); version = coding.getVersion(); display = coding.getDisplay(); - vm = context.validateCode(options, coding, vs); + validationResult = context.validateCode(options, coding, vs); } else if (p.hasParameter("codeableConcept")) { cc = (CodeableConcept) p.getParameterValue("codeableConcept"); - vm = context.validateCode(options, cc, vs); + validationResult = context.validateCode(options, cc, vs); } else { throw new Error("validate not done yet for this steup"); } } - if (oo == null && vm != null && vm.getSeverity() == org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity.FATAL) { - oo = new OperationOutcome(); - oo.getIssue().addAll(vm.getIssues()); + if (operationOutcome == null && validationResult != null && validationResult.getSeverity() == org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity.FATAL) { + operationOutcome = new OperationOutcome(); + operationOutcome.getIssue().addAll(validationResult.getIssues()); } - if (oo != null) { - TxTesterSorters.sortOperationOutcome(oo); - TxTesterScrubbers.scrubOO(oo, false); + if (operationOutcome != null) { + TxTesterSorters.sortOperationOutcome(operationOutcome); + TxTesterScrubbers.scrubOO(operationOutcome, false); - String pj = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(oo); - String diff = CompareUtilities.checkJsonSrcIsSame(resp, pj, ext); + String actualResponse = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(operationOutcome); + String diff = CompareUtilities.checkJsonSrcIsSame(expectedResponse, actualResponse, externals); if (diff != null) { Utilities.createDirectory(Utilities.getDirectoryForFile(fp)); - TextFile.stringToFile(pj, fp); + TextFile.stringToFile(actualResponse, fp); System.out.println("Test "+name+"failed: "+diff); } return diff; } else { if (res == null) { res = new org.hl7.fhir.r5.model.Parameters(); - if (vm.getSystem() != null) { - res.addParameter("system", new UriType(vm.getSystem())); + if (validationResult.getSystem() != null) { + res.addParameter("system", new UriType(validationResult.getSystem())); } else if (system != null) { res.addParameter("system", new UriType(system)); } - if (vm.getCode() != null) { - if (code != null && !code.equals(vm.getCode())) { + if (validationResult.getCode() != null) { + if (code != null && !code.equals(validationResult.getCode())) { res.addParameter("code", new CodeType(code)); - res.addParameter("normalized-code", new CodeType(vm.getCode())); + res.addParameter("normalized-code", new CodeType(validationResult.getCode())); } else { - res.addParameter("code", new CodeType(vm.getCode())); + res.addParameter("code", new CodeType(validationResult.getCode())); } } else if (code != null) { res.addParameter("code", new CodeType(code)); } - if (vm.getSeverity() == org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity.ERROR) { + if (validationResult.getSeverity() == org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity.ERROR) { res.addParameter("result", false); } else { res.addParameter("result", true); } - if (vm.getMessage() != null) { - res.addParameter("message", vm.getMessage()); + if (validationResult.getMessage() != null) { + res.addParameter("message", validationResult.getMessage()); } - if (vm.getVersion() != null) { - res.addParameter("version", vm.getVersion()); + if (validationResult.getVersion() != null) { + res.addParameter("version", validationResult.getVersion()); } else if (version != null) { res.addParameter("version", new StringType(version)); } - if (vm.getDisplay() != null) { - res.addParameter("display", vm.getDisplay()); + if (validationResult.getDisplay() != null) { + res.addParameter("display", validationResult.getDisplay()); } else if (display != null) { res.addParameter("display", new StringType(display)); } @@ -205,32 +205,32 @@ public class TxServiceTestHelper { if (cc != null) { res.addParameter("codeableConcept", cc); } - if (vm.isInactive()) { + if (validationResult.isInactive()) { res.addParameter("inactive", true); } - if (vm.getStatus() != null) { - res.addParameter("status", vm.getStatus()); + if (validationResult.getStatus() != null) { + res.addParameter("status", validationResult.getStatus()); } - if (vm.getUnknownSystems() != null) { - for (String s : vm.getUnknownSystems()) { - res.addParameter(vm.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED ? "x-caused-by-unknown-system" : "x-unknown-system", new CanonicalType(s)); + if (validationResult.getUnknownSystems() != null) { + for (String s : validationResult.getUnknownSystems()) { + res.addParameter(validationResult.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED ? "x-caused-by-unknown-system" : "x-unknown-system", new CanonicalType(s)); } } - if (vm.getIssues().size() > 0) { - oo = new OperationOutcome(); - oo.getIssue().addAll(vm.getIssues()); - res.addParameter().setName("issues").setResource(oo); + if (validationResult.getIssues().size() > 0) { + operationOutcome = new OperationOutcome(); + operationOutcome.getIssue().addAll(validationResult.getIssues()); + res.addParameter().setName("issues").setResource(operationOutcome); } } TxTesterSorters.sortParameters(res); TxTesterScrubbers.scrubParams(res); - String pj = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(res); - String diff = CompareUtilities.checkJsonSrcIsSame(resp, pj, ext); + String actualResponse = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(res); + String diff = CompareUtilities.checkJsonSrcIsSame(expectedResponse, actualResponse, externals); if (diff != null) { Utilities.createDirectory(Utilities.getDirectoryForFile(fp)); - TextFile.stringToFile(pj, fp); + TextFile.stringToFile(actualResponse, fp); System.out.println("Test "+name+"failed: "+diff); } return diff; From 4793e574b1285247c9636e3a204ce1623f4838d2 Mon Sep 17 00:00:00 2001 From: "dotasek.dev" Date: Wed, 20 Mar 2024 18:52:20 -0400 Subject: [PATCH 3/9] Break out helper classes --- .../special/TxServiceTestHelper.java | 51 ------------------- .../fhir/validation/special/TxTestData.java | 47 +++++++++++++++++ .../fhir/validation/special/TxTestSetup.java | 17 +++++++ .../tests/TerminologyServiceTests.java | 29 +++-------- 4 files changed, 70 insertions(+), 74 deletions(-) create mode 100644 org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestData.java create mode 100644 org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestSetup.java diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java index b23a2f80d..ae074748f 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java @@ -24,57 +24,6 @@ import java.util.*; public class TxServiceTestHelper { - public static class TestSetup { - public TestSetup(JsonObject suite, JsonObject test) { - this.suite = suite; - this.test = test; - } - @Getter - private JsonObject suite; - @Getter - private JsonObject test; - - } - - public static class TestData { - - @Getter - private final JsonObject manifest; - - @Getter - private final JsonObject externals; - - @Getter - private final List testData; - - public TestData() throws IOException { - String contents = TestingUtilities.loadTestResource("tx", "test-cases.json"); - String externalSource = TestingUtilities.loadTestResource("tx", "messages-tx.fhir.org.json"); - externals = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(externalSource); - - Map examples = new HashMap(); - manifest = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(contents); - for (org.hl7.fhir.utilities.json.model.JsonObject suite : manifest.getJsonObjects("suites")) { - if (!"tx.fhir.org".equals(suite.asString("mode"))) { - String sn = suite.asString("name"); - for (org.hl7.fhir.utilities.json.model.JsonObject test : suite.getJsonObjects("tests")) { - String tn = test.asString("name"); - examples.put(sn+"."+tn, new TestSetup(suite, test)); - } - } - } - - List names = new ArrayList(examples.size()); - names.addAll(examples.keySet()); - Collections.sort(names); - - testData = new ArrayList(examples.size()); - for (String id : names) { - testData.add(new Object[]{id, examples.get(id)}); - } - } - } - public static String getDiffForValidation(IWorkerContext context, String name, Resource requestParameters, String expectedResponse, String lang, String fp, JsonObject externals, boolean isCodeSystem) throws JsonSyntaxException, FileNotFoundException, IOException { org.hl7.fhir.r5.model.Parameters p = (org.hl7.fhir.r5.model.Parameters) requestParameters; ValueSet vs = null; diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestData.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestData.java new file mode 100644 index 000000000..e8652f3db --- /dev/null +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestData.java @@ -0,0 +1,47 @@ +package org.hl7.fhir.validation.special; + +import lombok.Getter; +import org.hl7.fhir.r5.test.utils.TestingUtilities; +import org.hl7.fhir.utilities.json.model.JsonObject; + +import java.io.IOException; +import java.util.*; + +public class TxTestData { + + @Getter + private final JsonObject manifest; + + @Getter + private final JsonObject externals; + + @Getter + private final List testData; + + public TxTestData() throws IOException { + String contents = TestingUtilities.loadTestResource("tx", "test-cases.json"); + String externalSource = TestingUtilities.loadTestResource("tx", "messages-tx.fhir.org.json"); + externals = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(externalSource); + + Map examples = new HashMap(); + manifest = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(contents); + for (JsonObject suite : manifest.getJsonObjects("suites")) { + if (!"tx.fhir.org".equals(suite.asString("mode"))) { + String sn = suite.asString("name"); + for (JsonObject test : suite.getJsonObjects("tests")) { + String tn = test.asString("name"); + examples.put(sn + "." + tn, new TxTestSetup(suite, test)); + } + } + } + + List names = new ArrayList(examples.size()); + names.addAll(examples.keySet()); + Collections.sort(names); + + testData = new ArrayList(examples.size()); + for (String id : names) { + testData.add(new Object[]{id, examples.get(id)}); + } + } +} diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestSetup.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestSetup.java new file mode 100644 index 000000000..6937aaf0f --- /dev/null +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestSetup.java @@ -0,0 +1,17 @@ +package org.hl7.fhir.validation.special; + +import lombok.Getter; +import org.hl7.fhir.utilities.json.model.JsonObject; + +public class TxTestSetup { + public TxTestSetup(JsonObject suite, JsonObject test) { + this.suite = suite; + this.test = test; + } + + @Getter + private JsonObject suite; + @Getter + private JsonObject test; + +} 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 9c6fbf71e..d16544e43 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 @@ -4,11 +4,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import org.apache.commons.io.IOUtils; import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_50; @@ -21,35 +16,26 @@ import org.hl7.fhir.exceptions.FHIRFormatError; import org.hl7.fhir.r5.formats.IParser.OutputStyle; import org.hl7.fhir.r5.formats.JsonParser; import org.hl7.fhir.r5.formats.XmlParser; -import org.hl7.fhir.r5.model.CanonicalType; import org.hl7.fhir.r5.model.CodeType; -import org.hl7.fhir.r5.model.StringType; -import org.hl7.fhir.r5.model.CodeableConcept; -import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.Constants; import org.hl7.fhir.r5.model.OperationOutcome; import org.hl7.fhir.r5.model.OperationOutcome.IssueSeverity; import org.hl7.fhir.r5.model.OperationOutcome.IssueType; import org.hl7.fhir.r5.model.OperationOutcome.OperationOutcomeIssueComponent; -import org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent; import org.hl7.fhir.r5.model.Resource; -import org.hl7.fhir.r5.model.UriType; import org.hl7.fhir.r5.model.ValueSet; import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionParameterComponent; import org.hl7.fhir.r5.terminologies.expansion.ValueSetExpansionOutcome; -import org.hl7.fhir.r5.terminologies.utilities.TerminologyServiceErrorClass; -import org.hl7.fhir.r5.terminologies.utilities.ValidationResult; import org.hl7.fhir.r5.test.utils.CompareUtilities; import org.hl7.fhir.r5.test.utils.TestingUtilities; import org.hl7.fhir.utilities.FhirPublication; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; -import org.hl7.fhir.utilities.i18n.I18nConstants; import org.hl7.fhir.utilities.json.model.JsonObject; -import org.hl7.fhir.utilities.validation.ValidationOptions; import org.hl7.fhir.validation.ValidationEngine; import org.hl7.fhir.validation.special.TxServiceTestHelper; -import org.hl7.fhir.validation.special.TxServiceTestHelper.TestSetup; +import org.hl7.fhir.validation.special.TxTestSetup; +import org.hl7.fhir.validation.special.TxTestData; import org.hl7.fhir.validation.special.TxTesterScrubbers; import org.hl7.fhir.validation.special.TxTesterSorters; import org.hl7.fhir.validation.tests.utilities.TestUtilities; @@ -61,9 +47,6 @@ import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import com.google.common.base.Charsets; -import com.google.gson.JsonSyntaxException; - -import org.hl7.fhir.r5.context.IWorkerContext; import static org.junit.Assert.assertNull; @@ -72,22 +55,22 @@ import static org.junit.Assert.assertNull; public class TerminologyServiceTests { -private static TxServiceTestHelper.TestData testData; +private static TxTestData testData; @Parameters(name = "{index}: id {0}") public static Iterable data() throws IOException { - testData = new TxServiceTestHelper.TestData(); + testData = new TxTestData(); return testData.getTestData(); } - private final TestSetup setup; + private final TxTestSetup setup; private final String version; private final String name; private static ValidationEngine baseEngine; - public TerminologyServiceTests(String name, TestSetup setup) { + public TerminologyServiceTests(String name, TxTestSetup setup) { this.name = name; this.setup = setup; version = "5.0.0"; From 0549903adeebb8a3877d5a7f62df082bc56e4907 Mon Sep 17 00:00:00 2001 From: "dotasek.dev" Date: Wed, 20 Mar 2024 19:27:19 -0400 Subject: [PATCH 4/9] Separate loading logic from data --- .../validation/special/TxServiceTestHelper.java | 16 ++++++++++++++++ .../hl7/fhir/validation/special/TxTestData.java | 16 ++++++++++++---- .../tests/TerminologyServiceTests.java | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java index ae074748f..4fedcb7c6 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java @@ -17,6 +17,7 @@ import org.hl7.fhir.utilities.i18n.I18nConstants; import org.hl7.fhir.utilities.json.model.JsonObject; import org.hl7.fhir.utilities.validation.ValidationOptions; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.*; @@ -176,8 +177,12 @@ public class TxServiceTestHelper { TxTesterScrubbers.scrubParams(res); String actualResponse = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(res); + + + String diff = CompareUtilities.checkJsonSrcIsSame(expectedResponse, actualResponse, externals); if (diff != null) { + dumparoo("/Users/david.otasek/IN/2024-02-05-hapi-core-bump-6-2.16/core-test", name, expectedResponse, actualResponse); Utilities.createDirectory(Utilities.getDirectoryForFile(fp)); TextFile.stringToFile(actualResponse, fp); System.out.println("Test "+name+"failed: "+diff); @@ -185,4 +190,15 @@ public class TxServiceTestHelper { return diff; } } + + public static void dumparoo(String rootDirectory, String testName, String expected, String actual) throws IOException { + String fullDirectory = rootDirectory + "/" + testName; + File directory = new File(fullDirectory); + if (!directory.exists()) { + directory.mkdirs(); + } +TextFile.stringToFile(expected, fullDirectory + "/expected.json"); + TextFile.stringToFile(actual, fullDirectory + "/actual.json"); + + } } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestData.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestData.java index e8652f3db..a012a3138 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestData.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxTestData.java @@ -18,13 +18,19 @@ public class TxTestData { @Getter private final List testData; - public TxTestData() throws IOException { + private TxTestData(List testData, JsonObject manifest, JsonObject externals) throws IOException { + this.testData = testData; + this.manifest = manifest; + this.externals = externals; + } + + public static TxTestData loadTestDataFromDefaultClassPath() throws IOException { String contents = TestingUtilities.loadTestResource("tx", "test-cases.json"); String externalSource = TestingUtilities.loadTestResource("tx", "messages-tx.fhir.org.json"); - externals = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(externalSource); + JsonObject externals = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(externalSource); Map examples = new HashMap(); - manifest = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(contents); + JsonObject manifest = org.hl7.fhir.utilities.json.parser.JsonParser.parseObject(contents); for (JsonObject suite : manifest.getJsonObjects("suites")) { if (!"tx.fhir.org".equals(suite.asString("mode"))) { String sn = suite.asString("name"); @@ -39,9 +45,11 @@ public class TxTestData { names.addAll(examples.keySet()); Collections.sort(names); - testData = new ArrayList(examples.size()); + List testData = new ArrayList(examples.size()); for (String id : names) { testData.add(new Object[]{id, examples.get(id)}); } + + return new TxTestData(testData, manifest, externals); } } 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 d16544e43..4704f72cb 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 @@ -59,7 +59,7 @@ private static TxTestData testData; @Parameters(name = "{index}: id {0}") public static Iterable data() throws IOException { - testData = new TxTestData(); + testData = TxTestData.loadTestDataFromDefaultClassPath(); return testData.getTestData(); } From 6032e64ffceaf9eeb98f7020c97a2309afcbb3e1 Mon Sep 17 00:00:00 2001 From: "dotasek.dev" Date: Fri, 22 Mar 2024 11:10:01 -0400 Subject: [PATCH 5/9] more renaming --- .../special/TxServiceTestHelper.java | 108 ++++++++++-------- 1 file changed, 58 insertions(+), 50 deletions(-) diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java index 4fedcb7c6..609d5b198 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java @@ -27,15 +27,15 @@ public class TxServiceTestHelper { public static String getDiffForValidation(IWorkerContext context, String name, Resource requestParameters, String expectedResponse, String lang, String fp, JsonObject externals, boolean isCodeSystem) throws JsonSyntaxException, FileNotFoundException, IOException { org.hl7.fhir.r5.model.Parameters p = (org.hl7.fhir.r5.model.Parameters) requestParameters; - ValueSet vs = null; - String vsurl = null; + ValueSet valueSet = null; + String valueSetUrl = null; if (!isCodeSystem) { if (p.hasParameter("valueSetVersion")) { - vsurl = p.getParameterValue("url").primitiveValue()+"|"+p.getParameterValue("valueSetVersion").primitiveValue(); - vs = context.fetchResource(ValueSet.class, p.getParameterValue("url").primitiveValue(), p.getParameterValue("valueSetVersion").primitiveValue()); + valueSetUrl = p.getParameterValue("url").primitiveValue()+"|"+p.getParameterValue("valueSetVersion").primitiveValue(); + valueSet = context.fetchResource(ValueSet.class, p.getParameterValue("url").primitiveValue(), p.getParameterValue("valueSetVersion").primitiveValue()); } else { - vsurl = p.getParameterValue("url").primitiveValue(); - vs = context.fetchResource(ValueSet.class, p.getParameterValue("url").primitiveValue()); + valueSetUrl = p.getParameterValue("url").primitiveValue(); + valueSet = context.fetchResource(ValueSet.class, p.getParameterValue("url").primitiveValue()); } } ValidationResult validationResult = null; @@ -43,16 +43,16 @@ public class TxServiceTestHelper { String system = null; String version = null; String display = null; - CodeableConcept cc = null; - org.hl7.fhir.r5.model.Parameters res = null; + CodeableConcept codeableConcept = null; + org.hl7.fhir.r5.model.Parameters parameters = null; OperationOutcome operationOutcome = null; - if (vs == null && vsurl != null) { - String msg = context.formatMessage(I18nConstants.UNABLE_TO_RESOLVE_VALUE_SET_, vsurl); + if (valueSet == null && valueSetUrl != null) { + String msg = context.formatMessage(I18nConstants.UNABLE_TO_RESOLVE_VALUE_SET_, valueSetUrl); operationOutcome = new OperationOutcome(); - CodeableConcept codeableConcept = operationOutcome.addIssue().setSeverity(OperationOutcome.IssueSeverity.ERROR).setCode(OperationOutcome.IssueType.NOTFOUND).getDetails(); - codeableConcept.addCoding("http://hl7.org/fhir/tools/CodeSystem/tx-issue-type", "not-found", null); - codeableConcept.setText(msg); + CodeableConcept codeableConceptWhenNullValueSet = operationOutcome.addIssue().setSeverity(OperationOutcome.IssueSeverity.ERROR).setCode(OperationOutcome.IssueType.NOTFOUND).getDetails(); + codeableConceptWhenNullValueSet.addCoding("http://hl7.org/fhir/tools/CodeSystem/tx-issue-type", "not-found", null); + codeableConceptWhenNullValueSet.setText(msg); } else { ValidationOptions options = new ValidationOptions(FhirPublication.R5); if (p.hasParameter("displayLanguage")) { @@ -82,17 +82,17 @@ public class TxServiceTestHelper { display = p.getParameterString("display"); validationResult = context.validateCode(options.withGuessSystem(), p.getParameterString(isCodeSystem ? "url" : "system"), p.getParameterString(isCodeSystem ? "version" : "systemVersion"), - p.getParameterString("code"), p.getParameterString("display"), vs); + p.getParameterString("code"), p.getParameterString("display"), valueSet); } else if (p.hasParameter("coding")) { Coding coding = (Coding) p.getParameterValue("coding"); code = coding.getCode(); system = coding.getSystem(); version = coding.getVersion(); display = coding.getDisplay(); - validationResult = context.validateCode(options, coding, vs); + validationResult = context.validateCode(options, coding, valueSet); } else if (p.hasParameter("codeableConcept")) { - cc = (CodeableConcept) p.getParameterValue("codeableConcept"); - validationResult = context.validateCode(options, cc, vs); + codeableConcept = (CodeableConcept) p.getParameterValue("codeableConcept"); + validationResult = context.validateCode(options, codeableConcept, valueSet); } else { throw new Error("validate not done yet for this steup"); } @@ -106,6 +106,9 @@ public class TxServiceTestHelper { TxTesterScrubbers.scrubOO(operationOutcome, false); String actualResponse = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(operationOutcome); + + dumparoo("/Users/david.otasek/IN/2024-02-05-hapi-core-bump-6-2.16/core-test", name, expectedResponse, actualResponse); + String diff = CompareUtilities.checkJsonSrcIsSame(expectedResponse, actualResponse, externals); if (diff != null) { Utilities.createDirectory(Utilities.getDirectoryForFile(fp)); @@ -114,76 +117,75 @@ public class TxServiceTestHelper { } return diff; } else { - if (res == null) { - res = new org.hl7.fhir.r5.model.Parameters(); + if (parameters == null) { + parameters = new org.hl7.fhir.r5.model.Parameters(); if (validationResult.getSystem() != null) { - res.addParameter("system", new UriType(validationResult.getSystem())); + parameters.addParameter("system", new UriType(validationResult.getSystem())); } else if (system != null) { - res.addParameter("system", new UriType(system)); + parameters.addParameter("system", new UriType(system)); } if (validationResult.getCode() != null) { if (code != null && !code.equals(validationResult.getCode())) { - res.addParameter("code", new CodeType(code)); - res.addParameter("normalized-code", new CodeType(validationResult.getCode())); + parameters.addParameter("code", new CodeType(code)); + parameters.addParameter("normalized-code", new CodeType(validationResult.getCode())); } else { - res.addParameter("code", new CodeType(validationResult.getCode())); + parameters.addParameter("code", new CodeType(validationResult.getCode())); } } else if (code != null) { - res.addParameter("code", new CodeType(code)); + parameters.addParameter("code", new CodeType(code)); } if (validationResult.getSeverity() == org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity.ERROR) { - res.addParameter("result", false); + parameters.addParameter("result", false); } else { - res.addParameter("result", true); + parameters.addParameter("result", true); } if (validationResult.getMessage() != null) { - res.addParameter("message", validationResult.getMessage()); + parameters.addParameter("message", validationResult.getMessage()); } if (validationResult.getVersion() != null) { - res.addParameter("version", validationResult.getVersion()); + parameters.addParameter("version", validationResult.getVersion()); } else if (version != null) { - res.addParameter("version", new StringType(version)); + parameters.addParameter("version", new StringType(version)); } if (validationResult.getDisplay() != null) { - res.addParameter("display", validationResult.getDisplay()); + parameters.addParameter("display", validationResult.getDisplay()); } else if (display != null) { - res.addParameter("display", new StringType(display)); + parameters.addParameter("display", new StringType(display)); } // if (vm.getCodeableConcept() != null) { // res.addParameter("codeableConcept", vm.getCodeableConcept()); // } else - if (cc != null) { - res.addParameter("codeableConcept", cc); + if (codeableConcept != null) { + parameters.addParameter("codeableConcept", codeableConcept); } if (validationResult.isInactive()) { - res.addParameter("inactive", true); + parameters.addParameter("inactive", true); } if (validationResult.getStatus() != null) { - res.addParameter("status", validationResult.getStatus()); + parameters.addParameter("status", validationResult.getStatus()); } if (validationResult.getUnknownSystems() != null) { for (String s : validationResult.getUnknownSystems()) { - res.addParameter(validationResult.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED ? "x-caused-by-unknown-system" : "x-unknown-system", new CanonicalType(s)); + parameters.addParameter(validationResult.getErrorClass() == TerminologyServiceErrorClass.CODESYSTEM_UNSUPPORTED ? "x-caused-by-unknown-system" : "x-unknown-system", new CanonicalType(s)); } } if (validationResult.getIssues().size() > 0) { operationOutcome = new OperationOutcome(); operationOutcome.getIssue().addAll(validationResult.getIssues()); - res.addParameter().setName("issues").setResource(operationOutcome); + parameters.addParameter().setName("issues").setResource(operationOutcome); } } - TxTesterSorters.sortParameters(res); - TxTesterScrubbers.scrubParams(res); - - String actualResponse = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(res); + TxTesterSorters.sortParameters(parameters); + TxTesterScrubbers.scrubParams(parameters); + String actualResponse = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(parameters); + dumparoo("/Users/david.otasek/IN/2024-02-05-hapi-core-bump-6-2.16/core-test", name, expectedResponse, actualResponse); String diff = CompareUtilities.checkJsonSrcIsSame(expectedResponse, actualResponse, externals); if (diff != null) { - dumparoo("/Users/david.otasek/IN/2024-02-05-hapi-core-bump-6-2.16/core-test", name, expectedResponse, actualResponse); - Utilities.createDirectory(Utilities.getDirectoryForFile(fp)); + Utilities.createDirectory(Utilities.getDirectoryForFile(fp)); TextFile.stringToFile(actualResponse, fp); System.out.println("Test "+name+"failed: "+diff); } @@ -192,13 +194,19 @@ public class TxServiceTestHelper { } public static void dumparoo(String rootDirectory, String testName, String expected, String actual) throws IOException { - String fullDirectory = rootDirectory + "/" + testName; - File directory = new File(fullDirectory); - if (!directory.exists()) { - directory.mkdirs(); + String fullExpected = rootDirectory + "/expected/"; + String fullActual = rootDirectory + "/actual/"; + File expectedDirectory = new File(fullExpected); + if (!expectedDirectory.exists()) { + expectedDirectory.mkdirs(); } -TextFile.stringToFile(expected, fullDirectory + "/expected.json"); - TextFile.stringToFile(actual, fullDirectory + "/actual.json"); + + File actualDirectory = new File(fullActual); + if (!actualDirectory.exists()) { + actualDirectory.mkdirs(); + } + TextFile.stringToFile(expected, fullExpected + testName + ".json"); + TextFile.stringToFile(actual, fullActual + testName + ".json"); } } From 2500f26290452b0b83269115baf7b756745c2d90 Mon Sep 17 00:00:00 2001 From: "dotasek.dev" Date: Thu, 4 Apr 2024 09:05:53 -0400 Subject: [PATCH 6/9] Fix compilation --- .../org/hl7/fhir/terminology/tests/TerminologyServiceTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ca6b5cf30..08fab1ea0 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 @@ -94,7 +94,7 @@ private static TxTestData testData; String fn = setup.getTest().has("response:tx.fhir.org") ? setup.getTest().asString("response:tx.fhir.org") : setup.getTest().asString("response"); String resp = TestingUtilities.loadTestResource("tx", fn); String fp = Utilities.path("[tmp]", "tx", fn); - JsonObject ext = externals == null ? null : externals.getJsonObject(fn); + JsonObject ext = testData.getExternals() == null ? null : testData.getExternals().getJsonObject(fn); File fo = ManagedFileAccess.file(fp); if (fo.exists()) { fo.delete(); From 81f26a80e26b22a51f3782b4b68d45249753d4f7 Mon Sep 17 00:00:00 2001 From: dotasek Date: Fri, 5 Apr 2024 09:59:04 -0400 Subject: [PATCH 7/9] Fix destination for diff output of TerminologyServiceTests --- .../validation/special/TxServiceTestHelper.java | 15 +++++++++------ .../tests/TerminologyServiceTests.java | 1 - 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java index 30011c2ea..b9ea0ffdb 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/special/TxServiceTestHelper.java @@ -1,7 +1,6 @@ package org.hl7.fhir.validation.special; import com.google.gson.JsonSyntaxException; -import lombok.Getter; import org.hl7.fhir.r5.formats.JsonParser; import org.hl7.fhir.r5.context.IWorkerContext; import org.hl7.fhir.r5.formats.IParser; @@ -9,7 +8,6 @@ import org.hl7.fhir.r5.model.*; import org.hl7.fhir.r5.terminologies.utilities.TerminologyServiceErrorClass; import org.hl7.fhir.r5.terminologies.utilities.ValidationResult; import org.hl7.fhir.r5.test.utils.CompareUtilities; -import org.hl7.fhir.r5.test.utils.TestingUtilities; import org.hl7.fhir.utilities.FhirPublication; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; @@ -20,7 +18,6 @@ import org.hl7.fhir.utilities.validation.ValidationOptions; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.*; public class TxServiceTestHelper { @@ -107,7 +104,9 @@ public class TxServiceTestHelper { String actualResponse = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(operationOutcome); - dumparoo("/Users/david.otasek/IN/2024-02-05-hapi-core-bump-6-2.16/core-test", name, expectedResponse, actualResponse); + + + writeDiffToFileSystem( name, expectedResponse, actualResponse); String diff = CompareUtilities.checkJsonSrcIsSame(id, expectedResponse, actualResponse, externals); if (diff != null) { @@ -181,7 +180,7 @@ public class TxServiceTestHelper { String actualResponse = new JsonParser().setOutputStyle(IParser.OutputStyle.PRETTY).composeString(parameters); - dumparoo("/Users/david.otasek/IN/2024-02-05-hapi-core-bump-6-2.16/core-test", name, expectedResponse, actualResponse); + writeDiffToFileSystem(name, expectedResponse, actualResponse); String diff = CompareUtilities.checkJsonSrcIsSame(id, expectedResponse, actualResponse, externals); if (diff != null) { @@ -193,7 +192,11 @@ public class TxServiceTestHelper { } } - public static void dumparoo(String rootDirectory, String testName, String expected, String actual) throws IOException { + public static void writeDiffToFileSystem(String testName, String expected, String actual) throws IOException { + String rootDirectory = System.getenv("TX_SERVICE_TEST_DIFF_TARGET"); + if (rootDirectory == null || rootDirectory.isEmpty()) { + return; + } String fullExpected = rootDirectory + "/expected/"; String fullActual = rootDirectory + "/actual/"; File expectedDirectory = new File(fullExpected); 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 08fab1ea0..6662d2a23 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 @@ -32,7 +32,6 @@ import org.hl7.fhir.utilities.FhirPublication; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; -import org.hl7.fhir.utilities.i18n.I18nConstants; import org.hl7.fhir.utilities.json.model.JsonObject; import org.hl7.fhir.validation.ValidationEngine; import org.hl7.fhir.validation.special.TxServiceTestHelper; From d26ce8e10d2688f64432639867278f85f06418ff Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Sat, 13 Apr 2024 05:03:33 +1000 Subject: [PATCH 8/9] fix for NPE --- .../org/hl7/fhir/validation/instance/InstanceValidator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/instance/InstanceValidator.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/instance/InstanceValidator.java index d4ecb2c94..af0311e58 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/instance/InstanceValidator.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/instance/InstanceValidator.java @@ -7488,7 +7488,7 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat // first case: the type value set is wrong for primitive special types for (OperationOutcomeIssueComponent iss : vr.getIssues()) { - if (iss.getDetails().getText().startsWith("Unable to resolve system - value set expansion has no matches for code 'http://hl7.org/fhirpath/System")) { + if (iss.hasDetails() && iss.getDetails().getText().startsWith("Unable to resolve system - value set expansion has no matches for code 'http://hl7.org/fhirpath/System")) { return new ValidationResult("http://hl7.org/fhirpath/System", null, null, null); } } From 59a749a8424ab785596bd556b4d81129cb5fca1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rk=20Czotter?= Date: Tue, 16 Apr 2024 02:24:03 +0200 Subject: [PATCH 9/9] build(utilities): make sure JUnit 4 and 5 dependencies are optional (#1596) Also removed duplicate managed versions of JUnit 4.13.2 and mockwebserver 4.11.0. Fixes #1561 --- org.hl7.fhir.utilities/pom.xml | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/org.hl7.fhir.utilities/pom.xml b/org.hl7.fhir.utilities/pom.xml index 24b9eeb5b..036fba65d 100644 --- a/org.hl7.fhir.utilities/pom.xml +++ b/org.hl7.fhir.utilities/pom.xml @@ -89,7 +89,6 @@ true - com.ibm.icu icu4j @@ -107,18 +106,21 @@ org.junit.jupiter junit-jupiter-api provided + true net.bytebuddy byte-buddy provided + true org.junit.jupiter junit-jupiter-engine provided + true @@ -126,12 +128,14 @@ junit-vintage-engine ${junit_jupiter_version} provided + true org.junit.jupiter junit-jupiter-params provided + true @@ -139,6 +143,7 @@ junit-platform-launcher ${junit_platform_launcher_version} provided + true @@ -146,13 +151,14 @@ fhir-test-cases ${validator_test_case_version} provided + true junit junit - 4.13.2 provided + true @@ -161,14 +167,13 @@ 3.1.0 test + com.squareup.okhttp3 mockwebserver - 4.11.0 test -