Skip to content

Commit

Permalink
context function
Browse files Browse the repository at this point in the history
  • Loading branch information
yesamer committed Jun 27, 2024
1 parent 865712e commit d186f66
Show file tree
Hide file tree
Showing 3 changed files with 58 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,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);
}
}

0 comments on commit d186f66

Please sign in to comment.