Search in sources :

Example 6 with FhirValidator

use of ca.uhn.fhir.validation.FhirValidator in project synthea by synthetichealth.

the class HospitalExporterTestStu3 method testFHIRExport.

@Test
public void testFHIRExport() throws Exception {
    FhirContext ctx = FhirStu3.getContext();
    FhirValidator validator = ctx.newValidator();
    validator.setValidateAgainstStandardSchema(true);
    validator.setValidateAgainstStandardSchematron(true);
    File tempOutputFolder = tempFolder.newFolder();
    Config.set("exporter.baseDirectory", tempOutputFolder.toString());
    Config.set("exporter.hospital.fhir_stu3.export", "true");
    Config.set("exporter.fhir.transaction_bundle", "true");
    // set this manually, in case it has already been loaded.
    FhirStu3.TRANSACTION_BUNDLE = true;
    TestHelper.loadTestProperties();
    Generator.DEFAULT_STATE = Config.get("test_state.default", "Massachusetts");
    Location location = new Location(Generator.DEFAULT_STATE, null);
    Provider.clear();
    Provider.loadProviders(location, ProviderTest.providerRandom);
    assertNotNull(Provider.getProviderList());
    assertFalse(Provider.getProviderList().isEmpty());
    Provider.getProviderList().get(0).incrementEncounters(EncounterType.WELLNESS, 0);
    HospitalExporterStu3.export(0L);
    File expectedExportFolder = tempOutputFolder.toPath().resolve("fhir_stu3").toFile();
    assertTrue(expectedExportFolder.exists() && expectedExportFolder.isDirectory());
    File expectedExportFile = expectedExportFolder.toPath().resolve("hospitalInformation0.json").toFile();
    assertTrue(expectedExportFile.exists() && expectedExportFile.isFile());
    FileReader fileReader = new FileReader(expectedExportFile.getPath());
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    StringBuilder fhirJson = new StringBuilder();
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        fhirJson.append(line);
    }
    bufferedReader.close();
    IBaseResource resource = ctx.newJsonParser().parseResource(fhirJson.toString());
    ValidationResult result = validator.validateWithResult(resource);
    if (result.isSuccessful() == false) {
        for (SingleValidationMessage message : result.getMessages()) {
            System.out.println(message.getSeverity().toString() + ": " + message.getMessage());
        }
    }
    assertTrue(result.isSuccessful());
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) SingleValidationMessage(ca.uhn.fhir.validation.SingleValidationMessage) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) FhirValidator(ca.uhn.fhir.validation.FhirValidator) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) ValidationResult(ca.uhn.fhir.validation.ValidationResult) File(java.io.File) Location(org.mitre.synthea.world.geography.Location) Test(org.junit.Test) ProviderTest(org.mitre.synthea.world.agents.ProviderTest)

Example 7 with FhirValidator

use of ca.uhn.fhir.validation.FhirValidator in project synthea by synthetichealth.

the class HospitalExporterTestDstu2 method testFHIRExport.

@Test
public void testFHIRExport() throws Exception {
    FhirContext ctx = FhirDstu2.getContext();
    FhirValidator validator = ctx.newValidator();
    validator.setValidateAgainstStandardSchema(true);
    validator.setValidateAgainstStandardSchematron(true);
    File tempOutputFolder = tempFolder.newFolder();
    Config.set("exporter.baseDirectory", tempOutputFolder.toString());
    Config.set("exporter.hospital.fhir_dstu2.export", "true");
    Config.set("exporter.fhir.transaction_bundle", "true");
    // set this manually, in case it has already been loaded.
    FhirDstu2.TRANSACTION_BUNDLE = true;
    TestHelper.loadTestProperties();
    Generator.DEFAULT_STATE = Config.get("test_state.default", "Massachusetts");
    Location location = new Location(Generator.DEFAULT_STATE, null);
    Provider.clear();
    Provider.loadProviders(location, ProviderTest.providerRandom);
    assertNotNull(Provider.getProviderList());
    assertFalse(Provider.getProviderList().isEmpty());
    Provider.getProviderList().get(0).incrementEncounters(EncounterType.WELLNESS, 0);
    HospitalExporterDstu2.export(0L);
    File expectedExportFolder = tempOutputFolder.toPath().resolve("fhir_dstu2").toFile();
    assertTrue(expectedExportFolder.exists() && expectedExportFolder.isDirectory());
    File expectedExportFile = expectedExportFolder.toPath().resolve("hospitalInformation0.json").toFile();
    assertTrue(expectedExportFile.exists() && expectedExportFile.isFile());
    FileReader fileReader = new FileReader(expectedExportFile.getPath());
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    StringBuilder fhirJson = new StringBuilder();
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        fhirJson.append(line);
    }
    bufferedReader.close();
    IBaseResource resource = ctx.newJsonParser().parseResource(fhirJson.toString());
    ValidationResult result = validator.validateWithResult(resource);
    if (result.isSuccessful() == false) {
        for (SingleValidationMessage message : result.getMessages()) {
            System.out.println(message.getSeverity().toString() + ": " + message.getMessage());
        }
    }
    assertTrue(result.isSuccessful());
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) SingleValidationMessage(ca.uhn.fhir.validation.SingleValidationMessage) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) FhirValidator(ca.uhn.fhir.validation.FhirValidator) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) ValidationResult(ca.uhn.fhir.validation.ValidationResult) File(java.io.File) Location(org.mitre.synthea.world.geography.Location) Test(org.junit.Test) ProviderTest(org.mitre.synthea.world.agents.ProviderTest)

Aggregations

FhirValidator (ca.uhn.fhir.validation.FhirValidator)7 ValidationResult (ca.uhn.fhir.validation.ValidationResult)6 FhirContext (ca.uhn.fhir.context.FhirContext)5 SingleValidationMessage (ca.uhn.fhir.validation.SingleValidationMessage)5 Test (org.junit.Test)5 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileReader (java.io.FileReader)3 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)3 ProviderTest (org.mitre.synthea.world.agents.ProviderTest)3 Location (org.mitre.synthea.world.geography.Location)3 IParser (ca.uhn.fhir.parser.IParser)2 ArrayList (java.util.ArrayList)2 Bundle (ca.uhn.fhir.model.dstu2.resource.Bundle)1 Entry (ca.uhn.fhir.model.dstu2.resource.Bundle.Entry)1 IValidatorModule (ca.uhn.fhir.validation.IValidatorModule)1 SchemaBaseValidator (ca.uhn.fhir.validation.SchemaBaseValidator)1 FhirInstanceValidator (org.hl7.fhir.dstu3.hapi.validation.FhirInstanceValidator)1 Bundle (org.hl7.fhir.dstu3.model.Bundle)1 BundleEntryComponent (org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent)1