From d186f665064a78bbefacff22959f3aa50279754a Mon Sep 17 00:00:00 2001 From: Yeser Amer Date: Thu, 27 Jun 2024 09:57:23 +0200 Subject: [PATCH] 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