From e0d2b2a00df6874b2136ddac92ad937470c846d8 Mon Sep 17 00:00:00 2001 From: Oliver Egger Date: Thu, 4 Jun 2020 23:56:41 +0200 Subject: [PATCH] refactor to ValidationEngine/ElementModel --- .../r5/test/FHIRMappingLanguageTests.java | 128 ++++++------------ 1 file changed, 44 insertions(+), 84 deletions(-) diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/r5/test/FHIRMappingLanguageTests.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/r5/test/FHIRMappingLanguageTests.java index a39648b17..d9e3fe208 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/r5/test/FHIRMappingLanguageTests.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/r5/test/FHIRMappingLanguageTests.java @@ -1,41 +1,28 @@ package org.hl7.fhir.r5.test; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; import javax.xml.parsers.ParserConfigurationException; -import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.r5.context.SimpleWorkerContext; -import org.hl7.fhir.r5.elementmodel.Manager; import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat; -import org.hl7.fhir.r5.formats.IParser.OutputStyle; -import org.hl7.fhir.r5.formats.JsonParser; -import org.hl7.fhir.r5.model.Base; -import org.hl7.fhir.r5.model.Coding; +import org.hl7.fhir.r5.formats.IParser; import org.hl7.fhir.r5.model.Resource; -import org.hl7.fhir.r5.model.ResourceFactory; import org.hl7.fhir.r5.model.StructureDefinition; -import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind; import org.hl7.fhir.r5.model.StructureMap; -import org.hl7.fhir.r5.terminologies.ConceptMapEngine; import org.hl7.fhir.r5.test.utils.TestingUtilities; -import org.hl7.fhir.r5.utils.StructureMapUtilities; -import org.hl7.fhir.r5.utils.StructureMapUtilities.ITransformerServices; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; -import org.hl7.fhir.utilities.cache.FilesystemPackageCacheManager; -import org.hl7.fhir.utilities.cache.ToolsVersion; import org.hl7.fhir.utilities.xml.XMLUtil; +import org.hl7.fhir.validation.ValidationEngine; import org.hl7.fhir.validation.instance.InstanceValidatorFactory; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.params.ParameterizedTest; @@ -45,12 +32,13 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; -public class FHIRMappingLanguageTests implements ITransformerServices { +public class FHIRMappingLanguageTests { private List outputs = new ArrayList(); + static private ValidationEngine validationEngine; + static private SimpleWorkerContext context; - static private JsonParser jsonParser; public static Stream data() throws FileNotFoundException, IOException, ParserConfigurationException, SAXException { @@ -67,91 +55,63 @@ public class FHIRMappingLanguageTests implements ITransformerServices { @BeforeAll public static void setUp() throws Exception { - FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION); - context = SimpleWorkerContext.fromPackage(pcm.loadPackage("hl7.fhir.r5.core", "current")); + validationEngine = new ValidationEngine("hl7.fhir.r4.core#4.0.1"); + context = validationEngine.getContext(); + context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "any.xml"), "any.xml", null); + context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "ii.xml"), "ii.xml", null); + context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "cd.xml"), "cd.xml", null); + context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "ce.xml"), "ce.xml", null); + context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "ed.xml"), "ed.xml", null); + context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "st.xml"), "st.xml", null); + context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "cda.xml"), "cda.xml", null); + for (StructureDefinition sd : context.getStructures()) { + if (!sd.hasSnapshot()) { + System.out.println("generate snapshot for " + sd.getUrl()); + context.generateSnapshot(sd, true); + } + } if (context.getValidatorFactory() == null) { context.setValidatorFactory(new InstanceValidatorFactory()); } - jsonParser = new JsonParser(); - jsonParser.setOutputStyle(OutputStyle.PRETTY); } @ParameterizedTest(name = "{index}: {0}") @MethodSource("data") public void test(String name, String source, String map, String output) throws Exception { - InputStream fileSource = TestingUtilities.loadTestResourceStream("r5", "fml", source); - InputStream fileMap = TestingUtilities.loadTestResourceStream("r5", "fml", map); + byte[] byteSource = TestingUtilities.loadTestResourceBytes("r5", "fml", source); + String stringMap = TestingUtilities.loadTestResource("r5", "fml", map); String outputJson = TestingUtilities.loadTestResource("r5", "fml", output); String fileOutputRes = TestingUtilities.tempFile("fml", output) + ".out"; - + String fileOutputResOrig = TestingUtilities.tempFile("fml", output) + ".orig.out"; + ByteArrayOutputStream s = null; outputs.clear(); - boolean ok = false; String msg = null; - Resource resource = null; try { - StructureMapUtilities scu = new StructureMapUtilities(context, this); - org.hl7.fhir.r5.elementmodel.Element src = Manager.parse(context, - new ByteArrayInputStream(TextFile.streamToBytes(fileSource)), FhirFormat.JSON); - StructureMap structureMap = scu.parse(TextFile.streamToString(fileMap), name); - String typeName = scu.getTargetType(structureMap).getType(); - resource = ResourceFactory.createResource(typeName); - scu.transform(null, src, structureMap, resource); - ok = true; + StructureMap r = new org.hl7.fhir.r5.utils.StructureMapUtilities(context).parse(stringMap, map); + context.cacheResource(r); + org.hl7.fhir.r5.elementmodel.Element element = validationEngine.transform(byteSource, FhirFormat.JSON, r.getUrl()); + s = new ByteArrayOutputStream(); + if (output.endsWith(".json")) + new org.hl7.fhir.r5.elementmodel.JsonParser(context).compose(element, s, IParser.OutputStyle.PRETTY, null); + else + new org.hl7.fhir.r5.elementmodel.XmlParser(context).compose(element, s, IParser.OutputStyle.PRETTY, null); + context.dropResource(r); } catch (Exception e) { - ok = false; - msg = e.getMessage(); + assertTrue(e.getMessage(), false); } - if (ok) { - ByteArrayOutputStream boas = new ByteArrayOutputStream(); - jsonParser.compose(boas, resource); - String result = boas.toString(); - log(result); - TextFile.bytesToFile(boas.toByteArray(), fileOutputRes); - msg = TestingUtilities.checkJsonSrcIsSame(result, outputJson); - assertTrue(msg, Utilities.noString(msg)); - } else - assertEquals("Error, but proper output was expected (" + msg + ")", "$error", output); - } - - @Override - public void log(String message) { - System.out.println(message); - } - - @Override - public Base createType(Object appInfo, String name) throws FHIRException { - StructureDefinition sd = context.fetchResource(StructureDefinition.class, name); - if (sd != null && sd.getKind() == StructureDefinitionKind.LOGICAL) { - return Manager.build(context, sd); + if (output.endsWith("json")) { + msg = TestingUtilities.checkJsonSrcIsSame(s.toString(), outputJson); } else { - if (name.startsWith("http://hl7.org/fhir/StructureDefinition/")) - name = name.substring("http://hl7.org/fhir/StructureDefinition/".length()); - return ResourceFactory.createResourceOrType(name); + TextFile.bytesToFile(s.toByteArray(), fileOutputRes); + TextFile.bytesToFile(outputJson.getBytes(), fileOutputResOrig); + msg = TestingUtilities.checkXMLIsSame(new FileInputStream(fileOutputRes), new FileInputStream(fileOutputResOrig)); } + if (!Utilities.noString(msg)) { + System.out.print(s.toString()); + } + assertTrue(msg, Utilities.noString(msg)); } - @Override - public Base createResource(Object appInfo, Base res, boolean atRootofTransform) { - if (atRootofTransform) - outputs.add((Resource) res); - return res; - } - - @Override - public Coding translate(Object appInfo, Coding source, String conceptMapUrl) throws FHIRException { - ConceptMapEngine cme = new ConceptMapEngine(context); - return cme.translate(source, conceptMapUrl); - } - - @Override - public Base resolveReference(Object appContext, String url) throws FHIRException { - throw new FHIRException("resolveReference is not supported yet"); - } - - @Override - public List performSearch(Object appContext, String url) throws FHIRException { - throw new FHIRException("performSearch is not supported yet"); - } } \ No newline at end of file