From acf916ece94b095fe6c503b3560e2481b6c0af4b Mon Sep 17 00:00:00 2001 From: Jinbo Wang Date: Fri, 28 Jun 2019 12:48:15 +0800 Subject: [PATCH] Add test case to cover the logic of computing runtime classpath (#602) Signed-off-by: Jinbo Wang --- TestPlan.md | 5 + .../insurance-decision/.gitignore | 15 + .../insurance-decision/pom.xml | 111 ++++++ .../META-INF/kie-deployment-descriptor.xml | 18 + .../src/main/resources/META-INF/kmodule.xml | 1 + .../main/resources/META-INF/persistence.xml | 16 + .../resources/com/demo/InsurancePricing.dmn | 237 +++++++++++++ .../src/test/java/testscenario/Launch.java | 8 + .../ScenarioJunitActivatorTest.java | 7 + .../resources/com/demo/test-scenario.scesim | 331 ++++++++++++++++++ .../resolveClasspath/kie-client/.gitignore | 15 + .../resolveClasspath/kie-client/pom.xml | 116 ++++++ .../src/main/java/client/EmbedMain.java | 53 +++ .../kie-client/src/main/java/client/Main.java | 66 ++++ .../src/main/resources/META-INF/MANIFEST.MF | 3 + .../kie-client/src/main/resources/logback.xml | 17 + 16 files changed, 1019 insertions(+) create mode 100644 testprojects/resolveClasspath/insurance-decision/.gitignore create mode 100644 testprojects/resolveClasspath/insurance-decision/pom.xml create mode 100644 testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/kie-deployment-descriptor.xml create mode 100644 testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/kmodule.xml create mode 100644 testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/persistence.xml create mode 100644 testprojects/resolveClasspath/insurance-decision/src/main/resources/com/demo/InsurancePricing.dmn create mode 100644 testprojects/resolveClasspath/insurance-decision/src/test/java/testscenario/Launch.java create mode 100644 testprojects/resolveClasspath/insurance-decision/src/test/java/testscenario/ScenarioJunitActivatorTest.java create mode 100644 testprojects/resolveClasspath/insurance-decision/src/test/resources/com/demo/test-scenario.scesim create mode 100644 testprojects/resolveClasspath/kie-client/.gitignore create mode 100644 testprojects/resolveClasspath/kie-client/pom.xml create mode 100644 testprojects/resolveClasspath/kie-client/src/main/java/client/EmbedMain.java create mode 100644 testprojects/resolveClasspath/kie-client/src/main/java/client/Main.java create mode 100644 testprojects/resolveClasspath/kie-client/src/main/resources/META-INF/MANIFEST.MF create mode 100644 testprojects/resolveClasspath/kie-client/src/main/resources/logback.xml diff --git a/TestPlan.md b/TestPlan.md index 7d7952a..f2ca1b5 100644 --- a/TestPlan.md +++ b/TestPlan.md @@ -539,3 +539,8 @@ Exception in thread "main" java.lang.IllegalStateException 6. Open VS Code menu `Help -> Open Process Explorer`, find the Java Debuggger process in the `Process Explorer`. And its command line string should contain `--enable-preview` flag. ![image](https://user-images.githubusercontent.com/14052197/56105328-40d12380-5f6e-11e9-94bc-8f3f3d298750.png) + +## Auto resolve classpath for maven projects +1. Open `resolveClasspath` project in VS Code and wait for Java extensions are activated. +2. Open `kie-client/src/main/java/client/EmbedMain.java` file, and click `Run` or `Debug` CodeLens to launch the application. Check DEBUG CONSOLE view to verify it is launched successfully. +3. Open `insurance-decision/src/test/java/testscenario/Launch.java` file, and click `Run` or `Debug` CodeLens to launch the application. Check whether the message `Main Class in Test Folder!` is printed in DEBUG CONSOLE view. \ No newline at end of file diff --git a/testprojects/resolveClasspath/insurance-decision/.gitignore b/testprojects/resolveClasspath/insurance-decision/.gitignore new file mode 100644 index 0000000..d2a8dd3 --- /dev/null +++ b/testprojects/resolveClasspath/insurance-decision/.gitignore @@ -0,0 +1,15 @@ +/target +/local +**/project.repositories + +# Eclipse, Netbeans and IntelliJ files +**/.* +!.gitignore +!.gitattributes +**/nbproject +**/*.ipr +**/*.iws +**/*.iml + +# Repository wide ignore mac DS_Store files +.DS_Store diff --git a/testprojects/resolveClasspath/insurance-decision/pom.xml b/testprojects/resolveClasspath/insurance-decision/pom.xml new file mode 100644 index 0000000..625f056 --- /dev/null +++ b/testprojects/resolveClasspath/insurance-decision/pom.xml @@ -0,0 +1,111 @@ + + + 4.0.0 + com.demo + InsuranceDecision + 1.0-SNAPSHOT + kjar + InsuranceDecision + + + 7.3.1.GA-redhat-00002 + + + + + com.redhat.ba + ba-platform-bom + ${ba.version} + import + pom + + + + + + org.kie + kie-internal + provided + + + org.kie + kie-api + provided + + + + com.thoughtworks.xstream + xstream + test + + + junit + junit + test + + + org.drools + drools-wb-scenario-simulation-editor-api + test + + + org.drools + drools-wb-scenario-simulation-editor-backend + test + + + org.drools + drools-compiler + test + + + org.kie + kie-dmn-feel + test + + + org.kie + kie-dmn-api + test + + + org.kie + kie-dmn-core + test + + + + + + org.kie + kie-maven-plugin + 7.18.0.Final-redhat-00004 + true + + + + + + jboss-ga-repository + https://maven.repository.redhat.com/ga/ + + true + + + false + + + + + + jboss-ga-plugin-repository + https://maven.repository.redhat.com/ga/ + + true + + + false + + + + \ No newline at end of file diff --git a/testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/kie-deployment-descriptor.xml b/testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/kie-deployment-descriptor.xml new file mode 100644 index 0000000..4454be1 --- /dev/null +++ b/testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/kie-deployment-descriptor.xml @@ -0,0 +1,18 @@ + + + org.jbpm.domain + org.jbpm.domain + JPA + JPA + SINGLETON + + + + + + + + + + true + diff --git a/testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/kmodule.xml b/testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/kmodule.xml new file mode 100644 index 0000000..2ba4076 --- /dev/null +++ b/testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/kmodule.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/persistence.xml b/testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..972eba9 --- /dev/null +++ b/testprojects/resolveClasspath/insurance-decision/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,16 @@ + + + + org.hibernate.jpa.HibernatePersistenceProvider + java:jboss/datasources/ExampleDS + true + + + + + + + + + + diff --git a/testprojects/resolveClasspath/insurance-decision/src/main/resources/com/demo/InsurancePricing.dmn b/testprojects/resolveClasspath/insurance-decision/src/main/resources/com/demo/InsurancePricing.dmn new file mode 100644 index 0000000..d2a85d7 --- /dev/null +++ b/testprojects/resolveClasspath/insurance-decision/src/main/resources/com/demo/InsurancePricing.dmn @@ -0,0 +1,237 @@ + + + + + previous Incidents + + + + car age + + + + + + + + + + + + + + + + + Age + + + + + PreviousIncidents + + + + + + >25 + + + false + + + 1000 + + + + + >25 + + + true + + + 1250 + + + + + [18..25] + + + false + + + 1500 + + + + + [18..25] + + + true + + + 2000 + + + + + + Insurance Final Price + + + + + + + + + + + + +Insurance Base Price * Car reliability factor(CarAge) + + + + + + + + + + car age + + + + + + <=2 + + + 1 + + + + + [3, 4] + + + 1.1 + + + + + [5,7] + + + 1.2 + + + + + >=8 + + + 1.3 + + + + + + + + + + + 50.0 + 100.0 + 100.0 + 100.0 + 100.0 + + + 506.0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testprojects/resolveClasspath/insurance-decision/src/test/java/testscenario/Launch.java b/testprojects/resolveClasspath/insurance-decision/src/test/java/testscenario/Launch.java new file mode 100644 index 0000000..11fc4d9 --- /dev/null +++ b/testprojects/resolveClasspath/insurance-decision/src/test/java/testscenario/Launch.java @@ -0,0 +1,8 @@ +package testscenario; + +public class Launch { + + public static void main(String[] args) { + System.out.println("Main Class in Test Folder!"); + } +} \ No newline at end of file diff --git a/testprojects/resolveClasspath/insurance-decision/src/test/java/testscenario/ScenarioJunitActivatorTest.java b/testprojects/resolveClasspath/insurance-decision/src/test/java/testscenario/ScenarioJunitActivatorTest.java new file mode 100644 index 0000000..958dc75 --- /dev/null +++ b/testprojects/resolveClasspath/insurance-decision/src/test/java/testscenario/ScenarioJunitActivatorTest.java @@ -0,0 +1,7 @@ +package testscenario; +/** +* Do not remove this file +*/ +@org.junit.runner.RunWith(org.drools.workbench.screens.scenariosimulation.backend.server.runner.ScenarioJunitActivator.class) +public class ScenarioJunitActivatorTest { +} \ No newline at end of file diff --git a/testprojects/resolveClasspath/insurance-decision/src/test/resources/com/demo/test-scenario.scesim b/testprojects/resolveClasspath/insurance-decision/src/test/resources/com/demo/test-scenario.scesim new file mode 100644 index 0000000..c6365a3 --- /dev/null +++ b/testprojects/resolveClasspath/insurance-decision/src/test/resources/com/demo/test-scenario.scesim @@ -0,0 +1,331 @@ + + + + + + + + Index + OTHER + + + # + java.lang.Integer + + java.lang.Integer + # + + + + + Description + OTHER + + + Scenario description + java.lang.String + + java.lang.String + Scenario description + + + + + Age + + + + 0|1 + GIVEN + + + Age + Age + + number + Age + value + + + + + CarAge + + + + 0|2 + GIVEN + + + CarAge + CarAge + + number + CarAge + value + + + + + PreviousIncidents + + + + 0|3 + GIVEN + + + PreviousIncidents + PreviousIncidents + + boolean + PreviousIncidents + value + + + + + FinalPrice + + + + 0|4 + EXPECT + + + FinalPrice + FinalPrice + + number + FinalPrice + value + + + + + Insurance Base Price + + + + 0|5 + EXPECT + + + Insurance Base Price + Insurance Base Price + + number + Insurance Base Price + value + + + src/main/resources/com/demo/InsurancePricing.dmn + DMN + + + + + + + + young client + + + + + 25 + + + + + 0 + + + + + false + + + + + 1500 + + + + + 1500 + + + + + 1 + + + + + + + + + + young client with accident + + + + + 25 + + + + + 0 + + + + + true + + + + + 2000 + + + + + 2000 + + + + + 2 + + + + + + + + + + young client with accident car 3yo + + + + + 25 + + + + + 3 + + + + + true + + + + + 2200 + + + + + 2000 + + + + + 3 + + + + + + + + + + young client with accident car 3yo + + + + + 18 + + + + + 5 + + + + + true + + + + + 2400 + + + + + 2000 + + + + + 4 + + + + + + + + + + experienced client + + + + + 26 + + + + + 0 + + + + + false + + + + + 1000 + + + + + 1000 + + + + + 5 + + + + + + + + + + \ No newline at end of file diff --git a/testprojects/resolveClasspath/kie-client/.gitignore b/testprojects/resolveClasspath/kie-client/.gitignore new file mode 100644 index 0000000..d2a8dd3 --- /dev/null +++ b/testprojects/resolveClasspath/kie-client/.gitignore @@ -0,0 +1,15 @@ +/target +/local +**/project.repositories + +# Eclipse, Netbeans and IntelliJ files +**/.* +!.gitignore +!.gitattributes +**/nbproject +**/*.ipr +**/*.iws +**/*.iml + +# Repository wide ignore mac DS_Store files +.DS_Store diff --git a/testprojects/resolveClasspath/kie-client/pom.xml b/testprojects/resolveClasspath/kie-client/pom.xml new file mode 100644 index 0000000..456b78b --- /dev/null +++ b/testprojects/resolveClasspath/kie-client/pom.xml @@ -0,0 +1,116 @@ + + + 4.0.0 + kie-client + com.demo + 1.0-SNAPSHOT + + 7.3.1.GA-redhat-00002 + UTF-8 + 1.8 + 1.8 + + + + + com.redhat.ba + ba-platform-bom + ${ba.version} + pom + import + + + + + + + org.kie.server + kie-server-client + + + + com.demo + InsuranceDecision + 1.0-SNAPSHOT + + + org.kie + kie-dmn-core + + + org.kie + kie-ci + + + + org.slf4j + slf4j-api + + + ch.qos.logback + logback-classic + runtime + + + junit + junit + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + + java + + + + + client.EmbedMain + + -verbose:class + + + + + + + + jboss-ga-repository + https://maven.repository.redhat.com/ga/ + + true + + + false + + + + + + jboss-ga-plugin-repository + https://maven.repository.redhat.com/ga/ + + true + + + false + + + + \ No newline at end of file diff --git a/testprojects/resolveClasspath/kie-client/src/main/java/client/EmbedMain.java b/testprojects/resolveClasspath/kie-client/src/main/java/client/EmbedMain.java new file mode 100644 index 0000000..5d1f6c2 --- /dev/null +++ b/testprojects/resolveClasspath/kie-client/src/main/java/client/EmbedMain.java @@ -0,0 +1,53 @@ +package client; + +import java.net.URL; +import java.net.URLClassLoader; + +import org.kie.api.KieServices; +import org.kie.api.runtime.KieContainer; +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; + +/** + * EmbedMain + */ +public class EmbedMain { + + public static void main(String[] args) { + try { + Class.forName("org.drools.compiler.commons.jci.readers.ResourceReader"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + ClassLoader cl = EmbedMain.class.getClassLoader(); + + URL[] urls = ((URLClassLoader) cl).getURLs(); + + for (URL url : urls) { + System.out.println(url.getFile()); + } + + KieServices kieServices = KieServices.Factory.get(); + + KieContainer kieContainer = kieServices.getKieClasspathContainer(); + + DMNRuntime dmnRuntime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class); + + DMNContext dmnContext = dmnRuntime.newContext(); + + dmnContext.set("PreviousIncidents", false); + dmnContext.set("Age", 28); + dmnContext.set("CarAge", 3); + + String namespace = "http://www.trisotech.com/definitions/_bb8b9304-b29f-462e-9f88-03d0d868aec5"; + String modelName = "Insurance Pricing"; + + DMNModel dmnModel = dmnRuntime.getModel(namespace, modelName); + + DMNResult dmnResult = dmnRuntime.evaluateAll(dmnModel, dmnContext); + + System.out.println(dmnResult); + } +} \ No newline at end of file diff --git a/testprojects/resolveClasspath/kie-client/src/main/java/client/Main.java b/testprojects/resolveClasspath/kie-client/src/main/java/client/Main.java new file mode 100644 index 0000000..a4d095d --- /dev/null +++ b/testprojects/resolveClasspath/kie-client/src/main/java/client/Main.java @@ -0,0 +1,66 @@ +package client; + +import java.util.Map; + +import org.kie.dmn.api.core.DMNContext; +import org.kie.dmn.api.core.DMNResult; +import org.kie.server.api.model.ServiceResponse; +import org.kie.server.client.DMNServicesClient; +import org.kie.server.client.KieServicesClient; +import org.kie.server.client.KieServicesConfiguration; +import org.kie.server.client.KieServicesFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Main { + + final static Logger log = LoggerFactory.getLogger(Main.class); + + private static final String URL = "http://localhost:8080/kie-server/services/rest/server"; + private static final String user = System.getProperty("username", "donato"); + private static final String password = System.getProperty("password", "donato"); + private static final String CONTAINER = "InsuranceDecision_1.0-SNAPSHOT"; + + public static void main(String[] args) { + Main clientApp = new Main(); + + long start = System.currentTimeMillis(); + + clientApp.evaluateDMN(); + + long end = System.currentTimeMillis(); + System.out.println("elapsed time: " + (end - start)); + } + + private void evaluateDMN() { + KieServicesClient client = getClient(); + DMNServicesClient dmnClient = client.getServicesClient(DMNServicesClient.class); + + String namespace = "http://www.trisotech.com/definitions/_bb8b9304-b29f-462e-9f88-03d0d868aec5"; + String modelName = "Insurance Pricing"; + + DMNContext dmnContext = dmnClient.newContext(); + + dmnContext.set("PreviousIncidents", false); + dmnContext.set("Age", 28); + dmnContext.set("CarAge", 3); + + ServiceResponse result = dmnClient.evaluateAll(CONTAINER, namespace, modelName, dmnContext); + System.out.println(result); + + } + + private KieServicesClient getClient() { + KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(URL, user, password); + + // Configuration for JMS + // KieServicesConfiguration config = KieServicesFactory.newJMSConfiguration(connectionFactory, requestQueue, responseQueue, username, password) + + Map headers = null; + config.setHeaders(headers); + KieServicesClient client = KieServicesFactory.newKieServicesClient(config); + + return client; + } + +} diff --git a/testprojects/resolveClasspath/kie-client/src/main/resources/META-INF/MANIFEST.MF b/testprojects/resolveClasspath/kie-client/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..301b748 --- /dev/null +++ b/testprojects/resolveClasspath/kie-client/src/main/resources/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 + +Main-Class: client.Main \ No newline at end of file diff --git a/testprojects/resolveClasspath/kie-client/src/main/resources/logback.xml b/testprojects/resolveClasspath/kie-client/src/main/resources/logback.xml new file mode 100644 index 0000000..9af6bda --- /dev/null +++ b/testprojects/resolveClasspath/kie-client/src/main/resources/logback.xml @@ -0,0 +1,17 @@ + + + + + + %d [%t] %-5p %m%n + + + + + + + + + + +