11package cms .gov .madie .measure .services ;
22
3+ import cms .gov .madie .measure .dto .MadieFeatureFlag ;
34import cms .gov .madie .measure .dto .PackageDto ;
45import cms .gov .madie .measure .exceptions .BadVersionRequestException ;
56import cms .gov .madie .measure .exceptions .BundleOperationException ;
1011import cms .gov .madie .measure .repositories .CqmMeasureRepository ;
1112import cms .gov .madie .measure .repositories .ExportRepository ;
1213import cms .gov .madie .measure .repositories .MeasureRepository ;
14+ import cms .gov .madie .measure .utils .TestCaseServiceUtil ;
1315import gov .cms .madie .models .common .ModelType ;
1416import gov .cms .madie .models .common .Version ;
1517import gov .cms .madie .models .cqm .CqmMeasure ;
16- import gov .cms .madie .models .measure .ElmJson ;
17- import gov .cms .madie .models .measure .Group ;
18- import gov .cms .madie .models .measure .Measure ;
19- import gov .cms .madie .models .measure .MeasureMetaData ;
20- import gov .cms .madie .models .measure .TestCase ;
2118import gov .cms .madie .packaging .utils .PackagingUtilityFactory ;
2219import gov .cms .madie .packaging .utils .qicore411 .PackagingUtilityImpl ;
2320import gov .cms .madie .models .measure .*;
3330import org .mockito .Mock ;
3431import org .mockito .MockedStatic ;
3532import org .mockito .junit .jupiter .MockitoExtension ;
36- import org .springframework .http .ResponseEntity ;
3733
3834import java .io .ByteArrayInputStream ;
3935import java .time .Instant ;
5046import static org .mockito .ArgumentMatchers .anyBoolean ;
5147import static org .mockito .ArgumentMatchers .anyInt ;
5248import static org .mockito .ArgumentMatchers .anyString ;
49+ import static org .mockito .ArgumentMatchers .eq ;
5350import static org .mockito .Mockito .*;
5451
5552@ ExtendWith (MockitoExtension .class )
@@ -70,6 +67,8 @@ public class VersionServiceTest {
7067 @ Mock ElmTranslatorClient elmTranslatorClient ;
7168 @ Mock FhirServicesClient fhirServicesClient ;
7269
70+ @ Mock private TestCaseValidationService testCaseValidationService ;
71+
7372 @ Captor private ArgumentCaptor <Measure > measureCaptor ;
7473 @ Captor private ArgumentCaptor <CqmMeasure > cqmMeasureCaptor ;
7574 @ Captor private ArgumentCaptor <Export > exportArgumentCaptor ;
@@ -859,8 +858,6 @@ public void testCreateDraftSuccessfullyForQiCoreJsonInvalid() {
859858 .thenReturn (false );
860859 when (measureRepository .save (any (Measure .class ))).thenReturn (versionedCopy );
861860 when (actionLogService .logAction (anyString (), any (), any (), anyString ())).thenReturn (true );
862- when (fhirServicesClient .validateBundle (anyString (), any (ModelType .class ), anyString ()))
863- .thenReturn (ResponseEntity .ok (validTestCaseHapiOperationOutcome ));
864861
865862 Measure draft =
866863 versionService .createDraft (
@@ -925,8 +922,6 @@ public void testCreateDraftSuccessfullyForQiCore() {
925922 .thenReturn (false );
926923 when (measureRepository .save (any (Measure .class ))).thenReturn (versionedCopy );
927924 when (actionLogService .logAction (anyString (), any (), any (), anyString ())).thenReturn (true );
928- when (fhirServicesClient .validateBundle (anyString (), any (ModelType .class ), anyString ()))
929- .thenReturn (ResponseEntity .ok (validTestCaseHapiOperationOutcome ));
930925
931926 Measure draft =
932927 versionService .createDraft (
@@ -1043,8 +1038,6 @@ public void testCreateDraftWithUpdatedModelSuccessfully() {
10431038 .thenReturn (false );
10441039 when (measureRepository .save (any (Measure .class ))).thenReturn (versionedCopy );
10451040 when (actionLogService .logAction (anyString (), any (), any (), anyString ())).thenReturn (true );
1046- when (fhirServicesClient .validateBundle (anyString (), any (ModelType .class ), anyString ()))
1047- .thenReturn (ResponseEntity .ok (validTestCaseHapiOperationOutcome ));
10481041
10491042 versionService .createDraft (
10501043 versionedMeasure .getId (), "Test" , "QI-Core v6.0.0" , "test-user" , TEST_ACCESS_TOKEN );
@@ -1228,8 +1221,6 @@ public void testCreateDraftCopyCaseNumberFromExistingTestCase() {
12281221 .thenReturn (false );
12291222 when (measureRepository .save (any (Measure .class ))).thenReturn (versionedCopy );
12301223 when (actionLogService .logAction (anyString (), any (), any (), anyString ())).thenReturn (true );
1231- when (fhirServicesClient .validateBundle (anyString (), any (ModelType .class ), anyString ()))
1232- .thenReturn (ResponseEntity .ok (invalidTestCaseHapiOperationOutcome ));
12331224 Measure draft =
12341225 versionService .createDraft (
12351226 versionedMeasure .getId (), "Test" , MODEL_QI_CORE , "test-user" , TEST_ACCESS_TOKEN );
@@ -1299,8 +1290,6 @@ public void testCreateDraftCopyCaseNumberFromSequenceGenerator() {
12991290 when (measureRepository .save (any (Measure .class ))).thenReturn (versionedCopy );
13001291 when (actionLogService .logAction (anyString (), any (), any (), anyString ())).thenReturn (true );
13011292 when (sequenceService .generateSequence (anyString ())).thenReturn (1 );
1302- when (fhirServicesClient .validateBundle (anyString (), any (ModelType .class ), anyString ()))
1303- .thenReturn (ResponseEntity .ok (validTestCaseHapiOperationOutcome ));
13041293 Measure draft =
13051294 versionService .createDraft (
13061295 versionedMeasure .getId (), "Test" , MODEL_QI_CORE , "test-user" , TEST_ACCESS_TOKEN );
@@ -1471,8 +1460,6 @@ public void testCreateQiCore600DraftSuccessfully() {
14711460 when (measureRepository .existsByMeasureSetIdAndActiveAndMeasureMetaDataDraft (
14721461 anyString (), anyBoolean (), anyBoolean ()))
14731462 .thenReturn (false );
1474- when (fhirServicesClient .validateBundle (anyString (), any (ModelType .class ), anyString ()))
1475- .thenReturn (ResponseEntity .ok (validTestCaseHapiOperationOutcome ));
14761463 when (measureRepository .save (any (Measure .class ))).thenReturn (versionedCopy );
14771464 when (actionLogService .logAction (anyString (), any (), any (), anyString ())).thenReturn (true );
14781465
@@ -1538,8 +1525,6 @@ public void testCreateQiCore700DraftSuccessfully() {
15381525 when (measureRepository .existsByMeasureSetIdAndActiveAndMeasureMetaDataDraft (
15391526 anyString (), anyBoolean (), anyBoolean ()))
15401527 .thenReturn (false );
1541- when (fhirServicesClient .validateBundle (anyString (), any (ModelType .class ), anyString ()))
1542- .thenReturn (ResponseEntity .ok (validTestCaseHapiOperationOutcome ));
15431528 when (measureRepository .save (any (Measure .class ))).thenReturn (versionedCopy );
15441529 when (actionLogService .logAction (anyString (), any (), any (), anyString ())).thenReturn (true );
15451530
@@ -1599,4 +1584,77 @@ public void testCreateDraftWhenMeasureMetaDataIsNull() {
15991584 equalTo (
16001585 "Can not create a draft for the measure \" Test\" . Only versioned measure can be drafted." )));
16011586 }
1587+
1588+ @ Test
1589+ public void testCreateDraftSuccessfullyWhenModelIsChanged () {
1590+ TestCaseGroupPopulation clonedTestCaseGroupPopulation =
1591+ TestCaseGroupPopulation .builder ()
1592+ .groupId ("clonedGroupId1" )
1593+ .scoring ("Cohort" )
1594+ .populationBasis ("boolean" )
1595+ .build ();
1596+ Measure versionedMeasure = buildBasicMeasure ();
1597+ testCase .setJson (json .replace ("}" , "" ));
1598+ versionedMeasure .setTestCases (List .of (testCase ));
1599+ MeasureMetaData metaData = new MeasureMetaData ();
1600+ metaData .setDraft (true );
1601+ Measure versionedCopy =
1602+ versionedMeasure .toBuilder ()
1603+ .id ("2" )
1604+ .model (MODEL_QI_CORE )
1605+ .versionId ("13-13-13-13" )
1606+ .measureName ("Test" )
1607+ .measureMetaData (metaData )
1608+ .groups (List .of (cvGroup .toBuilder ().id (ObjectId .get ().toString ()).build ()))
1609+ .testCases (
1610+ List .of (
1611+ testCase .toBuilder ()
1612+ .id (ObjectId .get ().toString ())
1613+ .groupPopulations (List .of (clonedTestCaseGroupPopulation ))
1614+ .hapiOperationOutcome (validTestCaseHapiOperationOutcome )
1615+ .build ()))
1616+ .build ();
1617+
1618+ when (measureRepository .findById (anyString ())).thenReturn (Optional .of (versionedMeasure ));
1619+ when (measureRepository .existsByMeasureSetIdAndActiveAndMeasureMetaDataDraft (
1620+ anyString (), anyBoolean (), anyBoolean ()))
1621+ .thenReturn (false );
1622+ when (measureRepository .save (any (Measure .class ))).thenReturn (versionedCopy );
1623+ when (actionLogService .logAction (anyString (), any (), any (), anyString ())).thenReturn (true );
1624+
1625+ when (appConfigService .isFlagEnabled (MadieFeatureFlag .STU_6_TEST_CASE_VALIDATION ))
1626+ .thenReturn (true );
1627+ // Mocks a validation request awaiting execution.
1628+ when (testCaseValidationService .validateResourceAsynchronously (
1629+ any (), any (TestCase .class ), eq (TestCaseServiceUtil .SAVE ), anyString ()))
1630+ .thenAnswer (
1631+ invocation ->
1632+ invocation .getArgument (1 , TestCase .class ).toBuilder ()
1633+ .validationStatus (TestCaseValidationStatus .PENDING .toString ())
1634+ .build ());
1635+
1636+ Measure draft =
1637+ versionService .createDraft (
1638+ versionedMeasure .getId (),
1639+ "Test" ,
1640+ ModelType .QI_CORE_6_0_0 .getValue (),
1641+ "test-user" ,
1642+ TEST_ACCESS_TOKEN );
1643+
1644+ assertThat (draft .getMeasureName (), is (equalTo ("Test" )));
1645+ assertThat (draft .getMeasureMetaData ().isDraft (), is (equalTo (true )));
1646+ assertThat (draft .getVersion ().getMajor (), is (equalTo (2 )));
1647+ assertThat (draft .getVersion ().getMinor (), is (equalTo (3 )));
1648+ assertThat (draft .getVersion ().getRevisionNumber (), is (equalTo (1 )));
1649+ assertThat (draft .getGroups ().size (), is (equalTo (1 )));
1650+ assertFalse (draft .getGroups ().stream ().anyMatch (item -> "xyz-p12r-12ert" .equals (item .getId ())));
1651+ assertThat (draft .getTestCases ().size (), is (equalTo (1 )));
1652+ assertFalse (draft .getGroups ().stream ().anyMatch (item -> "testId1" .equals (item .getId ())));
1653+ assertThat (
1654+ draft .getTestCases ().get (0 ).getGroupPopulations ().get (0 ).getGroupId (),
1655+ is (equalTo ("clonedGroupId1" )));
1656+ assertTrue (draft .getTestCases ().get (0 ).getHapiOperationOutcome ().isSuccessful ());
1657+ assertFalse (draft .getTestCases ().get (0 ).getJson ().contains ("2024-10-11T01:30:10.123+00:00" ));
1658+ assertFalse (draft .getTestCases ().get (0 ).getJson ().contains ("2024-10-10T01:31:20.456+00:00" ));
1659+ }
16021660}
0 commit comments