From fc7f71c1f60101f1f0b031bad72a68c7a5fa07ec Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Wed, 2 Sep 2020 16:34:34 +1000 Subject: [PATCH] rework rendering tests & fix bug in Parameters renderer --- .../fhir/r5/renderers/ParametersRenderer.java | 10 +++- .../fhir/r5/renderers/RendererFactory.java | 3 + .../fhir/r5/renderers/utils/BaseWrappers.java | 6 +- .../r5/test/NarrativeGenerationTests.java | 60 +++++++++++++++---- .../fhir/r5/test/ResourceRoundTripTests.java | 3 + 5 files changed, 65 insertions(+), 17 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ParametersRenderer.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ParametersRenderer.java index fc8e6436b..d6fcf528e 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ParametersRenderer.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/ParametersRenderer.java @@ -31,13 +31,19 @@ import org.hl7.fhir.utilities.xhtml.XhtmlNode; public class ParametersRenderer extends ResourceRenderer { + public ParametersRenderer(RenderingContext context) { + super(context); + } + public ParametersRenderer(RenderingContext context, ResourceContext rcontext) { super(context, rcontext); } - @Override public boolean render(XhtmlNode x, Resource r) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome { + x.h2().tx("Parameters"); + XhtmlNode tbl = x.table("grid"); + params(tbl, ((Parameters) r).getParameter(), 0); return false; } @@ -87,7 +93,7 @@ public class ParametersRenderer extends ResourceRenderer { } } else if (p.has("part")) { tr.td(); - PropertyWrapper pw = getProperty(p, "parameter"); + PropertyWrapper pw = getProperty(p, "part"); paramsW(tbl, pw.getValues(), 1); } } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/RendererFactory.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/RendererFactory.java index 9fb41cc12..5935729b4 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/RendererFactory.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/RendererFactory.java @@ -72,6 +72,9 @@ public class RendererFactory { if ("OperationOutcome".equals(resourceName)) { return new OperationOutcomeRenderer(context); } + if ("Parameters".equals(resourceName)) { + return new ParametersRenderer(context); + } return new ProfileDrivenRenderer(context); } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/BaseWrappers.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/BaseWrappers.java index b811ac7fe..005da8090 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/BaseWrappers.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/utils/BaseWrappers.java @@ -87,7 +87,7 @@ public class BaseWrappers { @Override public boolean has(String name) { for (PropertyWrapper p : children()) { - if (p.getName().equals(name)) { + if (p.getName().equals(name) || p.getName().equals(name+"[x]") ) { return p.hasValues(); } } @@ -97,7 +97,7 @@ public class BaseWrappers { @Override public Base get(String name) throws UnsupportedEncodingException, FHIRException, IOException { for (PropertyWrapper p : children()) { - if (p.getName().equals(name)) { + if (p.getName().equals(name) || p.getName().equals(name+"[x]")) { if (p.hasValues()) { return p.getValues().get(0).getBase(); } else { @@ -111,7 +111,7 @@ public class BaseWrappers { @Override public List children(String name) throws UnsupportedEncodingException, FHIRException, IOException { for (PropertyWrapper p : children()) { - if (p.getName().equals(name)) { + if (p.getName().equals(name) || p.getName().equals(name+"[x]")) { List res = new ArrayList<>(); for (BaseWrapper b : p.getValues()) { res.add(b); diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java index c320f912f..9011cd929 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java @@ -11,22 +11,32 @@ import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.SystemUtils; +import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.exceptions.FHIRFormatError; import org.hl7.fhir.r5.context.IWorkerContext; +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.formats.XmlParser; +import org.hl7.fhir.r5.model.Base; import org.hl7.fhir.r5.model.DomainResource; import org.hl7.fhir.r5.model.Questionnaire; +import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.renderers.RendererFactory; import org.hl7.fhir.r5.renderers.ResourceRenderer; +import org.hl7.fhir.r5.renderers.utils.ElementWrappers; import org.hl7.fhir.r5.renderers.utils.RenderingContext; +import org.hl7.fhir.r5.renderers.utils.RenderingContext.ITypeParser; import org.hl7.fhir.r5.renderers.utils.RenderingContext.QuestionnaireRendererMode; import org.hl7.fhir.r5.renderers.utils.RenderingContext.ResourceRendererMode; +import org.hl7.fhir.r5.test.NarrativeGenerationTests.TestTypeParser; import org.hl7.fhir.r5.test.utils.TestingUtilities; import org.hl7.fhir.utilities.TerminologyServiceOptions; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.xhtml.XhtmlComposer; +import org.hl7.fhir.utilities.xhtml.XhtmlNode; +import org.hl7.fhir.utilities.xhtml.XhtmlParser; import org.hl7.fhir.utilities.xml.XMLUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -40,6 +50,15 @@ import org.xml.sax.SAXException; public class NarrativeGenerationTests { + public class TestTypeParser implements ITypeParser { + + @Override + public Base parseType(String xml, String type) throws FHIRFormatError, IOException, FHIRException { + return new org.hl7.fhir.r5.formats.XmlParser().parseType(xml, type); + } + + } + public static final String WINDOWS = "WINDOWS"; private static final String HEADER = ""+ @@ -57,11 +76,13 @@ public class NarrativeGenerationTests { public static class TestDetails { private String id; private boolean header; + private boolean meta; public TestDetails(Element test) { super(); id = test.getAttribute("id"); header = "true".equals(test.getAttribute("header")); + meta = "true".equals(test.getAttribute("meta")); } public String getId() { @@ -70,6 +91,10 @@ public class NarrativeGenerationTests { public boolean isHeader() { return header; + } + + public boolean isMeta() { + return meta; } } @@ -107,19 +132,30 @@ public class NarrativeGenerationTests { rc.setHeader(test.isHeader()); rc.setDefinitionsTarget("test.html"); rc.setTerminologyServiceOptions(TerminologyServiceOptions.defaults()); - IOUtils.copy(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + "-expected.xml"), new FileOutputStream(TestingUtilities.tempFile("narrative", test.getId() + "-expected.xml"))); - DomainResource source; - if (TestingUtilities.findTestResource("r5", "narrative", test.getId() + "-input.json")) { - source = (DomainResource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + "-input.json")); + rc.setParser(new TestTypeParser()); + Resource source; + if (TestingUtilities.findTestResource("r5", "narrative", test.getId() + ".json")) { + source = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".json")); } else { - source = (DomainResource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + "-input.xml")); + source = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".xml")); + } + + XhtmlNode x = RendererFactory.factory(source, rc).build(source); + String target = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".html")); + String output = HEADER+new XhtmlComposer(true, true).compose(x)+FOOTER; + TextFile.stringToFile(target, TestingUtilities.tempFile("narrative", test.getId() + ".target.html")); + TextFile.stringToFile(output, TestingUtilities.tempFile("narrative", test.getId() + ".output.html")); + Assertions.assertTrue(output.equals(target), "Output does not match expected"); + + if (test.isMeta()) { + org.hl7.fhir.r5.elementmodel.Element e = Manager.parse(context, TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".xml"), FhirFormat.XML); + x = RendererFactory.factory(source, rc).render(new ElementWrappers.ResourceWrapperMetaElement(rc, e)); + + target = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + "-meta.html")); + output = HEADER+new XhtmlComposer(true).compose(x)+FOOTER; + TextFile.stringToFile(output, TestingUtilities.tempFile("narrative", test.getId() + "-meta.output.html")); + Assertions.assertTrue(output.equals(target), "Output does not match expected (meta)"); } - DomainResource target = (DomainResource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + "-expected.xml")); - RendererFactory.factory(source, rc).render(source); - new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.tempFile("narrative", test.getId() + "-actual.xml")), source); - source = (DomainResource) new XmlParser().parse(new FileInputStream(TestingUtilities.tempFile("narrative", test.getId() + "-actual.xml"))); - String html = HEADER+new XhtmlComposer(true).compose(source.getText().getDiv())+FOOTER; - TextFile.stringToFile(html, TestingUtilities.tempFile("narrative", test.getId() + ".html")); - Assertions.assertTrue(source.equalsDeep(target), "Output does not match expected"); } + } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/ResourceRoundTripTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/ResourceRoundTripTests.java index 6406fb930..ba462e423 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/ResourceRoundTripTests.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/ResourceRoundTripTests.java @@ -13,6 +13,7 @@ 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.Bundle; +import org.hl7.fhir.r5.model.DateTimeType; import org.hl7.fhir.r5.model.DomainResource; import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.renderers.RendererFactory; @@ -20,6 +21,7 @@ import org.hl7.fhir.r5.renderers.utils.RenderingContext; import org.hl7.fhir.r5.renderers.utils.RenderingContext.ResourceRendererMode; import org.hl7.fhir.r5.test.utils.TestingUtilities; import org.hl7.fhir.r5.utils.EOperationOutcome; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class ResourceRoundTripTests { @@ -52,4 +54,5 @@ public class ResourceRoundTripTests { if (result == null) throw new FHIRException("Bundle was null"); } + } \ No newline at end of file