This is an automated email from the ASF dual-hosted git repository.
gitgabrio pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git
The following commit(s) were added to refs/heads/main by this push:
new 9c8e99dab0 [incubator-kie-issues#1367] The `context` function should
throw an error when providing objects with the same keys (#6003)
9c8e99dab0 is described below
commit 9c8e99dab030ffc44e8a1d8ac9e2c7d627766899
Author: Yeser Amer <[email protected]>
AuthorDate: Fri Jun 28 09:33:31 2024 +0200
[incubator-kie-issues#1367] The `context` function should throw an error
when providing objects with the same keys (#6003)
* context function
* context function test
* oops
* Tests
---
.../functions/extended/ContextFunction.java | 3 ++
.../feel/runtime/KieFEELExtendedFunctionsTest.java | 1 +
.../functions/extended/ContextFunctionTest.java | 61 ++++++++++++++++++++++
3 files changed, 65 insertions(+)
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 21a01a1bf6..08ffd5e302 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 class ContextFunction extends BaseFEELFunction {
} else {
return FEELFnResult.ofError(new
InvalidParametersEvent(FEELEvent.Severity.ERROR, "entry of index " + (h_index)
+ " is missing a `value` entry"));
}
+ if (result.containsKey(key)) {
+ return FEELFnResult.ofError(new
InvalidParametersEvent(FEELEvent.Severity.ERROR, "entry of index " + (h_index)
+ " contains duplicate key"));
+ }
result.put(key, value);
} else {
return FEELFnResult.ofError(new
InvalidParametersEvent(FEELEvent.Severity.ERROR, "entry of index " + (h_index)
+ " is not a valid context"));
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 050681eb83..8e5b0ed40b 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
@@ -128,6 +128,7 @@ public class KieFEELExtendedFunctionsTest extends
BaseFEELTest {
{ "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 0000000000..e4909074e7
--- /dev/null
+++
b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/extended/ContextFunctionTest.java
@@ -0,0 +1,61 @@
+/**
+ * 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;
+import java.util.Map;
+
+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 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(
+ 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
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]