-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #751 from MeasureAuthoringTool/MAT-7933/updatingSt…
…ratificationAssociationInTestCasesWhenItIsChangedInGroups MAT-7933 updating stratification association in test cases when it is changed in groups
- Loading branch information
Showing
4 changed files
with
281 additions
and
31 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...main/java/cms/gov/madie/measure/config/UpdateQICoreTestCaseStratificationsChangeUnit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cms.gov.madie.measure.config; | ||
|
||
import cms.gov.madie.measure.repositories.MeasureRepository; | ||
import gov.cms.madie.models.common.ModelType; | ||
import gov.cms.madie.models.measure.Measure; | ||
import gov.cms.madie.models.measure.Stratification; | ||
import gov.cms.madie.models.measure.TestCasePopulationValue; | ||
import io.mongock.api.annotations.ChangeUnit; | ||
import io.mongock.api.annotations.Execution; | ||
import io.mongock.api.annotations.RollbackExecution; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@ChangeUnit(id = "qi_core_testcase_strat_update", order = "1", author = "madie_dev") | ||
public class UpdateQICoreTestCaseStratificationsChangeUnit { | ||
@Setter @Getter private List<Measure> tempMeasures; | ||
|
||
@Execution | ||
public void updateQICoreTestCaseStratifications(MeasureRepository measureRepository) | ||
throws Exception { | ||
log.debug("Entering updateQICoreTestCaseStratifications()"); | ||
List<Measure> measureList = measureRepository.findAllByModel(ModelType.QI_CORE.getValue()); | ||
log.info("found QI Core measures: {}", measureList.size()); | ||
|
||
measureList.forEach( | ||
measure -> { | ||
if (!measure.isActive() | ||
|| CollectionUtils.isEmpty(measure.getGroups()) | ||
|| CollectionUtils.isEmpty(measure.getTestCases())) { | ||
return; | ||
} | ||
for (var testCase : measure.getTestCases()) { | ||
if (CollectionUtils.isEmpty(testCase.getGroupPopulations())) { | ||
continue; | ||
} | ||
for (var groupPopulation : testCase.getGroupPopulations()) { | ||
if (CollectionUtils.isEmpty(groupPopulation.getStratificationValues())) { | ||
continue; | ||
} | ||
for (var stratificationValue : groupPopulation.getStratificationValues()) { | ||
Optional<Stratification> matchingGroupStratification = | ||
measure.getGroups().stream() | ||
.flatMap(group -> group.getStratifications().stream()) | ||
.filter(strat -> strat.getId().equals(stratificationValue.getId())) | ||
.findFirst(); | ||
|
||
if (matchingGroupStratification.isPresent() | ||
&& stratificationValue.getPopulationValues() != null) { | ||
List<TestCasePopulationValue> filteredStratifiedPopulationValues = | ||
stratificationValue.getPopulationValues().stream() | ||
.filter( | ||
populationValue -> | ||
matchingGroupStratification.get().getAssociations().stream() | ||
.anyMatch( | ||
validAssociation -> | ||
validAssociation | ||
.getDisplay() | ||
.equalsIgnoreCase( | ||
populationValue.getName().getDisplay()))) | ||
.collect(Collectors.toList()); | ||
stratificationValue.setPopulationValues(filteredStratifiedPopulationValues); | ||
} | ||
} | ||
} | ||
} | ||
|
||
measure.setTestCases(measure.getTestCases()); | ||
measureRepository.save(measure); | ||
}); | ||
} | ||
|
||
@RollbackExecution | ||
public void rollbackExecution(MeasureRepository measureRepository) throws Exception { | ||
log.debug("Entering rollbackExecution() "); | ||
if (CollectionUtils.isNotEmpty(tempMeasures)) { | ||
tempMeasures.forEach(measureRepository::save); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
.../java/cms/gov/madie/measure/config/UpdateQICoreTestCaseStratificationsChangeUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package cms.gov.madie.measure.config; | ||
|
||
import cms.gov.madie.measure.repositories.MeasureRepository; | ||
import gov.cms.madie.models.common.ModelType; | ||
import gov.cms.madie.models.measure.*; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.List; | ||
|
||
import static org.hamcrest.CoreMatchers.*; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class UpdateQICoreTestCaseStratificationsChangeUnitTest { | ||
|
||
@Mock private MeasureRepository measureRepository; | ||
|
||
@Captor private ArgumentCaptor<Measure> measureArgumentCaptor; | ||
|
||
@InjectMocks | ||
UpdateQICoreTestCaseStratificationsChangeUnit updateQICoreTestCaseStratificationsChangeUnit; | ||
|
||
@Test | ||
void testStratsAreUpdatedInTestCasesAndRollbackHoldsMeasures() throws Exception { | ||
List<TestCasePopulationValue> testCaseStratificationPopulationValues = | ||
List.of( | ||
TestCasePopulationValue.builder() | ||
.name(PopulationType.INITIAL_POPULATION) | ||
.expected(true) | ||
.build(), | ||
TestCasePopulationValue.builder() | ||
.name(PopulationType.DENOMINATOR) | ||
.expected(true) | ||
.build(), | ||
TestCasePopulationValue.builder() | ||
.name(PopulationType.NUMERATOR) | ||
.expected(true) | ||
.build()); | ||
|
||
final TestCaseGroupPopulation tcGroupPop = | ||
TestCaseGroupPopulation.builder() | ||
.groupId("group-1") | ||
.scoring(MeasureScoring.CONTINUOUS_VARIABLE.toString()) | ||
.stratificationValues( | ||
List.of( | ||
TestCaseStratificationValue.builder() | ||
.id("strat-1") | ||
.populationValues(testCaseStratificationPopulationValues) | ||
.build())) | ||
.build(); | ||
|
||
List<Measure> qiCoreMeasures = | ||
List.of( | ||
Measure.builder().measureName("M0_DEAD").active(false).build(), | ||
Measure.builder().measureName("M0_NOGROUPS").active(true).groups(List.of()).build(), | ||
Measure.builder() | ||
.measureName("M1") | ||
.active(true) | ||
.model(ModelType.QI_CORE.getValue()) | ||
.groups( | ||
List.of( | ||
Group.builder() | ||
.id("group-1") | ||
.stratifications( | ||
List.of( | ||
Stratification.builder() | ||
.id("strat-1") | ||
.associations( | ||
List.of( | ||
PopulationType.INITIAL_POPULATION, | ||
PopulationType.NUMERATOR)) | ||
.build())) | ||
.build())) | ||
.testCases( | ||
List.of(TestCase.builder().groupPopulations(List.of(tcGroupPop)).build())) | ||
.build()); | ||
|
||
when(measureRepository.findAllByModel(anyString())).thenReturn(qiCoreMeasures); | ||
updateQICoreTestCaseStratificationsChangeUnit.updateQICoreTestCaseStratifications( | ||
measureRepository); | ||
verify(measureRepository, times(1)).save(measureArgumentCaptor.capture()); | ||
List<Measure> allUpdatedMeasures = measureArgumentCaptor.getAllValues(); | ||
assertThat(allUpdatedMeasures, is(notNullValue())); | ||
assertThat(allUpdatedMeasures.size(), is(equalTo(1))); | ||
} | ||
|
||
@Test | ||
void testRollbackDoesNothingForNullTempMeasures() throws Exception { | ||
updateQICoreTestCaseStratificationsChangeUnit.setTempMeasures(null); | ||
updateQICoreTestCaseStratificationsChangeUnit.rollbackExecution(measureRepository); | ||
verifyNoInteractions(measureRepository); | ||
} | ||
|
||
@Test | ||
void testRollbackDoesNothingForEmptyTempMeasures() throws Exception { | ||
updateQICoreTestCaseStratificationsChangeUnit.setTempMeasures(List.of()); | ||
updateQICoreTestCaseStratificationsChangeUnit.rollbackExecution(measureRepository); | ||
verifyNoInteractions(measureRepository); | ||
} | ||
|
||
@Test | ||
void testRollbackHandlesRollback() throws Exception { | ||
updateQICoreTestCaseStratificationsChangeUnit.setTempMeasures( | ||
List.of(Measure.builder().build(), Measure.builder().build())); | ||
updateQICoreTestCaseStratificationsChangeUnit.rollbackExecution(measureRepository); | ||
verify(measureRepository, times(2)).save(measureArgumentCaptor.capture()); | ||
} | ||
} |
Oops, something went wrong.