Search in sources :

Example 41 with FhirServerConfig

use of com.ibm.cohort.fhir.client.config.FhirServerConfig in project quality-measure-and-cohort-service by Alvearie.

the class CqlTemporalTests method eventHappensWithin4Days.

@Test
public void eventHappensWithin4Days() throws Exception {
    Patient patient = getPatient("123", Enumerations.AdministrativeGender.FEMALE, null);
    FhirServerConfig fhirConfig = getFhirServerConfig();
    Observation observationIN = new Observation();
    DateTimeType observationEffective = new DateTimeType(new Date());
    observationEffective.setYear(2015);
    observationEffective.setMonth(1);
    observationEffective.setDay(15);
    observationIN.setEffective(observationEffective);
    observationIN.setId("FIRST");
    Observation observationOUT = new Observation();
    DateTimeType observationEffective2 = new DateTimeType(new Date());
    observationEffective2.setYear(2015);
    observationEffective2.setMonth(1);
    observationEffective2.setDay(16);
    observationOUT.setEffective(observationEffective2);
    observationOUT.setId("SECOND");
    Bundle bundle = new Bundle();
    Bundle.BundleEntryComponent firstEncounter = new Bundle.BundleEntryComponent();
    firstEncounter.setResource(observationIN);
    Bundle.BundleEntryComponent secondEncounter = new Bundle.BundleEntryComponent();
    secondEncounter.setResource(observationOUT);
    bundle.addEntry(firstEncounter);
    bundle.addEntry(secondEncounter);
    mockFhirResourceRetrieval("/Observation?subject=Patient%2F123&_format=json", getFhirParser(), bundle, fhirConfig);
    mockFhirResourceRetrieval("/Condition?subject=Patient%2F123&_format=json", getFhirParser(), CONDITION_IN, fhirConfig);
    CqlEvaluator evaluator = setupTestFor(patient, "cql.temporal", ClasspathCqlLibraryProvider.FHIR_HELPERS_CLASSPATH);
    String expression = "ValidObservation within 4 days";
    CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("Test5", "1.0.0"), null, newPatientContext("123"), Collections.singleton(expression));
    Map<String, Object> expected = new HashMap<>();
    expected.put(expression, true);
    Assert.assertEquals(expected, actual.getExpressionResults());
}
Also used : HashMap(java.util.HashMap) Bundle(org.hl7.fhir.r4.model.Bundle) Patient(org.hl7.fhir.r4.model.Patient) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) Date(java.util.Date) DateTimeType(org.hl7.fhir.r4.model.DateTimeType) Observation(org.hl7.fhir.r4.model.Observation) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Example 42 with FhirServerConfig

use of com.ibm.cohort.fhir.client.config.FhirServerConfig in project quality-measure-and-cohort-service by Alvearie.

the class CqlTemporalTests method doesNotFindEventAfterSecondEvent.

@Test
public void doesNotFindEventAfterSecondEvent() throws Exception {
    Patient patient = getPatient("123", Enumerations.AdministrativeGender.FEMALE, null);
    Observation observationOUT = new Observation();
    DateTimeType observationEffective2 = new DateTimeType(new Date());
    observationEffective2.setYear(2017);
    observationEffective2.setMonth(1);
    observationEffective2.setDay(15);
    observationOUT.setEffective(observationEffective2);
    FhirServerConfig fhirConfig = getFhirServerConfig();
    mockFhirResourceRetrieval("/Condition?subject=Patient%2F123&_format=json", getFhirParser(), CONDITION_IN, fhirConfig);
    mockFhirResourceRetrieval("/Encounter?subject=Patient%2F123&_format=json", getFhirParser(), ENCOUNTER_1, fhirConfig);
    mockFhirResourceRetrieval("/Observation?subject=Patient%2F123&_format=json", getFhirParser(), observationOUT, fhirConfig);
    CqlEvaluator evaluator = setupTestFor(patient, "cql.temporal", ClasspathCqlLibraryProvider.FHIR_HELPERS_CLASSPATH);
    String expression = "Observations Exist";
    CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("Test1", "1.0.0"), null, newPatientContext("123"), Collections.singleton(expression));
    Map<String, Object> expected = new HashMap<>();
    expected.put(expression, false);
    Assert.assertEquals(expected, actual.getExpressionResults());
}
Also used : DateTimeType(org.hl7.fhir.r4.model.DateTimeType) HashMap(java.util.HashMap) Observation(org.hl7.fhir.r4.model.Observation) Patient(org.hl7.fhir.r4.model.Patient) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) Date(java.util.Date) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Example 43 with FhirServerConfig

use of com.ibm.cohort.fhir.client.config.FhirServerConfig in project quality-measure-and-cohort-service by Alvearie.

the class CqlTemporalTests method confirmCanFindEventAfterSecondEvent.

@Test
public void confirmCanFindEventAfterSecondEvent() throws Exception {
    Patient patient = getPatient("123", Enumerations.AdministrativeGender.FEMALE, null);
    Observation observationIN = new Observation();
    DateTimeType observationEffective = new DateTimeType(new Date());
    observationEffective.setYear(2015);
    observationEffective.setMonth(1);
    observationEffective.setDay(15);
    observationIN.setEffective(observationEffective);
    FhirServerConfig fhirConfig = getFhirServerConfig();
    mockFhirResourceRetrieval("/Condition?subject=Patient%2F123&_format=json", getFhirParser(), CONDITION_IN, fhirConfig);
    mockFhirResourceRetrieval("/Encounter?subject=Patient%2F123&_format=json", getFhirParser(), ENCOUNTER_1, fhirConfig);
    mockFhirResourceRetrieval("/Observation?subject=Patient%2F123&_format=json", getFhirParser(), observationIN, fhirConfig);
    CqlEvaluator evaluator = setupTestFor(patient, "cql.temporal", ClasspathCqlLibraryProvider.FHIR_HELPERS_CLASSPATH);
    String expression = "Observations Exist";
    CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("Test1", "1.0.0"), null, newPatientContext("123"), Collections.singleton(expression));
    Map<String, Object> expected = new HashMap<>();
    expected.put(expression, true);
    Assert.assertEquals(expected, actual.getExpressionResults());
}
Also used : DateTimeType(org.hl7.fhir.r4.model.DateTimeType) HashMap(java.util.HashMap) Observation(org.hl7.fhir.r4.model.Observation) Patient(org.hl7.fhir.r4.model.Patient) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) Date(java.util.Date) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Example 44 with FhirServerConfig

use of com.ibm.cohort.fhir.client.config.FhirServerConfig in project quality-measure-and-cohort-service by Alvearie.

the class CqlTemporalTests method determineIfAnEventFollows.

@Test
public void determineIfAnEventFollows() throws Exception {
    Patient patient = getPatient("123", Enumerations.AdministrativeGender.FEMALE, null);
    FhirServerConfig fhirConfig = getFhirServerConfig();
    mockFhirResourceRetrieval("/Encounter?subject=Patient%2F123&_format=json", getFhirParser(), ENCOUNTER_3, fhirConfig);
    mockFhirResourceRetrieval("/Condition?subject=Patient%2F123&_format=json", getFhirParser(), CONDITION_IN, fhirConfig);
    CqlEvaluator evaluator = setupTestFor(patient, "cql.temporal", ClasspathCqlLibraryProvider.FHIR_HELPERS_CLASSPATH);
    String expression = "NotFollowedByCondition";
    CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("Test4", "1.0.0"), null, newPatientContext("123"), Collections.singleton(expression));
    Map<String, Object> expected = new HashMap<>();
    expected.put(expression, true);
    Assert.assertEquals(expected, actual.getExpressionResults());
}
Also used : HashMap(java.util.HashMap) Patient(org.hl7.fhir.r4.model.Patient) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Example 45 with FhirServerConfig

use of com.ibm.cohort.fhir.client.config.FhirServerConfig in project quality-measure-and-cohort-service by Alvearie.

the class CohortEngineRestHandler method createValueSet.

@POST
@Path("/valueset/")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_JSON })
@ApiImplicitParams({ // This is necessary for the dark launch feature
@ApiImplicitParam(access = DarkFeatureSwaggerFilter.DARK_FEATURE_CONTROLLED, paramType = "header", dataType = "string"), @ApiImplicitParam(name = FHIR_DATA_SERVER_CONFIG_PART, value = CohortEngineRestHandler.EXAMPLE_DATA_SERVER_CONFIG_JSON, dataTypeClass = FhirServerConfig.class, required = true, paramType = "form", type = "file"), @ApiImplicitParam(name = VALUE_SET_PART, value = VALUE_SET_DESC, dataTypeClass = File.class, required = true, paramType = "form", type = "file"), @ApiImplicitParam(name = CUSTOM_CODE_SYSTEM, value = CUSTOM_CODE_SYSTEM_DESC, dataTypeClass = File.class, paramType = "form", type = "file") })
@ApiResponses(value = { @ApiResponse(code = 201, message = "Successful Operation"), @ApiResponse(code = 400, message = "Bad Request", response = ServiceErrorList.class), @ApiResponse(code = 409, message = "Conflict", response = ServiceErrorList.class), @ApiResponse(code = 500, message = "Server Error", response = ServiceErrorList.class) })
@ApiOperation(value = "Insert a new value set to the fhir server or, if it already exists, update it in place", notes = CohortEngineRestHandler.VALUE_SET_API_NOTES, tags = { "ValueSet" }, nickname = "create_value_set", extensions = { @Extension(properties = { @ExtensionProperty(name = DarkFeatureSwaggerFilter.DARK_FEATURE_NAME, value = CohortEngineRestConstants.DARK_LAUNCHED_VALUE_SET_UPLOAD) }) })
public Response createValueSet(@DefaultValue(ServiceBuildConstants.DATE) @ApiParam(value = ServiceBaseConstants.MINOR_VERSION_DESCRIPTION, required = true, defaultValue = ServiceBuildConstants.DATE) @QueryParam(CohortEngineRestHandler.VERSION) String version, @ApiParam(hidden = true, type = "file", required = true) IMultipartBody multipartBody, @ApiParam(value = CohortEngineRestHandler.VALUE_SET_UPDATE_IF_EXISTS_DESC, defaultValue = "false") @DefaultValue("false") @QueryParam(CohortEngineRestHandler.UPDATE_IF_EXISTS_PARM) boolean updateIfExists) {
    String methodName = MethodNames.CREATE_VALUE_SET.getName();
    Response response;
    ServiceBaseUtility.isDarkFeatureEnabled(CohortEngineRestConstants.DARK_LAUNCHED_VALUE_SET_UPLOAD);
    try {
        // Perform api setup
        Response errorResponse = ServiceBaseUtility.apiSetup(version, logger, methodName);
        if (errorResponse != null) {
            return errorResponse;
        }
        IAttachment dataSourceAttachment = multipartBody.getAttachment(FHIR_DATA_SERVER_CONFIG_PART);
        if (dataSourceAttachment == null) {
            throw new IllegalArgumentException(String.format("Missing '%s' MIME attachment", FHIR_DATA_SERVER_CONFIG_PART));
        }
        // deserialize the MeasuresEvaluation request
        ObjectMapper om = new ObjectMapper();
        FhirServerConfig fhirServerConfig = om.readValue(dataSourceAttachment.getDataHandler().getInputStream(), FhirServerConfig.class);
        // validate the contents of the fhirServerConfig
        validateBean(fhirServerConfig);
        // get the fhir client object used to call to FHIR
        FhirClientBuilder clientBuilder = FhirClientBuilderFactory.newInstance().newFhirClientBuilder();
        IGenericClient terminologyClient = clientBuilder.createFhirClient(fhirServerConfig);
        IAttachment valueSetAttachment = multipartBody.getAttachment(VALUE_SET_PART);
        if (valueSetAttachment == null) {
            throw new IllegalArgumentException(String.format("Missing '%s' MIME attachment", VALUE_SET_PART));
        }
        IAttachment customCodes = multipartBody.getAttachment(CUSTOM_CODE_SYSTEM);
        Map<String, String> customCodeMap = null;
        if (customCodes != null) {
            customCodeMap = ValueSetUtil.getMapFromInputStream(customCodes.getDataHandler().getInputStream());
        }
        ValueSetArtifact artifact;
        try (InputStream is = valueSetAttachment.getDataHandler().getInputStream()) {
            artifact = ValueSetUtil.createArtifact(is, customCodeMap);
        }
        ValueSetUtil.validateArtifact(artifact);
        String valueSetId = ValueSetUtil.importArtifact(terminologyClient, artifact, updateIfExists);
        if (valueSetId == null) {
            return Response.status(Response.Status.CONFLICT).header("Content-Type", "application/json").entity("{\"message\":\"Value Set already exists! Rerun with updateIfExists set to true!\"}").build();
        }
        response = Response.status(Response.Status.CREATED).header("Content-Type", "application/json").entity("{\"valueSetId\":\"" + valueSetId + "\"}").build();
    } catch (Throwable e) {
        return new CohortServiceExceptionMapper().toResponse(e);
    } finally {
        // Perform api cleanup
        Response errorResponse = ServiceBaseUtility.apiCleanup(logger, methodName);
        if (errorResponse != null) {
            response = errorResponse;
        }
    }
    return response;
}
Also used : FhirClientBuilder(com.ibm.cohort.fhir.client.config.FhirClientBuilder) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ValueSetArtifact(com.ibm.cohort.valueset.ValueSetArtifact) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(javax.ws.rs.Path) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

FhirServerConfig (com.ibm.cohort.fhir.client.config.FhirServerConfig)63 Test (org.junit.Test)52 Patient (org.hl7.fhir.r4.model.Patient)39 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)36 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 HashMap (java.util.HashMap)21 File (java.io.File)18 IAttachment (com.ibm.websphere.jaxrs20.multipart.IAttachment)15 Response (javax.ws.rs.core.Response)15 CqlEvaluator (com.ibm.cohort.cql.evaluation.CqlEvaluator)14 CqlVersionedIdentifier (com.ibm.cohort.cql.library.CqlVersionedIdentifier)14 FileWriter (java.io.FileWriter)14 CqlEvaluationResult (com.ibm.cohort.cql.evaluation.CqlEvaluationResult)13 PrintStream (java.io.PrintStream)13 Writer (java.io.Writer)13 ByteArrayInputStream (java.io.ByteArrayInputStream)11 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)10 IMultipartBody (com.ibm.websphere.jaxrs20.multipart.IMultipartBody)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 IParser (ca.uhn.fhir.parser.IParser)9