Skip to content

Commit

Permalink
Do not allow duplicate element names
Browse files Browse the repository at this point in the history
  • Loading branch information
Rikkola committed May 7, 2024
1 parent d0bb0e3 commit 840d8ba
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -92,10 +92,16 @@ private YaRDDefinitions parse(String yaml) throws IOException {
}

private void appendUnits(List<Element> list) {
final List<String> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@

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() {
return name;
}

public Map<String, Object> evaluate(final Map<String, Object> map) {
return units.evaluate(map);
return definitions.evaluate(map);
}

public String evaluate(String jsonInputCxt) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 840d8ba

Please sign in to comment.