Skip to content

Commit

Permalink
Merge pull request #464 from MeasureAuthoringTool/MAT-6058_UpdateQdmS…
Browse files Browse the repository at this point in the history
…tratificationAssociation

MAT-6058: Adding mongock script to update QDM measure stratification …
  • Loading branch information
gregory-akins authored Sep 28, 2023
2 parents f2a477a + 0ce40d7 commit 05565ec
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 0 deletions.
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);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ public interface MeasureRepository

boolean existsByMeasureSetIdAndActiveAndMeasureMetaDataDraft(
String setId, boolean active, boolean draft);

// Map measureSetId, boolean (ie.,
// id 1 - drafted , 2 - versioned setId 4 4, false
// id 1 - versioned , 2 - versioned setId 4 4, true
List<Measure> findAllByMeasureSetIdInAndActiveAndMeasureMetaDataDraft(
List<String> setIds, boolean active, boolean draft);

List<Measure> findAllByModel(String model);
}
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());
}
}

0 comments on commit 05565ec

Please sign in to comment.