From 14abbde3c50aeabb8509ef6906fab1f89fb60051 Mon Sep 17 00:00:00 2001 From: Gabriele-Cardosi Date: Mon, 15 Jul 2024 12:17:34 +0200 Subject: [PATCH] [incubator-kie-issues#1370] Implemented CustomFunction invocation test --- .../functions/BaseFEELFunctionTest.java | 85 ++++++++----------- 1 file changed, 37 insertions(+), 48 deletions(-) diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionTest.java index ceca90221c0..dde34f0d62b 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionTest.java @@ -21,6 +21,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Parameter; +import java.math.BigDecimal; import java.time.Duration; import java.time.LocalDate; import java.time.LocalTime; @@ -37,11 +38,14 @@ import org.kie.dmn.feel.lang.ast.InfixOperator; import org.kie.dmn.feel.lang.ast.NameRefNode; import org.kie.dmn.feel.lang.ast.NullNode; +import org.kie.dmn.feel.lang.ast.NumberNode; import org.kie.dmn.feel.lang.impl.NamedParameter; import org.kie.dmn.feel.lang.types.BuiltInType; import org.kie.dmn.feel.runtime.FEELFunction; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -54,6 +58,39 @@ public void setUp() { ctx = CodegenTestUtil.newEmptyEvaluationContext(); } + @Test + void invokeReflectiveCustomFunction() { + List parameters = List.of(new FEELFunction.Param("foo", BuiltInType.UNKNOWN), + new FEELFunction.Param("person's age", BuiltInType.UNKNOWN)); + + BaseNode left = new InfixOpNode(InfixOperator.EQ, + new NameRefNode(BuiltInType.UNKNOWN, "foo"), + new NullNode(""), + "foo = null"); + BaseNode right = new InfixOpNode(InfixOperator.LT, + new NameRefNode(BuiltInType.UNKNOWN, "person's age"), + new NumberNode(BigDecimal.valueOf(18), "18"), + "person's age < 18"); + BaseNode body = new InfixOpNode(InfixOperator.AND, left, right, "foo = null and person's age < 18"); + BaseFEELFunction toTest = new CustomFEELFunction("", + parameters, + body, + ctx); + Object[] params = {new NamedParameter("foo", null), + new NamedParameter("person's age", 16)}; + Object retrieved = toTest.invokeReflectively(ctx, params); + assertNotNull(retrieved); + assertInstanceOf(Boolean.class, retrieved); + assertTrue((Boolean) retrieved); + + params = new Object[]{new NamedParameter("foo", null), + new NamedParameter("person's age", 19)}; + retrieved = toTest.invokeReflectively(ctx, params); + assertNotNull(retrieved); + assertInstanceOf(Boolean.class, retrieved); + assertFalse((Boolean) retrieved); + } + @Test void getAllFunctionCandidateMethod() { BaseFEELFunction toTest = AllFunction.INSTANCE; @@ -288,52 +325,4 @@ void getStddevFunctionCandidateMethod() { assertEquals(List.class, parametersRetrieved[0].getType()); } - - -// @Test -// void getCustomFunctionCandidateMethod() { - // TODO {gcardosi}: verify behavior with CustomFEELFunction - // -// List params = List.of(new FEELFunction.Param("foo", BuiltInType.UNKNOWN), -// new FEELFunction.Param("foo", BuiltInType.UNKNOWN)); -// -// BaseNode body = new InfixOpNode(InfixOperator.EQ, -// new NameRefNode(BuiltInType.UNKNOWN, "foo"), -// new NullNode((String) null), -// "foo = null and person's age < 18"); -// BaseFEELFunction toTest = new CustomFEELFunction("", -// params, -// body, -// ctx); -// -// // invoke(@ParameterName( "ctx" ) EvaluationContext ctx, -// // @ParameterName("list") List list, -// // @ParameterName("precedes") FEELFunction function -// Object[] parameters = {List.of(1, 2), AllFunction.INSTANCE}; -// BaseFEELFunction.CandidateMethod candidateMethodRetrieved = toTest.getCandidateMethod(ctx, parameters, false); -// assertNotNull(candidateMethodRetrieved); -// Method retrieved = candidateMethodRetrieved.getActualMethod(); -// assertNotNull(retrieved); -// assertTrue(Modifier.isPublic(retrieved.getModifiers())); -// assertEquals("invoke", retrieved.getName()); -// Parameter[] parametersRetrieved = retrieved.getParameters(); -// assertNotNull(parametersRetrieved); -// assertEquals(3, parametersRetrieved.length); -// assertEquals(EvaluationContext.class, parametersRetrieved[0].getType()); -// assertEquals(List.class, parametersRetrieved[1].getType()); -// assertEquals(FEELFunction.class, parametersRetrieved[2].getType()); -// -// // invoke(@ParameterName("list") List list) -// parameters = new Object[]{List.of(1, 3, 5)}; -// candidateMethodRetrieved = toTest.getCandidateMethod(ctx, parameters, false); -// assertNotNull(candidateMethodRetrieved); -// retrieved = candidateMethodRetrieved.getActualMethod(); -// assertNotNull(retrieved); -// assertTrue(Modifier.isPublic(retrieved.getModifiers())); -// assertEquals("invoke", retrieved.getName()); -// parametersRetrieved = retrieved.getParameters(); -// assertNotNull(parametersRetrieved); -// assertEquals(1, parametersRetrieved.length); -// assertEquals(List.class, parametersRetrieved[0].getType()); -// } } \ No newline at end of file