From fd9b4076c32c8152662c7c85f5706e920baf8bd2 Mon Sep 17 00:00:00 2001 From: sb-cecilialiu Date: Mon, 23 Dec 2024 11:50:29 -0600 Subject: [PATCH] MAT-7998 update error message when definition name is key word --- .../filters/CqlTranslatorExceptionFilter.java | 13 ++++++---- .../CqlTranslatorExceptionFilterTest.java | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/gov/cms/mat/cql_elm_translation/service/filters/CqlTranslatorExceptionFilter.java b/src/main/java/gov/cms/mat/cql_elm_translation/service/filters/CqlTranslatorExceptionFilter.java index afd2bd56..c027d1b9 100644 --- a/src/main/java/gov/cms/mat/cql_elm_translation/service/filters/CqlTranslatorExceptionFilter.java +++ b/src/main/java/gov/cms/mat/cql_elm_translation/service/filters/CqlTranslatorExceptionFilter.java @@ -4,6 +4,7 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.cqframework.cql.cql2elm.CqlCompilerException; import org.hl7.elm.r1.VersionedIdentifier; @@ -113,18 +114,20 @@ private List filterBySyntax( } /* - * MAT-7995: error: "No Viable Input at 'define :'" + * MAT-7995: error: "no viable alternative at input 'define :'" * should be customized as: "Definition is missing a name." - * This is done in cql-antlr-parse, so on the frontend we don't want a duplicate error message - * therefore we are filtering it out here. + * MAT-7998: error: "no viable alternative at input 'define KEYWORD'" + * should be customized as: "Definition names must not be a reserved word." + * The errors are handled in cql-antlr-parser, so on the front-end we don't want + * duplicate error message therefore we are filtering it out here. */ private List filterOutCustomErrors( List filteredCqlTranslatorExceptions) { return filteredCqlTranslatorExceptions.stream() .filter( cqlCompilerException -> - !"no viable alternative at input 'define :'" - .equalsIgnoreCase(cqlCompilerException.getMessage())) + !StringUtils.containsIgnoreCase( + cqlCompilerException.getMessage(), "no viable alternative at input 'define")) .toList(); } } diff --git a/src/test/java/gov/cms/mat/cql_elm_translation/service/filters/CqlTranslatorExceptionFilterTest.java b/src/test/java/gov/cms/mat/cql_elm_translation/service/filters/CqlTranslatorExceptionFilterTest.java index 36bfa92a..408e6611 100644 --- a/src/test/java/gov/cms/mat/cql_elm_translation/service/filters/CqlTranslatorExceptionFilterTest.java +++ b/src/test/java/gov/cms/mat/cql_elm_translation/service/filters/CqlTranslatorExceptionFilterTest.java @@ -72,4 +72,28 @@ public void testFilterSyntaxException() { CqlCompilerException syntaxException = filteredExceptions.get(0); assertEquals(syntaxException.getClass(), CqlCompilerException.class); } + + @Test + public void testFilterOutCustomErrors() { + CqlCompilerException definitionKeyWordException = + new CqlCompilerException("no viable alternative at input 'define :'"); + cqlTranslatorExceptions.add(definitionKeyWordException); + CqlTranslatorExceptionFilter filter = + new CqlTranslatorExceptionFilter(cqlData, false, cqlTranslatorExceptions); + + List filteredExceptions = filter.filter(); + assertTrue(filteredExceptions.size() == 0); + } + + @Test + public void testFilterOutDefinitionWithKeywords() { + CqlCompilerException definitionKeyWordException = + new CqlCompilerException("no viable alternative at input 'define year'"); + cqlTranslatorExceptions.add(definitionKeyWordException); + CqlTranslatorExceptionFilter filter = + new CqlTranslatorExceptionFilter(cqlData, false, cqlTranslatorExceptions); + + List filteredExceptions = filter.filter(); + assertTrue(filteredExceptions.size() == 0); + } }