This is an automated email from the ASF dual-hosted git repository.

gitgabrio pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-kogito-apps.git


The following commit(s) were added to refs/heads/main by this push:
     new a045acaac [incubator-kie-issues#1240] Enable dmn runtime checks inside 
jitexecutor (#2057)
a045acaac is described below

commit a045acaac659dd77f74bb9b84c1e7487100452d4
Author: Gabriele Cardosi <[email protected]>
AuthorDate: Tue May 21 13:38:07 2024 +0200

    [incubator-kie-issues#1240] Enable dmn runtime checks inside jitexecutor 
(#2057)
    
    Co-authored-by: Gabriele-Cardosi <[email protected]>
---
 .../kie/kogito/jitexecutor/dmn/DMNEvaluator.java   |   3 +
 .../org/kie/kogito/jitexecutor/dmn/DMN15Test.java  | 126 +++++++++++++++++----
 2 files changed, 107 insertions(+), 22 deletions(-)

diff --git 
a/jitexecutor/jitexecutor-dmn/src/main/java/org/kie/kogito/jitexecutor/dmn/DMNEvaluator.java
 
b/jitexecutor/jitexecutor-dmn/src/main/java/org/kie/kogito/jitexecutor/dmn/DMNEvaluator.java
index b8c849638..979f4159b 100644
--- 
a/jitexecutor/jitexecutor-dmn/src/main/java/org/kie/kogito/jitexecutor/dmn/DMNEvaluator.java
+++ 
b/jitexecutor/jitexecutor-dmn/src/main/java/org/kie/kogito/jitexecutor/dmn/DMNEvaluator.java
@@ -29,6 +29,8 @@ import org.kie.dmn.api.core.DMNContext;
 import org.kie.dmn.api.core.DMNModel;
 import org.kie.dmn.api.core.DMNResult;
 import org.kie.dmn.api.core.DMNRuntime;
+import org.kie.dmn.core.compiler.RuntimeTypeCheckOption;
+import org.kie.dmn.core.impl.DMNRuntimeImpl;
 import org.kie.dmn.core.internal.utils.DMNRuntimeBuilder;
 import org.kie.dmn.core.internal.utils.DynamicDMNContextBuilder;
 import org.kie.internal.io.ResourceFactory;
@@ -52,6 +54,7 @@ public class DMNEvaluator {
     private DMNEvaluator(DMNModel dmnModel, DMNRuntime dmnRuntime) {
         this.dmnModel = dmnModel;
         this.dmnRuntime = dmnRuntime;
+        ((DMNRuntimeImpl) this.dmnRuntime).setOption(new 
RuntimeTypeCheckOption(true));
     }
 
     public DMNModel getDmnModel() {
diff --git 
a/jitexecutor/jitexecutor-dmn/src/test/java/org/kie/kogito/jitexecutor/dmn/DMN15Test.java
 
b/jitexecutor/jitexecutor-dmn/src/test/java/org/kie/kogito/jitexecutor/dmn/DMN15Test.java
index c89120ac9..8c017a028 100644
--- 
a/jitexecutor/jitexecutor-dmn/src/test/java/org/kie/kogito/jitexecutor/dmn/DMN15Test.java
+++ 
b/jitexecutor/jitexecutor-dmn/src/test/java/org/kie/kogito/jitexecutor/dmn/DMN15Test.java
@@ -19,6 +19,7 @@
 package org.kie.kogito.jitexecutor.dmn;
 
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
@@ -40,6 +41,7 @@ import io.restassured.response.ValidatableResponse;
 
 import static io.restassured.RestAssured.given;
 import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.kie.kogito.jitexecutor.dmn.TestingUtils.MAPPER;
 import static org.kie.kogito.jitexecutor.dmn.TestingUtils.getModel;
@@ -71,45 +73,84 @@ class DMN15Test {
                 "valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
     }
 
-    private void commonUnnamedImport(String importingModelRef, String 
importedModelRef) throws IOException {
-        ResourceWithURI model1 = new ResourceWithURI(importingModelRef, 
getModelFromIoUtils(importingModelRef));
-        ResourceWithURI model2 = new ResourceWithURI(importedModelRef, 
getModelFromIoUtils(importedModelRef));
+    @Test
+    void collectionTypeConstraintSucceed() throws IOException {
+        String modelRef = "valid_models/DMNv1_5/TypeConstraintsChecks.dmn";
+        ResourceWithURI model = new ResourceWithURI(modelRef, 
getModelFromIoUtils(modelRef));
         Map<String, Object> context =
-                Map.of("A Person", Map.of("name", "John", "age", 47));
-        JITDMNPayload jitdmnpayload = new JITDMNPayload(importingModelRef, 
List.of(model1, model2),
-                context);
+                Map.of("p1", Map.of("Name", "P3", "Interests", 
Arrays.asList("Ski")));
+        JITDMNPayload jitdmnpayload = new JITDMNPayload(modelRef, 
List.of(model),
+                                                        context);
         given()
                 .contentType(ContentType.JSON)
                 .body(jitdmnpayload)
                 .when().post("/jitdmn")
                 .then()
                 .statusCode(200)
-                .body("'Local Hello'", is("function Local Hello( Person )"))
-                .body("'Say Hello'", is("function Say Hello( Person )"));
+                .body("'MyDecision'", is("The Person P3 likes 1 thing(s)."));
+    }
 
-        String response = given()
+    @Test
+    void collectionTypeConstraintFails() throws IOException {
+        String modelRef = "valid_models/DMNv1_5/TypeConstraintsChecks.dmn";
+        ResourceWithURI model = new ResourceWithURI(modelRef, 
getModelFromIoUtils(modelRef));
+        Map<String, Object> context =
+                Map.of("p1", Map.of("Name", "P3", "Interests", 
Arrays.asList("Ski", "Golf")));
+        JITDMNPayload jitdmnpayload = new JITDMNPayload(modelRef, 
List.of(model),
+                                                        context);
+        given()
                 .contentType(ContentType.JSON)
-                .body(new MultipleResourcesPayload(importingModelRef, 
List.of(model1, model2)))
-                .when()
-                .post("/jitdmn/validate")
+                .body(jitdmnpayload)
+                .when().post("/jitdmn")
                 .then()
                 .statusCode(200)
-                .extract()
-                .asString();
-        LOG.debug("Validate response: {}", response);
-        List<JITDMNMessage> messages = MAPPER.readValue(response, 
LIST_OF_MSGS);
-        assertEquals(0, messages.size());
+                .body("'MyDecision'", nullValue());
+    }
 
-        jitdmnpayload = new JITDMNPayload(importingModelRef, List.of(model1, 
model2), context);
+    @Test
+    void collectionAllowedValuesSucceed() throws IOException {
+        String modelRef = 
"valid_models/DMNv1_5/AllowedValuesChecksInsideCollection.dmn";
+        ResourceWithURI model = new ResourceWithURI(modelRef, 
getModelFromIoUtils(modelRef));
+        Map<String, Object> context =
+                Map.of("p1", Map.of("Name", "P3", "Interests", 
Arrays.asList("Golf")));
+        JITDMNPayload jitdmnpayload = new JITDMNPayload(modelRef, 
List.of(model),
+                                                        context);
         given()
                 .contentType(ContentType.JSON)
                 .body(jitdmnpayload)
-                .when()
-                .post("/jitdmn/dmnresult")
+                .when().post("/jitdmn")
                 .then()
                 .statusCode(200)
-                .body("decisionResults.result[0]", is("Hello John!"))
-                .body("decisionResults.result[1]", is("Local Hello John!"));
+                .body("'MyDecision'", is("The Person P3 likes 1 thing(s)."));
+
+        context =
+                Map.of("p1", Map.of("Name", "P3", "Interests", 
Arrays.asList("Golf", "Jogging")));
+        jitdmnpayload = new JITDMNPayload(modelRef, List.of(model),
+                                                        context);
+        given()
+                .contentType(ContentType.JSON)
+                .body(jitdmnpayload)
+                .when().post("/jitdmn")
+                .then()
+                .statusCode(200)
+                .body("'MyDecision'", is("The Person P3 likes 2 thing(s)."));
+    }
+
+    @Test
+    void collectionAllowedValuesFails() throws IOException {
+        String modelRef = 
"valid_models/DMNv1_5/AllowedValuesChecksInsideCollection.dmn";
+        ResourceWithURI model = new ResourceWithURI(modelRef, 
getModelFromIoUtils(modelRef));
+        Map<String, Object> context =
+                Map.of("p1", Map.of("Name", "P3", "Interests", 
Arrays.asList("Ski")));
+        JITDMNPayload jitdmnpayload = new JITDMNPayload(modelRef, 
List.of(model),
+                                                        context);
+        given()
+                .contentType(ContentType.JSON)
+                .body(jitdmnpayload)
+                .when().post("/jitdmn")
+                .then()
+                .statusCode(200)
+                .body("'MyDecision'", nullValue());
     }
 
     @Test
@@ -166,6 +207,47 @@ class DMN15Test {
         result(model, Map.of("decisionResults.result[0]", 
"2021-05-31T00:00:00Z"));
     }
 
+    private void commonUnnamedImport(String importingModelRef, String 
importedModelRef) throws IOException {
+        ResourceWithURI model1 = new ResourceWithURI(importingModelRef, 
getModelFromIoUtils(importingModelRef));
+        ResourceWithURI model2 = new ResourceWithURI(importedModelRef, 
getModelFromIoUtils(importedModelRef));
+        Map<String, Object> context =
+                Map.of("A Person", Map.of("name", "John", "age", 47));
+        JITDMNPayload jitdmnpayload = new JITDMNPayload(importingModelRef, 
List.of(model1, model2),
+                                                        context);
+        given()
+                .contentType(ContentType.JSON)
+                .body(jitdmnpayload)
+                .when().post("/jitdmn")
+                .then()
+                .statusCode(200)
+                .body("'Local Hello'", is("function Local Hello( Person )"))
+                .body("'Say Hello'", is("function Say Hello( Person )"));
+
+        String response = given()
+                .contentType(ContentType.JSON)
+                .body(new MultipleResourcesPayload(importingModelRef, 
List.of(model1, model2)))
+                .when()
+                .post("/jitdmn/validate")
+                .then()
+                .statusCode(200)
+                .extract()
+                .asString();
+        LOG.debug("Validate response: {}", response);
+        List<JITDMNMessage> messages = MAPPER.readValue(response, 
LIST_OF_MSGS);
+        assertEquals(0, messages.size());
+
+        jitdmnpayload = new JITDMNPayload(importingModelRef, List.of(model1, 
model2), context);
+        given()
+                .contentType(ContentType.JSON)
+                .body(jitdmnpayload)
+                .when()
+                .post("/jitdmn/dmnresult")
+                .then()
+                .statusCode(200)
+                .body("decisionResults.result[0]", is("Hello John!"))
+                .body("decisionResults.result[1]", is("Local Hello John!"));
+    }
+
     private void result(String model, Map<String, Object> expectedValues) {
         String payload = "{ \"model\": " + model + ", \"context\": {\n" +
                 "}}";


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to