Skip to content

Instantly share code, notes, and snippets.

@martinweiler
Created December 5, 2016 16:11
Show Gist options
  • Save martinweiler/8b961086c3b8e864b64048f67debbab6 to your computer and use it in GitHub Desktop.
Save martinweiler/8b961086c3b8e864b64048f67debbab6 to your computer and use it in GitHub Desktop.
*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* 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.drools.compiler.CommonTestMethodBase;
import org.junit.Test;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.ReleaseId;
import org.kie.api.definition.KiePackage;
import org.kie.api.definition.process.Process;
import org.kie.api.definition.rule.Rule;
import org.kie.api.runtime.KieContainer;
import java.util.Collection;
// DROOLS-xxx
public class ArtifactNameTest extends CommonTestMethodBase {
/**
* Test the creation of a KieBase when the artifactId ends with bpmn
* <p/>
*/
@Test
public void testKieBaseWithArtifactName() {
testKieBase("test");
}
@Test
public void testKieBaseWithArtifactNameContainingBPMNSuffix() {
testKieBase("test.bpmn");
}
public void testKieBase(String artifactId) {
// @formatter:off
String pomContent1 = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
"<modelVersion>4.0.0</modelVersion>\n" +
"<groupId>org.kie</groupId>\n" +
"<artifactId>"+ artifactId + "</artifactId>\n" +
"<version>1.0.0</version>\n" +
"<packaging>jar</packaging>\n" +
"</project>\n";
String kmoduleContent1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" +
"<kbase name=\"kbase1\" equalsBehavior=\"equality\" default=\"true\" packages=\"rules\">\n" +
"<ksession name=\"ksession1\" default=\"true\" type=\"stateful\"/>\n" +
"</kbase>\n"+
"</kmodule>";
String drl1 = "package org.rules\n" +
"\n" +
"rule \"Rule in KieBase\"\n" +
"when\n" +
"then\n" +
"System.out.println(\"Rule in KieBase\");\n" +
"end";
// @formatter:on
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId( "org.kie", artifactId, "1.0.0" );
KieFileSystem kfs1 = ks.newKieFileSystem()
.generateAndWritePomXML(releaseId1)
.writePomXML(pomContent1)
.write("src/main/resources/rules/rules.drl", drl1)
.writeKModuleXML(kmoduleContent1);
ks.newKieBuilder(kfs1).buildAll();
KieContainer kc = ks.newKieContainer( releaseId1 );
KieBase kieBase = kc.getKieBase();
// Assert the number of rules in the KieBase.
long nrOfRules = getNumberOfRules(kieBase);
// We should have 1 rule in our KieBase.
assertEquals(1, nrOfRules);
// Assert the number of processes in the KieBase.
long nrOfProcesses = getNumberOfProcesses(kieBase);
// We should have no process in our KieBase.
assertEquals(0, nrOfProcesses);
}
/**
* Helper method which determines the number of rules in the {@link KieBase}.
*
* @param kieBase
* the {@link KieBase}
* @return the number of rules in the {@link KieBase}
*/
private static long getNumberOfRules(KieBase kieBase) {
long nrOfRules = 0;
Collection<KiePackage> kiePackages = kieBase.getKiePackages();
for (KiePackage nextKiePackage : kiePackages) {
Collection<Rule> rules = nextKiePackage.getRules();
System.out.println(rules);
nrOfRules += rules.size();
}
return nrOfRules;
}
/**
* Helper method which determines the number of processes in the {@link KieBase}.
*
* @param kieBase
* the {@link KieBase}
* @return the number of rules in the {@link KieBase}
*/
private static long getNumberOfProcesses(KieBase kieBase) {
long nrOfProcesses = 0;
Collection<KiePackage> kiePackages = kieBase.getKiePackages();
for (KiePackage nextKiePackage : kiePackages) {
Collection<Process> process = nextKiePackage.getProcesses();
System.out.println(process);
nrOfProcesses += process.size();
}
return nrOfProcesses;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment