Search in sources :

Example 1 with CacheVerificationLogger

use of org.hl7.fhir.utilities.tests.CacheVerificationLogger in project org.hl7.fhir.core by hapifhir.

the class ValidationEngineTests method test140.

@Test
public void test140() throws Exception {
    if (inbuild) {
        Assertions.assertTrue(true);
        return;
    }
    if (!TestUtilities.silent)
        System.out.println("Test140: Validate patient-example.xml in v1.4.0 version");
    ValidationEngine ve = TestUtilities.getValidationEngine("hl7.fhir.r2b.core#1.4.0", DEF_TX, FhirPublication.DSTU2016May, "1.4.0");
    CacheVerificationLogger logger = new CacheVerificationLogger();
    ve.getContext().getTxClient().setLogger(logger);
    OperationOutcome op = ve.validate(FhirFormat.XML, TestingUtilities.loadTestResourceStream("validator", "patient140.xml"), null);
    if (!TestUtilities.silent)
        for (OperationOutcomeIssueComponent iss : op.getIssue()) {
            System.out.println("    " + iss.getDetails().getText());
        }
    int e = errors(op);
    int w = warnings(op);
    int h = hints(op);
    Assertions.assertEquals(1, e);
    Assertions.assertEquals(0, w);
    Assertions.assertEquals(0, h);
    assertTrue(logger.verifyHasNoRequests(), "Unexpected request to TX server");
    if (!TestUtilities.silent)
        System.out.println("  .. done: " + Integer.toString(e) + " errors, " + Integer.toString(w) + " warnings, " + Integer.toString(h) + " information messages");
}
Also used : OperationOutcomeIssueComponent(org.hl7.fhir.r5.model.OperationOutcome.OperationOutcomeIssueComponent) CacheVerificationLogger(org.hl7.fhir.utilities.tests.CacheVerificationLogger) OperationOutcome(org.hl7.fhir.r5.model.OperationOutcome) ValidationEngine(org.hl7.fhir.validation.ValidationEngine) Test(org.junit.jupiter.api.Test)

Example 2 with CacheVerificationLogger

use of org.hl7.fhir.utilities.tests.CacheVerificationLogger in project org.hl7.fhir.core by hapifhir.

the class ValidationTests method test.

@SuppressWarnings("deprecation")
@Test
public void test() throws Exception {
    CacheVerificationLogger logger = new CacheVerificationLogger();
    long setup = System.nanoTime();
    this.name = name;
    System.out.println("---- " + name + " ----------------------------------------------------------------");
    System.out.println("** Core: ");
    String txLog = null;
    if (content.has("txLog")) {
        txLog = content.get("txLog").getAsString();
    }
    version = "5.0";
    List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    if (content.has("version")) {
        version = content.get("version").getAsString();
    }
    version = VersionUtilities.getMajMin(version);
    if (!ve.containsKey(version)) {
        if (version.startsWith("5.0"))
            ve.put(version, TestUtilities.getValidationEngine("hl7.fhir.r5.core#4.5.0", ValidationEngineTests.DEF_TX, txLog, FhirPublication.R5, true, "4.5.0"));
        else if (version.startsWith("4.3"))
            ve.put(version, TestUtilities.getValidationEngine("hl7.fhir.r4b.core#4.3.0", ValidationEngineTests.DEF_TX, txLog, FhirPublication.R4B, true, "4.3.0"));
        else if (version.startsWith("4.0"))
            ve.put(version, TestUtilities.getValidationEngine("hl7.fhir.r4.core#4.0.1", ValidationEngineTests.DEF_TX, txLog, FhirPublication.R4, true, "4.0.1"));
        else if (version.startsWith("3.0"))
            ve.put(version, TestUtilities.getValidationEngine("hl7.fhir.r3.core#3.0.2", ValidationEngineTests.DEF_TX, txLog, FhirPublication.STU3, true, "3.0.2"));
        else if (version.startsWith("1.4"))
            ve.put(version, TestUtilities.getValidationEngine("hl7.fhir.r2b.core#1.4.0", ValidationEngineTests.DEF_TX, txLog, FhirPublication.DSTU2016May, true, "1.4.0"));
        else if (version.startsWith("1.0"))
            ve.put(version, TestUtilities.getValidationEngine("hl7.fhir.r2.core#1.0.2", ValidationEngineTests.DEF_TX, txLog, FhirPublication.DSTU2, true, "1.0.2"));
        else
            throw new Exception("unknown version " + version);
    }
    vCurr = ve.get(version);
    vCurr.getContext().getTxClient().setLogger(logger);
    igLoader = new IgLoader(vCurr.getPcm(), vCurr.getContext(), vCurr.getVersion(), true);
    if (TestingUtilities.fcontexts == null) {
        TestingUtilities.fcontexts = new HashMap<>();
    }
    TestingUtilities.fcontexts.put(version, vCurr.getContext());
    if (content.has("use-test") && !content.get("use-test").getAsBoolean())
        return;
    byte[] testCaseContent = TestingUtilities.loadTestResource("validator", JSONUtil.str(content, "file")).getBytes(StandardCharsets.UTF_8);
    // load and process content
    FhirFormat fmt = determineFormat(content, testCaseContent);
    InstanceValidator val = vCurr.getValidator(fmt);
    val.setWantCheckSnapshotUnchanged(true);
    val.getContext().setClientRetryCount(4);
    val.setDebug(false);
    if (content.has("fetcher") && "standalone".equals(JSONUtil.str(content, "fetcher"))) {
        val.setFetcher(vCurr);
        vCurr.setFetcher(new StandAloneValidatorFetcher(vCurr.getPcm(), vCurr.getContext(), vCurr));
    } else {
        val.setFetcher(this);
    }
    val.setPolicyAdvisor(this);
    if (content.has("allowed-extension-domain"))
        val.getExtensionDomains().add(content.get("allowed-extension-domain").getAsString());
    if (content.has("allowed-extension-domains"))
        for (JsonElement a : content.getAsJsonArray("allowed-extension-domains")) val.getExtensionDomains().add(a.getAsString());
    if (content.has("language"))
        val.setValidationLanguage(content.get("language").getAsString());
    else
        val.setValidationLanguage(null);
    if (content.has("default-version")) {
        val.setBaseOptions(val.getBaseOptions().setVersionFlexible(content.get("default-version").getAsBoolean()));
    } else {
        val.setBaseOptions(val.getBaseOptions().setVersionFlexible(false));
    }
    if (content.has("packages")) {
        for (JsonElement e : content.getAsJsonArray("packages")) {
            String n = e.getAsString();
            InputStream cnt = n.endsWith(".tgz") ? TestingUtilities.loadTestResourceStream("validator", n) : null;
            if (cnt != null) {
                igLoader.loadPackage(NpmPackage.fromPackage(cnt));
            } else {
                igLoader.loadIg(vCurr.getIgs(), vCurr.getBinaries(), n, true);
            }
        }
    }
    if (content.has("crumb-trail")) {
        val.setCrumbTrails(content.get("crumb-trail").getAsBoolean());
    }
    if (content.has("supporting")) {
        for (JsonElement e : content.getAsJsonArray("supporting")) {
            String filename = e.getAsString();
            String contents = TestingUtilities.loadTestResource("validator", filename);
            CanonicalResource mr = (CanonicalResource) loadResource(filename, contents);
            val.getContext().cacheResource(mr);
            if (mr instanceof ImplementationGuide) {
                val.getImplementationGuides().add((ImplementationGuide) mr);
            }
        }
    }
    val.getBundleValidationRules().clear();
    if (content.has("bundle-param")) {
        val.getBundleValidationRules().add(new BundleValidationRule(content.getAsJsonObject("bundle-param").get("rule").getAsString(), content.getAsJsonObject("bundle-param").get("profile").getAsString()));
    }
    if (content.has("profiles")) {
        for (JsonElement je : content.getAsJsonArray("profiles")) {
            String filename = je.getAsString();
            String contents = TestingUtilities.loadTestResource("validator", filename);
            StructureDefinition sd = loadProfile(filename, contents, messages, val.isDebug());
            val.getContext().cacheResource(sd);
        }
    }
    List<ValidationMessage> errors = new ArrayList<ValidationMessage>();
    if (content.getAsJsonObject("java").has("debug")) {
        val.setDebug(content.getAsJsonObject("java").get("debug").getAsBoolean());
    } else {
        val.setDebug(false);
    }
    if (content.has("best-practice")) {
        val.setBestPracticeWarningLevel(BestPracticeWarningLevel.valueOf(content.get("best-practice").getAsString()));
    }
    if (content.has("examples")) {
        val.setAllowExamples(content.get("examples").getAsBoolean());
    } else {
        val.setAllowExamples(true);
    }
    if (content.has("security-checks")) {
        val.setSecurityChecks(content.get("security-checks").getAsBoolean());
    }
    if (content.has("logical") == false) {
        val.setAssumeValidRestReferences(content.has("assumeValidRestReferences") ? content.get("assumeValidRestReferences").getAsBoolean() : false);
        System.out.println(String.format("Start Validating (%d to set up)", (System.nanoTime() - setup) / 1000000));
        val.validate(null, errors, new ByteArrayInputStream(testCaseContent), fmt);
        System.out.println(val.reportTimes());
        checkOutcomes(errors, content, null, name);
    }
    if (content.has("profile")) {
        System.out.print("** Profile: ");
        JsonObject profile = content.getAsJsonObject("profile");
        if (profile.has("packages")) {
            for (JsonElement e : profile.getAsJsonArray("packages")) {
                igLoader.loadIg(vCurr.getIgs(), vCurr.getBinaries(), e.getAsString(), true);
            }
        }
        if (profile.getAsJsonObject("java").has("debug")) {
            val.setDebug(profile.getAsJsonObject("java").get("debug").getAsBoolean());
        }
        if (profile.has("supporting")) {
            for (JsonElement e : profile.getAsJsonArray("supporting")) {
                String filename = e.getAsString();
                String contents = TestingUtilities.loadTestResource("validator", filename);
                CanonicalResource mr = (CanonicalResource) loadResource(filename, contents);
                val.getContext().cacheResource(mr);
                if (mr instanceof ImplementationGuide) {
                    val.getImplementationGuides().add((ImplementationGuide) mr);
                }
            }
        }
        StructureDefinition sd = null;
        String filename = profile.get("source").getAsString();
        if (Utilities.isAbsoluteUrl(filename)) {
            sd = val.getContext().fetchResource(StructureDefinition.class, filename);
        } else {
            String contents = TestingUtilities.loadTestResource("validator", filename);
            System.out.println("Name: " + name + " - profile : " + profile.get("source").getAsString());
            version = content.has("version") ? content.get("version").getAsString() : Constants.VERSION;
            sd = loadProfile(filename, contents, messages, val.isDebug());
            val.getContext().cacheResource(sd);
        }
        val.setAssumeValidRestReferences(profile.has("assumeValidRestReferences") ? profile.get("assumeValidRestReferences").getAsBoolean() : false);
        List<ValidationMessage> errorsProfile = new ArrayList<ValidationMessage>();
        val.validate(null, errorsProfile, new ByteArrayInputStream(testCaseContent), fmt, asSdList(sd));
        System.out.println(val.reportTimes());
        checkOutcomes(errorsProfile, profile, filename, name);
    }
    if (content.has("logical")) {
        System.out.print("** Logical: ");
        JsonObject logical = content.getAsJsonObject("logical");
        if (logical.has("supporting")) {
            for (JsonElement e : logical.getAsJsonArray("supporting")) {
                String filename = e.getAsString();
                String contents = TestingUtilities.loadTestResource("validator", filename);
                CanonicalResource mr = (CanonicalResource) loadResource(filename, contents);
                if (mr instanceof StructureDefinition) {
                    val.getContext().generateSnapshot((StructureDefinition) mr, true);
                }
                val.getContext().cacheResource(mr);
            }
        }
        if (logical.has("packages")) {
            for (JsonElement e : logical.getAsJsonArray("packages")) {
                igLoader.loadIg(vCurr.getIgs(), vCurr.getBinaries(), e.getAsString(), true);
            }
        }
        List<ValidationMessage> errorsLogical = new ArrayList<ValidationMessage>();
        Element le = val.validate(null, errorsLogical, new ByteArrayInputStream(testCaseContent), fmt);
        if (logical.has("expressions")) {
            FHIRPathEngine fp = new FHIRPathEngine(val.getContext());
            for (JsonElement e : logical.getAsJsonArray("expressions")) {
                String exp = e.getAsString();
                Assert.assertTrue(fp.evaluateToBoolean(null, le, le, le, fp.parse(exp)));
            }
        }
        checkOutcomes(errorsLogical, logical, "logical", name);
    }
    logger.verifyHasNoRequests();
}
Also used : IgLoader(org.hl7.fhir.validation.IgLoader) BundleValidationRule(org.hl7.fhir.r5.utils.validation.BundleValidationRule) InstanceValidator(org.hl7.fhir.validation.instance.InstanceValidator) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ImplementationGuide(org.hl7.fhir.r5.model.ImplementationGuide) FHIRPathEngine(org.hl7.fhir.r5.utils.FHIRPathEngine) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonElement(com.google.gson.JsonElement) Element(org.hl7.fhir.r5.elementmodel.Element) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) FhirFormat(org.hl7.fhir.r5.elementmodel.Manager.FhirFormat) FileNotFoundException(java.io.FileNotFoundException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ByteArrayInputStream(java.io.ByteArrayInputStream) CacheVerificationLogger(org.hl7.fhir.utilities.tests.CacheVerificationLogger) JsonElement(com.google.gson.JsonElement) CanonicalResource(org.hl7.fhir.r5.model.CanonicalResource) StandAloneValidatorFetcher(org.hl7.fhir.validation.cli.services.StandAloneValidatorFetcher) Test(org.junit.Test)

Example 3 with CacheVerificationLogger

use of org.hl7.fhir.utilities.tests.CacheVerificationLogger in project org.hl7.fhir.core by hapifhir.

the class ValidationEngineTests method test102.

@Test
public void test102() throws Exception {
    if (inbuild) {
        Assertions.assertTrue(true);
        return;
    }
    if (!org.hl7.fhir.validation.tests.utilities.TestUtilities.silent)
        System.out.println("Test102: Validate patient-example.xml in v1.0.2 version");
    ValidationEngine ve = TestUtilities.getValidationEngine("hl7.fhir.r2.core#1.0.2", DEF_TX, FhirPublication.DSTU2, "1.0.2");
    ve.setNoInvariantChecks(true);
    CacheVerificationLogger logger = new CacheVerificationLogger();
    ve.getContext().getTxClient().setLogger(logger);
    OperationOutcome op = ve.validate(FhirFormat.XML, TestingUtilities.loadTestResourceStream("validator", "patient102.xml"), null);
    if (!TestUtilities.silent)
        for (OperationOutcomeIssueComponent iss : op.getIssue()) {
            System.out.println("  " + iss.getSeverity().toCode() + ": " + iss.getDetails().getText());
        }
    int e = errors(op);
    int w = warnings(op);
    int h = hints(op);
    Assertions.assertEquals(1, e);
    Assertions.assertEquals(0, w);
    Assertions.assertEquals(0, h);
    assertTrue(logger.verifyHasNoRequests(), "Unexpected request to TX server");
    if (!TestUtilities.silent)
        System.out.println("  .. done: " + Integer.toString(e) + " errors, " + Integer.toString(w) + " warnings, " + Integer.toString(h) + " information messages");
}
Also used : OperationOutcomeIssueComponent(org.hl7.fhir.r5.model.OperationOutcome.OperationOutcomeIssueComponent) CacheVerificationLogger(org.hl7.fhir.utilities.tests.CacheVerificationLogger) OperationOutcome(org.hl7.fhir.r5.model.OperationOutcome) ValidationEngine(org.hl7.fhir.validation.ValidationEngine) Test(org.junit.jupiter.api.Test)

Example 4 with CacheVerificationLogger

use of org.hl7.fhir.utilities.tests.CacheVerificationLogger in project org.hl7.fhir.core by hapifhir.

the class ValidationEngineTests method testCurrentJson.

@Test
public void testCurrentJson() throws Exception {
    if (!TestUtilities.silent)
        System.out.println("TestCurrentJson: Validate patient-example.json in Current version");
    ValidationEngine ve = TestUtilities.getValidationEngine("hl7.fhir.r4.core#4.0.1", DEF_TX, FhirPublication.R4, "4.0.1");
    CacheVerificationLogger logger = new CacheVerificationLogger();
    ve.getContext().getTxClient().setLogger(logger);
    OperationOutcome op = ve.validate(FhirFormat.JSON, TestingUtilities.loadTestResourceStream("validator", "patient-example.json"), null);
    int e = errors(op);
    int w = warnings(op);
    int h = hints(op);
    Assertions.assertEquals(0, e);
    Assertions.assertEquals(0, w);
    Assertions.assertEquals(1, h);
    assertTrue(logger.verifyHasNoRequests(), "Unexpected request to TX server");
    if (!TestUtilities.silent)
        System.out.println("  .. done: " + Integer.toString(e) + " errors, " + Integer.toString(w) + " warnings, " + Integer.toString(h) + " information messages");
}
Also used : CacheVerificationLogger(org.hl7.fhir.utilities.tests.CacheVerificationLogger) OperationOutcome(org.hl7.fhir.r5.model.OperationOutcome) ValidationEngine(org.hl7.fhir.validation.ValidationEngine) Test(org.junit.jupiter.api.Test)

Example 5 with CacheVerificationLogger

use of org.hl7.fhir.utilities.tests.CacheVerificationLogger in project org.hl7.fhir.core by hapifhir.

the class ValidationEngineTests method test301USCore.

@Test
public void test301USCore() throws Exception {
    if (!TestUtilities.silent)
        System.out.println("Test301USCore: Validate patient300.xml against US-Core");
    ValidationEngine ve = TestUtilities.getValidationEngine("hl7.fhir.r3.core#3.0.2", DEF_TX, FhirPublication.STU3, "3.0.2");
    CacheVerificationLogger logger = new CacheVerificationLogger();
    ve.getContext().getTxClient().setLogger(logger);
    IgLoader igLoader = new IgLoader(ve.getPcm(), ve.getContext(), ve.getVersion(), true);
    if (!TestUtilities.silent)
        System.out.println("  .. load USCore");
    igLoader.loadIg(ve.getIgs(), ve.getBinaries(), "hl7.fhir.us.core#1.0.1", false);
    List<String> profiles = new ArrayList<>();
    profiles.add("http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient");
    OperationOutcome op = ve.validate(FhirFormat.XML, TestingUtilities.loadTestResourceStream("validator", "patient301.xml"), profiles);
    if (!TestUtilities.silent)
        for (OperationOutcomeIssueComponent issue : op.getIssue()) System.out.println("  - " + issue.getDetails().getText());
    int e = errors(op);
    int w = warnings(op);
    int h = hints(op);
    Assertions.assertEquals(1, e);
    Assertions.assertEquals(0, w);
    Assertions.assertEquals(0, h);
    assertTrue(logger.verifyHasNoRequests(), "Unexpected request to TX server");
    if (!TestUtilities.silent)
        System.out.println("  .. done: " + Integer.toString(e) + " errors, " + Integer.toString(w) + " warnings, " + Integer.toString(h) + " information messages");
}
Also used : IgLoader(org.hl7.fhir.validation.IgLoader) OperationOutcomeIssueComponent(org.hl7.fhir.r5.model.OperationOutcome.OperationOutcomeIssueComponent) CacheVerificationLogger(org.hl7.fhir.utilities.tests.CacheVerificationLogger) OperationOutcome(org.hl7.fhir.r5.model.OperationOutcome) ArrayList(java.util.ArrayList) ValidationEngine(org.hl7.fhir.validation.ValidationEngine) Test(org.junit.jupiter.api.Test)

Aggregations

CacheVerificationLogger (org.hl7.fhir.utilities.tests.CacheVerificationLogger)8 OperationOutcome (org.hl7.fhir.r5.model.OperationOutcome)7 ValidationEngine (org.hl7.fhir.validation.ValidationEngine)7 Test (org.junit.jupiter.api.Test)7 OperationOutcomeIssueComponent (org.hl7.fhir.r5.model.OperationOutcome.OperationOutcomeIssueComponent)6 ArrayList (java.util.ArrayList)2 IgLoader (org.hl7.fhir.validation.IgLoader)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 NotImplementedException (org.apache.commons.lang3.NotImplementedException)1 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)1 FHIRException (org.hl7.fhir.exceptions.FHIRException)1 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)1 Element (org.hl7.fhir.r5.elementmodel.Element)1 FhirFormat (org.hl7.fhir.r5.elementmodel.Manager.FhirFormat)1