From b3cbb86b102f1c02474a3827cbe0d8188fd4da0c Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Fri, 10 Mar 2023 16:51:28 +0100 Subject: [PATCH] Extending the service pricing example to new engine --- .../kie/yard/impl2/ServicePricingTest.java | 54 +++++++++++++++++++ .../test/resources/managed-services-price.yml | 25 +++++++++ 2 files changed, 79 insertions(+) create mode 100644 yard-impl2/src/test/java/org/kie/yard/impl2/ServicePricingTest.java create mode 100644 yard-impl2/src/test/resources/managed-services-price.yml diff --git a/yard-impl2/src/test/java/org/kie/yard/impl2/ServicePricingTest.java b/yard-impl2/src/test/java/org/kie/yard/impl2/ServicePricingTest.java new file mode 100644 index 0000000..ee5070d --- /dev/null +++ b/yard-impl2/src/test/java/org/kie/yard/impl2/ServicePricingTest.java @@ -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 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 outputJSONasMap = evaluate(CTX); + assertThat(outputJSONasMap).hasFieldOrPropertyWithValue("Price for the Month", 24000); + } + + private Map 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 inputContext = readJSON(jsonInputCxt); + + Map tempOutCtx = units.evaluate(inputContext); + final String OUTPUT_JSON = jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(tempOutCtx); + Map outputJSONasMap = readJSON(OUTPUT_JSON); + + LOG.info("{}", OUTPUT_JSON); + return outputJSONasMap; + } +} diff --git a/yard-impl2/src/test/resources/managed-services-price.yml b/yard-impl2/src/test/resources/managed-services-price.yml new file mode 100644 index 0000000..3c9303f --- /dev/null +++ b/yard-impl2/src/test/resources/managed-services-price.yml @@ -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 +---