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#1023] DMN - move significant models to test-resources module #2021

Closed
wants to merge 1 commit into from
Closed
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
Expand Up @@ -19,33 +19,20 @@
package org.kie.kogito.jitexecutor.dmn;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.drools.util.IoUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.kie.dmn.api.core.DMNDecisionResult;
import org.kie.dmn.api.core.DMNMessage;
import org.kie.dmn.api.core.DMNResult;
import org.kie.kogito.jitexecutor.common.requests.MultipleResourcesPayload;
import org.kie.kogito.jitexecutor.common.requests.ResourceWithURI;
import org.kie.kogito.jitexecutor.dmn.requests.JITDMNPayload;
import org.kie.kogito.jitexecutor.dmn.responses.JITDMNDecisionResult;
import org.kie.kogito.jitexecutor.dmn.responses.JITDMNMessage;
import org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
Expand All @@ -55,24 +42,14 @@
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.kie.kogito.jitexecutor.dmn.TestingUtils.MAPPER;
import static org.kie.kogito.jitexecutor.dmn.TestingUtils.getModel;
import static org.kie.kogito.jitexecutor.dmn.TestingUtils.getModelFromIoUtils;

@QuarkusTest
class DMN15Test {

private static final Logger LOG = LoggerFactory.getLogger(DMN15Test.class);
private static final ObjectMapper MAPPER;

static {
final var jitModule = new SimpleModule().addAbstractTypeMapping(DMNResult.class, JITDMNResult.class)
.addAbstractTypeMapping(DMNDecisionResult.class, JITDMNDecisionResult.class)
.addAbstractTypeMapping(DMNMessage.class, JITDMNMessage.class);

MAPPER = new ObjectMapper()
.registerModule(new Jdk8Module())
.registerModule(jitModule);
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

private static final CollectionType LIST_OF_MSGS = MAPPER.getTypeFactory()
.constructCollectionType(List.class,
Expand All @@ -85,8 +62,8 @@ public static void setup() {

@Test
void unnamedImport() throws IOException {
String URI1 = "Importing_EmptyNamed_Model.dmn";
String URI2 = "Imported_Model_Unamed.dmn";
String URI1 = "valid_models/DMNv1_5/Importing_EmptyNamed_Model.dmn";
String URI2 = "valid_models/DMNv1_5/Imported_Model_Unamed.dmn";
ResourceWithURI model1 = new ResourceWithURI(URI1, getModelFromIoUtils(URI1));
ResourceWithURI model2 = new ResourceWithURI(URI2, getModelFromIoUtils(URI2));

Expand Down Expand Up @@ -129,7 +106,7 @@ void unnamedImport() throws IOException {

@Test
void forLoopDatesEvaluate() throws IOException {
String modelFileName = "ForLoopDatesEvaluate.dmn";
String modelFileName = "valid_models/DMNv1_5/ForLoopDatesEvaluate.dmn";
validate(getModelFromIoUtils(modelFileName));
String model = getModel(modelFileName);
Map<String, Object> expectedValues = Map.of("forloopdates[0]", "2021-01-02",
Expand All @@ -144,7 +121,7 @@ void forLoopDatesEvaluate() throws IOException {

@Test
void listReplaceEvaluate() throws IOException {
String modelFileName = "ListReplaceEvaluate.dmn";
String modelFileName = "valid_models/DMNv1_5/ListReplaceEvaluate.dmn";
validate(getModelFromIoUtils(modelFileName));
String model = getModel(modelFileName);
Map<String, Object> expectedValues = Map.of("listreplacenumbers[0]", 2,
Expand All @@ -161,7 +138,7 @@ void listReplaceEvaluate() throws IOException {

@Test
void negationOfDurationEvaluate() throws IOException {
String modelFileName = "NegationOfDurationEvaluate.dmn";
String modelFileName = "valid_models/DMNv1_5/NegationOfDurationEvaluate.dmn";
validate(getModelFromIoUtils(modelFileName));
String model = getModel(modelFileName);
Map<String, Object> expectedValues = Map.of("durationnegation", true);
Expand All @@ -172,7 +149,7 @@ void negationOfDurationEvaluate() throws IOException {

@Test
void dateToDateTimeFunction() throws IOException {
String modelFileName = "DateToDateTimeFunction.dmn";
String modelFileName = "valid_models/DMNv1_5/DateToDateTimeFunction.dmn";
validate(getModelFromIoUtils(modelFileName));
String model = getModel(modelFileName);
Map<String, Object> expectedValues = Map.of("normal", "function normal( a, b )",
Expand Down Expand Up @@ -221,16 +198,4 @@ private void validate(String model) throws IOException {
List<JITDMNMessage> messages = MAPPER.readValue(response, LIST_OF_MSGS);
assertEquals(0, messages.size());
}

private String getModelFromIoUtils(String modelFileName) throws IOException {
return new String(IoUtils.readBytesFromInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(modelFileName)));
}

private String getModel(String modelFileName) throws IOException {
URL resource = Thread.currentThread().getContextClassLoader().getResource(modelFileName);
assertNotNull(resource);
try (InputStream is = resource.openStream()) {
return MAPPER.writeValueAsString(new String(is.readAllBytes(), StandardCharsets.UTF_8));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@
import java.util.HashMap;
import java.util.Map;

import org.drools.util.IoUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.kie.kogito.jitexecutor.dmn.api.JITDMNResourceTest;
import org.kie.kogito.jitexecutor.dmn.responses.DMNResultWithExplanation;
import org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult;

import static org.kie.kogito.jitexecutor.dmn.TestingUtils.getModelFromIoUtils;

public class JITDMNServiceImplTest {

private static String model;
private static JITDMNService jitdmnService;

@BeforeAll
public static void setup() throws IOException {
model = new String(IoUtils.readBytesFromInputStream(JITDMNResourceTest.class.getResourceAsStream("/test.dmn")));
model = getModelFromIoUtils("invalid_models/DMNv1_x/test.dmn");
jitdmnService = new JITDMNServiceImpl(300, 1);
}

Expand All @@ -58,7 +58,7 @@ public void testModelEvaluation() {

@Test
public void testExplainability() throws IOException {
String allTypesModel = new String(IoUtils.readBytesFromInputStream(JITDMNResourceTest.class.getResourceAsStream("/allTypes.dmn")));
String allTypesModel = getModelFromIoUtils("valid_models/DMNv1_x/allTypes.dmn");

Map<String, Object> context = new HashMap<>();
context.put("stringInput", "test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@

import java.io.IOException;

import org.drools.util.IoUtils;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;

import static org.hamcrest.CoreMatchers.is;
import static org.kie.kogito.jitexecutor.dmn.TestingUtils.getModel;

@QuarkusTest
public class OneOfEachTypeTest {
Expand All @@ -40,7 +38,7 @@ public class OneOfEachTypeTest {

@Test
public void allTypes() throws IOException {
String model = new ObjectMapper().writeValueAsString(new String(IoUtils.readBytesFromInputStream(OneOfEachTypeTest.class.getResourceAsStream("/OneOfEachType.dmn"))));
String model = getModel("valid_models/DMNv1_x/OneOfEachType.dmn");
String payload = "{ \"model\": " + model + ", \"context\": {\n" +
" \"InputBoolean\": true,\n" +
" \"InputDate\": \"2020-04-02\",\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* 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.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import org.drools.util.IoUtils;
import org.kie.dmn.api.core.DMNDecisionResult;
import org.kie.dmn.api.core.DMNMessage;
import org.kie.dmn.api.core.DMNResult;
import org.kie.kogito.jitexecutor.dmn.responses.JITDMNDecisionResult;
import org.kie.kogito.jitexecutor.dmn.responses.JITDMNMessage;
import org.kie.kogito.jitexecutor.dmn.responses.JITDMNResult;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class TestingUtils {

public static final ObjectMapper MAPPER;

static {
final var jitModule = new SimpleModule().addAbstractTypeMapping(DMNResult.class, JITDMNResult.class)
.addAbstractTypeMapping(DMNDecisionResult.class, JITDMNDecisionResult.class)
.addAbstractTypeMapping(DMNMessage.class, JITDMNMessage.class);

MAPPER = new ObjectMapper()
.registerModule(new Jdk8Module())
.registerModule(jitModule);
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

public static String getModelFromIoUtils(String modelFileName) throws IOException {
return new String(IoUtils.readBytesFromInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(modelFileName)));
}

public static String getModel(String modelFileName) throws IOException {
URL resource = Thread.currentThread().getContextClassLoader().getResource(modelFileName);
assertNotNull(resource);
try (InputStream is = resource.openStream()) {
return MAPPER.writeValueAsString(new String(is.readAllBytes(), StandardCharsets.UTF_8));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.IOException;
import java.util.List;

import org.drools.util.IoUtils;
import org.junit.jupiter.api.Test;
import org.kie.dmn.api.core.DMNMessageType;
import org.kie.kogito.jitexecutor.dmn.responses.JITDMNMessage;
Expand All @@ -40,6 +39,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.kie.kogito.jitexecutor.dmn.TestingUtils.getModelFromIoUtils;

@QuarkusTest
public class DMNValidatorResourceTest {
Expand All @@ -55,7 +55,7 @@ public class DMNValidatorResourceTest {

@Test
public void test() throws IOException {
final String MODEL = new String(IoUtils.readBytesFromInputStream(JITDMNResourceTest.class.getResourceAsStream("/loan.dmn")));
final String MODEL = getModelFromIoUtils("valid_models/DMNv1_x/loan.dmn");
String response = given()
.contentType(ContentType.XML)
.body(MODEL)
Expand All @@ -76,7 +76,7 @@ public void test() throws IOException {

@Test
public void testOverlap() throws IOException {
final String MODEL = new String(IoUtils.readBytesFromInputStream(JITDMNResourceTest.class.getResourceAsStream("/loan_withOverlap.dmn")));
final String MODEL = getModelFromIoUtils("invalid_models/DMNv1_x/loan_withOverlap.dmn");
String response = given()
.contentType(ContentType.XML)
.body(MODEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.HashMap;
import java.util.Map;

import org.drools.util.IoUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.kie.kogito.jitexecutor.dmn.requests.JITDMNPayload;
Expand All @@ -32,6 +31,7 @@

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.containsString;
import static org.kie.kogito.jitexecutor.dmn.TestingUtils.getModelFromIoUtils;

@QuarkusTest
public class JITDMNResourceTest {
Expand All @@ -41,8 +41,8 @@ public class JITDMNResourceTest {

@BeforeAll
public static void setup() throws IOException {
model = new String(IoUtils.readBytesFromInputStream(JITDMNResourceTest.class.getResourceAsStream("/test.dmn")));
modelWithExtensionElements = new String(IoUtils.readBytesFromInputStream(JITDMNResourceTest.class.getResourceAsStream("/testWithExtensionElements.dmn")));
model = getModelFromIoUtils("invalid_models/DMNv1_x/test.dmn");
modelWithExtensionElements = getModelFromIoUtils("valid_models/DMNv1_x/testWithExtensionElements.dmn");
}

@Test
Expand Down
Loading
Loading