Search in sources :

Example 6 with MedicareSegment

use of gov.cms.bfd.server.war.commons.MedicareSegment in project beneficiary-fhir-data by CMSgov.

the class CoverageTransformerTest method transformSampleARecord.

/**
 * Verifies that {@link
 * gov.cms.bfd.server.war.stu3.providers.CoverageTransformer#transform(MedicareSegment,
 * Beneficiary)} works as expected when run against the {@link StaticRifResource#SAMPLE_A_CARRIER}
 * {@link Beneficiary}.
 *
 * @throws FHIRException (indicates test failure)
 */
@Test
public void transformSampleARecord() throws FHIRException {
    List<Object> parsedRecords = ServerTestUtils.parseData(Arrays.asList(StaticRifResourceGroup.SAMPLE_A.getResources()));
    Beneficiary beneficiary = parsedRecords.stream().filter(r -> r instanceof Beneficiary).map(r -> (Beneficiary) r).findFirst().get();
    beneficiary.setLastUpdated(Instant.now());
    Coverage partACoverage = CoverageTransformer.transform(new MetricRegistry(), MedicareSegment.PART_A, beneficiary);
    assertPartAMatches(beneficiary, partACoverage);
    Coverage partBCoverage = CoverageTransformer.transform(new MetricRegistry(), MedicareSegment.PART_B, beneficiary);
    assertPartBMatches(beneficiary, partBCoverage);
    Coverage partCCoverage = CoverageTransformer.transform(new MetricRegistry(), MedicareSegment.PART_C, beneficiary);
    assertPartCMatches(beneficiary, partCCoverage);
    Coverage partDCoverage = CoverageTransformer.transform(new MetricRegistry(), MedicareSegment.PART_D, beneficiary);
    assertPartDMatches(beneficiary, partDCoverage);
    // Test with null lastUpdated
    beneficiary.setLastUpdated(Optional.empty());
    Coverage partACoverageNullLastUpdated = CoverageTransformer.transform(new MetricRegistry(), MedicareSegment.PART_A, beneficiary);
    assertPartAMatches(beneficiary, partACoverageNullLastUpdated);
}
Also used : Coverage(org.hl7.fhir.dstu3.model.Coverage) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) MetricRegistry(com.codahale.metrics.MetricRegistry) StaticRifResource(gov.cms.bfd.model.rif.samples.StaticRifResource) MedicareSegment(gov.cms.bfd.server.war.commons.MedicareSegment) CoverageStatus(org.hl7.fhir.dstu3.model.Coverage.CoverageStatus) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) List(java.util.List) TransformerConstants(gov.cms.bfd.server.war.commons.TransformerConstants) CcwCodebookVariable(gov.cms.bfd.model.codebook.data.CcwCodebookVariable) Optional(java.util.Optional) FHIRException(org.hl7.fhir.exceptions.FHIRException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) StaticRifResourceGroup(gov.cms.bfd.model.rif.samples.StaticRifResourceGroup) ServerTestUtils(gov.cms.bfd.server.war.ServerTestUtils) MetricRegistry(com.codahale.metrics.MetricRegistry) Coverage(org.hl7.fhir.dstu3.model.Coverage) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) Test(org.junit.jupiter.api.Test)

Example 7 with MedicareSegment

use of gov.cms.bfd.server.war.commons.MedicareSegment in project beneficiary-fhir-data by CMSgov.

the class CoverageResourceProvider method read.

/**
 * Adds support for the FHIR "read" operation, for {@link Coverage}s. The {@link Read} annotation
 * indicates that this method supports the read operation.
 *
 * <p>Read operations take a single parameter annotated with {@link IdParam}, and should return a
 * single resource instance.
 *
 * @param coverageId The read operation takes one parameter, which must be of type {@link IdType}
 *     and must be annotated with the {@link IdParam} annotation.
 * @return Returns a resource matching the specified {@link IdDt}, or <code>null</code> if none
 *     exists.
 */
@Read(version = false)
@Trace
public Coverage read(@IdParam IdType coverageId) {
    if (coverageId == null)
        throw new IllegalArgumentException();
    if (coverageId.getVersionIdPartAsLong() != null)
        throw new IllegalArgumentException();
    String coverageIdText = coverageId.getIdPart();
    if (coverageIdText == null || coverageIdText.trim().isEmpty())
        throw new IllegalArgumentException();
    Operation operation = new Operation(Operation.Endpoint.V1_COVERAGE);
    operation.setOption("by", "id");
    operation.publishOperationName();
    Matcher coverageIdMatcher = COVERAGE_ID_PATTERN.matcher(coverageIdText);
    if (!coverageIdMatcher.matches())
        throw new IllegalArgumentException("Unsupported ID pattern: " + coverageIdText);
    String coverageIdSegmentText = coverageIdMatcher.group(1);
    Optional<MedicareSegment> coverageIdSegment = MedicareSegment.selectByUrlPrefix(coverageIdSegmentText);
    if (!coverageIdSegment.isPresent())
        throw new ResourceNotFoundException(coverageId);
    String coverageIdBeneficiaryIdText = coverageIdMatcher.group(2);
    Beneficiary beneficiaryEntity;
    try {
        beneficiaryEntity = findBeneficiaryById(coverageIdBeneficiaryIdText, null);
        if (!beneficiaryEntity.getBeneEnrollmentReferenceYear().isPresent()) {
            throw new ResourceNotFoundException("Cannot find coverage for non present enrollment year");
        }
    } catch (NoResultException e) {
        throw new ResourceNotFoundException(new IdDt(Beneficiary.class.getSimpleName(), coverageIdBeneficiaryIdText));
    }
    Coverage coverage = CoverageTransformer.transform(metricRegistry, coverageIdSegment.get(), beneficiaryEntity);
    return coverage;
}
Also used : Matcher(java.util.regex.Matcher) MedicareSegment(gov.cms.bfd.server.war.commons.MedicareSegment) IdDt(ca.uhn.fhir.model.primitive.IdDt) Coverage(org.hl7.fhir.dstu3.model.Coverage) Operation(gov.cms.bfd.server.war.Operation) NoResultException(javax.persistence.NoResultException) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) Read(ca.uhn.fhir.rest.annotation.Read) Trace(com.newrelic.api.agent.Trace)

Aggregations

Beneficiary (gov.cms.bfd.model.rif.Beneficiary)7 MedicareSegment (gov.cms.bfd.server.war.commons.MedicareSegment)7 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)6 StaticRifResourceGroup (gov.cms.bfd.model.rif.samples.StaticRifResourceGroup)5 ServerTestUtils (gov.cms.bfd.server.war.ServerTestUtils)5 TransformerConstants (gov.cms.bfd.server.war.commons.TransformerConstants)5 Instant (java.time.Instant)5 Arrays (java.util.Arrays)5 List (java.util.List)5 FHIRException (org.hl7.fhir.exceptions.FHIRException)5 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)5 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)5 Test (org.junit.jupiter.api.Test)5 Constants (ca.uhn.fhir.rest.api.Constants)4 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)4 DateRangeParam (ca.uhn.fhir.rest.param.DateRangeParam)4 InvalidRequestException (ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)4 PipelineTestUtils (gov.cms.bfd.pipeline.sharedutils.PipelineTestUtils)4 Date (java.util.Date)4 Coverage (org.hl7.fhir.dstu3.model.Coverage)4