Skip to content
Draft
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
@@ -0,0 +1,54 @@
package org.kie.yard.impl2;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Map;

import org.drools.util.IoUtils;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.json.JsonMapper;

public class ServicePricingTest {
private static final Logger LOG = LoggerFactory.getLogger(InsuranceBasePriceTest.class);

private JsonMapper jsonMapper = JsonMapper.builder().build();

@SuppressWarnings("unchecked")
private Map<String, Object> readJSON(final String CONTEXT) throws JsonProcessingException, JsonMappingException {
return jsonMapper.readValue(CONTEXT, Map.class);
}

@Test
public void testScenario1() throws Exception {
final String CTX = """
{
"Customer Plan": "Premium",
"Requests": 40000
}
""";
Map<String, Object> outputJSONasMap = evaluate(CTX);
assertThat(outputJSONasMap).hasFieldOrPropertyWithValue("Price for the Month", 24000);
}

private Map<String, Object> evaluate(String jsonInputCxt) throws Exception {
String yamlDecision = new String(IoUtils.readBytesFromInputStream(this.getClass().getResourceAsStream("/managed-services-price.yml"), true));
LOG.info("INPUT:\n{}", jsonInputCxt);

YaRDParser parser = new YaRDParser();
YaRDRuleUnits units = parser.parse(yamlDecision);

Map<String, Object> inputContext = readJSON(jsonInputCxt);

Map<String, Object> tempOutCtx = units.evaluate(inputContext);
final String OUTPUT_JSON = jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(tempOutCtx);
Map<String, Object> outputJSONasMap = readJSON(OUTPUT_JSON);

LOG.info("{}", OUTPUT_JSON);
return outputJSONasMap;
}
}
25 changes: 25 additions & 0 deletions yard-impl2/src/test/resources/managed-services-price.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
specVersion: alpha
kind: YaRD
name: 'BasePrice'
expressionLang: Drools
inputs:
- name: 'Customer Plan'
type: string
- name: 'Requests'
type: integer
elements:
- name: 'Price for the Month'
type: Decision
logic:
type: DecisionTable
inputs: ['Customer Plan', 'Requests']
rules:
- when: ['"Free"', '<1000']
then: 500
- when: ['"Free"', '>=1000']
then: 1000 + 1* Requests
- when: ['"Premium"', '<10000']
then: 4000
- when: ['"Premium"', '>=10000']
then: 4000 + 0.5 * Requests
---