Skip to content

Commit

Permalink
MAT-6491 fixed QI Core TC expected values dropped when observations e…
Browse files Browse the repository at this point in the history
…xist
  • Loading branch information
sb-prateekkeerthi committed Dec 11, 2024
1 parent c2e33bb commit 603fdd8
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,12 @@ public ResponseEntity<String> deleteCmsId(
@RequestParam(name = "cmsId") Integer cmsId,
@Value("${admin-api-key}") String apiKey,
Principal principal) {
log.info("User [{}] - Started admin task [deleteCmsId] and is attempting to delete " +
"CMS id [{}] from measure with measure id [{}]", principal.getName(), cmsId, measureId);
log.info(
"User [{}] - Started admin task [deleteCmsId] and is attempting to delete "
+ "CMS id [{}] from measure with measure id [{}]",
principal.getName(),
cmsId,
measureId);
return ResponseEntity.status(HttpStatus.OK)
.body(measureSetService.deleteCmsId(measureId, cmsId));
}
Expand Down
47 changes: 30 additions & 17 deletions src/main/java/cms/gov/madie/measure/services/MeasureSetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,45 +155,58 @@ public String deleteCmsId(String measureId, Integer cmsId) {

if (measure.isPresent()) {
String measureSetId = measure.get().getMeasureSetId();
Optional<MeasureSet> optionalMeasureSet = measureSetRepository.findByMeasureSetId(measureSetId);
Optional<MeasureSet> optionalMeasureSet =
measureSetRepository.findByMeasureSetId(measureSetId);

if (optionalMeasureSet.isEmpty()) {
throw new ResourceNotFoundException("No measure set exists for measure with measure set id of "
+ measureSetId);
throw new ResourceNotFoundException(
"No measure set exists for measure with measure set id of " + measureSetId);
}

MeasureSet measureSet = optionalMeasureSet.get();

if (measureSet.getCmsId() == null) {
throw new ResourceNotFoundException(String.format("No CMS id of %s exists to be deleted " +
"within measure set with measure set id of %s", cmsId, measureSetId));
throw new ResourceNotFoundException(
String.format(
"No CMS id of %s exists to be deleted "
+ "within measure set with measure set id of %s",
cmsId, measureSetId));
}

if (!measureSet.getCmsId().equals(cmsId)) {
throw new InvalidIdException(
String.format("CMS id of %s passed in does not match CMS id of %s within " +
"measure set with measure set id of %s", cmsId,measureSet.getCmsId(), measureSetId));
String.format(
"CMS id of %s passed in does not match CMS id of %s within "
+ "measure set with measure set id of %s",
cmsId, measureSet.getCmsId(), measureSetId));
}

List<Measure> measures = measureRepository.findAllByMeasureSetIdAndActive(measureSetId, true);

if (measures.size() > 1) {
throw new InvalidRequestException(String.format("Measure set with measure set id of %s contains more than 1 measure. " +
"Cannot delete CMS id when measure set has more than 1 version of measure.", measureSetId));
throw new InvalidRequestException(
String.format(
"Measure set with measure set id of %s contains more than 1 measure. "
+ "Cannot delete CMS id when measure set has more than 1 version of measure.",
measureSetId));
}

measureSet.setCmsId(null);
measureSetRepository.save(measureSet);

log.info("With the measure id of [{}], successfully queried " +
"for its measure set with measure set id of [{}] and deleted CMS id " +
"of [{}] from the measure set", measureId, measureSetId, cmsId);

return String.format("CMS id of %s was deleted successfully from " +
"measure set with measure set id of %s", cmsId, measureSetId);
log.info(
"With the measure id of [{}], successfully queried "
+ "for its measure set with measure set id of [{}] and deleted CMS id "
+ "of [{}] from the measure set",
measureId,
measureSetId,
cmsId);

return String.format(
"CMS id of %s was deleted successfully from " + "measure set with measure set id of %s",
cmsId, measureSetId);
} else {
throw new ResourceNotFoundException(
"No measure exists with measure id of " + measureId);
throw new ResourceNotFoundException("No measure exists with measure id of " + measureId);
}
}

Expand Down
28 changes: 26 additions & 2 deletions src/main/java/cms/gov/madie/measure/utils/TestCaseServiceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,24 @@ public static List<TestCasePopulationValue> getObservationPopulations(
return null;
}

// exported test cases doesn't have criteria references
// this method will filter the populations and gets the relevant criteria reference id for
// observation
private static String getCriteriaReferenceFromPopulations(
List<Population> populations, String observationName) {
String relevantPopulation =
PopulationType.DENOMINATOR_OBSERVATION.name().equalsIgnoreCase(observationName)
? "DENOMINATOR"
: "NUMERATOR";
Optional<String> matchedCriteriaReference =
populations.stream()
.filter(
(population) -> population.getName().name().equalsIgnoreCase(relevantPopulation))
.map(population -> population.getId())
.findFirst();
return matchedCriteriaReference.orElse(null);
}

public static List<TestCaseGroupPopulation> assignObservationIdAndCriteriaReferenceCVAndRatio(
List<TestCaseGroupPopulation> testCaseGroupPopulations, List<Group> measureGroups) {
if (isNotEmpty(measureGroups)
Expand Down Expand Up @@ -495,10 +513,16 @@ && isNotEmpty(testCaseGroupPopulations)
int number = 0;
for (TestCasePopulationValue value : denomAndNumerObservations) {
value.setId(
(number == 0 ? "denominator" : "numerator")
(PopulationType.DENOMINATOR_OBSERVATION
.name()
.equalsIgnoreCase(value.getName().name())
? "denominator"
: "numerator")
+ "Observation"
+ String.valueOf(number));
value.setCriteriaReference(observations.get(number).getCriteriaReference());
value.setCriteriaReference(
getCriteriaReferenceFromPopulations(
group.getPopulations(), value.getName().name()));
number++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,16 @@ void deleteCmsId() {
.acls(null)
.build();

String expectedBody = String.format("CMS Id of %s was deleted successfully from measure set with measure set id of %s", measureSet.getCmsId(), measureSet.getMeasureSetId());
String expectedBody =
String.format(
"CMS Id of %s was deleted successfully from measure set with measure set id of %s",
measureSet.getCmsId(), measureSet.getMeasureSetId());

when(measureSetService.deleteCmsId(anyString(), anyInt())).thenReturn(expectedBody);

ResponseEntity<String> response = controller.deleteCmsId(mockHttpServletRequest, measureId, measureSet.getCmsId(), "apiKey", principal);
ResponseEntity<String> response =
controller.deleteCmsId(
mockHttpServletRequest, measureId, measureSet.getCmsId(), "apiKey", principal);

assertThat(response.getBody(), is(notNullValue()));
assertEquals(expectedBody, response.getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,25 @@ public void testCreateCmsIdWhenCmsIdAlreadyExistsInMeasureSet() {
public void testDeleteCmsId() {
Integer cmsId = 1;
Measure measure =
Measure.builder()
.model(ModelType.QI_CORE.getValue())
.measureSetId("measureSetId1")
.build();
Measure.builder().model(ModelType.QI_CORE.getValue()).measureSetId("measureSetId1").build();

List<Measure> measures = Collections.singletonList(measure);
MeasureSet measureSet = MeasureSet.builder().measureSetId("1").cmsId(1).build();
String measureId = "measureId";

when(measureRepository.findById(anyString())).thenReturn(Optional.of( measure));
when(measureRepository.findById(anyString())).thenReturn(Optional.of(measure));
when(measureSetRepository.findByMeasureSetId(anyString())).thenReturn(Optional.of(measureSet));
when(measureRepository.findAllByMeasureSetIdAndActive(anyString(), anyBoolean())).thenReturn(measures);
when(measureRepository.findAllByMeasureSetIdAndActive(anyString(), anyBoolean()))
.thenReturn(measures);
when(measureSetRepository.save(any(MeasureSet.class))).thenReturn(measureSet);

String responseBody = measureSetService.deleteCmsId(measureId, cmsId);

assertEquals(responseBody, String.format("CMS id of %s was deleted successfully from measure set with measure set id of %s", cmsId, measure.getMeasureSetId()));
assertEquals(
responseBody,
String.format(
"CMS id of %s was deleted successfully from measure set with measure set id of %s",
cmsId, measure.getMeasureSetId()));
verify(measureRepository, times(1)).findById(anyString());
verify(measureSetRepository, times(1)).findByMeasureSetId(anyString());
verify(measureRepository, times(1)).findAllByMeasureSetIdAndActive(anyString(), anyBoolean());
Expand All @@ -295,8 +297,7 @@ public void testDeleteCmsIdWhenMeasureWithMeasureIdIsNotFound() {

Exception ex =
assertThrows(
ResourceNotFoundException.class,
() -> measureSetService.deleteCmsId(measureId, 1));
ResourceNotFoundException.class, () -> measureSetService.deleteCmsId(measureId, 1));

assertTrue(
ex.getMessage()
Expand All @@ -309,24 +310,23 @@ public void testDeleteCmsIdWhenMeasureWithMeasureIdIsNotFound() {
@Test
public void testDeleteCmsIdWhenMeasureSetIsNotFound() {
Measure measure =
Measure.builder()
.model(ModelType.QI_CORE.getValue())
.measureSetId("measureSetId")
.build();
Measure.builder().model(ModelType.QI_CORE.getValue()).measureSetId("measureSetId").build();

String measureId = "measureId";

when(measureRepository.findById(anyString())).thenReturn(Optional.of( measure));
when(measureRepository.findById(anyString())).thenReturn(Optional.of(measure));
when(measureSetRepository.findByMeasureSetId(anyString())).thenReturn(Optional.empty());

Exception ex =
assertThrows(
ResourceNotFoundException.class,
() -> measureSetService.deleteCmsId(measureId, 1));
ResourceNotFoundException.class, () -> measureSetService.deleteCmsId(measureId, 1));

assertTrue(
ex.getMessage()
.contains(String.format("No measure set exists for measure with measure set id of %s", measure.getMeasureSetId())));
.contains(
String.format(
"No measure set exists for measure with measure set id of %s",
measure.getMeasureSetId())));
verify(measureRepository, times(1)).findById(anyString());
verify(measureSetRepository, times(1)).findByMeasureSetId(anyString());
verify(measureSetRepository, times(0)).save(any(MeasureSet.class));
Expand All @@ -336,25 +336,24 @@ public void testDeleteCmsIdWhenMeasureSetIsNotFound() {
public void testDeleteCmsIdWhenCmsIdIsNotFoundInMeasureSet() {
Integer cmsId = 1;
Measure measure =
Measure.builder()
.model(ModelType.QI_CORE.getValue())
.measureSetId("measureSetId")
.build();
Measure.builder().model(ModelType.QI_CORE.getValue()).measureSetId("measureSetId").build();

MeasureSet measureSet = MeasureSet.builder().measureSetId("1").build();
String measureId = "measureId";

when(measureRepository.findById(anyString())).thenReturn(Optional.of( measure));
when(measureRepository.findById(anyString())).thenReturn(Optional.of(measure));
when(measureSetRepository.findByMeasureSetId(anyString())).thenReturn(Optional.of(measureSet));

Exception ex =
assertThrows(
ResourceNotFoundException.class,
() -> measureSetService.deleteCmsId(measureId, cmsId));
ResourceNotFoundException.class, () -> measureSetService.deleteCmsId(measureId, cmsId));

assertTrue(
ex.getMessage()
.contains(String.format("No CMS id of %s exists to be deleted within measure set with measure set id of %s", cmsId, measure.getMeasureSetId())));
.contains(
String.format(
"No CMS id of %s exists to be deleted within measure set with measure set id of %s",
cmsId, measure.getMeasureSetId())));
verify(measureRepository, times(1)).findById(anyString());
verify(measureSetRepository, times(1)).findByMeasureSetId(anyString());
verify(measureSetRepository, times(0)).save(any(MeasureSet.class));
Expand All @@ -364,60 +363,58 @@ public void testDeleteCmsIdWhenCmsIdIsNotFoundInMeasureSet() {
public void testDeleteCmsIdWhenCmsIdToDeleteDoesNotMatchCmsIdInMeasureSet() {
Integer cmsId = 1;
Measure measure =
Measure.builder()
.model(ModelType.QI_CORE.getValue())
.measureSetId("measureSetId")
.build();
Measure.builder().model(ModelType.QI_CORE.getValue()).measureSetId("measureSetId").build();

MeasureSet measureSet = MeasureSet.builder().measureSetId("1").cmsId(2).build();
String measureId = "measureId";

when(measureRepository.findById(anyString())).thenReturn(Optional.of( measure));
when(measureRepository.findById(anyString())).thenReturn(Optional.of(measure));
when(measureSetRepository.findByMeasureSetId(anyString())).thenReturn(Optional.of(measureSet));

Exception ex =
assertThrows(
InvalidIdException.class,
() -> measureSetService.deleteCmsId(measureId, cmsId));
InvalidIdException.class, () -> measureSetService.deleteCmsId(measureId, cmsId));

assertTrue(
ex.getMessage()
.contains(String.format("CMS id of %s passed in does not match CMS id of %s within measure set with measure set id of %s", cmsId, measureSet.getCmsId(), measure.getMeasureSetId())));
.contains(
String.format(
"CMS id of %s passed in does not match CMS id of %s within measure set with measure set id of %s",
cmsId, measureSet.getCmsId(), measure.getMeasureSetId())));
verify(measureRepository, times(1)).findById(anyString());
verify(measureSetRepository, times(1)).findByMeasureSetId(anyString());
verify(measureSetRepository, times(0)).save(any(MeasureSet.class));
}

@Test
public void testDeleteCmsIdWhenMeasureHasMultipleVersions() {
Integer cmsId = 1;
Measure measure1 =
Measure.builder()
.model(ModelType.QI_CORE.getValue())
.measureSetId("measureSetId1")
.build();
Measure.builder().model(ModelType.QI_CORE.getValue()).measureSetId("measureSetId1").build();

Measure measure2 =
Measure.builder()
.model(ModelType.QI_CORE.getValue())
.measureSetId("measureSetId2")
.build();
Measure.builder().model(ModelType.QI_CORE.getValue()).measureSetId("measureSetId2").build();

List<Measure> measures = Arrays.asList(measure1, measure2);
MeasureSet measureSet = MeasureSet.builder().measureSetId("1").cmsId(1).build();
String measureId = "measureId";

when(measureRepository.findById(anyString())).thenReturn(Optional.of( measure1));
when(measureRepository.findById(anyString())).thenReturn(Optional.of(measure1));
when(measureSetRepository.findByMeasureSetId(anyString())).thenReturn(Optional.of(measureSet));
when(measureRepository.findAllByMeasureSetIdAndActive(anyString(), anyBoolean())).thenReturn(measures);
when(measureRepository.findAllByMeasureSetIdAndActive(anyString(), anyBoolean()))
.thenReturn(measures);

Exception ex =
assertThrows(
InvalidRequestException.class,
() -> measureSetService.deleteCmsId(measureId, cmsId));
InvalidRequestException.class, () -> measureSetService.deleteCmsId(measureId, cmsId));

assertTrue(
ex.getMessage()
.contains(String.format(String.format("Measure set with measure set id of %s contains more than 1 measure. Cannot delete CMS id when measure set has more than 1 version of measure.", measure1.getMeasureSetId()))));
.contains(
String.format(
String.format(
"Measure set with measure set id of %s contains more than 1 measure. Cannot delete CMS id when measure set has more than 1 version of measure.",
measure1.getMeasureSetId()))));
verify(measureRepository, times(1)).findById(anyString());
verify(measureSetRepository, times(1)).findByMeasureSetId(anyString());
verify(measureRepository, times(1)).findAllByMeasureSetIdAndActive(anyString(), anyBoolean());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ void testAssignObservationIdAndCriteriaReferenceRatio() {
Group.builder()
.id("group1")
.scoring(MeasureScoring.RATIO.toString())
.populations(List.of(population1, population2, population3, population4, population5))
.measureObservations(List.of(measureObservation1, measureObservation2))
.build();

Expand Down Expand Up @@ -1317,12 +1318,12 @@ void testAssignObservationIdAndCriteriaReferenceRatio() {
is(equalTo("denominatorObservation0")));
assertThat(
results.get(0).getPopulationValues().get(2).getCriteriaReference(),
is(equalTo("criteriaReference1")));
is(equalTo("Population2Id")));
assertThat(
results.get(0).getPopulationValues().get(4).getId(), is(equalTo("numeratorObservation1")));
assertThat(
results.get(0).getPopulationValues().get(4).getCriteriaReference(),
is(equalTo("criteriaReference2")));
is(equalTo("Population4Id")));
}

@Test
Expand Down

0 comments on commit 603fdd8

Please sign in to comment.