From d186f665064a78bbefacff22959f3aa50279754a Mon Sep 17 00:00:00 2001 From: Yeser Amer Date: Thu, 27 Jun 2024 09:57:23 +0200 Subject: [PATCH 1/4] context function --- .../functions/extended/ContextFunction.java | 3 ++ .../runtime/KieFEELExtendedFunctionsTest.java | 1 + .../extended/ContextFunctionTest.java | 54 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunctionTest.java diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunction.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunction.java index 21a01a1bf62..08ffd5e3020 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunction.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunction.java @@ -63,6 +63,9 @@ public FEELFnResult> invoke(@ParameterName("entries") List data() { { "context([{key: \"name\", value: \"John Doe\"},{\"key\":\"age\", \"value\":47}])", mapOf(entry("name", "John Doe"),entry("age", new BigDecimal(47))), null }, { "context([{key: \"name\", value: \"John Doe\"},{\"key\":\"age\", \"value\":47, \"something\":\"else\"}])", mapOf(entry("name", "John Doe"),entry("age", new BigDecimal(47))), null }, { "context([{key: \"name\", value: \"John Doe\"},{\"key\":\"age\"}])", null, FEELEvent.Severity.ERROR }, + { "context([{key: \"name\", value: \"John Doe\"},{\"key\": \"name\" \"value\": \"Doe John\"}])", null, FEELEvent.Severity.ERROR }, { "time(10, 20, 30)", LocalTime.of(10, 20, 30), null }, { "date( 2020, 2, 31 )", null, FEELEvent.Severity.ERROR}, { "date( \"2020-02-31\" )", null, FEELEvent.Severity.ERROR}, diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunctionTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunctionTest.java new file mode 100644 index 00000000000..48dca308cb8 --- /dev/null +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunctionTest.java @@ -0,0 +1,54 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.kie.dmn.feel.runtime.functions.extended; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import org.kie.dmn.feel.runtime.events.InvalidParametersEvent; +import org.kie.dmn.feel.runtime.functions.FunctionTestUtil; + +import java.util.List; + +class ContextFunctionTest { + + private ContextFunction contextFunction; + private record ContextEntry(String key, Object value) {} + + @BeforeEach + void setUp() { + contextFunction = new ContextFunction(); + } + + @Test + void invokeListNull() { + FunctionTestUtil.assertResultError(contextFunction.invoke(null), InvalidParametersEvent.class); + } + + @Test + void invokeContainsNull() { + FunctionTestUtil.assertResultError(contextFunction.invoke(List.of(new ContextEntry("name", null))), InvalidParametersEvent.class); + FunctionTestUtil.assertResultError(contextFunction.invoke(List.of(new ContextEntry(null, "John Doe"))), InvalidParametersEvent.class); + } + + @Test + void invokeDuplicateKey() { + FunctionTestUtil.assertResultError(contextFunction.invoke(List.of(new ContextEntry("name", "John Doe"), new ContextEntry("name", "Doe John"))), InvalidParametersEvent.class); + } +} \ No newline at end of file From 6fe18353d9e990d34a8b86b352f272376cbd9d42 Mon Sep 17 00:00:00 2001 From: Yeser Amer Date: Thu, 27 Jun 2024 10:08:56 +0200 Subject: [PATCH 2/4] context function test --- .../dmn/feel/runtime/KieFEELExtendedFunctionsTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/KieFEELExtendedFunctionsTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/KieFEELExtendedFunctionsTest.java index 51f98651663..c66c05a48fd 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/KieFEELExtendedFunctionsTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/KieFEELExtendedFunctionsTest.java @@ -43,7 +43,7 @@ protected void instanceTest(String expression, Object result, FEELEvent.Severity private static Collection data() { final Object[][] cases = new Object[][] { - { "string(\"Happy %.0fth birthday, Mr %s!\", 38, \"Doe\")", "Happy 38th birthday, Mr Doe!", null}, + /*{ "string(\"Happy %.0fth birthday, Mr %s!\", 38, \"Doe\")", "Happy 38th birthday, Mr Doe!", null}, { "now()", ZonedDateTime.class , null}, { "today()", LocalDate.class, null }, { "string join([\"a\",\"b\",\"c\"], \"_and_\")", "a_and_b_and_c", null}, @@ -127,9 +127,9 @@ private static Collection data() { { "context merge([{name: \"John Doe\", age: 0}, {age: 47}])", mapOf(entry("name", "John Doe"),entry("age", new BigDecimal(47))), null }, { "context([{key: \"name\", value: \"John Doe\"},{\"key\":\"age\", \"value\":47}])", mapOf(entry("name", "John Doe"),entry("age", new BigDecimal(47))), null }, { "context([{key: \"name\", value: \"John Doe\"},{\"key\":\"age\", \"value\":47, \"something\":\"else\"}])", mapOf(entry("name", "John Doe"),entry("age", new BigDecimal(47))), null }, - { "context([{key: \"name\", value: \"John Doe\"},{\"key\":\"age\"}])", null, FEELEvent.Severity.ERROR }, - { "context([{key: \"name\", value: \"John Doe\"},{\"key\": \"name\" \"value\": \"Doe John\"}])", null, FEELEvent.Severity.ERROR }, - { "time(10, 20, 30)", LocalTime.of(10, 20, 30), null }, + { "context([{key: \"name\", value: \"John Doe\"},{\"key\":\"age\"}])", null, FEELEvent.Severity.ERROR }, */ + { "context([{key: \"name\", value: \"John Doe\"},{key: \"name\", value: \"Doe John\"}])", null, FEELEvent.Severity.ERROR }, + /*{ "time(10, 20, 30)", LocalTime.of(10, 20, 30), null }, { "date( 2020, 2, 31 )", null, FEELEvent.Severity.ERROR}, { "date( \"2020-02-31\" )", null, FEELEvent.Severity.ERROR}, { "range(\"[1..10]\")", Range.class, null }, @@ -143,7 +143,7 @@ private static Collection data() { { "range(\"[1..10[\").end included", false, null }, { "range(\"[date(\\\"2022-01-01\\\")..date(\\\"2023-01-01\\\"))\")", Range.class, null }, { "range(\"[date(\\\"2022-01-01\\\")..date(\\\"2023-01-01\\\"))\").end included", false, null }, - { "range(\"[date(\\\"2022-01-01\\\")..date(\\\"2023-01-01\\\"))\").end.year", new BigDecimal("2023"), null }, + { "range(\"[date(\\\"2022-01-01\\\")..date(\\\"2023-01-01\\\"))\").end.year", new BigDecimal("2023"), null },*/ }; return addAdditionalParameters(cases, true); } From 863573186e46821a601f202d71ffc7a6f33bb8c7 Mon Sep 17 00:00:00 2001 From: Yeser Amer Date: Thu, 27 Jun 2024 10:10:04 +0200 Subject: [PATCH 3/4] oops --- .../dmn/feel/runtime/KieFEELExtendedFunctionsTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/KieFEELExtendedFunctionsTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/KieFEELExtendedFunctionsTest.java index c66c05a48fd..8e5b0ed40bd 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/KieFEELExtendedFunctionsTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/KieFEELExtendedFunctionsTest.java @@ -43,7 +43,7 @@ protected void instanceTest(String expression, Object result, FEELEvent.Severity private static Collection data() { final Object[][] cases = new Object[][] { - /*{ "string(\"Happy %.0fth birthday, Mr %s!\", 38, \"Doe\")", "Happy 38th birthday, Mr Doe!", null}, + { "string(\"Happy %.0fth birthday, Mr %s!\", 38, \"Doe\")", "Happy 38th birthday, Mr Doe!", null}, { "now()", ZonedDateTime.class , null}, { "today()", LocalDate.class, null }, { "string join([\"a\",\"b\",\"c\"], \"_and_\")", "a_and_b_and_c", null}, @@ -127,9 +127,9 @@ private static Collection data() { { "context merge([{name: \"John Doe\", age: 0}, {age: 47}])", mapOf(entry("name", "John Doe"),entry("age", new BigDecimal(47))), null }, { "context([{key: \"name\", value: \"John Doe\"},{\"key\":\"age\", \"value\":47}])", mapOf(entry("name", "John Doe"),entry("age", new BigDecimal(47))), null }, { "context([{key: \"name\", value: \"John Doe\"},{\"key\":\"age\", \"value\":47, \"something\":\"else\"}])", mapOf(entry("name", "John Doe"),entry("age", new BigDecimal(47))), null }, - { "context([{key: \"name\", value: \"John Doe\"},{\"key\":\"age\"}])", null, FEELEvent.Severity.ERROR }, */ + { "context([{key: \"name\", value: \"John Doe\"},{\"key\":\"age\"}])", null, FEELEvent.Severity.ERROR }, { "context([{key: \"name\", value: \"John Doe\"},{key: \"name\", value: \"Doe John\"}])", null, FEELEvent.Severity.ERROR }, - /*{ "time(10, 20, 30)", LocalTime.of(10, 20, 30), null }, + { "time(10, 20, 30)", LocalTime.of(10, 20, 30), null }, { "date( 2020, 2, 31 )", null, FEELEvent.Severity.ERROR}, { "date( \"2020-02-31\" )", null, FEELEvent.Severity.ERROR}, { "range(\"[1..10]\")", Range.class, null }, @@ -143,7 +143,7 @@ private static Collection data() { { "range(\"[1..10[\").end included", false, null }, { "range(\"[date(\\\"2022-01-01\\\")..date(\\\"2023-01-01\\\"))\")", Range.class, null }, { "range(\"[date(\\\"2022-01-01\\\")..date(\\\"2023-01-01\\\"))\").end included", false, null }, - { "range(\"[date(\\\"2022-01-01\\\")..date(\\\"2023-01-01\\\"))\").end.year", new BigDecimal("2023"), null },*/ + { "range(\"[date(\\\"2022-01-01\\\")..date(\\\"2023-01-01\\\"))\").end.year", new BigDecimal("2023"), null }, }; return addAdditionalParameters(cases, true); } From 0021ed971d296a65bba517c9e028398536b7bdd9 Mon Sep 17 00:00:00 2001 From: Yeser Amer Date: Thu, 27 Jun 2024 14:56:12 +0200 Subject: [PATCH 4/4] Tests --- .../functions/extended/ContextFunctionTest.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunctionTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunctionTest.java index 48dca308cb8..e4909074e77 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunctionTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunctionTest.java @@ -25,6 +25,7 @@ import org.kie.dmn.feel.runtime.functions.FunctionTestUtil; import java.util.List; +import java.util.Map; class ContextFunctionTest { @@ -42,13 +43,19 @@ void invokeListNull() { } @Test - void invokeContainsNull() { - FunctionTestUtil.assertResultError(contextFunction.invoke(List.of(new ContextEntry("name", null))), InvalidParametersEvent.class); - FunctionTestUtil.assertResultError(contextFunction.invoke(List.of(new ContextEntry(null, "John Doe"))), InvalidParametersEvent.class); - } + void invokeContainsNoKeyAndValue() { + FunctionTestUtil.assertResultError(contextFunction.invoke(List.of( + Map.of("test", "name", "value", "John Doe"), + Map.of("key", "name", "test", "John Doe"))), InvalidParametersEvent.class); +} @Test void invokeDuplicateKey() { - FunctionTestUtil.assertResultError(contextFunction.invoke(List.of(new ContextEntry("name", "John Doe"), new ContextEntry("name", "Doe John"))), InvalidParametersEvent.class); + FunctionTestUtil.assertResultError(contextFunction.invoke(List.of( + Map.of("key", "name", "value", "John Doe"), + Map.of("key", "name", "value", "John Doe"))), InvalidParametersEvent.class); + FunctionTestUtil.assertResultNotError(contextFunction.invoke(List.of( + Map.of("key", "name", "value", "John Doe"), + Map.of("key", "age", "value", 12)))); } } \ No newline at end of file