package com.example.kieserverspringboot;
import com.example.kieserverspringboot.listener.SampleAgendaEventListener;
import com.model.Person;
import org.appformer.maven.integration.MavenRepository;
import org.drools.compiler.kie.builder.impl.InternalKieModule;
import org.drools.core.io.impl.ClassPathResource;
import org.junit.*;
import org.junit.runner.RunWith;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.builder.ReleaseId;
import org.kie.api.command.Command;
import org.kie.api.command.KieCommands;
import org.kie.api.runtime.ExecutionResults;
import org.kie.server.api.KieServerConstants;
import org.kie.server.api.marshalling.MarshallingFormat;
import org.kie.server.api.model.KieContainerResource;
import org.kie.server.api.model.KieServerMode;
import org.kie.server.api.model.KieServiceResponse;
import org.kie.server.api.model.ServiceResponse;
import org.kie.server.client.KieServicesClient;
import org.kie.server.client.KieServicesConfiguration;
import org.kie.server.client.KieServicesFactory;
import org.kie.server.client.RuleServicesClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
import static org.appformer.maven.integration.MavenRepository.getMavenRepository;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {KieServerSpringBootApplication.class}, webEnvironment = WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations="classpath:application-rules.properties")
public class RulesOnlyKieServerTest {
static final String ARTIFACT_ID = "business-rules";
static final String GROUP_ID = "com.yst";
static final String VERSION = "1.0-SNAPSHOT";
@LocalServerPort
private int port;
private String user = "john";
private String password = "john@pwd1";
private String containerId = "rules1";
private KieServicesClient kieServicesClient;
@Autowired
private SampleAgendaEventListener listener;
@BeforeClass
public static void generalSetup() {
System.setProperty(KieServerConstants.KIE_SERVER_MODE, KieServerMode.DEVELOPMENT.name());
createKJar();
}
@AfterClass
public static void generalCleanup() {
System.clearProperty(KieServerConstants.KIE_SERVER_MODE);
}
@Before
public void setup() {
listener.clear();
org.kie.server.api.model.ReleaseId releaseId = new org.kie.server.api.model.ReleaseId(GROUP_ID, ARTIFACT_ID, VERSION);
String serverUrl = "http://localhost:" + port + "/rest/server";
KieServicesConfiguration configuration = KieServicesFactory.newRestConfiguration(serverUrl, user, password);
configuration.setTimeout(60000);
configuration.setMarshallingFormat(MarshallingFormat.JSON);
this.kieServicesClient = KieServicesFactory.newKieServicesClient(configuration);
KieContainerResource resource = new KieContainerResource(containerId, releaseId);
kieServicesClient.createContainer(containerId, resource);
}
@After
public void cleanup() {
kieServicesClient.disposeContainer(containerId);
}
@Test
public void testInvokeRulesOnStateless() {
testInvokeRulesOn("defaultStatelessKieSession");
}
@Test
public void testInvokeRulesOnStatefull() {
testInvokeRulesOn("defaultKieSession");
}
private void testInvokeRulesOn(String ksessionName) {
List<Command<?>> commands = new ArrayList<Command<?>>();
KieCommands cmdFactory = KieServices.Factory.get().getCommands();
commands.add(cmdFactory.newInsert("John"));
commands.add(cmdFactory.newInsert(new Person()));
commands.add(cmdFactory.newFireAllRules("fire-identifier"));
RuleServicesClient rulesClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
ServiceResponse<ExecutionResults> reply = rulesClient.executeCommandsWithResults("rules1", cmdFactory.newBatchExecution(commands, ksessionName));
if (!KieServiceResponse.ResponseType.SUCCESS.equals(reply.getType())){
throw new RuntimeException("executeRule failed with message: " + reply.getMsg());
}
ExecutionResults result = reply.getResult();
assertNotNull(result);
assertEquals(1, listener.getFired().size());
assertEquals("Your First Rule",listener.getFired().get(0));
}
private static void createKJar() {
KieServices kieServices = KieServices.get();
ReleaseId releaseId = kieServices.newReleaseId(GROUP_ID, ARTIFACT_ID, VERSION);
KieFileSystem kfs = kieServices.newKieFileSystem();
kfs.generateAndWritePomXML(releaseId);
byte[] pom = kfs.read("pom.xml");
kfs.write("src/main/resources/sample-rules.drl", new ClassPathResource("sample-rules.drl"));
KieBuilder kb = kieServices.newKieBuilder(kfs).buildAll();
}
}
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-server-spring-boot-starter</artifactId>
<version>7.24.0.Final</version>
</dependency>
<dependency>
<groupId>org.kie.server</groupId>
<artifactId>kie-server-client</artifactId>
<version>7.24.0.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>