Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Incubator-kie-issues#1605] DMN JIT Executor successfully executing invalid DMN #2142

Merged
merged 10 commits into from
Nov 15, 2024
5 changes: 5 additions & 0 deletions jitexecutor/jitexecutor-dmn/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.kie.api.builder.Message;
import org.kie.api.io.Resource;
import org.kie.dmn.api.core.DMNContext;
import org.kie.dmn.api.core.DMNMessage;
import org.kie.dmn.api.core.DMNModel;
import org.kie.dmn.api.core.DMNResult;
import org.kie.dmn.api.core.DMNRuntime;
Expand All @@ -51,7 +55,13 @@ public static DMNEvaluator fromXML(String modelXML) {
.fromResources(Collections.singletonList(modelResource)).getOrElseThrow(RuntimeException::new);
dmnRuntime.addListener(new JITDMNListener());
DMNModel dmnModel = dmnRuntime.getModels().get(0);
return new DMNEvaluator(dmnModel, dmnRuntime);
if (dmnModel.hasErrors()) {
List<DMNMessage> messages = dmnModel.getMessages(DMNMessage.Severity.ERROR);
String errorMessage = messages.stream().map(Message::getText).collect(Collectors.joining(", "));
throw new IllegalStateException(errorMessage);
} else {
return new DMNEvaluator(dmnModel, dmnRuntime);
}
}

private DMNEvaluator(DMNModel dmnModel, DMNRuntime dmnRuntime) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.kogito.jitexecutor.dmn.api;

import java.util.function.Supplier;

import jakarta.ws.rs.core.Response;

public class DMNResourceHelper {

private DMNResourceHelper() {
}

public static Response manageResponse(Supplier<Response> responseSupplier) {
try {
return responseSupplier.get();
} catch (Exception e) {
String errorMessage = e.getMessage() != null ? e.getMessage() : "Failed to get result due to " + e.getClass().getName();
return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Supplier;

import org.kie.dmn.core.internal.utils.MarshallingStubUtils;
import org.kie.kogito.jitexecutor.dmn.JITDMNService;
Expand Down Expand Up @@ -73,8 +74,11 @@ public Response jitdmnResult(JITDMNPayload payload) {
LOGGER.debug("jitdmn/dmnresult");
LOGGER.debug(payload.toString());
LOGGER.debug(LINEBREAK);
JITDMNResult dmnResult = payload.getModel() != null ? jitdmnService.evaluateModel(payload.getModel(), payload.getContext()) : jitdmnService.evaluateModel(payload, payload.getContext());
return Response.ok(dmnResult).build();
Supplier<Response> supplier = () -> {
JITDMNResult dmnResult = payload.getModel() != null ? jitdmnService.evaluateModel(payload.getModel(), payload.getContext()) : jitdmnService.evaluateModel(payload, payload.getContext());
return Response.ok(dmnResult).build();
};
return DMNResourceHelper.manageResponse(supplier);
}

@POST
Expand All @@ -86,9 +90,12 @@ public Response jitEvaluateAndExplain(JITDMNPayload payload) {
LOGGER.debug("jitdmn/evaluateAndExplain");
LOGGER.debug(payload.toString());
LOGGER.debug(LINEBREAK);
DMNResultWithExplanation response =
payload.getModel() != null ? jitdmnService.evaluateModelAndExplain(payload.getModel(), payload.getContext()) : jitdmnService.evaluateModelAndExplain(payload, payload.getContext());
return Response.ok(response).build();
Supplier<Response> supplier = () -> {
DMNResultWithExplanation response =
payload.getModel() != null ? jitdmnService.evaluateModelAndExplain(payload.getModel(), payload.getContext()) : jitdmnService.evaluateModelAndExplain(payload, payload.getContext());
return Response.ok(response).build();
};
return DMNResourceHelper.manageResponse(supplier);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import org.eclipse.microprofile.openapi.OASFactory;
import org.eclipse.microprofile.openapi.models.OpenAPI;
Expand Down Expand Up @@ -75,9 +76,12 @@ public Response schema(String payload) {
LOGGER.debug("jitdmn/validate");
LOGGER.debug(payload);
LOGGER.debug(LINEBREAK);
DMNModel dmnModel = DMNEvaluator.fromXML(payload).getDmnModel();
DMNOASResult oasResult = DMNOASGeneratorFactory.generator(Collections.singletonList(dmnModel)).build();
return fullSchema(dmnModel, oasResult, true);
Supplier<Response> supplier = () -> {
DMNModel dmnModel = DMNEvaluator.fromXML(payload).getDmnModel();
DMNOASResult oasResult = DMNOASGeneratorFactory.generator(Collections.singletonList(dmnModel)).build();
return fullSchema(dmnModel, oasResult, true);
};
return DMNResourceHelper.manageResponse(supplier);
}

private Response fullSchema(DMNModel dmnModel, DMNOASResult oasResult, final boolean singleModel) {
Expand Down Expand Up @@ -122,9 +126,12 @@ public Response schema(MultipleResourcesPayload payload) {
@Produces(MediaType.APPLICATION_JSON)
@Path("form")
public Response form(String payload) {
DMNModel dmnModel = DMNEvaluator.fromXML(payload).getDmnModel();
DMNOASResult oasResult = DMNOASGeneratorFactory.generator(Collections.singletonList(dmnModel)).build();
return formSchema(dmnModel, oasResult);
Supplier<Response> supplier = () -> {
DMNModel dmnModel = DMNEvaluator.fromXML(payload).getDmnModel();
DMNOASResult oasResult = DMNOASGeneratorFactory.generator(Collections.singletonList(dmnModel)).build();
return formSchema(dmnModel, oasResult);
};
return DMNResourceHelper.manageResponse(supplier);
}

@POST
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.kogito.jitexecutor.dmn;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.kie.dmn.api.core.DMNModel;
import org.kie.dmn.api.core.DMNRuntime;

import java.io.IOException;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.kie.kogito.jitexecutor.dmn.TestingUtils.getModelFromIoUtils;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class DMNEvaluatorTest {

private static String model;
private static String invalidModel;
private static DMNRuntime dmnRuntime;
private static DMNModel dmnModel;

@BeforeAll
public static void setup() throws IOException {
model = getModelFromIoUtils("invalid_models/DMNv1_x/test.dmn");
invalidModel = getModelFromIoUtils("invalid_models/DMNv1_5/DMN-Invalid.dmn");
dmnRuntime = mock(DMNRuntime.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the need of those mocked instances ? 🤔

dmnModel = mock(DMNModel.class);
}

@Test
void testFromXMLSuccessModel() {
String modelXML = model;
when(dmnRuntime.getModels()).thenReturn(Collections.singletonList(dmnModel));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the need of this mocked
when(dmnRuntime.getModels()).thenReturn(Collections.singletonList(dmnModel)); ? 🤔


DMNEvaluator evaluator = DMNEvaluator.fromXML(modelXML);
assertNotNull(evaluator);

}

@Test
public void testFromXMLModelWithError() {
String modelXML = invalidModel;
when(dmnRuntime.getModels()).thenReturn(Collections.singletonList(dmnModel));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above


assertThrows(IllegalStateException.class, () -> {
DMNEvaluator.fromXML(modelXML);
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.kogito.jitexecutor.dmn;

import java.util.function.Supplier;

import org.junit.jupiter.api.Test;
import org.kie.kogito.jitexecutor.dmn.api.DMNResourceHelper;

import jakarta.ws.rs.core.Response;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class DMNResourceHelperTest {

@Test
public void testManageResponseWithSuccess() {
Supplier<Response> responseSupplier = () -> Response.ok("Success").build();
Response response = DMNResourceHelper.manageResponse(responseSupplier);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @AthiraHari77
many thanks for the PR!
Could you please implement the auto-closeable try-with-resources here ?

assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
assertEquals("Success", response.getEntity());
}

@Test
public void testManageResponseWithFailure() {
Supplier<Response> responseSupplier = mock(Supplier.class);
when(responseSupplier.get()).thenThrow(new IllegalStateException("Error : Failed to validate"));
Response response = DMNResourceHelper.manageResponse(responseSupplier);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above (try-with resources)

assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
assertEquals("Error : Failed to validate", response.getEntity());
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* 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
Expand All @@ -7,7 +7,7 @@
* "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
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
@QuarkusTest
public class JITDMNResourceTest {

private static String model;
private static String invalidModel;
private static String modelWithExtensionElements;
private static String modelWithMultipleEvaluationHitIds;
Expand All @@ -60,14 +61,15 @@ public class JITDMNResourceTest {

@BeforeAll
public static void setup() throws IOException {
invalidModel = getModelFromIoUtils("invalid_models/DMNv1_x/test.dmn");
model = getModelFromIoUtils("invalid_models/DMNv1_x/test.dmn");
invalidModel = getModelFromIoUtils("invalid_models/DMNv1_5/DMN-Invalid.dmn");
modelWithExtensionElements = getModelFromIoUtils("valid_models/DMNv1_x/testWithExtensionElements.dmn");
modelWithMultipleEvaluationHitIds = getModelFromIoUtils("valid_models/DMNv1_5/MultipleHitRules.dmn");
}

@Test
void testjitEndpoint() {
JITDMNPayload jitdmnpayload = new JITDMNPayload(invalidModel, buildContext());
JITDMNPayload jitdmnpayload = new JITDMNPayload(model, buildContext());
given()
.contentType(ContentType.JSON)
.body(jitdmnpayload)
Expand Down Expand Up @@ -116,7 +118,7 @@ void testjitdmnResultEndpointWithEvaluationHitIds() throws JsonProcessingExcepti

@Test
void testjitExplainabilityEndpoint() {
JITDMNPayload jitdmnpayload = new JITDMNPayload(invalidModel, buildContext());
JITDMNPayload jitdmnpayload = new JITDMNPayload(model, buildContext());
given()
.contentType(ContentType.JSON)
.body(jitdmnpayload)
Expand All @@ -143,6 +145,30 @@ void testjitdmnWithExtensionElements() {
.body(containsString("m"), containsString("n"), containsString("sum"));
}

@Test
void testjitdmnEvaluateInvalidModel() {
JITDMNPayload jitdmnpayload = new JITDMNPayload(invalidModel, buildInvalidModelContext());
given()
.contentType(ContentType.JSON)
.body(jitdmnpayload)
.when().post("/jitdmn/dmnresult")
.then()
.statusCode(400)
.body(containsString("Error compiling FEEL expression 'Person Age >= 18' for name 'Can Drive?' on node 'Can Drive?': syntax error near 'Age'"));
}

@Test
void testjitdmnEvaluateAndExplainInvalidModel() {
JITDMNPayload jitdmnpayload = new JITDMNPayload(invalidModel, buildInvalidModelContext());
given()
.contentType(ContentType.JSON)
.body(jitdmnpayload)
.when().post("/jitdmn/evaluateAndExplain")
.then()
.statusCode(400)
.body(containsString("Error compiling FEEL expression 'Person Age >= 18' for name 'Can Drive?' on node 'Can Drive?': syntax error near 'Age'"));
}

private Map<String, Object> buildContext() {
Map<String, Object> context = new HashMap<>();
context.put("FICO Score", 800);
Expand All @@ -160,4 +186,12 @@ private Map<String, Object> buildMultipleHitContext() {
context.put("Numbers", numbers);
return context;
}

private Map<String, Object> buildInvalidModelContext() {
Map<String, Object> context = new HashMap<>();
context.put("Can Drive?", false);
context.put("Person Age", 14);
context.put("Id", 1);
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,17 @@ public void test() throws IOException {
.statusCode(200)
.body(containsString("InputSet"), containsString("x-dmn-type"));
}

@Test
public void testJitDmnValidate() throws IOException {
final String MODEL = getModelFromIoUtils("invalid_models/DMNv1_5/DMN-Invalid.dmn");
given()
.contentType(ContentType.XML)
.body(MODEL)
.when().post("/jitdmn/schema")
.then()
.statusCode(400)
.body(containsString("Error compiling FEEL expression 'Person Age >= 18' for name 'Can Drive?' on node 'Can Drive?': syntax error near 'Age'"));

}
}
Loading