Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lucamolteni/72020031b530df64f255726cacba54c9 to your computer and use it in GitHub Desktop.
Save lucamolteni/72020031b530df64f255726cacba54c9 to your computer and use it in GitHub Desktop.
/*
* Copyright 2005 JBoss Inc
*
* Licensed 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.drools.compiler.integrationtests;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.Results;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import static org.junit.Assert.*;
@RunWith(Parameterized.class)
public class KieBaseIncludeDeclaredTypesTest extends KieBaseIncludeTest{
public KieBaseIncludeDeclaredTypesTest(boolean useModel) {
super(useModel);
}
@Parameterized.Parameters(name = "{0}")
public static Object[] params() {
return new Object[]{false, true};
}
@Test
public void testKJarIncludedDependency() {
KieServices ks = KieServices.Factory.get();
String ruleDeclaredType = "package org.test.customtype; declare POJO field : String end";
ReleaseId includedReleaseIdPojo = createChildKJar(ks, "KBasePojo", "KSessionPojo", "test-project-pojo", ruleDeclaredType);
String rule = "package org.test.rule\n" +
"import org.test.customtype.*;" +
"rule R2\n" +
"when\n" +
" POJO()\n" +
"then\n" +
" insert(2);\n" +
"end\n";
ReleaseId includedReleaseIdRule = createChildKJar(ks, "KBaseRule", "KSessionRule", "test-project-rule", rule);
ReleaseId containerReleaseId = KieServices.Factory.get().newReleaseId("org.kie", "test-container", "1.0.0-SNAPSHOT");
KieFileSystem kfs = ks.newKieFileSystem();
KieModuleModel kproj = ks.newKieModuleModel();
kproj.newKieBaseModel("KBaseContainer")
.addInclude("KBasePojo")
.addInclude("KBaseRule")
.newKieSessionModel("KSessionContainer");
kfs.writeKModuleXML(kproj.toXML());
kfs.writePomXML(getPom(containerReleaseId, includedReleaseIdRule, includedReleaseIdPojo));
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
Results buildAll = kieBuilder.buildAll(projectType).getResults();
Assertions.assertThat(buildAll.getMessages()).isEmpty();
KieContainer kieContainer = ks.newKieContainer(containerReleaseId);
KieSession ksession = kieContainer.newKieSession("KSessionContainer");
ksession.insert("test");
assertEquals(1, ksession.fireAllRules());
ksession.dispose();
}
private ReleaseId createChildKJar(KieServices ks, String kBaseName, String kSessionName, String artifactId, String... rules) {
ReleaseId includedReleaseId = KieServices.Factory.get().newReleaseId("org.kie", artifactId, "1.0.0-SNAPSHOT");
KieModuleModel kproj1 = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kproj1.newKieBaseModel(kBaseName);
kieBaseModel1.newKieSessionModel(kSessionName);
KieFileSystem kfs1 = ks.newKieFileSystem();
kfs1.writeKModuleXML(kproj1.toXML());
kfs1.writePomXML(getPom(includedReleaseId));
for (int i = 0; i < rules.length; i++) {
String file = "org/test/r" + kBaseName + i + ".drl";
kfs1.write("src/main/resources/KBase1/" + file, rules[i]);
}
KieBuilder kieBuilder1 = ks.newKieBuilder(kfs1);
Results buildKjar = kieBuilder1.buildAll(projectType).getResults();
Assertions.assertThat(buildKjar.getMessages()).isEmpty();
return includedReleaseId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment