Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incubator-kie-issues#1367] The context function should throw an error when providing objects with the same keys #6003

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public FEELFnResult<Map<String, Object>> invoke(@ParameterName("entries") List<O
} 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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private static Collection<Object[]> 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},
Expand Down
Original file line number Diff line number Diff line change
@@ -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
gitgabrio marked this conversation as resolved.
Show resolved Hide resolved
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))));
}
}
Loading