Search in sources :

Example 71 with IParser

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

the class PractitionerResourceTest method testUpdatePractitionerNotImplemented.

@Test
public void testUpdatePractitionerNotImplemented() throws IOException {
    IGenericClient client = APIAuthHelpers.buildAuthenticatedClient(ctx, getBaseURL(), ORGANIZATION_TOKEN, PUBLIC_KEY_ID, PRIVATE_KEY);
    final IParser parser = ctx.newJsonParser();
    APITestHelpers.setupPractitionerTest(client, parser);
    // Grab a practitioner to update
    final Bundle practitioners = client.search().forResource(Practitioner.class).returnBundle(Bundle.class).encodedJson().execute();
    final Practitioner practitioner = (Practitioner) practitioners.getEntryFirstRep().getResource();
    practitioner.setBirthDate(Date.valueOf("1989-01-01"));
    IUpdateExecutable update = client.update().resource(practitioner).withId(practitioner.getId());
    assertThrows(NotImplementedOperationException.class, update::execute);
}
Also used : Practitioner(org.hl7.fhir.dstu3.model.Practitioner) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) Bundle(org.hl7.fhir.dstu3.model.Bundle) IUpdateExecutable(ca.uhn.fhir.rest.gclient.IUpdateExecutable) IParser(ca.uhn.fhir.parser.IParser) AbstractSecureApplicationTest(gov.cms.dpc.api.AbstractSecureApplicationTest) Test(org.junit.jupiter.api.Test)

Example 72 with IParser

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

the class GroupResourceTest method testMissingProvenance.

@Test
void testMissingProvenance() throws IOException {
    final IParser parser = ctx.newJsonParser();
    IGenericClient client = APIAuthHelpers.buildAuthenticatedClient(ctx, getBaseURL(), ORGANIZATION_TOKEN, PUBLIC_KEY_ID, PRIVATE_KEY);
    APITestHelpers.setupPatientTest(client, parser);
    APITestHelpers.setupPractitionerTest(client, parser);
    // Create a patient
    final Bundle specificSearch = client.search().forResource(Patient.class).where(Patient.IDENTIFIER.exactly().systemAndCode(DPCIdentifierSystem.MBI.getSystem(), "4S41C00AA00")).returnBundle(Bundle.class).encodedJson().execute();
    assertEquals(1, specificSearch.getTotal(), "Should have a single patient");
    final Patient patient = (Patient) specificSearch.getEntryFirstRep().getResource();
    // Create the practitioner
    final Bundle practSearch = client.search().forResource(Practitioner.class).where(Practitioner.IDENTIFIER.exactly().code("1232131239")).returnBundle(Bundle.class).encodedJson().execute();
    assertEquals(1, practSearch.getTotal(), "Should have a specific provider");
    // Fetch the provider directly
    final Practitioner foundProvider = (Practitioner) practSearch.getEntryFirstRep().getResource();
    final Group group = SeedProcessor.createBaseAttributionGroup(FHIRExtractors.getProviderNPI(foundProvider), ORGANIZATION_ID);
    final Reference patientRef = new Reference(patient.getIdElement());
    group.addMember().setEntity(patientRef);
    // Submit the group
    final ICreateTyped creation = client.create().resource(group).encodedJson();
    final InvalidRequestException invalidRequestException = assertThrows(InvalidRequestException.class, creation::execute, "Should throw a 400");
    final OperationOutcome operationOutcome = (OperationOutcome) invalidRequestException.getOperationOutcome();
    assertAll(() -> assertEquals(HttpStatus.BAD_REQUEST_400, invalidRequestException.getStatusCode(), "Should have 400 status"), () -> assertEquals("Must have X-Provenance header", operationOutcome.getIssueFirstRep().getDetails().getText(), "Should have correct message"));
    // Try again with provenance
    final Provenance provenance = new Provenance();
    provenance.setRecorded(Date.valueOf(Instant.now().atZone(ZoneOffset.UTC).toLocalDate()));
    final Coding coding = new Coding();
    coding.setSystem("http://hl7.org/fhir/v3/ActReason");
    coding.setCode("TREAT");
    provenance.setReason(Collections.singletonList(coding));
    provenance.setTarget(Collections.singletonList(patientRef));
    final Provenance.ProvenanceAgentComponent component = new Provenance.ProvenanceAgentComponent();
    final Coding roleCode = new Coding();
    roleCode.setSystem(V3RoleClass.AGNT.getSystem());
    roleCode.setCode(V3RoleClass.AGNT.toCode());
    final CodeableConcept roleConcept = new CodeableConcept();
    roleConcept.addCoding(roleCode);
    component.setRole(Collections.singletonList(roleConcept));
    component.setWho(new Reference(new IdType("Organization", ORGANIZATION_ID)));
    component.setOnBehalfOf(new Reference(foundProvider.getIdElement()));
    provenance.addAgent(component);
    creation.withAdditionalHeader("X-Provenance", ctx.newJsonParser().encodeResourceToString(provenance));
    creation.execute();
}
Also used : APITestHelpers.createProvenance(gov.cms.dpc.api.APITestHelpers.createProvenance) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) ICreateTyped(ca.uhn.fhir.rest.gclient.ICreateTyped) IBaseOperationOutcome(org.hl7.fhir.instance.model.api.IBaseOperationOutcome) InvalidRequestException(ca.uhn.fhir.rest.server.exceptions.InvalidRequestException) IParser(ca.uhn.fhir.parser.IParser) AbstractSecureApplicationTest(gov.cms.dpc.api.AbstractSecureApplicationTest) Test(org.junit.jupiter.api.Test)

Example 73 with IParser

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

the class SeedCommand method seedProviderBundle.

private void seedProviderBundle(FHIREntityConverter converter, DSLContext context, IParser parser, UUID organizationID) throws IOException {
    try (final InputStream providerBundleStream = SeedCommand.class.getClassLoader().getResourceAsStream(PROVIDER_BUNDLE)) {
        final Parameters parameters = parser.parseResource(Parameters.class, providerBundleStream);
        final Bundle providerBundle = (Bundle) parameters.getParameterFirstRep().getResource();
        final List<ProviderEntity> providers = BundleParser.parse(Practitioner.class, providerBundle, (provider) -> converter.fromFHIR(ProviderEntity.class, provider), organizationID);
        providers.stream().map(entity -> providersEntityToRecord(context, entity)).forEach(context::executeInsert);
    }
}
Also used : FHIREntityConverter(gov.cms.dpc.fhir.converters.FHIREntityConverter) java.util(java.util) RosterUtils(gov.cms.dpc.attribution.jdbi.RosterUtils) DSL(org.jooq.impl.DSL) Connection(java.sql.Connection) LoggerFactory(org.slf4j.LoggerFactory) PatientsRecord(gov.cms.dpc.attribution.dao.tables.records.PatientsRecord) FhirContext(ca.uhn.fhir.context.FhirContext) Namespace(net.sourceforge.argparse4j.inf.Namespace) org.hl7.fhir.dstu3.model(org.hl7.fhir.dstu3.model) DSLContext(org.jooq.DSLContext) ProvidersRecord(gov.cms.dpc.attribution.dao.tables.records.ProvidersRecord) ZoneOffset(java.time.ZoneOffset) OrganizationEndpoints(gov.cms.dpc.attribution.dao.tables.OrganizationEndpoints) DPCAttributionConfiguration(gov.cms.dpc.attribution.DPCAttributionConfiguration) IParser(ca.uhn.fhir.parser.IParser) Environment(io.dropwizard.setup.Environment) OrganizationEndpointsRecord(gov.cms.dpc.attribution.dao.tables.records.OrganizationEndpointsRecord) Application(io.dropwizard.Application) EnvironmentCommand(io.dropwizard.cli.EnvironmentCommand) Logger(org.slf4j.Logger) ManagedDataSource(io.dropwizard.db.ManagedDataSource) Subparser(net.sourceforge.argparse4j.inf.Subparser) Settings(org.jooq.conf.Settings) IOException(java.io.IOException) Patients(gov.cms.dpc.attribution.dao.tables.Patients) PooledDataSourceFactory(io.dropwizard.db.PooledDataSourceFactory) RenderQuotedNames(org.jooq.conf.RenderQuotedNames) Providers(gov.cms.dpc.attribution.dao.tables.Providers) OffsetDateTime(java.time.OffsetDateTime) Organizations(gov.cms.dpc.attribution.dao.tables.Organizations) OrganizationsRecord(gov.cms.dpc.attribution.dao.tables.records.OrganizationsRecord) gov.cms.dpc.common.entities(gov.cms.dpc.common.entities) DBUtils(gov.cms.dpc.attribution.utils.DBUtils) SeedProcessor(gov.cms.dpc.common.utils.SeedProcessor) InputStream(java.io.InputStream) InputStream(java.io.InputStream)

Example 74 with IParser

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

the class ParamResourceFactoryTest method testNamedResource.

@Test
void testNamedResource() throws IOException {
    final Parameters parameters = new Parameters();
    final Patient unnamedPatient = new Patient();
    final Patient namedPatient = new Patient();
    namedPatient.addName().setFamily("Patient").addGiven("Test");
    parameters.addParameter().setResource(unnamedPatient);
    parameters.addParameter().setResource(namedPatient).setName("named");
    final HttpServletRequest mock = Mockito.mock(HttpServletRequest.class);
    final ServletInputStream mockStream = Mockito.mock(ServletInputStream.class);
    final IParser parser = Mockito.mock(IParser.class);
    Mockito.when(parser.parseResource(Mockito.eq(Parameters.class), Mockito.any(InputStream.class))).thenReturn(parameters);
    final Injector mockInjector = Mockito.mock(Injector.class);
    Mockito.when(mockInjector.getInstance(HttpServletRequest.class)).thenReturn(mock);
    Mockito.when(mock.getInputStream()).thenReturn(mockStream);
    final Parameter parameter = Mockito.mock(Parameter.class);
    final FHIRParameter annotation = Mockito.mock(FHIRParameter.class);
    Mockito.when(annotation.name()).thenReturn("named");
    Mockito.when(parameter.getAnnotation(FHIRParameter.class)).thenReturn(annotation);
    Mockito.when(parameter.getRawType()).thenAnswer(answer -> Patient.class);
    final ParamResourceFactory factory = new ParamResourceFactory(mockInjector, parameter, parser);
    assertAll(() -> assertTrue(namedPatient.equalsDeep((Patient) factory.provide()), "Should have returned dummy patient"), () -> assertFalse(unnamedPatient.equalsDeep((Patient) factory.provide()), "Should have returned dummy patient"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Parameters(org.hl7.fhir.dstu3.model.Parameters) ServletInputStream(javax.servlet.ServletInputStream) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) Injector(com.google.inject.Injector) FHIRParameter(gov.cms.dpc.fhir.annotations.FHIRParameter) Patient(org.hl7.fhir.dstu3.model.Patient) FHIRParameter(gov.cms.dpc.fhir.annotations.FHIRParameter) Parameter(org.glassfish.jersey.server.model.Parameter) IParser(ca.uhn.fhir.parser.IParser) Test(org.junit.jupiter.api.Test)

Example 75 with IParser

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

the class ParamResourceFactoryTest method testMissingResource.

@Test
void testMissingResource() throws IOException {
    final Parameters parameters = new Parameters();
    final Patient dummyPatient = new Patient();
    parameters.addParameter().setResource(dummyPatient);
    final HttpServletRequest mock = Mockito.mock(HttpServletRequest.class);
    final ServletInputStream mockStream = Mockito.mock(ServletInputStream.class);
    final IParser parser = Mockito.mock(IParser.class);
    Mockito.when(parser.parseResource(Mockito.eq(Parameters.class), Mockito.any(InputStream.class))).thenReturn(parameters);
    final Injector mockInjector = Mockito.mock(Injector.class);
    Mockito.when(mockInjector.getInstance(HttpServletRequest.class)).thenReturn(mock);
    Mockito.when(mock.getInputStream()).thenReturn(mockStream);
    final Parameter parameter = Mockito.mock(Parameter.class);
    final FHIRParameter annotation = Mockito.mock(FHIRParameter.class);
    Mockito.when(annotation.name()).thenReturn("missing");
    Mockito.when(parameter.getAnnotation(FHIRParameter.class)).thenReturn(annotation);
    Mockito.when(parameter.getRawType()).thenAnswer(answer -> Patient.class);
    final ParamResourceFactory factory = new ParamResourceFactory(mockInjector, parameter, parser);
    final WebApplicationException exception = assertThrows(WebApplicationException.class, factory::provide, "Should throw an exception");
    assertAll(() -> assertEquals(HttpStatus.BAD_REQUEST_400, exception.getResponse().getStatus(), "Should be a bad request"), () -> assertEquals("Cannot find matching parameter named `missing`", exception.getMessage(), "Should output which parameter is missing"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Parameters(org.hl7.fhir.dstu3.model.Parameters) ServletInputStream(javax.servlet.ServletInputStream) WebApplicationException(javax.ws.rs.WebApplicationException) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) Injector(com.google.inject.Injector) FHIRParameter(gov.cms.dpc.fhir.annotations.FHIRParameter) Patient(org.hl7.fhir.dstu3.model.Patient) FHIRParameter(gov.cms.dpc.fhir.annotations.FHIRParameter) Parameter(org.glassfish.jersey.server.model.Parameter) IParser(ca.uhn.fhir.parser.IParser) Test(org.junit.jupiter.api.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