Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/mat 7439 enforce fhir helpers #11

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading