From cf6ce99e126317889cc010f5d44ae6ca8f8d5497 Mon Sep 17 00:00:00 2001 From: CeciliaLiu8 Date: Tue, 20 Feb 2024 13:16:16 -0600 Subject: [PATCH] MAT-6724 add measure observation to human readable --- madie-checkstyle.xml | 2 +- .../controllers/CqlToolsController.java | 8 +-- .../service/HumanReadableService.java | 55 ++++++++++++++----- .../service/HumanReadableServiceTest.java | 31 ++++++++++- 4 files changed, 76 insertions(+), 20 deletions(-) diff --git a/madie-checkstyle.xml b/madie-checkstyle.xml index 48faca8d..4ea54c57 100644 --- a/madie-checkstyle.xml +++ b/madie-checkstyle.xml @@ -12,7 +12,7 @@ - + diff --git a/src/main/java/gov/cms/mat/cql_elm_translation/controllers/CqlToolsController.java b/src/main/java/gov/cms/mat/cql_elm_translation/controllers/CqlToolsController.java index 9fbb1b1b..f47fc7df 100644 --- a/src/main/java/gov/cms/mat/cql_elm_translation/controllers/CqlToolsController.java +++ b/src/main/java/gov/cms/mat/cql_elm_translation/controllers/CqlToolsController.java @@ -32,7 +32,9 @@ public class CqlToolsController { private final HumanReadableService humanReadableService; private final CqlParsingService cqlParsingService; - @PutMapping(value = "/cql/format", produces = {MediaType.TEXT_PLAIN_VALUE}) + @PutMapping( + value = "/cql/format", + produces = {MediaType.TEXT_PLAIN_VALUE}) public ResponseEntity formatCql(@RequestBody String cqlData, Principal principal) { try (var cqlDataStream = new ByteArrayInputStream(cqlData.getBytes())) { CqlFormatterVisitor.FormatResult formatResult = @@ -43,9 +45,7 @@ public ResponseEntity formatCql(@RequestBody String cqlData, Principal p "Unable to format CQL, because one or more Syntax errors are identified"); } log.info("User [{}] successfully formatted the CQL", principal.getName()); - return ResponseEntity.ok() - .contentType(MediaType.TEXT_PLAIN) - .body(formatResult.getOutput()); + return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(formatResult.getOutput()); } catch (IOException e) { log.info("User [{}] is unable to format the CQL", principal.getName()); throw new CqlFormatException(e.getMessage()); diff --git a/src/main/java/gov/cms/mat/cql_elm_translation/service/HumanReadableService.java b/src/main/java/gov/cms/mat/cql_elm_translation/service/HumanReadableService.java index fceea23e..2be8f95a 100644 --- a/src/main/java/gov/cms/mat/cql_elm_translation/service/HumanReadableService.java +++ b/src/main/java/gov/cms/mat/cql_elm_translation/service/HumanReadableService.java @@ -32,6 +32,7 @@ import java.text.Collator; import java.text.DateFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; @@ -180,8 +181,10 @@ List buildPopCriteria( .name(group.getGroupDescription()) .populations( Stream.concat( - buildPopulations(group, allDefinitions).stream(), - buildStratification(group, allDefinitions).stream()) + Stream.concat( + buildPopulations(group, allDefinitions).stream(), + buildStratification(group, allDefinitions).stream()), + buildMeasureObservation(group, allDefinitions).stream()) .toList()) .build()) .collect(Collectors.toList()); @@ -216,18 +219,42 @@ String getCQLDefinitionLogic(String id, Set allDefinitions) { List buildStratification( Group group, Set allDefinitions) { - return group.getStratifications().stream() - .map( - stratification -> - HumanReadablePopulationModel.builder() - .name("Stratification") - .id(stratification.getId()) - .display("Stratification") - .logic(getCQLDefinitionLogic(stratification.getCqlDefinition(), allDefinitions)) - .expressionName(stratification.getCqlDefinition()) - .inGroup(!StringUtils.isBlank(stratification.getCqlDefinition())) - .build()) - .collect(Collectors.toList()); + if (!CollectionUtils.isEmpty(group.getStratifications())) { + return group.getStratifications().stream() + .map( + stratification -> + HumanReadablePopulationModel.builder() + .name("Stratification") + .id(stratification.getId()) + .display("Stratification") + .logic( + getCQLDefinitionLogic(stratification.getCqlDefinition(), allDefinitions)) + .expressionName(stratification.getCqlDefinition()) + .inGroup(!StringUtils.isBlank(stratification.getCqlDefinition())) + .build()) + .collect(Collectors.toList()); + } + return Collections.emptyList(); + } + + List buildMeasureObservation( + Group group, Set allDefinitions) { + if (!CollectionUtils.isEmpty(group.getMeasureObservations())) { + return group.getMeasureObservations().stream() + .map( + measureObservation -> + HumanReadablePopulationModel.builder() + .name(measureObservation.getDefinition()) + .id(measureObservation.getId()) + .display(measureObservation.getDefinition()) + .logic( + getCQLDefinitionLogic(measureObservation.getDefinition(), allDefinitions)) + .expressionName(measureObservation.getDefinition()) + .inGroup(!StringUtils.isBlank(measureObservation.getDefinition())) + .build()) + .collect(Collectors.toList()); + } + return Collections.emptyList(); } List buildDefinitions(Set allDefinitions) { diff --git a/src/test/java/gov/cms/mat/cql_elm_translation/service/HumanReadableServiceTest.java b/src/test/java/gov/cms/mat/cql_elm_translation/service/HumanReadableServiceTest.java index c7135b66..b901a07a 100644 --- a/src/test/java/gov/cms/mat/cql_elm_translation/service/HumanReadableServiceTest.java +++ b/src/test/java/gov/cms/mat/cql_elm_translation/service/HumanReadableServiceTest.java @@ -17,6 +17,7 @@ import gov.cms.mat.cql_elm_translation.utils.cql.parsing.model.CQLDefinition; import gov.cms.mat.cql_elm_translation.utils.cql.parsing.model.CQLValueSet; +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; @@ -43,6 +44,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; @@ -147,6 +149,12 @@ void setUp() { .id("testStrat2Id") .cqlDefinition("") .build())) + .measureObservations( + List.of( + MeasureObservation.builder() + .definition(PopulationType.INITIAL_POPULATION.name()) + .build(), + MeasureObservation.builder().build())) .build())) .baseConfigurationTypes(List.of(BaseConfigurationTypes.OUTCOME)) .riskAdjustmentDescription("test risk adjustment") @@ -372,7 +380,10 @@ public void canBuildPopulationCriteriaModelFromMeasure() { assertThat(popCriteriaModel.getName(), is(equalTo(group.getGroupDescription()))); assertThat( popCriteriaModel.getPopulations().size(), - is(group.getPopulations().size() + group.getStratifications().size())); + is( + group.getPopulations().size() + + group.getStratifications().size() + + group.getMeasureObservations().size())); Population measurePopulation = group.getPopulations().get(0); HumanReadablePopulationModel populationModel = popCriteriaModel.getPopulations().get(0); @@ -623,4 +634,22 @@ public void testBuildValuesetTerminologyList() { terminologyModels.get(1).getName(), is(equalTo("Routes of Administration for Opioid Antagonists"))); } + + @Test + public void testbuildStratificationWithNoStratifications() { + Group group = measure.getGroups().get(0); + group.setStratifications(null); + List model = + humanReadableService.buildStratification(group, allDefinitions); + assertTrue(CollectionUtils.isEmpty(model)); + } + + @Test + public void testBuildMeasureObservationWithoutMeasureObservations() { + Group group = measure.getGroups().get(0); + group.setMeasureObservations(null); + List model = + humanReadableService.buildMeasureObservation(group, allDefinitions); + assertTrue(CollectionUtils.isEmpty(model)); + } }