From 10f651bca53057b6e8dbb8a5c01b1e9113d1e776 Mon Sep 17 00:00:00 2001 From: bncriju Date: Fri, 13 Sep 2024 17:28:15 +0530 Subject: [PATCH] exceptions handled and changes to util class --- kie-dmn/kie-dmn-feel/pom.xml | 18 +++++++++++------- .../runtime/functions/MatchesFunction.java | 19 ++++++++++++------- .../runtime/functions/ReplaceFunction.java | 2 +- .../org/kie/dmn/feel/util/XQueryImplUtil.java | 12 ++++++------ .../kie/dmn/feel/util/XQueryImplUtilTest.java | 8 ++++---- 5 files changed, 34 insertions(+), 25 deletions(-) diff --git a/kie-dmn/kie-dmn-feel/pom.xml b/kie-dmn/kie-dmn-feel/pom.xml index 06eea6e854f..43e611f5aef 100644 --- a/kie-dmn/kie-dmn-feel/pom.xml +++ b/kie-dmn/kie-dmn-feel/pom.xml @@ -83,8 +83,8 @@ - org.drools - drools-compiler + org.drools + drools-compiler @@ -92,11 +92,10 @@ javaparser-core - - ch.obermuhlner - big-math + ch.obermuhlner + big-math @@ -150,16 +149,20 @@ com.fasterxml.jackson.core jackson-databind + + org.apache.poi + poi + - ${project.basedir}/src/test/resources + ${project.basedir}/src/test/resources - ${project.basedir}/ + ${project.basedir}/../ ref-dmn-feel-builtin-functions.adoc @@ -186,4 +189,5 @@ + diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/MatchesFunction.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/MatchesFunction.java index d439229218d..9ea8d5b661c 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/MatchesFunction.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/MatchesFunction.java @@ -36,23 +36,28 @@ private MatchesFunction() { super( "matches" ); } - public FEELFnResult FEELFnResult(@ParameterName("input") String input, @ParameterName("pattern") String pattern) { + public FEELFnResult FEELFnResult(@ParameterName("input") String input, @ParameterName("pattern") String pattern) { return invoke( input, pattern, null ); } - public FEELFnResult invoke(@ParameterName("input") String input, @ParameterName("pattern") String pattern, @ParameterName("flags") String flags) { + public FEELFnResult invoke(@ParameterName("input") String input, @ParameterName("pattern") String pattern, @ParameterName("flags") String flags) { try { return matchFunctionWithFlags(input,pattern,flags); - } catch ( PatternSyntaxException t ) { - return FEELFnResult.ofError( new InvalidParametersEvent( Severity.ERROR, "pattern", "is invalid and can not be compiled", t ) ); - } catch (IllegalArgumentException t ) { + } catch (InvalidParameterException t ) { return FEELFnResult.ofError( new InvalidParametersEvent( Severity.ERROR, t.getMessage(), "cannot be null or is invalid", t ) ); } catch (Throwable t) { - return FEELFnResult.ofError( new InvalidParametersEvent( Severity.ERROR, "pattern", "is invalid and can not be compiled", t ) ); + String errorMessage; + if (t.getMessage() != null && !t.getMessage().isEmpty()) { + errorMessage = "Error: " + t.getMessage(); + } else { + errorMessage = String.format("Some of the provided parameters might be invalid. Input: '%s', Pattern: '%s', Flags: '%s'", + input, pattern, flags); + } + return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, errorMessage, t)); } } - static FEELFnResult matchFunctionWithFlags(String input, String pattern, String flags) { + static FEELFnResult matchFunctionWithFlags(String input, String pattern, String flags) { log.debug("Input: {} , Pattern: {}, Flags: {}", input, pattern, flags); if ( input == null ) { throw new InvalidParameterException("input"); diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/ReplaceFunction.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/ReplaceFunction.java index d64f2011c0b..9bee8fd0fd4 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/ReplaceFunction.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/ReplaceFunction.java @@ -48,7 +48,7 @@ public FEELFnResult invoke(@ParameterName("input") String input, @Parame return FEELFnResult.ofError( new InvalidParametersEvent( Severity.ERROR, "replacement", "cannot be null" ) ); } - return FEELFnResult.ofResult(XQueryImplUtil.executeReplaceFunction(input,pattern,replacement,flags).toString()); + return FEELFnResult.ofResult(String.valueOf(XQueryImplUtil.executeReplaceFunction(input,pattern,replacement,flags))); } } diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/util/XQueryImplUtil.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/util/XQueryImplUtil.java index b643dfc1397..db8415a31c7 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/util/XQueryImplUtil.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/util/XQueryImplUtil.java @@ -22,26 +22,26 @@ public class XQueryImplUtil { - public static Object executeMatchesFunction(String input, String pattern, String flags){ + public static Boolean executeMatchesFunction(String input, String pattern, String flags){ flags = flags == null ? "" : flags; String xQueryExpression = String.format("matches('%s', '%s', '%s')", input, pattern, flags); - return evaluateXQueryExpression(xQueryExpression); + return evaluateXQueryExpression(xQueryExpression, Boolean.class); } - public static Object executeReplaceFunction(String input, String pattern, String replacement, String flags) { + public static String executeReplaceFunction(String input, String pattern, String replacement, String flags) { flags = flags == null ? "" : flags; String xQueryExpression = String.format("replace('%s', '%s', '%s', '%s')", input, pattern, replacement, flags); - return evaluateXQueryExpression(xQueryExpression); + return evaluateXQueryExpression(xQueryExpression, String.class); } - static Object evaluateXQueryExpression (String expression) { + static T evaluateXQueryExpression (String expression, Class expectedTypeResult) { try { Processor processor = new Processor(false); XQueryCompiler compiler = processor.newXQueryCompiler(); XQueryExecutable executable = compiler.compile(expression); XQueryEvaluator queryEvaluator = executable.load(); XdmItem resultItem = queryEvaluator.evaluateSingle(); - return ((XdmAtomicValue) resultItem).getValue(); + return expectedTypeResult.cast((((XdmAtomicValue) resultItem).getValue())); } catch (ClassCastException | SaxonApiException e) { throw new IllegalStateException(e); } diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/util/XQueryImplUtilTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/util/XQueryImplUtilTest.java index 841ab5dda10..52b23b29203 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/util/XQueryImplUtilTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/util/XQueryImplUtilTest.java @@ -30,7 +30,7 @@ void executeMatchesFunctionTest() { String input = "test"; String pattern = "^test"; String flags = "i"; - Object retrieved = XQueryImplUtil.executeMatchesFunction(input, pattern, + boolean retrieved = XQueryImplUtil.executeMatchesFunction(input, pattern, flags); boolean expected = true; assertThat(retrieved).isNotNull().isEqualTo(expected); @@ -65,8 +65,8 @@ void executeReplaceFunctionTest() { String pattern = "^test"; String replacement = "ttt"; String flags = ""; - Object retrieved = XQueryImplUtil.executeReplaceFunction(input, pattern, replacement, - flags); + String retrieved = XQueryImplUtil.executeReplaceFunction(input, pattern, replacement, + flags).toString(); String expected = "tttString"; assertThat(retrieved).isNotNull().isEqualTo(expected); @@ -74,7 +74,7 @@ void executeReplaceFunctionTest() { pattern = "o.b"; replacement = "ttt"; flags = "s"; - retrieved = XQueryImplUtil.executeReplaceFunction(input, pattern, replacement, flags); + retrieved = XQueryImplUtil.executeReplaceFunction(input, pattern, replacement, flags).toString(); expected = "ftttar"; assertThat(retrieved).isNotNull().isEqualTo(expected); }