Skip to content

Commit

Permalink
Merge pull request #11 from MeasureAuthoringTool/feature/MAT-7439-enf…
Browse files Browse the repository at this point in the history
…orce-FHIRHelpers

Feature/mat 7439 enforce fhir helpers
  • Loading branch information
nmorasb authored Aug 5, 2024
2 parents 9242ae8 + dac71d4 commit 2d11447
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package gov.cms.mat.cql_elm_translation.exceptions;

import org.cqframework.cql.cql2elm.CqlCompilerException;
import org.cqframework.cql.elm.tracking.TrackBack;
import org.hl7.elm.r1.VersionedIdentifier;

public class MissingLibraryCqlCompilerException extends CqlCompilerException {
private static final String MESSAGE =
"%s is required as an included library for QI-Core. "
+ "Please add the appropriate version of %s to your CQL.";

public MissingLibraryCqlCompilerException(
final String library, VersionedIdentifier identifier, int lineNumber) {
super(
String.format(MESSAGE, library, library),
CqlCompilerException.ErrorSeverity.Error,
new TrackBack(identifier, lineNumber, 0, lineNumber, 0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import gov.cms.mat.cql_elm_translation.cql_translator.MadieLibrarySourceProvider;
import gov.cms.mat.cql_elm_translation.data.RequestData;
import gov.cms.mat.cql_elm_translation.exceptions.InternalServerException;
import gov.cms.mat.cql_elm_translation.exceptions.MissingLibraryCqlCompilerException;
import gov.cms.mat.cql_elm_translation.service.filters.AnnotationErrorFilter;
import gov.cms.mat.cql_elm_translation.service.filters.CqlTranslatorExceptionFilter;
import gov.cms.mat.cql_elm_translation.service.support.CqlExceptionErrorProcessor;
Expand Down Expand Up @@ -54,6 +55,9 @@ public CqlConversionPayload processCqlDataWithErrors(RequestData requestData) {
// Gets the translator results
CqlTranslator cqlTranslator = processCqlData(requestData);

// QI-Core measures require FHIRHelpers...enforce this validation only for measure CQL
processForMissingFhirHelpersLibrary(cqlTranslator, requestData.getCqlData());

List<CqlCompilerException> cqlTranslatorExceptions =
processErrors(
requestData.getCqlData(), requestData.isShowWarnings(), cqlTranslator.getExceptions());
Expand Down Expand Up @@ -89,6 +93,35 @@ public CqlConversionPayload processCqlDataWithErrors(RequestData requestData) {
return CqlConversionPayload.builder().json(jsonWithErrors).xml(cqlTranslator.toXml()).build();
}

/**
* MODIFIES INPUT PARAMETER Checks for FHIRHelpers library and adds an exception on the
* CqlTranslator object if missing. Exception is not added if the CQL is for the FHIRHelpers
* library itself.
*
* @param cqlTranslator
* @param cql
*/
public void processForMissingFhirHelpersLibrary(CqlTranslator cqlTranslator, String cql) {
VersionedIdentifier identifier =
cqlTranslator.getTranslatedLibrary().getLibrary().getIdentifier();
if (StringUtils.isNotBlank(cql)
&& identifier != null
&& !identifier.getId().contains("FHIRHelpers")) {
Library.Includes includes = cqlTranslator.getTranslatedLibrary().getLibrary().getIncludes();
if (includes == null
|| includes.getDef() == null
|| includes.getDef().isEmpty()
|| !includes.getDef().stream()
.anyMatch(includeDef -> includeDef.getPath().contains("FHIRHelpers"))) {
cqlTranslator
.getExceptions()
.add(
new MissingLibraryCqlCompilerException(
"FHIRHelpers", cqlTranslator.getTranslatedLibrary().getIdentifier(), 1));
}
}
}

public List<TranslatedLibrary> getTranslatedLibrariesForCql(String cql, String accessToken)
throws IOException {
if (StringUtils.isBlank(cql)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import gov.cms.mat.cql_elm_translation.cql_translator.MadieLibrarySourceProvider;
import gov.cms.mat.cql_elm_translation.data.RequestData;
import gov.cms.mat.cql_elm_translation.exceptions.InternalServerException;
import lombok.extern.slf4j.Slf4j;
import org.cqframework.cql.cql2elm.LibraryContentType;
import org.cqframework.cql.cql2elm.model.CompiledLibrary;
import org.hl7.elm.r1.Library;
Expand Down Expand Up @@ -43,6 +44,7 @@
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;

@Slf4j
@SpringBootTest
class CqlConversionServiceTest implements ResourceFileUtil {

Expand Down Expand Up @@ -84,9 +86,15 @@ void testProcessCqlDataWithErrors() {
try {
JsonNode jsonNode = objectMapper.readTree(resultJson);
assertNotNull(jsonNode);
JsonNode libraryNode = jsonNode.at("/errorExceptions");
assertNotNull(libraryNode);
assertTrue(libraryNode.isMissingNode());
JsonNode libraryNodeEx = jsonNode.at("/errorExceptions");
assertNotNull(libraryNodeEx);
assertFalse(libraryNodeEx.isMissingNode());
assertThat(libraryNodeEx.isArray(), is(true));
assertThat(
libraryNodeEx.get(0).get("message").textValue(),
is(
equalTo(
"FHIRHelpers is required as an included library for QI-Core. Please add the appropriate version of FHIRHelpers to your CQL.")));
} catch (JsonProcessingException e) {
fail(e.getMessage());
}
Expand Down Expand Up @@ -136,9 +144,15 @@ void testProcessCqlDataWithErrorsQICore() {
try {
JsonNode jsonNode = objectMapper.readTree(resultJson);
assertNotNull(jsonNode);
JsonNode libraryNode = jsonNode.at("/errorExceptions");
assertNotNull(libraryNode);
assertTrue(libraryNode.isMissingNode());
JsonNode libraryNodeEx = jsonNode.at("/errorExceptions");
assertNotNull(libraryNodeEx);
assertFalse(libraryNodeEx.isMissingNode());
assertThat(libraryNodeEx.isArray(), is(true));
assertThat(
libraryNodeEx.get(0).get("message").textValue(),
is(
equalTo(
"FHIRHelpers is required as an included library for QI-Core. Please add the appropriate version of FHIRHelpers to your CQL.")));
} catch (JsonProcessingException e) {
fail(e.getMessage());
}
Expand Down

0 comments on commit 2d11447

Please sign in to comment.