Skip to content

Commit

Permalink
[incubator-kie-issues#1367] The context function should throw an er…
Browse files Browse the repository at this point in the history
…ror when providing objects with the same keys (#6003)

* context function

* context function test

* oops

* Tests
  • Loading branch information
yesamer committed Jun 28, 2024
1 parent 865712e commit 9c8e99d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
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
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))));
}
}

0 comments on commit 9c8e99d

Please sign in to comment.