kie-maven-plugin/IntegrationTests.md
kie-maven-plugin integrations tests are tests that:
They are written following the maven standard layout and they features the maven-invoker-plugin. For each of those tests, there is a specific directory under src/it. Poms and sources are filtered to have the current drools version during execution. Surefire (unit) tests are excluded, while integration (failsafe) ones are executed. To allow that, test classes must be named with trailing "IT" (e.g. BuildPMMLTrustyTestIT). If java tests classes needs to be filtered, they have to be put under src/test/java-filtered directory (from here, they will be filtered and copied under scr/test/java one). Test compilations and reports may be found under target/it/{module_name} directory. target/it/{module_name}/build.log will contain overall build output, while additionally test reports may be found under target/it/{module_name}/target/failsafe-reports and target/it/{module_name}/target/surefire
To completely disable execution of those tests, the standard approach is to set the invoker.skip parameter to false:
mvn clean install -Dinvoker.skip=true
A specific configuration has also been implemented to disable those tests with the skiptITs parameter:
mvn clean install -DskipITs
To run only certain of those tests, use the invoker.test parameter with a pattern of the directory to be executed, e.g.:
mvn clean install -Dinvoker.test=kie-maven-plugin-test-kjar-1*
mvn clean install -Dinvoker.test=*-default
All those tests share some common code, mostly related to the KieContainer/KieBase/KieSession instantiation. To avoid duplication and enforce consistency, a couple of shared modules have been created:
The former define common dependencies and build setup; all the tests declare this as parent and may override/extend it. The latter contains the shared code, it is optional, and it is used by tests that actually require it.
To ensure that those twos are built and installed before the others, there are the following details
their invoker.properties file declares the clean install goals
they are nested under kie-maven-plugin-test-kjar-setup
inside kie-maven-plugin/pom.xml, the invoker configuration define kie-maven-plugin-test-kjar-setup to be invoked before the others (see here for more details)
<artifactId>maven-invoker-plugin</artifactId> <configuration> <setupIncludes> <setupInclude>kie-maven-plugin-test-kjar-setup/pom.xml</setupInclude> </setupIncludes> <pomIncludes> <pomInclude>*/pom.xml</pomInclude> </pomIncludes>
Tests using the SerializeMojo have been removed because such mojo was used for Android, whose support has been dropped down.
Due to the order of execution of different steps, KieContainer for a given kjar can not be instantiated from local repository. So, a workaround has been devised to create a KieContainer out of the kjar locally packaged (inside target folder).
Here's an example on how to achieve that (DeclaredTypesTestIT is the class this snippet has been taken from, DeclaredTypeKBase is the kiebase name defined in the companion kmodule.xml):
final URL targetLocation = DeclaredTypesTestIT.class.getProtectionDomain().getCodeSource().getLocation();
final File basedir = new File(targetLocation.getFile().replace("/test-classes/", ""));
final File kjarFile = new File(basedir, GAV_ARTIFACT_ID + "-" + GAV_VERSION + ".jar");
Assertions.assertThat(kjarFile).exists();
Set<URL> urls = new HashSet<>();
urls.add(kjarFile.toURI().toURL());
URLClassLoader projectClassLoader = URLClassLoader.newInstance(urls.toArray(new URL[0]), getClass().getClassLoader());
final KieServices kieServices = KieServices.get();
final KieContainer kieContainer = kieServices.getKieClasspathContainer(projectClassLoader);
KieSession kSession = null;
try {
final KieBase kieBase = kieContainer.getKieBase("DeclaredTypeKBase");
kSession = kieBase.newKieSession();
....`
To start with, a useful starting point would be to create, inside it directory, vanilla maven module containing the definition of the kjar project to test, with defined version (i.e. without version' placeholder). After that, look at other tests and at org.kie.maven.plugin.ittests.public class ITTestsUtils to see what kind of tests have already been implemented. If the scope of the integration test is only the successful packaging of the kjar, no test class is needed. At that phase the compilation and test execution can be done invoking standard maven commands from the root of the integration test module. Debug from IDE is also possible. Next step is to enable the integration test inside the invoker lifecycle; to do that
Then, issue
mvn clean install
from kie-maven-plugin directory.
There are different ways to debug the integration test after it is enabled as invoker module
inside invoker.properties file, enable remote debugging with the line
invoker.mavenOpts=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
launch maven build inside the target/it/{module_name}
add target/it/{module_name} as maven project inside IDE; this will allow IDE debugging (see below for detailed instructions).
The last two methods may be useful to debug/fix code; be aware to delete the sources file inside target/it/{module_name}/src/test/java-filtered before. The fixed code/pom may then be copied back to the original integration test.
This method has been tested on IntelliJ IDEA 2020.3.3 (Ultimate Edition), but should work on other versions and also inside different IDEs (with appropriate commands).
The project should now appear on the right panel "Maven".
This a screenshot before adding a project:
And this after it has been successfully added.
To remove a project from Maven panel, right click on it and select "Unlink Maven Projects".