-
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 #464 from MeasureAuthoringTool/MAT-6058_UpdateQdmS…
…tratificationAssociation MAT-6058: Adding mongock script to update QDM measure stratification …
- Loading branch information
Showing
3 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/main/java/cms/gov/madie/measure/config/UpdateStratificationAssociation.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,61 @@ | ||
package cms.gov.madie.measure.config; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.commons.collections4.CollectionUtils; | ||
|
||
import cms.gov.madie.measure.repositories.MeasureRepository; | ||
import gov.cms.madie.models.common.ModelType; | ||
import gov.cms.madie.models.measure.Group; | ||
import gov.cms.madie.models.measure.Measure; | ||
import gov.cms.madie.models.measure.Stratification; | ||
import io.mongock.api.annotations.ChangeUnit; | ||
import io.mongock.api.annotations.Execution; | ||
import io.mongock.api.annotations.RollbackExecution; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@ChangeUnit(id = "update_stratification-assocation", order = "1", author = "madie_dev") | ||
@Slf4j | ||
public class UpdateStratificationAssociation { | ||
|
||
@Setter private List<Measure> tempMeasures; | ||
|
||
@Execution | ||
public void removeAssociationFromStratification(MeasureRepository measureRepository) { | ||
List<Measure> measures = measureRepository.findAllByModel(ModelType.QDM_5_6.getValue()); | ||
if (CollectionUtils.isNotEmpty(measures)) { | ||
setTempMeasures(measures); | ||
measures.forEach( | ||
measure -> { | ||
List<Group> groups = measure.getGroups(); | ||
if (CollectionUtils.isNotEmpty(groups)) { | ||
groups.forEach( | ||
group -> { | ||
List<Stratification> strats = group.getStratifications(); | ||
if (CollectionUtils.isNotEmpty((strats))) { | ||
strats.forEach( | ||
strat -> { | ||
log.info("Stratification...", strat); | ||
strat.setAssociation(null); | ||
}); | ||
} | ||
group.setStratifications(strats); | ||
}); | ||
measure.setGroups(groups); | ||
measureRepository.save(measure); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@RollbackExecution | ||
public void rollbackExecution(MeasureRepository measureRepository) { | ||
if (CollectionUtils.isNotEmpty(tempMeasures)) { | ||
tempMeasures.forEach( | ||
measure -> { | ||
measureRepository.save(measure); | ||
}); | ||
} | ||
} | ||
} |
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
127 changes: 127 additions & 0 deletions
127
src/test/java/cms/gov/madie/measure/config/UpdateStratificationAssociationTest.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,127 @@ | ||
package cms.gov.madie.measure.config; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.doReturn; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import cms.gov.madie.measure.repositories.MeasureRepository; | ||
import gov.cms.madie.models.common.ModelType; | ||
import gov.cms.madie.models.measure.Group; | ||
import gov.cms.madie.models.measure.Measure; | ||
import gov.cms.madie.models.measure.PopulationType; | ||
import gov.cms.madie.models.measure.Stratification; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class UpdateStratificationAssociationTest { | ||
|
||
@Mock private MeasureRepository measureRepository; | ||
@Mock private Measure measure; | ||
@InjectMocks private UpdateStratificationAssociation updateStratificationAssociation; | ||
|
||
private Measure testMeasure; | ||
|
||
private List<Group> testGroups; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
Stratification stratification = new Stratification(); | ||
stratification.setAssociation(PopulationType.INITIAL_POPULATION); | ||
List<Group> groups = List.of(Group.builder().stratifications(List.of(stratification)).build()); | ||
testMeasure = | ||
Measure.builder() | ||
.id("testMeasureId") | ||
.model(ModelType.QDM_5_6.getValue()) | ||
.groups(groups) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void updateStratificationAssociation() { | ||
doReturn(List.of(testMeasure)) | ||
.when(measureRepository) | ||
.findAllByModel(ModelType.QDM_5_6.getValue()); | ||
updateStratificationAssociation.removeAssociationFromStratification(measureRepository); | ||
|
||
Stratification strat = testMeasure.getGroups().get(0).getStratifications().get(0); | ||
assertNotNull(strat); | ||
|
||
assertNull(strat.getAssociation()); | ||
} | ||
|
||
@Test | ||
public void updateStratificationAssociationWithoutGroups() { | ||
testMeasure.setGroups(new ArrayList<Group>()); | ||
doReturn(List.of(testMeasure)) | ||
.when(measureRepository) | ||
.findAllByModel(ModelType.QDM_5_6.getValue()); | ||
updateStratificationAssociation.removeAssociationFromStratification(measureRepository); | ||
|
||
List<Group> groups = testMeasure.getGroups(); | ||
assertTrue(CollectionUtils.isEmpty(groups)); | ||
} | ||
|
||
@Test | ||
public void updateStratificationAssociationWithoutStratifications() { | ||
testMeasure.getGroups().get(0).setStratifications(new ArrayList<Stratification>()); | ||
doReturn(List.of(testMeasure)) | ||
.when(measureRepository) | ||
.findAllByModel(ModelType.QDM_5_6.getValue()); | ||
updateStratificationAssociation.removeAssociationFromStratification(measureRepository); | ||
|
||
List<Stratification> strats = testMeasure.getGroups().get(0).getStratifications(); | ||
assertTrue(CollectionUtils.isEmpty(strats)); | ||
} | ||
|
||
@Test | ||
public void updateStratificationAssociation_ModelMismatch() { | ||
|
||
doReturn(new ArrayList<Measure>()) | ||
.when(measureRepository) | ||
.findAllByModel(ModelType.QDM_5_6.getValue()); | ||
updateStratificationAssociation.removeAssociationFromStratification(measureRepository); | ||
|
||
Stratification strat = testMeasure.getGroups().get(0).getStratifications().get(0); | ||
assertNotNull(strat); | ||
|
||
assertEquals(PopulationType.INITIAL_POPULATION, strat.getAssociation()); | ||
} | ||
|
||
@Test | ||
public void testRollbackExecutionHasMeasures() { | ||
ReflectionTestUtils.setField( | ||
updateStratificationAssociation, "tempMeasures", List.of(testMeasure)); | ||
|
||
updateStratificationAssociation.rollbackExecution(measureRepository); | ||
|
||
Stratification strat = testMeasure.getGroups().get(0).getStratifications().get(0); | ||
assertNotNull(strat); | ||
|
||
assertEquals(PopulationType.INITIAL_POPULATION, strat.getAssociation()); | ||
} | ||
|
||
@Test | ||
public void testRollbackExecutionNoMeasures() { | ||
ReflectionTestUtils.setField(updateStratificationAssociation, "tempMeasures", new ArrayList()); | ||
|
||
updateStratificationAssociation.rollbackExecution(measureRepository); | ||
|
||
Stratification strat = testMeasure.getGroups().get(0).getStratifications().get(0); | ||
assertNotNull(strat); | ||
|
||
assertEquals(PopulationType.INITIAL_POPULATION, strat.getAssociation()); | ||
} | ||
} |