Search in sources :

Example 71 with PrimitiveType

use of org.hl7.fhir.dstu3.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.

the class FHIRPathTests method test.

@SuppressWarnings("deprecation")
@ParameterizedTest(name = "{index}: file {0}")
@MethodSource("data")
public void test(String name, Element test) throws FileNotFoundException, IOException, FHIRException, org.hl7.fhir.exceptions.FHIRException, UcumException {
    // Setting timezone for this test. Grahame is in UTC+11, Travis is in GMT, and I'm here in Toronto, Canada with
    // all my time based tests failing locally...
    TimeZone.setDefault(TimeZone.getTimeZone("UTC+1100"));
    fp.setHostServices(new FHIRPathTestEvaluationServices());
    String input = test.getAttribute("inputfile");
    String expression = XMLUtil.getNamedChild(test, "expression").getTextContent();
    TestResultType fail = TestResultType.OK;
    if ("syntax".equals(XMLUtil.getNamedChild(test, "expression").getAttribute("invalid"))) {
        fail = TestResultType.SYNTAX;
    } else if ("semantic".equals(XMLUtil.getNamedChild(test, "expression").getAttribute("invalid"))) {
        fail = TestResultType.SEMANTICS;
    } else if ("execution".equals(XMLUtil.getNamedChild(test, "expression").getAttribute("invalid"))) {
        fail = TestResultType.EXECUTION;
    }
    ;
    fp.setAllowPolymorphicNames("lenient/polymorphics".equals(test.getAttribute("mode")));
    Resource res = null;
    List<Base> outcome = new ArrayList<Base>();
    System.out.println(name);
    ExpressionNode node = null;
    try {
        node = fp.parse(expression);
        Assertions.assertTrue(fail != TestResultType.SYNTAX, String.format("Expected exception didn't occur parsing %s", expression));
    } catch (Exception e) {
        System.out.println("Parsing Error: " + e.getMessage());
        Assertions.assertTrue(fail == TestResultType.SYNTAX, String.format("Unexpected exception parsing %s: " + e.getMessage(), expression));
    }
    if (node != null) {
        try {
            if (Utilities.noString(input)) {
                fp.check(null, null, node);
            } else {
                res = resources.get(input);
                if (res == null) {
                    if (input.endsWith(".json")) {
                        res = new JsonParser().parse(TestingUtilities.loadTestResourceStream("r4b", input));
                    } else {
                        res = new XmlParser().parse(TestingUtilities.loadTestResourceStream("r4b", input));
                    }
                    resources.put(input, res);
                }
                fp.check(res, res.getResourceType().toString(), res.getResourceType().toString(), node);
            }
            Assertions.assertTrue(fail != TestResultType.SEMANTICS, String.format("Expected exception didn't occur checking %s", expression));
        } catch (Exception e) {
            System.out.println("Checking Error: " + e.getMessage());
            Assertions.assertTrue(fail == TestResultType.SEMANTICS, String.format("Unexpected exception checking %s: " + e.getMessage(), expression));
            node = null;
        }
    }
    if (node != null) {
        try {
            outcome = fp.evaluate(res, node);
            Assertions.assertTrue(fail == TestResultType.OK, String.format("Expected exception didn't occur executing %s", expression));
        } catch (Exception e) {
            System.out.println("Execution Error: " + e.getMessage());
            Assertions.assertTrue(fail == TestResultType.EXECUTION, String.format("Unexpected exception executing %s: " + e.getMessage(), expression));
            node = null;
        }
    }
    if (fp.hasLog()) {
        System.out.println(name);
        System.out.println(fp.takeLog());
    }
    if (node != null) {
        if ("true".equals(test.getAttribute("predicate"))) {
            boolean ok = fp.convertToBoolean(outcome);
            outcome.clear();
            outcome.add(new BooleanType(ok));
        }
        List<Element> expected = new ArrayList<Element>();
        XMLUtil.getNamedChildren(test, "output", expected);
        Assertions.assertEquals(outcome.size(), expected.size(), String.format("Expected %d objects but found %d for expression %s", expected.size(), outcome.size(), expression));
        if ("false".equals(test.getAttribute("ordered"))) {
            for (int i = 0; i < Math.min(outcome.size(), expected.size()); i++) {
                String tn = outcome.get(i).fhirType();
                String s;
                if (outcome.get(i) instanceof Quantity) {
                    s = fp.convertToString(outcome.get(i));
                } else {
                    s = ((PrimitiveType) outcome.get(i)).asStringValue();
                }
                boolean found = false;
                for (Element e : expected) {
                    if ((Utilities.noString(e.getAttribute("type")) || e.getAttribute("type").equals(tn)) && (Utilities.noString(e.getTextContent()) || e.getTextContent().equals(s))) {
                        found = true;
                    }
                }
                Assertions.assertTrue(found, String.format("Outcome %d: Value %s of type %s not expected for %s", i, s, tn, expression));
            }
        } else {
            for (int i = 0; i < Math.min(outcome.size(), expected.size()); i++) {
                String tn = expected.get(i).getAttribute("type");
                if (!Utilities.noString(tn)) {
                    Assertions.assertEquals(tn, outcome.get(i).fhirType(), String.format("Outcome %d: Type should be %s but was %s", i, tn, outcome.get(i).fhirType()));
                }
                String v = expected.get(i).getTextContent();
                if (!Utilities.noString(v)) {
                    if (outcome.get(i) instanceof Quantity) {
                        Quantity q = fp.parseQuantityString(v);
                        Assertions.assertTrue(outcome.get(i).equalsDeep(q), String.format("Outcome %d: Value should be %s but was %s", i, v, outcome.get(i).toString()));
                    } else {
                        Assertions.assertTrue(outcome.get(i) instanceof PrimitiveType, String.format("Outcome %d: Value should be a primitive type but was %s", i, outcome.get(i).fhirType()));
                        if (!(v.equals(((PrimitiveType) outcome.get(i)).fpValue()))) {
                            System.out.println(name);
                            System.out.println(String.format("Outcome %d: Value should be %s but was %s for expression %s", i, v, ((PrimitiveType) outcome.get(i)).fpValue(), expression));
                        }
                        Assertions.assertEquals(v, ((PrimitiveType) outcome.get(i)).fpValue(), String.format("Outcome %d: Value should be %s but was %s for expression %s", i, v, ((PrimitiveType) outcome.get(i)).fpValue(), expression));
                    }
                }
            }
        }
    }
}
Also used : XmlParser(org.hl7.fhir.r4b.formats.XmlParser) Element(org.w3c.dom.Element) Resource(org.hl7.fhir.r4b.model.Resource) ArrayList(java.util.ArrayList) BooleanType(org.hl7.fhir.r4b.model.BooleanType) Quantity(org.hl7.fhir.r4b.model.Quantity) TestResultType(org.hl7.fhir.r4b.test.FHIRPathTests.TestResultType) Base(org.hl7.fhir.r4b.model.Base) NotImplementedException(org.apache.commons.lang3.NotImplementedException) UcumException(org.fhir.ucum.UcumException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) FHIRException(org.hl7.fhir.exceptions.FHIRException) ExpressionNode(org.hl7.fhir.r4b.model.ExpressionNode) PrimitiveType(org.hl7.fhir.r4b.model.PrimitiveType) JsonParser(org.hl7.fhir.r4b.formats.JsonParser) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 72 with PrimitiveType

use of org.hl7.fhir.dstu3.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.

the class NarrativeGenerator method displayLeaf.

private boolean displayLeaf(ResourceWrapper res, BaseWrapper ew, ElementDefinition defn, XhtmlNode x, String name, boolean showCodeDetails) throws FHIRException, UnsupportedEncodingException, IOException {
    if (ew == null)
        return false;
    Base e = ew.getBase();
    Map<String, String> displayHints = readDisplayHints(defn);
    if (name.endsWith("[x]"))
        name = name.substring(0, name.length() - 3);
    if (!showCodeDetails && e instanceof PrimitiveType && isDefault(displayHints, ((PrimitiveType) e)))
        return false;
    if (e instanceof StringType) {
        x.addText(name + ": " + ((StringType) e).getValue());
        return true;
    } else if (e instanceof CodeType) {
        x.addText(name + ": " + ((CodeType) e).getValue());
        return true;
    } else if (e instanceof IdType) {
        x.addText(name + ": " + ((IdType) e).getValue());
        return true;
    } else if (e instanceof DateTimeType) {
        x.addText(name + ": " + ((DateTimeType) e).toHumanDisplay());
        return true;
    } else if (e instanceof InstantType) {
        x.addText(name + ": " + ((InstantType) e).toHumanDisplay());
        return true;
    } else if (e instanceof Extension) {
        x.addText("Extensions: todo");
        return true;
    } else if (e instanceof org.hl7.fhir.dstu2016may.model.DateType) {
        x.addText(name + ": " + ((org.hl7.fhir.dstu2016may.model.DateType) e).toHumanDisplay());
        return true;
    } else if (e instanceof Enumeration) {
        // todo: look up a display name if there is one
        x.addText(((Enumeration<?>) e).getValue().toString());
        return true;
    } else if (e instanceof BooleanType) {
        if (((BooleanType) e).getValue()) {
            x.addText(name);
            return true;
        }
    } else if (e instanceof CodeableConcept) {
        renderCodeableConcept((CodeableConcept) e, x, showCodeDetails);
        return true;
    } else if (e instanceof Coding) {
        renderCoding((Coding) e, x, showCodeDetails);
        return true;
    } else if (e instanceof Annotation) {
        renderAnnotation((Annotation) e, x, showCodeDetails);
        return true;
    } else if (e instanceof org.hl7.fhir.dstu2016may.model.IntegerType) {
        x.addText(Integer.toString(((org.hl7.fhir.dstu2016may.model.IntegerType) e).getValue()));
        return true;
    } else if (e instanceof org.hl7.fhir.dstu2016may.model.DecimalType) {
        x.addText(((org.hl7.fhir.dstu2016may.model.DecimalType) e).getValue().toString());
        return true;
    } else if (e instanceof Identifier) {
        renderIdentifier((Identifier) e, x);
        return true;
    } else if (e instanceof HumanName) {
        renderHumanName((HumanName) e, x);
        return true;
    } else if (e instanceof SampledData) {
        renderSampledData((SampledData) e, x);
        return true;
    } else if (e instanceof Address) {
        renderAddress((Address) e, x);
        return true;
    } else if (e instanceof ContactPoint) {
        renderContactPoint((ContactPoint) e, x);
        return true;
    } else if (e instanceof Timing) {
        renderTiming((Timing) e, x);
        return true;
    } else if (e instanceof Quantity) {
        renderQuantity((Quantity) e, x, showCodeDetails);
        return true;
    } else if (e instanceof Ratio) {
        renderQuantity(((Ratio) e).getNumerator(), x, showCodeDetails);
        x.addText("/");
        renderQuantity(((Ratio) e).getDenominator(), x, showCodeDetails);
        return true;
    } else if (e instanceof Period) {
        Period p = (Period) e;
        x.addText(name + ": ");
        x.addText(!p.hasStart() ? "??" : p.getStartElement().toHumanDisplay());
        x.addText(" --> ");
        x.addText(!p.hasEnd() ? "(ongoing)" : p.getEndElement().toHumanDisplay());
        return true;
    } else if (e instanceof Reference) {
        Reference r = (Reference) e;
        if (r.hasDisplayElement())
            x.addText(r.getDisplay());
        else if (r.hasReferenceElement()) {
            ResourceWithReference tr = resolveReference(res, r.getReference());
            // getDisplayForReference(tr.getReference()));
            x.addText(tr == null ? r.getReference() : "????");
        } else
            x.addText("??");
        return true;
    } else if (e instanceof Narrative) {
        return false;
    } else if (e instanceof Resource) {
        return false;
    } else if (!(e instanceof Attachment))
        throw new NotImplementedException("type " + e.getClass().getName() + " not handled yet");
    return false;
}
Also used : Address(org.hl7.fhir.dstu2016may.model.Address) StringType(org.hl7.fhir.dstu2016may.model.StringType) NotImplementedException(org.apache.commons.lang3.NotImplementedException) Attachment(org.hl7.fhir.dstu2016may.model.Attachment) HumanName(org.hl7.fhir.dstu2016may.model.HumanName) ContactPoint(org.hl7.fhir.dstu2016may.model.ContactPoint) Identifier(org.hl7.fhir.dstu2016may.model.Identifier) Coding(org.hl7.fhir.dstu2016may.model.Coding) Narrative(org.hl7.fhir.dstu2016may.model.Narrative) SampledData(org.hl7.fhir.dstu2016may.model.SampledData) PrimitiveType(org.hl7.fhir.dstu2016may.model.PrimitiveType) Ratio(org.hl7.fhir.dstu2016may.model.Ratio) InstantType(org.hl7.fhir.dstu2016may.model.InstantType) Enumeration(org.hl7.fhir.dstu2016may.model.Enumeration) Reference(org.hl7.fhir.dstu2016may.model.Reference) BooleanType(org.hl7.fhir.dstu2016may.model.BooleanType) Resource(org.hl7.fhir.dstu2016may.model.Resource) DomainResource(org.hl7.fhir.dstu2016may.model.DomainResource) Quantity(org.hl7.fhir.dstu2016may.model.Quantity) Period(org.hl7.fhir.dstu2016may.model.Period) Base(org.hl7.fhir.dstu2016may.model.Base) Annotation(org.hl7.fhir.dstu2016may.model.Annotation) IdType(org.hl7.fhir.dstu2016may.model.IdType) Extension(org.hl7.fhir.dstu2016may.model.Extension) DateTimeType(org.hl7.fhir.dstu2016may.model.DateTimeType) CodeType(org.hl7.fhir.dstu2016may.model.CodeType) EventTiming(org.hl7.fhir.dstu2016may.model.Timing.EventTiming) Timing(org.hl7.fhir.dstu2016may.model.Timing) CodeableConcept(org.hl7.fhir.dstu2016may.model.CodeableConcept)

Example 73 with PrimitiveType

use of org.hl7.fhir.dstu3.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.

the class DataRenderer method renderExpression.

private boolean renderExpression(CommaSeparatedStringBuilder c, PrimitiveType p) {
    Extension exp = p.getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/cqf-expression");
    if (exp == null) {
        return false;
    }
    c.append(exp.getValueExpression().getExpression());
    return true;
}
Also used : Extension(org.hl7.fhir.r4b.model.Extension)

Example 74 with PrimitiveType

use of org.hl7.fhir.dstu3.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.

the class ObjectConverter method convertElement.

private Element convertElement(Property property, Base base) throws FHIRException {
    if (base == null)
        return null;
    String tn = base.fhirType();
    StructureDefinition sd = context.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(tn, context.getOverrideVersionNs()));
    if (sd == null)
        throw new FHIRException("Unable to find definition for type " + tn);
    Element res = new Element(property.getName(), property);
    if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE)
        res.setValue(((PrimitiveType) base).asStringValue());
    List<ElementDefinition> children = profileUtilities.getChildMap(sd, sd.getSnapshot().getElementFirstRep());
    for (ElementDefinition child : children) {
        String n = tail(child.getPath());
        if (sd.getKind() != StructureDefinitionKind.PRIMITIVETYPE || !"value".equals(n)) {
            Base[] values = base.getProperty(n.hashCode(), n, false);
            if (values != null)
                for (Base value : values) {
                    res.getChildren().add(convertElement(new Property(context, child, sd), value));
                }
        }
    }
    return res;
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) PrimitiveType(org.hl7.fhir.r5.model.PrimitiveType) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException) Base(org.hl7.fhir.r5.model.Base)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)18 EqualsBuilder (org.apache.commons.lang3.builder.EqualsBuilder)12 IPrimitiveType (org.hl7.fhir.instance.model.api.IPrimitiveType)12 IOException (java.io.IOException)10 NotImplementedException (org.apache.commons.lang3.NotImplementedException)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 PrimitiveType (org.hl7.fhir.r5.model.PrimitiveType)8 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)8 URISyntaxException (java.net.URISyntaxException)6 PrimitiveType (org.hl7.fhir.definitions.model.PrimitiveType)6 FileNotFoundException (java.io.FileNotFoundException)5 ArrayList (java.util.ArrayList)5 PrimitiveType (org.hl7.fhir.dstu3.model.PrimitiveType)5 PrimitiveType (org.hl7.fhir.r4b.model.PrimitiveType)5 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)5 URI (java.net.URI)4 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)4 StringType (org.hl7.fhir.dstu3.model.StringType)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3