use of gov.cms.bfd.model.rif.Beneficiary in project beneficiary-fhir-data by CMSgov.
the class RifLoaderIT method failOnUpdateBeneficiaryBeforeInsert.
/**
* Runs {@link RifLoader} against the {@link StaticRifResourceGroup#SAMPLE_A} data for an <code>
* UPDATE</code> {@link Beneficiary} record that there hasn't been a previous <code>INSERT</code>
* on, to verify that this fails as expected.
*/
@Test
public void failOnUpdateBeneficiaryBeforeInsert() {
// Tweak the SAMPLE_A beneficiary to be an UPDATE.
Stream<RifFile> samplesStream = filterSamples(r -> r.getFileType() == RifFileType.BENEFICIARY, StaticRifResourceGroup.SAMPLE_A.getResources());
Function<RifRecordEvent<?>, List<List<String>>> recordEditor = rifRecordEvent -> {
CSVRecord beneCsvRow = rifRecordEvent.getRawCsvRecords().get(0);
List<String> beneCsvValues = StreamSupport.stream(beneCsvRow.spliterator(), false).collect(Collectors.toList());
beneCsvValues.set(0, "UPDATE");
return List.of(beneCsvValues);
};
Function<RifFile, RifFile> fileEditor = sample -> editSampleRecords(sample, recordEditor);
Stream<RifFile> editedSample = editSamples(samplesStream, fileEditor);
// Load the edited sample to verify that it fails, as expected.
AssertionFailedError thrown = assertThrows(AssertionFailedError.class, () -> {
loadSample("SAMPLE_A, bene only, UPDATE", CcwRifLoadTestUtils.getLoadOptions(), editedSample);
});
assertTrue(thrown.getMessage().contains("Load errors encountered"));
}
use of gov.cms.bfd.model.rif.Beneficiary in project beneficiary-fhir-data by CMSgov.
the class RifLoaderIT method loadInitialEnrollmentShouldCount20SinceThereIsAUpdateOf8Months.
/*
* This test checks that all enrollment data for 2 years has been loaded into the beneficiary
* monthly table and than does an update of 8 years without the 4 other months for the year
*/
@Test
public void loadInitialEnrollmentShouldCount20SinceThereIsAUpdateOf8Months() {
// Loads first year of data
loadSample(Arrays.asList(StaticRifResourceGroup.SAMPLE_A.getResources()));
// Loads second year of data
loadSample(Arrays.asList(StaticRifResourceGroup.SAMPLE_U.getResources()));
// Loads second year of data with only 8 months
loadSample(Arrays.asList(StaticRifResourceGroup.SAMPLE_U_BENES_CHANGED_WITH_8_MONTHS.getResources()));
EntityManagerFactory entityManagerFactory = PipelineTestUtils.get().getPipelineApplicationState().getEntityManagerFactory();
EntityManager entityManager = null;
try {
entityManager = entityManagerFactory.createEntityManager();
Beneficiary beneficiaryFromDb = entityManager.find(Beneficiary.class, "567834");
// Checks to make sure we only have 20 months of data
assertEquals(20, beneficiaryFromDb.getBeneficiaryMonthlys().size());
} finally {
if (entityManager != null)
entityManager.close();
}
}
use of gov.cms.bfd.model.rif.Beneficiary in project beneficiary-fhir-data by CMSgov.
the class RifLoaderTest method isBeneficiaryHistoryEqual.
/**
* Runs a couple of fake HICNs through {@link RifLoader#computeHicnHash(LoadAppOptions,
* SecretKeyFactory, String)} to verify that the expected result is produced.
*/
@Test
public void isBeneficiaryHistoryEqual() {
Beneficiary newBene = new Beneficiary();
LocalDate birthDate = LocalDate.of(1960, 1, 8);
String hicn = "2332j3l2";
Optional<String> hicnUnhased = Optional.of("323232");
char sex = 'M';
Optional<String> medicareBeneficiaryId = Optional.of("beneficiaryId");
Optional<String> mbiHash = Optional.of("mbiHash");
Optional<LocalDate> mbiEffectiveDate = Optional.of(LocalDate.of(2020, 1, 1));
Optional<LocalDate> mbiObsoleteDate = Optional.of(LocalDate.of(2020, 1, 8));
newBene.setBirthDate(birthDate);
newBene.setHicn(hicn);
newBene.setHicnUnhashed(hicnUnhased);
newBene.setSex(sex);
newBene.setMedicareBeneficiaryId(medicareBeneficiaryId);
newBene.setMbiHash(mbiHash);
newBene.setMbiEffectiveDate(mbiEffectiveDate);
newBene.setMbiObsoleteDate(mbiObsoleteDate);
Beneficiary oldBene = new Beneficiary();
oldBene.setBirthDate(birthDate);
oldBene.setHicn(hicn);
oldBene.setHicnUnhashed(hicnUnhased);
oldBene.setSex(sex);
oldBene.setMedicareBeneficiaryId(medicareBeneficiaryId);
oldBene.setMbiHash(mbiHash);
oldBene.setMbiEffectiveDate(mbiEffectiveDate);
oldBene.setMbiObsoleteDate(mbiObsoleteDate);
// Both old and new beneficiary have the same values return true
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary birth date is not the same as old should assert false
newBene.setBirthDate(LocalDate.of(1950, 1, 8));
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Undo New beneficiary birth date and set it back to old should assert true
newBene.setBirthDate(birthDate);
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary hicn is not the same as old should assert false
newBene.setHicn("difHicn");
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Undo New beneficiary hicn and set it back to old should assert true
newBene.setHicn(hicn);
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary hicnunHashed is not the same as old should assert false
newBene.setHicnUnhashed(Optional.of("difHicn"));
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Undo New beneficiary hicnUnhased and set it back to old should assert true
newBene.setHicnUnhashed(hicnUnhased);
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary sex is not the same as old should assert false
newBene.setSex('F');
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Undo New beneficiary sex and set it back to old should assert true
newBene.setSex(sex);
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary mediciarybeneficiaryid is not the same as old should assert false
newBene.setMedicareBeneficiaryId(Optional.of("diff"));
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Undo New beneficiary mediciarybeneficiaryid and set it back to old should assert true
newBene.setMedicareBeneficiaryId(medicareBeneficiaryId);
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary mbihash is not the same as old should assert false
newBene.setMbiHash(Optional.of("mbihashdiff"));
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Undo New beneficiary mbihash and set it back to old should assert true
newBene.setMbiHash(mbiHash);
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary mbiEffectiveDate is not the same as old should assert false
newBene.setMbiEffectiveDate(Optional.of(LocalDate.of(2020, 1, 8)));
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Undo New beneficiary mbiEffectiveDate and set it back to old should assert true
newBene.setMbiEffectiveDate(mbiEffectiveDate);
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary mbiObsoleteDate is not the same as old should assert true
// becasue mbiObsoleteDate is no longer part of the equality check.
newBene.setMbiObsoleteDate(Optional.of(LocalDate.of(2020, 1, 2)));
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Undo New beneficiary mbiObsoleteDate and set it back to old should assert true
newBene.setMbiObsoleteDate(mbiObsoleteDate);
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Check for nulls on fields
// New beneficiary hicnUnhashed is null and the return result should assert false
newBene.setHicnUnhashed(Optional.empty());
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Undo New beneficiary hicnUnhashed and set it back to old should assert true
newBene.setHicnUnhashed(hicnUnhased);
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary sex is null and the return result should assert false
newBene.setSex(Character.MIN_VALUE);
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Undo New beneficiary sex and set it back to old should assert true
newBene.setSex(sex);
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary mediciarybeneficiaryid is null and the return result should assert false
newBene.setMedicareBeneficiaryId(Optional.empty());
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// Undo beneficiary mediciarybeneficiaryid and set it back to old should assert true
newBene.setMedicareBeneficiaryId(medicareBeneficiaryId);
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary mbiEffectiveDate is null and the return result should assert false
newBene.setMbiEffectiveDate(Optional.empty());
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// old beneficiary mbiEffectiveDate was empty and new beneficiary has mbiEffectiveDate
newBene.setMbiEffectiveDate(mbiEffectiveDate);
oldBene.setMbiEffectiveDate(Optional.empty());
assertFalse(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
// New beneficiary mbiObsoleteDate is null and the return result should assert true
// since the test will ignore empty setMbiObsoleteDate.
oldBene.setMbiEffectiveDate(mbiEffectiveDate);
newBene.setMbiObsoleteDate(Optional.empty());
assertTrue(RifLoader.isBeneficiaryHistoryEqual(newBene, oldBene));
}
use of gov.cms.bfd.model.rif.Beneficiary in project beneficiary-fhir-data by CMSgov.
the class R4ExplanationOfBenefitResourceProvider method findClaimTypeByPatient.
/**
* @param claimType the {@link ClaimTypeV2} to find
* @param patientId the {@link Beneficiary#getBeneficiaryId()} to filter by
* @param lastUpdated the update time to filter by
* @return the matching claim/event entities
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Trace
private <T> List<T> findClaimTypeByPatient(ClaimTypeV2 claimType, String patientId, DateRangeParam lastUpdated, DateRangeParam serviceDate) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery((Class) claimType.getEntityClass());
Root root = criteria.from(claimType.getEntityClass());
claimType.getEntityLazyAttributes().stream().forEach(a -> root.fetch(a));
criteria.select(root).distinct(true);
// Search for a beneficiary's records. Use lastUpdated if present
Predicate wherePredicate = builder.equal(root.get(claimType.getEntityBeneficiaryIdAttribute()), patientId);
if (lastUpdated != null && !lastUpdated.isEmpty()) {
Predicate predicate = QueryUtils.createLastUpdatedPredicate(builder, root, lastUpdated);
wherePredicate = builder.and(wherePredicate, predicate);
}
criteria.where(wherePredicate);
List<T> claimEntities = null;
Long eobsByBeneIdQueryNanoSeconds = null;
Timer.Context timerEobQuery = metricRegistry.timer(MetricRegistry.name(metricRegistry.getClass().getSimpleName(), "query", "eobs_by_bene_id", claimType.name().toLowerCase())).time();
try {
claimEntities = entityManager.createQuery(criteria).getResultList();
} finally {
eobsByBeneIdQueryNanoSeconds = timerEobQuery.stop();
TransformerUtilsV2.recordQueryInMdc(String.format("eobs_by_bene_id.%s", claimType.name().toLowerCase()), eobsByBeneIdQueryNanoSeconds, claimEntities == null ? 0 : claimEntities.size());
}
if (claimEntities != null && serviceDate != null && !serviceDate.isEmpty()) {
final Instant lowerBound = serviceDate.getLowerBoundAsInstant() != null ? serviceDate.getLowerBoundAsInstant().toInstant() : null;
final Instant upperBound = serviceDate.getUpperBoundAsInstant() != null ? serviceDate.getUpperBoundAsInstant().toInstant() : null;
final java.util.function.Predicate<LocalDate> lowerBoundCheck = lowerBound == null ? (date) -> true : (date) -> compareLocalDate(date, lowerBound.atZone(ZoneId.systemDefault()).toLocalDate(), serviceDate.getLowerBound().getPrefix());
final java.util.function.Predicate<LocalDate> upperBoundCheck = upperBound == null ? (date) -> true : (date) -> compareLocalDate(date, upperBound.atZone(ZoneId.systemDefault()).toLocalDate(), serviceDate.getUpperBound().getPrefix());
return claimEntities.stream().filter(entity -> lowerBoundCheck.test(claimType.getServiceEndAttributeFunction().apply(entity)) && upperBoundCheck.test(claimType.getServiceEndAttributeFunction().apply(entity))).collect(Collectors.toList());
}
return claimEntities;
}
use of gov.cms.bfd.model.rif.Beneficiary in project beneficiary-fhir-data by CMSgov.
the class R4PatientResourceProvider method searchByCoverageContractAndYearMonth.
@Trace
private Bundle searchByCoverageContractAndYearMonth(// of relational search is more common.
TokenParam coverageId, LocalDate yearMonth, RequestDetails requestDetails) {
checkCoverageId(coverageId);
RequestHeaders requestHeader = RequestHeaders.getHeaderWrapper(requestDetails);
// requested.
if (!requestHeader.isMBIinIncludeIdentifiers() || requestHeader.isHICNinIncludeIdentifiers()) {
throw new InvalidRequestException(String.format("This endpoint requires the '%s: mbi' header.", CommonHeaders.HEADER_NAME_INCLUDE_IDENTIFIERS));
}
PatientLinkBuilder paging = new PatientLinkBuilder(requestDetails.getCompleteUrl());
Operation operation = new Operation(Operation.Endpoint.V2_PATIENT);
operation.setOption("by", "coverageContractForYearMonth");
requestHeader.getNVPairs().forEach((n, v) -> operation.setOption(n, v.toString()));
operation.publishOperationName();
List<Beneficiary> matchingBeneficiaries = fetchBeneficiariesByContractAndYearMonth(coverageId, yearMonth, paging);
boolean hasAnotherPage = matchingBeneficiaries.size() > paging.getPageSize();
if (hasAnotherPage) {
matchingBeneficiaries = matchingBeneficiaries.subList(0, paging.getPageSize());
paging = new PatientLinkBuilder(paging, hasAnotherPage);
}
List<IBaseResource> patients = matchingBeneficiaries.stream().map(b -> BeneficiaryTransformerV2.transform(metricRegistry, b, requestHeader)).collect(Collectors.toList());
Bundle bundle = TransformerUtilsV2.createBundle(patients, paging, loadedFilterManager.getTransactionTime());
TransformerUtilsV2.workAroundHAPIIssue1585(requestDetails);
return bundle;
}
Aggregations