Search in sources :

Example 86 with JsonParser

use of org.hl7.fhir.dstu2.formats.JsonParser in project org.hl7.fhir.core by hapifhir.

the class VocabTests method test.

@ParameterizedTest(name = "{index}: file {0}")
@MethodSource("data")
public void test(String id, TestDetails test) throws Exception {
    Resource source;
    if (test.getSource().endsWith(".json")) {
        source = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getSource()));
    } else {
        source = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getSource()));
    }
    Resource target;
    if (test.getTarget().endsWith(".json")) {
        target = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getTarget()));
    } else {
        target = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getTarget()));
    }
    if (test.getType() == TestType.Expansion) {
        testExpansion(test, (ValueSet) source, (ValueSet) target);
    } else {
        Assertions.fail("not done yet");
    }
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) Resource(org.hl7.fhir.r5.model.Resource) JsonParser(org.hl7.fhir.r5.formats.JsonParser) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 87 with JsonParser

use of org.hl7.fhir.dstu2.formats.JsonParser in project org.hl7.fhir.core by hapifhir.

the class ResourceTest method test.

public Resource test() throws FHIRFormatError, FileNotFoundException, IOException {
    IParser p;
    if (isJson())
        p = new JsonParser();
    else
        p = new XmlParser(false);
    Resource rf = p.parse(new FileInputStream(source));
    FileOutputStream out = new FileOutputStream(source.getAbsoluteFile() + ".out.json");
    JsonParser json1 = new JsonParser();
    json1.setOutputStyle(OutputStyle.PRETTY);
    json1.compose(out, rf);
    out.close();
    JsonParser json = new JsonParser();
    rf = json.parse(new FileInputStream(source.getAbsoluteFile() + ".out.json"));
    out = new FileOutputStream(source.getAbsoluteFile() + ".out.xml");
    XmlParser atom = new XmlParser();
    atom.setOutputStyle(OutputStyle.PRETTY);
    atom.compose(out, rf, true);
    out.close();
    return rf;
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.r5.model.Resource) FileInputStream(java.io.FileInputStream) IParser(org.hl7.fhir.r5.formats.IParser) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 88 with JsonParser

use of org.hl7.fhir.dstu2.formats.JsonParser in project org.hl7.fhir.core by hapifhir.

the class JsonDirectTests method test.

@Test
// Hard coded path here
@Disabled
public void test() throws FHIRFormatError, FileNotFoundException, IOException {
    File src = new File(Utilities.path("[tmp]", "obs.xml"));
    File xml = new File(Utilities.path("[tmp]", "xml.xml"));
    File json = new File(Utilities.path("[tmp]", "json.json"));
    File json2 = new File(Utilities.path("[tmp]", "json2.json"));
    FileUtils.copyFile(new File("C:\\work\\org.hl7.fhir\\build\\publish\\observation-decimal.xml"), src);
    Observation obs = (Observation) new XmlParser().parse(new FileInputStream(src));
    new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(json), obs);
    obs = (Observation) new JsonParser().parse(new FileInputStream(json));
    new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(json2), obs);
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(xml), obs);
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Observation(org.hl7.fhir.r5.model.Observation) File(java.io.File) FileInputStream(java.io.FileInputStream) JsonParser(org.hl7.fhir.r5.formats.JsonParser) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 89 with JsonParser

use of org.hl7.fhir.dstu2.formats.JsonParser in project org.hl7.fhir.core by hapifhir.

the class NarrativeGenerationTests method test.

@ParameterizedTest(name = "{index}: file {0}")
@MethodSource("data")
public void test(String id, TestDetails test) throws Exception {
    RenderingContext rc = new RenderingContext(context, null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
    rc.setDestDir("");
    rc.setHeader(test.isHeader());
    rc.setDefinitionsTarget("test.html");
    rc.setTerminologyServiceOptions(TerminologyServiceOptions.defaults());
    rc.setParser(new TestTypeParser());
    // getting timezones correct (well, at least consistent, so tests pass on any computer)
    rc.setLocale(new java.util.Locale("en", "AU"));
    rc.setTimeZoneId(ZoneId.of("Australia/Sydney"));
    rc.setDateTimeFormatString("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
    rc.setDateFormatString("yyyy-MM-dd");
    rc.setMode(test.technical ? ResourceRendererMode.TECHNICAL : ResourceRendererMode.END_USER);
    Resource source;
    if (TestingUtilities.findTestResource("r5", "narrative", test.getId() + ".json")) {
        source = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".json"));
    } else {
        source = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".xml"));
    }
    XhtmlNode x = RendererFactory.factory(source, rc).build(source);
    String expected = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".html"));
    String actual = HEADER + new XhtmlComposer(true, true).compose(x) + FOOTER;
    String expectedFileName = CompareUtilities.tempFile("narrative", test.getId() + ".expected.html");
    String actualFileName = CompareUtilities.tempFile("narrative", test.getId() + ".actual.html");
    TextFile.stringToFile(expected, expectedFileName);
    TextFile.stringToFile(actual, actualFileName);
    String msg = CompareUtilities.checkXMLIsSame(expectedFileName, actualFileName);
    Assertions.assertTrue(msg == null, "Output does not match expected: " + msg);
    if (test.isMeta()) {
        org.hl7.fhir.r5.elementmodel.Element e = Manager.parseSingle(context, TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".xml"), FhirFormat.XML);
        x = RendererFactory.factory(source, rc).render(new ElementWrappers.ResourceWrapperMetaElement(rc, e));
        expected = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + "-meta.html"));
        actual = HEADER + new XhtmlComposer(true, true).compose(x) + FOOTER;
        actualFileName = CompareUtilities.tempFile("narrative", test.getId() + "-meta.actual.html");
        TextFile.stringToFile(actual, actualFileName);
        msg = CompareUtilities.checkXMLIsSame(expectedFileName, actualFileName);
        Assertions.assertTrue(msg == null, "Meta output does not match expected: " + msg);
    }
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r5.formats.XmlParser) Resource(org.hl7.fhir.r5.model.Resource) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) JsonParser(org.hl7.fhir.r5.formats.JsonParser) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 90 with JsonParser

use of org.hl7.fhir.dstu2.formats.JsonParser in project org.hl7.fhir.core by hapifhir.

the class BaseValidator method loadFoundResource.

protected Resource loadFoundResource(List<ValidationMessage> errors, String path, Element resource, Class<? extends Resource> class1) throws FHIRException {
    try {
        FhirPublication v = FhirPublication.fromCode(context.getVersion());
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        new JsonParser(context).compose(resource, bs, OutputStyle.NORMAL, resource.getIdBase());
        byte[] json = bs.toByteArray();
        Resource r5 = null;
        switch(v) {
            case DSTU1:
                rule(errors, IssueType.INVALID, resource.line(), resource.col(), path, false, I18nConstants.UNSUPPORTED_VERSION_R1, resource.getIdBase());
                // this can't happen
                return null;
            case DSTU2:
                org.hl7.fhir.dstu2.model.Resource r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(json);
                r5 = VersionConvertorFactory_10_50.convertResource(r2);
                break;
            case DSTU2016May:
                org.hl7.fhir.dstu2016may.model.Resource r2a = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(json);
                r5 = VersionConvertorFactory_14_50.convertResource(r2a);
                break;
            case STU3:
                org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(json);
                r5 = VersionConvertorFactory_30_50.convertResource(r3);
                break;
            case R4:
                org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(json);
                r5 = VersionConvertorFactory_40_50.convertResource(r4);
                break;
            case R5:
                r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(json);
                break;
            default:
                // this can't happen
                return null;
        }
        if (class1.isInstance(r5))
            return (Resource) r5;
        else {
            rule(errors, IssueType.INVALID, resource.line(), resource.col(), path, false, I18nConstants.REFERENCE_REF_WRONGTARGET_LOAD, resource.getIdBase(), class1.toString(), r5.fhirType());
            return null;
        }
    } catch (IOException e) {
        throw new FHIRException(e);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FhirPublication(org.hl7.fhir.utilities.FhirPublication) org.hl7.fhir.r5.model(org.hl7.fhir.r5.model) JsonParser(org.hl7.fhir.r5.elementmodel.JsonParser)

Aggregations

FileOutputStream (java.io.FileOutputStream)82 JsonParser (org.hl7.fhir.r5.formats.JsonParser)66 FHIRException (org.hl7.fhir.exceptions.FHIRException)58 FileInputStream (java.io.FileInputStream)53 IOException (java.io.IOException)49 XmlParser (org.hl7.fhir.r5.formats.XmlParser)44 ByteArrayOutputStream (java.io.ByteArrayOutputStream)41 File (java.io.File)36 JsonParser (org.hl7.fhir.r4.formats.JsonParser)35 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)29 JsonParser (org.hl7.fhir.dstu3.formats.JsonParser)26 JsonParser (org.hl7.fhir.r4b.formats.JsonParser)26 CSFile (org.hl7.fhir.utilities.CSFile)26 ArrayList (java.util.ArrayList)23 TextFile (org.hl7.fhir.utilities.TextFile)20 InputStream (java.io.InputStream)18 IParser (org.hl7.fhir.r5.formats.IParser)18 Resource (org.hl7.fhir.dstu3.model.Resource)17 FileNotFoundException (java.io.FileNotFoundException)16 Resource (org.hl7.fhir.r4.model.Resource)16