Search in sources :

Example 1 with TestResultType

use of org.hl7.fhir.r4b.test.FHIRPathTests.TestResultType 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("r5", input));
                    } else {
                        res = new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", 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.r5.formats.XmlParser) Element(org.w3c.dom.Element) Resource(org.hl7.fhir.r5.model.Resource) ArrayList(java.util.ArrayList) BooleanType(org.hl7.fhir.r5.model.BooleanType) Quantity(org.hl7.fhir.r5.model.Quantity) Base(org.hl7.fhir.r5.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.r5.model.ExpressionNode) PrimitiveType(org.hl7.fhir.r5.model.PrimitiveType) JsonParser(org.hl7.fhir.r5.formats.JsonParser) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with TestResultType

use of org.hl7.fhir.r4b.test.FHIRPathTests.TestResultType 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)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 NotImplementedException (org.apache.commons.lang3.NotImplementedException)2 UcumException (org.fhir.ucum.UcumException)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 Element (org.w3c.dom.Element)2 SAXException (org.xml.sax.SAXException)2 JsonParser (org.hl7.fhir.r4b.formats.JsonParser)1 XmlParser (org.hl7.fhir.r4b.formats.XmlParser)1 Base (org.hl7.fhir.r4b.model.Base)1 BooleanType (org.hl7.fhir.r4b.model.BooleanType)1 ExpressionNode (org.hl7.fhir.r4b.model.ExpressionNode)1 PrimitiveType (org.hl7.fhir.r4b.model.PrimitiveType)1 Quantity (org.hl7.fhir.r4b.model.Quantity)1 Resource (org.hl7.fhir.r4b.model.Resource)1