diff --git a/kie-yard/kie-yard-core/src/main/java/org/kie/yard/core/YaRDParser.java b/kie-yard/kie-yard-core/src/main/java/org/kie/yard/core/YaRDParser.java index 6f8076fd43d..e1bc1e53507 100644 --- a/kie-yard/kie-yard-core/src/main/java/org/kie/yard/core/YaRDParser.java +++ b/kie-yard/kie-yard-core/src/main/java/org/kie/yard/core/YaRDParser.java @@ -71,8 +71,8 @@ private String read(Reader reader) throws Exception { int numRead; while ((numRead = reader.read(buf)) != -1) { String readData = String.valueOf(buf, - 0, - numRead); + 0, + numRead); fileData.append(readData); buf = new char[1024]; } @@ -92,10 +92,16 @@ private YaRDDefinitions parse(String yaml) throws IOException { } private void appendUnits(List list) { + final List existingNames = new ArrayList<>(); for (Element hi : list) { - String nameString = hi.getName(); + final String nameString = hi.getName(); + if (existingNames.contains(nameString)) { + throw new IllegalArgumentException("Two element definitions with the same name are not allowed."); + } else { + existingNames.add(nameString); + } LOG.debug("parsing {}", nameString); - Firable decisionLogic = createDecisionLogic(nameString, hi.getLogic()); + final Firable decisionLogic = createDecisionLogic(nameString, hi.getLogic()); definitions.units().add(decisionLogic); } } diff --git a/kie-yard/kie-yard-core/src/main/java/org/kie/yard/core/YaRDRunner.java b/kie-yard/kie-yard-core/src/main/java/org/kie/yard/core/YaRDRunner.java index 24b4bca1e31..8f616bf382f 100644 --- a/kie-yard/kie-yard-core/src/main/java/org/kie/yard/core/YaRDRunner.java +++ b/kie-yard/kie-yard-core/src/main/java/org/kie/yard/core/YaRDRunner.java @@ -25,14 +25,14 @@ public class YaRDRunner { - private final YaRDDefinitions units; + private final YaRDDefinitions definitions; private final JsonMapper jsonMapper = JsonMapper.builder().build(); private final String name; public YaRDRunner(final String yaml) throws IOException { final YaRDParser parser = new YaRDParser(yaml); name = parser.getModel().getName(); - units = parser.getDefinitions(); + definitions = parser.getDefinitions(); } public String getName() { @@ -40,7 +40,7 @@ public String getName() { } public Map evaluate(final Map map) { - return units.evaluate(map); + return definitions.evaluate(map); } public String evaluate(String jsonInputCxt) throws Exception { diff --git a/kie-yard/kie-yard-core/src/test/java/org/kie/yard/core/DublicateElementNameTest.java b/kie-yard/kie-yard-core/src/test/java/org/kie/yard/core/DublicateElementNameTest.java new file mode 100644 index 00000000000..3f1228f4275 --- /dev/null +++ b/kie-yard/kie-yard-core/src/test/java/org/kie/yard/core/DublicateElementNameTest.java @@ -0,0 +1,44 @@ +/* + * 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.yard.core; + +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class DublicateElementNameTest + extends TestBase { + + private static final String FILE_NAME = "/two-elements-with-duplicate-name.yml"; + + @Test + public void testNoDublicateNames() throws Exception { + final String CTX = """ + { + "Age": 10 + } + """; + assertThrows(IllegalArgumentException.class, () -> + evaluate(CTX, FILE_NAME) + ); + } +} diff --git a/kie-yard/kie-yard-core/src/test/java/org/kie/yard/core/MVELJSONTest.java b/kie-yard/kie-yard-core/src/test/java/org/kie/yard/core/MVELJSONTest.java index c1921c2414f..f69723e310c 100644 --- a/kie-yard/kie-yard-core/src/test/java/org/kie/yard/core/MVELJSONTest.java +++ b/kie-yard/kie-yard-core/src/test/java/org/kie/yard/core/MVELJSONTest.java @@ -57,5 +57,4 @@ public void testMVELManagesJSONMaps() throws Exception { final Map mapTest = (Map) output.get("Map Test"); assertEquals("Hello", mapTest.get("Map")); } - // TODO Validate two elements can not have the same name } diff --git a/kie-yard/kie-yard-core/src/test/resources/two-elements-with-duplicate-name.yml b/kie-yard/kie-yard-core/src/test/resources/two-elements-with-duplicate-name.yml new file mode 100644 index 00000000000..7f954a64bb5 --- /dev/null +++ b/kie-yard/kie-yard-core/src/test/resources/two-elements-with-duplicate-name.yml @@ -0,0 +1,19 @@ +specVersion: alpha +kind: YaRD +name: 'BasePrice' +inputs: + - name: Age + type: number +elements: + - name: Ten + type: Decision + logic: + type: LiteralExpression + expression: | + 10 + - name: Ten + type: Decision + logic: + type: LiteralExpression + expression: | + 5 + 5