Skip to content

Commit

Permalink
Merge pull request #488 from MeasureAuthoringTool/MAT-6295_qdmStratif…
Browse files Browse the repository at this point in the history
…icationExpectedValues

MAT-6295 fix group population and stratification changes for QDM test…
  • Loading branch information
sb-cecilialiu authored Oct 30, 2023
2 parents 664643b + a527438 commit d3a1428
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 21 deletions.
43 changes: 42 additions & 1 deletion src/main/java/cms/gov/madie/measure/services/GroupService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.util.CollectionUtils;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -282,7 +283,7 @@ private TestCasePopulationValue updateTestCasePopulation(
return testCasePopulation;
}

private TestCaseStratificationValue updateTestCaseStratification(
protected TestCaseStratificationValue updateTestCaseStratification(
Stratification stratification, TestCaseGroupPopulation testCaseGroup, String strataName) {
// if no cql definition(optional), no need to consider stratification
if (StringUtils.isEmpty(stratification.getCqlDefinition())) {
Expand All @@ -308,9 +309,49 @@ private TestCaseStratificationValue updateTestCaseStratification(
.build();
}
testCaseStrata.setName(strataName);
handlePopulationChange(testCaseStrata, testCaseGroup);

return testCaseStrata;
}

private void handlePopulationChange(
TestCaseStratificationValue testCaseStrata, TestCaseGroupPopulation testCaseGroup) {
List<TestCasePopulationValue> testCasePopulationValues = testCaseStrata.getPopulationValues();
List<TestCasePopulationValue> testCasePopulationValuesFromGroup =
testCaseGroup.getPopulationValues();

if (!CollectionUtils.isEmpty(testCasePopulationValuesFromGroup)) {
if (!CollectionUtils.isEmpty(testCasePopulationValues)) {
for (TestCasePopulationValue testCasePopulationValueFromGroup :
testCasePopulationValuesFromGroup) {
// if there is new population value from testCasePopulationValuesFromGroup
if (!findExistsTestCasePopulationValue(
testCasePopulationValueFromGroup.getId(), testCasePopulationValues)) {
testCasePopulationValues.add(testCasePopulationValueFromGroup);
}
// delete any that is not in testCasePopulationValuesFromGroup
List<TestCasePopulationValue> tempTestCasePopulationValues = new ArrayList<>();
for (TestCasePopulationValue tempTestCasePopulationValue : testCasePopulationValues) {
if (findExistsTestCasePopulationValue(
tempTestCasePopulationValue.getId(), testCasePopulationValuesFromGroup)) {
tempTestCasePopulationValues.add(tempTestCasePopulationValue);
}
}
testCaseStrata.setPopulationValues(tempTestCasePopulationValues);
}
} // when there is new strat
else {
testCaseStrata.setPopulationValues(testCasePopulationValuesFromGroup);
}
}
}

private boolean findExistsTestCasePopulationValue(
String id, List<TestCasePopulationValue> testCasePopulationValues) {
return testCasePopulationValues.stream()
.anyMatch(testCasePopulationValue -> id.equalsIgnoreCase(testCasePopulationValue.getId()));
}

private TestCasePopulationValue findTestCasePopulation(
String populationId, TestCaseGroupPopulation testCaseGroup) {
return testCaseGroup.getPopulationValues().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,13 @@ public void testAdminMeasurePermaDelete() throws Exception {
when(measureService.findMeasureById(anyString())).thenReturn(testMsr);
doNothing().when(measureRepository).delete(any(Measure.class));

mockMvc.perform(
MockMvcRequestBuilders.delete("/admin/measures/{id}", "12345")
.with(csrf())
.with(user(TEST_USER_ID))
.header(ADMIN_TEST_API_KEY_HEADER, ADMIN_TEST_API_KEY_HEADER_VALUE)
.header("Authorization", "test-okta"))
mockMvc
.perform(
MockMvcRequestBuilders.delete("/admin/measures/{id}", "12345")
.with(csrf())
.with(user(TEST_USER_ID))
.header(ADMIN_TEST_API_KEY_HEADER, ADMIN_TEST_API_KEY_HEADER_VALUE)
.header("Authorization", "test-okta"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", equalTo("12345")));
}
Expand All @@ -320,7 +321,8 @@ public void testAdminMeasurePermaDelete() throws Exception {
public void testAdminMeasureDeleteThrowsWhenMeasureNotFound() throws Exception {
when(measureService.findMeasureById(anyString())).thenReturn(null);

mockMvc.perform(
mockMvc
.perform(
MockMvcRequestBuilders.delete("/admin/measures/{id}", "12345")
.with(csrf())
.with(user(TEST_USER_ID))
Expand All @@ -331,11 +333,12 @@ public void testAdminMeasureDeleteThrowsWhenMeasureNotFound() throws Exception {

@Test
public void testBlocksNonAuthorizedDeleteRequests() throws Exception {
mockMvc.perform(
MockMvcRequestBuilders.delete("/admin/measures/{id}", "12345")
.with(csrf())
.with(user(TEST_USER_ID))
.header("Authorization", "test-okta"))
mockMvc
.perform(
MockMvcRequestBuilders.delete("/admin/measures/{id}", "12345")
.with(csrf())
.with(user(TEST_USER_ID))
.header("Authorization", "test-okta"))
.andExpect(status().isForbidden());
}
}
191 changes: 183 additions & 8 deletions src/test/java/cms/gov/madie/measure/services/GroupServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public class GroupServiceTest implements ResourceUtil {
private Group ratioGroup;
private Measure measure;
private Stratification strata1;
private Stratification stratification;
private TestCasePopulationValue testCasePopulationValue1;
private TestCasePopulationValue testCasePopulationValue2;
private TestCasePopulationValue testCasePopulationValue3;
private TestCasePopulationValue testCasePopulationValue4;

@BeforeEach
public void setUp() {
Expand Down Expand Up @@ -206,6 +211,38 @@ public void setUp() {
.lastModifiedBy("test user")
.measureMetaData(MeasureMetaData.builder().draft(true).build())
.build();

stratification = new Stratification();
stratification.setId("stratId");
stratification.setCqlDefinition("truebool1");
testCasePopulationValue1 =
TestCasePopulationValue.builder()
.id("testCasePopulationValue1")
.name(PopulationType.INITIAL_POPULATION)
.expected(Boolean.FALSE)
.actual(Boolean.FALSE)
.build();
testCasePopulationValue2 =
TestCasePopulationValue.builder()
.id("testCasePopulationValue2")
.name(PopulationType.DENOMINATOR)
.expected(Boolean.FALSE)
.actual(Boolean.FALSE)
.build();
testCasePopulationValue3 =
TestCasePopulationValue.builder()
.id("testCasePopulationValue3")
.name(PopulationType.NUMERATOR)
.expected(Boolean.FALSE)
.actual(Boolean.FALSE)
.build();
testCasePopulationValue4 =
TestCasePopulationValue.builder()
.id("testCasePopulationValue4")
.name(PopulationType.DENOMINATOR_EXCLUSION)
.expected(Boolean.FALSE)
.actual(Boolean.FALSE)
.build();
}

@Test
Expand Down Expand Up @@ -921,14 +958,18 @@ public void updateTestCaseGroupToAddMeasurePopulationsAndStratification() {
public void updateTestCaseGroupToAddMeasurePopulationsHandlesNoAssociation() {
// measure group with 4 populations and 2 stratification
Group measureGroup = ratioGroup.toBuilder().build();
measureGroup.setStratifications(measureGroup.getStratifications().stream().map(stratification -> {
Stratification strat = new Stratification();
strat.setId(stratification.getId());
strat.setCqlDefinition(stratification.getCqlDefinition());
strat.setDescription(stratification.getDescription());
strat.setAssociation(null);
return strat;
}).collect(Collectors.toList()));
measureGroup.setStratifications(
measureGroup.getStratifications().stream()
.map(
stratification -> {
Stratification strat = new Stratification();
strat.setId(stratification.getId());
strat.setCqlDefinition(stratification.getCqlDefinition());
strat.setDescription(stratification.getDescription());
strat.setAssociation(null);
return strat;
})
.collect(Collectors.toList()));
TestCaseGroupPopulation testCaseGroup = buildTestCaseRatioGroup();
// no testcase stratification
testCaseGroup.setStratificationValues(null);
Expand Down Expand Up @@ -1302,4 +1343,138 @@ public void testHandleQdmGroupReturnTypesNonPatientBasisThrowsException()
() -> groupService.handleQdmGroupReturnTypes(qdmGroup, qdmMeasure),
"Invalid elm json");
}

@Test
public void testUpdateTestCaseStratificationForAddedGroupPopulation() {

List<TestCasePopulationValue> testCasePopulationValues = new ArrayList<>();

testCasePopulationValues.add(testCasePopulationValue1);
testCasePopulationValues.add(testCasePopulationValue2);
testCasePopulationValues.add(testCasePopulationValue3);

List<TestCaseStratificationValue> testCaseStratificationValues = new ArrayList<>();
List<TestCasePopulationValue> testCaseStrataPopulationValues = new ArrayList<>();
testCaseStrataPopulationValues.add(testCasePopulationValue1);
testCaseStrataPopulationValues.add(testCasePopulationValue2);
TestCaseStratificationValue testCaseStratificationValue1 =
TestCaseStratificationValue.builder()
.id("stratId")
.name("Strata-1")
.expected(Boolean.TRUE)
.actual(Boolean.FALSE)
.populationValues(testCaseStrataPopulationValues)
.build();
testCaseStratificationValues.add(testCaseStratificationValue1);

TestCaseGroupPopulation testCaseGroupPopulation =
TestCaseGroupPopulation.builder()
.groupId("testGroupId")
.scoring("Proportion")
.populationBasis("true")
.populationValues(testCasePopulationValues)
.stratificationValues(testCaseStratificationValues)
.build();

TestCaseStratificationValue testCaseStratificationValue =
groupService.updateTestCaseStratification(
stratification, testCaseGroupPopulation, "Strata-1");
assertTrue(testCaseStratificationValue != null);
assertEquals(testCaseStratificationValue.getPopulationValues().size(), 3);
}

@Test
public void testUpdateTestCaseStratificationForDeletedGroupPopulation() {

List<TestCasePopulationValue> testCasePopulationValues = new ArrayList<>();

testCasePopulationValues.add(testCasePopulationValue1);
testCasePopulationValues.add(testCasePopulationValue2);
testCasePopulationValues.add(testCasePopulationValue3);

List<TestCaseStratificationValue> testCaseStratificationValues = new ArrayList<>();
List<TestCasePopulationValue> testCaseStrataPopulationValues = new ArrayList<>();
testCaseStrataPopulationValues.add(testCasePopulationValue1);
testCaseStrataPopulationValues.add(testCasePopulationValue2);
testCaseStrataPopulationValues.add(testCasePopulationValue3);
testCaseStrataPopulationValues.add(testCasePopulationValue4);

TestCaseStratificationValue testCaseStratificationValue1 =
TestCaseStratificationValue.builder()
.id("stratId")
.name("Strata-1")
.expected(Boolean.TRUE)
.actual(Boolean.FALSE)
.populationValues(testCaseStrataPopulationValues)
.build();
testCaseStratificationValues.add(testCaseStratificationValue1);

TestCaseGroupPopulation testCaseGroupPopulation =
TestCaseGroupPopulation.builder()
.groupId("testGroupId")
.scoring("Proportion")
.populationBasis("true")
.populationValues(testCasePopulationValues)
.stratificationValues(testCaseStratificationValues)
.build();

TestCaseStratificationValue testCaseStratificationValue =
groupService.updateTestCaseStratification(
stratification, testCaseGroupPopulation, "Strata-1");
assertTrue(testCaseStratificationValue != null);
assertEquals(testCaseStratificationValue.getPopulationValues().size(), 3);
}

@Test
public void testUpdateTestCaseStratificationForChangedGroupPopulation() {

List<TestCasePopulationValue> testCasePopulationValues = new ArrayList<>();

testCasePopulationValues.add(testCasePopulationValue1);
testCasePopulationValues.add(testCasePopulationValue2);

List<TestCaseStratificationValue> testCaseStratificationValues = new ArrayList<>();
List<TestCasePopulationValue> testCaseStrataPopulationValues = new ArrayList<>();
testCaseStrataPopulationValues.add(testCasePopulationValue2);
testCaseStrataPopulationValues.add(testCasePopulationValue3);

TestCaseStratificationValue testCaseStratificationValue1 =
TestCaseStratificationValue.builder()
.id("stratId")
.name("Strata-1")
.expected(Boolean.TRUE)
.actual(Boolean.FALSE)
.populationValues(testCaseStrataPopulationValues)
.build();
testCaseStratificationValues.add(testCaseStratificationValue1);

TestCaseGroupPopulation testCaseGroupPopulation =
TestCaseGroupPopulation.builder()
.groupId("testGroupId")
.scoring("Proportion")
.populationBasis("true")
.populationValues(testCasePopulationValues)
.stratificationValues(testCaseStratificationValues)
.build();

TestCaseStratificationValue testCaseStratificationValue =
groupService.updateTestCaseStratification(
stratification, testCaseGroupPopulation, "Strata-1");
assertTrue(testCaseStratificationValue != null);
assertEquals(testCaseStratificationValue.getPopulationValues().size(), 2);
assertNotEquals(
testCaseStratificationValue.getPopulationValues().get(0).getId(),
testCasePopulationValue3.getId());
assertNotEquals(
testCaseStratificationValue.getPopulationValues().get(1).getId(),
testCasePopulationValue3.getId());
}

@Test
public void testUpdateTestCaseStratificationForNonTestCasePopulationValuesFromGroup() {
TestCaseStratificationValue testCaseStratificationValue =
groupService.updateTestCaseStratification(
stratification, new TestCaseGroupPopulation(), "Strata-1");
assertNull(testCaseStratificationValue.getPopulationValues());
}
}

0 comments on commit d3a1428

Please sign in to comment.