Search in sources :

Example 76 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project dpc-app by CMSgov.

the class ParamResourceFactoryTest method testNonParameter.

@Test
void testNonParameter() throws IOException {
    final HttpServletRequest mock = Mockito.mock(HttpServletRequest.class);
    final ServletInputStream mockStream = Mockito.mock(ServletInputStream.class);
    final Injector mockInjector = Mockito.mock(Injector.class);
    Mockito.when(mockInjector.getInstance(HttpServletRequest.class)).thenReturn(mock);
    Mockito.when(mock.getInputStream()).thenReturn(mockStream);
    final IParser parser = Mockito.mock(IParser.class);
    Mockito.when(parser.parseResource(Mockito.eq(Parameters.class), Mockito.any(InputStream.class))).thenThrow(DataFormatException.class);
    final ParamResourceFactory factory = new ParamResourceFactory(mockInjector, null, parser);
    final WebApplicationException exception = assertThrows(WebApplicationException.class, factory::provide, "Should throw exception");
    assertAll(() -> assertEquals(HttpStatus.BAD_REQUEST_400, exception.getResponse().getStatus(), "Should be a bad request"), () -> assertEquals("Resource type must be `Parameters`", exception.getMessage(), "Should have wrong resource message"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletInputStream(javax.servlet.ServletInputStream) Parameters(org.hl7.fhir.dstu3.model.Parameters) WebApplicationException(javax.ws.rs.WebApplicationException) Injector(com.google.inject.Injector) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) IParser(ca.uhn.fhir.parser.IParser) Test(org.junit.jupiter.api.Test)

Example 77 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project dpc-app by CMSgov.

the class ParamResourceFactoryTest method testCannotReadIS.

@Test
void testCannotReadIS() throws IOException {
    final HttpServletRequest mock = Mockito.mock(HttpServletRequest.class);
    final ServletInputStream mockStream = Mockito.mock(ServletInputStream.class);
    final Injector mockInjector = Mockito.mock(Injector.class);
    Mockito.when(mockInjector.getInstance(HttpServletRequest.class)).thenReturn(mock);
    Mockito.when(mock.getInputStream()).thenReturn(mockStream);
    final IParser parser = Mockito.mock(IParser.class);
    Mockito.when(parser.parseResource(Mockito.eq(Parameters.class), Mockito.any(InputStream.class))).thenAnswer(answer -> {
        throw new IOException();
    });
    final ParamResourceFactory factory = new ParamResourceFactory(mockInjector, null, parser);
    final WebApplicationException exception = assertThrows(WebApplicationException.class, factory::provide, "Should throw exception");
    assertAll(() -> assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, exception.getResponse().getStatus(), "Should be an internal exception"), () -> assertEquals("Cannot read input stream", exception.getMessage(), "Should have wrong resource message"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletInputStream(javax.servlet.ServletInputStream) Parameters(org.hl7.fhir.dstu3.model.Parameters) WebApplicationException(javax.ws.rs.WebApplicationException) Injector(com.google.inject.Injector) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) IParser(ca.uhn.fhir.parser.IParser) Test(org.junit.jupiter.api.Test)

Example 78 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project dpc-app by CMSgov.

the class DPCProfileSupport method loadProfiles.

private Map<String, StructureDefinition> loadProfiles(FhirContext ctx) {
    logger.info("Loading resource profiles");
    final Map<String, StructureDefinition> definitionMap = new HashMap<>();
    // Generate a validator to pull the base definitions from.
    final DefaultProfileValidationSupport defaultValidation = new DefaultProfileValidationSupport();
    final HapiWorkerContext hapiWorkerContext = new HapiWorkerContext(ctx, defaultValidation);
    final ProfileUtilities profileUtilities = new ProfileUtilities(hapiWorkerContext, new ArrayList<>(), null);
    final IParser parser = ctx.newJsonParser();
    ServiceLoaderHelpers.getLoaderStream(IProfileLoader.class).map(profileLoader -> toStructureDefinition(parser, profileLoader.getPath())).filter(Objects::nonNull).map(diffStructure -> mergeDiff(ctx, defaultValidation, profileUtilities, diffStructure)).forEach(structure -> definitionMap.put(structure.getUrl(), structure));
    return definitionMap;
}
Also used : HapiWorkerContext(org.hl7.fhir.dstu3.hapi.ctx.HapiWorkerContext) java.util(java.util) ServiceLoaderHelpers(gov.cms.dpc.fhir.helpers.ServiceLoaderHelpers) Logger(org.slf4j.Logger) HapiWorkerContext(org.hl7.fhir.dstu3.hapi.ctx.HapiWorkerContext) DataFormatException(ca.uhn.fhir.parser.DataFormatException) IValidationSupport(org.hl7.fhir.dstu3.hapi.ctx.IValidationSupport) LoggerFactory(org.slf4j.LoggerFactory) DefaultProfileValidationSupport(org.hl7.fhir.dstu3.hapi.ctx.DefaultProfileValidationSupport) IOException(java.io.IOException) CodeSystem(org.hl7.fhir.dstu3.model.CodeSystem) Inject(javax.inject.Inject) FhirContext(ca.uhn.fhir.context.FhirContext) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) IProfileLoader(gov.cms.dpc.fhir.validations.profiles.IProfileLoader) ProfileUtilities(org.hl7.fhir.dstu3.conformance.ProfileUtilities) Nonnull(javax.annotation.Nonnull) IParser(ca.uhn.fhir.parser.IParser) ValueSet(org.hl7.fhir.dstu3.model.ValueSet) InputStream(java.io.InputStream) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) ProfileUtilities(org.hl7.fhir.dstu3.conformance.ProfileUtilities) DefaultProfileValidationSupport(org.hl7.fhir.dstu3.hapi.ctx.DefaultProfileValidationSupport) IParser(ca.uhn.fhir.parser.IParser)

Example 79 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project synthea by synthetichealth.

the class FHIRSTU3ExporterTest method testSampledDataExport.

@Test
public void testSampledDataExport() throws Exception {
    Person person = new Person(0L);
    person.attributes.put(Person.GENDER, "F");
    person.attributes.put(Person.FIRST_LANGUAGE, "spanish");
    person.attributes.put(Person.RACE, "other");
    person.attributes.put(Person.ETHNICITY, "hispanic");
    person.attributes.put(Person.INCOME, Integer.parseInt(Config.get("generate.demographics.socioeconomic.income.poverty")) * 2);
    person.attributes.put(Person.OCCUPATION_LEVEL, 1.0);
    person.attributes.put(Person.CONTACT_EMAIL, "test@test.test");
    person.attributes.put(Person.CONTACT_GIVEN_NAME, "John");
    person.attributes.put(Person.CONTACT_FAMILY_NAME, "Appleseed");
    person.history = new LinkedList<>();
    Provider mock = Mockito.mock(Provider.class);
    Mockito.when(mock.getResourceID()).thenReturn("Mock-UUID");
    person.setProvider(EncounterType.AMBULATORY, mock);
    person.setProvider(EncounterType.WELLNESS, mock);
    person.setProvider(EncounterType.EMERGENCY, mock);
    person.setProvider(EncounterType.INPATIENT, mock);
    Long time = System.currentTimeMillis();
    int age = 35;
    long birthTime = time - Utilities.convertTime("years", age);
    person.attributes.put(Person.BIRTHDATE, birthTime);
    Payer.loadNoInsurance();
    for (int i = 0; i < age; i++) {
        long yearTime = time - Utilities.convertTime("years", i);
        person.coverage.setPayerAtTime(yearTime, Payer.noInsurance);
    }
    Module module = TestHelper.getFixture("observation.json");
    State encounter = module.getState("SomeEncounter");
    assertTrue(encounter.process(person, time));
    person.history.add(encounter);
    State physiology = module.getState("Simulate_CVS");
    assertTrue(physiology.process(person, time));
    person.history.add(physiology);
    State sampleObs = module.getState("SampledDataObservation");
    assertTrue(sampleObs.process(person, time));
    person.history.add(sampleObs);
    FhirContext ctx = FhirStu3.getContext();
    IParser parser = ctx.newJsonParser().setPrettyPrint(true);
    String fhirJson = FhirStu3.convertToFHIRJson(person, System.currentTimeMillis());
    Bundle bundle = parser.parseResource(Bundle.class, fhirJson);
    for (BundleEntryComponent entry : bundle.getEntry()) {
        if (entry.getResource() instanceof Observation) {
            Observation obs = (Observation) entry.getResource();
            assertTrue(obs.getValue() instanceof SampledData);
            SampledData data = (SampledData) obs.getValue();
            // 0.01s == 10ms
            assertEquals(10, data.getPeriod().doubleValue(), 0.001);
            assertEquals(3, (int) data.getDimensions());
        }
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) Bundle(org.hl7.fhir.dstu3.model.Bundle) Provider(org.mitre.synthea.world.agents.Provider) BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) State(org.mitre.synthea.engine.State) Observation(org.hl7.fhir.dstu3.model.Observation) SampledData(org.hl7.fhir.dstu3.model.SampledData) Module(org.mitre.synthea.engine.Module) Person(org.mitre.synthea.world.agents.Person) IParser(ca.uhn.fhir.parser.IParser) Test(org.junit.Test)

Example 80 with IParser

use of org.hl7.fhir.r4b.formats.IParser in project synthea by synthetichealth.

the class FHIRSTU3ExporterTest method testObservationAttachment.

@Test
public void testObservationAttachment() throws Exception {
    Person person = new Person(0L);
    person.attributes.put(Person.GENDER, "F");
    person.attributes.put(Person.FIRST_LANGUAGE, "spanish");
    person.attributes.put(Person.RACE, "other");
    person.attributes.put(Person.ETHNICITY, "hispanic");
    person.attributes.put(Person.INCOME, Integer.parseInt(Config.get("generate.demographics.socioeconomic.income.poverty")) * 2);
    person.attributes.put(Person.OCCUPATION_LEVEL, 1.0);
    person.attributes.put("Pulmonary Resistance", 0.1552);
    person.attributes.put("BMI Multiplier", 0.055);
    person.setVitalSign(VitalSign.BMI, 21.0);
    person.history = new LinkedList<>();
    Provider mock = Mockito.mock(Provider.class);
    Mockito.when(mock.getResourceID()).thenReturn("Mock-UUID");
    person.setProvider(EncounterType.AMBULATORY, mock);
    person.setProvider(EncounterType.WELLNESS, mock);
    person.setProvider(EncounterType.EMERGENCY, mock);
    person.setProvider(EncounterType.INPATIENT, mock);
    Long time = System.currentTimeMillis();
    int age = 35;
    long birthTime = time - Utilities.convertTime("years", age);
    person.attributes.put(Person.BIRTHDATE, birthTime);
    Payer.loadNoInsurance();
    for (int i = 0; i < age; i++) {
        long yearTime = time - Utilities.convertTime("years", i);
        person.coverage.setPayerAtTime(yearTime, Payer.noInsurance);
    }
    Module module = TestHelper.getFixture("observation.json");
    State physiology = module.getState("Simulate_CVS");
    assertTrue(physiology.process(person, time));
    person.history.add(physiology);
    State encounter = module.getState("SomeEncounter");
    assertTrue(encounter.process(person, time));
    person.history.add(encounter);
    State chartState = module.getState("ChartObservation");
    assertTrue(chartState.process(person, time));
    person.history.add(chartState);
    State urlState = module.getState("UrlObservation");
    assertTrue(urlState.process(person, time));
    person.history.add(urlState);
    FhirContext ctx = FhirStu3.getContext();
    IParser parser = ctx.newJsonParser().setPrettyPrint(true);
    String fhirJson = FhirStu3.convertToFHIRJson(person, System.currentTimeMillis());
    Bundle bundle = parser.parseResource(Bundle.class, fhirJson);
    for (BundleEntryComponent entry : bundle.getEntry()) {
        if (entry.getResource() instanceof Media) {
            Media media = (Media) entry.getResource();
            if (media.getContent().getData() != null) {
                assertEquals(400, media.getWidth());
                assertEquals(200, media.getHeight());
                assertEquals("Invasive arterial pressure", media.getReasonCode().get(0).getText());
                assertTrue(Base64.isBase64(media.getContent().getDataElement().getValueAsString()));
            } else if (media.getContent().getUrl() != null) {
                assertEquals("https://example.com/image/12498596132", media.getContent().getUrl());
                assertEquals("en-US", media.getContent().getLanguage());
                assertTrue(media.getContent().getSize() > 0);
            } else {
                fail("Invalid Media element in output JSON");
            }
        }
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) Bundle(org.hl7.fhir.dstu3.model.Bundle) Media(org.hl7.fhir.dstu3.model.Media) Provider(org.mitre.synthea.world.agents.Provider) BundleEntryComponent(org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent) State(org.mitre.synthea.engine.State) Module(org.mitre.synthea.engine.Module) Person(org.mitre.synthea.world.agents.Person) IParser(ca.uhn.fhir.parser.IParser) Test(org.junit.Test)

Aggregations

IParser (ca.uhn.fhir.parser.IParser)89 FhirContext (ca.uhn.fhir.context.FhirContext)43 IOException (java.io.IOException)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 Test (org.junit.Test)24 InputStream (java.io.InputStream)22 IParser (org.hl7.fhir.r5.formats.IParser)19 JsonParser (org.hl7.fhir.r5.formats.JsonParser)18 Test (org.junit.jupiter.api.Test)18 FHIRException (org.hl7.fhir.exceptions.FHIRException)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 File (java.io.File)16 FileInputStream (java.io.FileInputStream)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Bundle (org.hl7.fhir.r4.model.Bundle)14 FileOutputStream (java.io.FileOutputStream)12 Bundle (org.hl7.fhir.dstu3.model.Bundle)12 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)12 XmlParser (org.hl7.fhir.r5.formats.XmlParser)12 ArrayList (java.util.ArrayList)11