Skip to content

Commit

Permalink
BFD-3064: Updated test and PatientProviderResource classes IRT 404 er…
Browse files Browse the repository at this point in the history
…r. (#2081)
  • Loading branch information
aadedejifearless authored Dec 7, 2023
1 parent ea92f71 commit abe26d1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.newrelic.api.agent.Trace;
import gov.cms.bfd.model.codebook.data.CcwCodebookVariable;
import gov.cms.bfd.model.rif.entities.Beneficiary;
Expand Down Expand Up @@ -50,7 +51,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
Expand Down Expand Up @@ -907,24 +907,27 @@ private Patient queryDatabaseByHash(
matchingBenes.size());
}

// Then, if we found more than one distinct BENE_ID, or none, throw an error.
long distinctBeneIds =
List<Long> distinctBeneIds =
matchingBenes.stream()
.map(Beneficiary::getBeneficiaryId)
.filter(Objects::nonNull)
.distinct()
.count();
Beneficiary beneficiary = null;
if (distinctBeneIds <= 0) {
.sorted()
.collect(ImmutableList.toImmutableList());

Beneficiary beneficiary;
if (distinctBeneIds.size() == 0) {
throw new NoResultException();
} else if (distinctBeneIds > 1) {
} else if (distinctBeneIds.size() > 1) {
BfdMDC.put(
"database_query_by_hash_collision_distinct_bene_ids", Long.toString(distinctBeneIds));
"database_query_by_hash_collision_distinct_bene_ids",
Long.toString(distinctBeneIds.size()));
throw new ResourceNotFoundException(
"By hash query found more than one distinct BENE_ID: " + Long.toString(distinctBeneIds));
} else if (distinctBeneIds == 1) {
beneficiary = matchingBenes.get(0);
"By hash query found more than one distinct BENE_ID: "
+ distinctBeneIds.size()
+ ", DistinctBeneIdsList: "
+ distinctBeneIds);
}
beneficiary = matchingBenes.get(0);

// Null out the unhashed HICNs; in v2 we are ignoring HICNs
beneficiary.setHicnUnhashed(Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.newrelic.api.agent.Trace;
import gov.cms.bfd.model.codebook.data.CcwCodebookVariable;
import gov.cms.bfd.model.rif.entities.Beneficiary;
Expand Down Expand Up @@ -50,7 +51,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
Expand Down Expand Up @@ -932,24 +932,29 @@ private Patient queryDatabaseByHash(
}

// Then, if we found more than one distinct BENE_ID, or none, throw an error.
long distinctBeneIds =
List<Long> distinctBeneIds =
matchingBenes.stream()
.map(Beneficiary::getBeneficiaryId)
.filter(Objects::nonNull)
.distinct()
.count();
Beneficiary beneficiary = null;
if (distinctBeneIds <= 0) {
.sorted()
.collect(ImmutableList.toImmutableList());

Beneficiary beneficiary;
if (distinctBeneIds.size() == 0) {
throw new NoResultException();
} else if (distinctBeneIds > 1) {
} else if (distinctBeneIds.size() > 1) {
BfdMDC.put(
"database_query_by_hash_collision_distinct_bene_ids", Long.toString(distinctBeneIds));
"database_query_by_hash_collision_distinct_bene_ids",
Long.toString(distinctBeneIds.size()));
throw new ResourceNotFoundException(
"By hash query found more than one distinct BENE_ID: " + Long.toString(distinctBeneIds));
} else if (distinctBeneIds == 1) {
beneficiary = matchingBenes.get(0);
"By hash query found more than one distinct BENE_ID: "
+ distinctBeneIds.size()
+ ", DistinctBeneIdsList: "
+ distinctBeneIds);
}

beneficiary = matchingBenes.get(0);

// Null out the unhashed HICNs if we're not supposed to be returning them
if (!requestHeader.isHICNinIncludeIdentifiers()) {
beneficiary.setHicnUnhashed(Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
Expand Down Expand Up @@ -160,9 +162,22 @@ public void testPatientByIdentifierWhenMbiHashWithBeneDupesExpect404() {
// bene ids
List<Object> loadedRecords = loadDataWithAdditionalBeneHistory();
// history has the same mbi, so returns the same hash;
// basically second param doesnt matter for this test
// basically second param doesn't matter for this test
String mbiHash = getMbiHash(currentMbi, false, loadedRecords);

Map<Optional<String>, List<Beneficiary>> beneficiaryMap =
loadedRecords.stream()
.filter(Beneficiary.class::isInstance)
.map(Beneficiary.class::cast)
.collect(Collectors.groupingBy(Beneficiary::getMbiHash));

List<Long> distinctBeneIdList =
beneficiaryMap.get(Optional.of(mbiHash)).stream()
.map(Beneficiary::getBeneficiaryId)
.distinct()
.sorted()
.toList();

String requestString =
patientEndpoint
+ "?identifier="
Expand All @@ -177,7 +192,13 @@ public void testPatientByIdentifierWhenMbiHashWithBeneDupesExpect404() {
.expect()
.statusCode(404)
.body("issue.severity", hasItem("error"))
.body("issue.diagnostics", hasItem("By hash query found more than one distinct BENE_ID: 5"))
.body(
"issue.diagnostics",
hasItem(
"By hash query found more than one distinct BENE_ID: "
+ distinctBeneIdList.size()
+ ", DistinctBeneIdsList: "
+ distinctBeneIdList))
.when()
.get(requestString);
}
Expand All @@ -193,6 +214,19 @@ public void testPatientByIdentifierWhenMbiHashWithHistoryBeneDupesExpect404() {
List<Object> loadedRecords = loadDataWithAdditionalBeneHistory();
String mbiHash = getMbiHash("DUPHISTMBI", true, loadedRecords);

Map<Optional<String>, List<BeneficiaryHistory>> beneficiaryMap =
loadedRecords.stream()
.filter(BeneficiaryHistory.class::isInstance)
.map(BeneficiaryHistory.class::cast)
.distinct()
.collect(Collectors.groupingBy(BeneficiaryHistory::getMbiHash));

List<Long> distinctBeneIdList =
beneficiaryMap.get(Optional.of(mbiHash)).stream()
.map(BeneficiaryHistory::getBeneficiaryId)
.sorted()
.toList();

String requestString =
patientEndpoint
+ "?identifier="
Expand All @@ -207,7 +241,13 @@ public void testPatientByIdentifierWhenMbiHashWithHistoryBeneDupesExpect404() {
.expect()
.statusCode(404)
.body("issue.severity", hasItem("error"))
.body("issue.diagnostics", hasItem("By hash query found more than one distinct BENE_ID: 2"))
.body(
"issue.diagnostics",
hasItem(
"By hash query found more than one distinct BENE_ID: "
+ distinctBeneIdList.size()
+ ", DistinctBeneIdsList: "
+ distinctBeneIdList))
.when()
.get(requestString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.hamcrest.Matcher;
import org.hl7.fhir.dstu3.model.Patient;
Expand Down Expand Up @@ -435,6 +436,15 @@ public void testPatientByIdentifierWhenMbiHashWithBeneDupesExpect404() {
// basically second param doesnt matter for this test
String hicnHash = getHicnHash("543217066U", false, loadedRecords);

Map<String, List<Beneficiary>> beneficiaryMap =
loadedRecords.stream()
.filter(Beneficiary.class::isInstance)
.map(Beneficiary.class::cast)
.collect(Collectors.groupingBy(Beneficiary::getHicn));

List<Long> distinctBeneIdList =
beneficiaryMap.get(hicnHash).stream().map(Beneficiary::getBeneficiaryId).sorted().toList();

String requestString =
patientEndpoint
+ "?identifier="
Expand All @@ -449,7 +459,13 @@ public void testPatientByIdentifierWhenMbiHashWithBeneDupesExpect404() {
.expect()
.statusCode(404)
.body("issue.severity", hasItem("error"))
.body("issue.diagnostics", hasItem("By hash query found more than one distinct BENE_ID: 5"))
.body(
"issue.diagnostics",
hasItem(
"By hash query found more than one distinct BENE_ID: "
+ distinctBeneIdList.size()
+ ", DistinctBeneIdsList: "
+ distinctBeneIdList))
.when()
.get(requestString);
}
Expand All @@ -466,6 +482,18 @@ public void testPatientByIdentifierWhenHicnHashWithHistoryBeneDupesExpect404() {
List<Object> loadedRecords = loadDataWithAdditionalBeneHistory();
String hicnHash = getHicnHash("DUPHISTHIC", true, loadedRecords);

Map<String, List<BeneficiaryHistory>> beneficiaryMap =
loadedRecords.stream()
.filter(BeneficiaryHistory.class::isInstance)
.map(BeneficiaryHistory.class::cast)
.collect(Collectors.groupingBy(BeneficiaryHistory::getHicn));

List<Long> distinctBeneIdList =
beneficiaryMap.get(hicnHash).stream()
.map(BeneficiaryHistory::getBeneficiaryId)
.sorted()
.toList();

String requestString =
patientEndpoint
+ "?identifier="
Expand All @@ -480,7 +508,13 @@ public void testPatientByIdentifierWhenHicnHashWithHistoryBeneDupesExpect404() {
.expect()
.statusCode(404)
.body("issue.severity", hasItem("error"))
.body("issue.diagnostics", hasItem("By hash query found more than one distinct BENE_ID: 2"))
.body(
"issue.diagnostics",
hasItem(
"By hash query found more than one distinct BENE_ID: "
+ distinctBeneIdList.size()
+ ", DistinctBeneIdsList: "
+ distinctBeneIdList))
.when()
.get(requestString);
}
Expand Down

0 comments on commit abe26d1

Please sign in to comment.