Skip to content

Commit

Permalink
[KOGITO-4593] Improve testing strategy and coverage for Processes
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Sep 7, 2021
1 parent bc3ace3 commit 328c8ab
Show file tree
Hide file tree
Showing 178 changed files with 16,391 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public FileSystemProcessInstances createProcessInstances(Process<?> process) {

public abstract String path();

public abstract void setPath(String path);

}
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,11 @@ public FileSystemProcessInstances createProcessInstances(Process<?> process) {
public String path() {
return "target";
}

@Override
public void setPath(String path) {
// TODO Auto-generated method stub

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ default Collection<ProcessInstance<T>> values() {
return values(ProcessInstanceReadMode.READ_ONLY);
}

default Optional<ProcessInstance<T>> findByBusinessKey(String businessKey) {
return findByBusinessKey(businessKey, ProcessInstanceReadMode.READ_ONLY);
}

default Optional<ProcessInstance<T>> findByBusinessKey(String businessKey, ProcessInstanceReadMode mode) {
for (ProcessInstance<T> instance : values(mode)) {
if (businessKey.equals(instance.businessKey())) {
return Optional.of(instance);
}
}
return Optional.empty();
}

Collection<ProcessInstance<T>> values(ProcessInstanceReadMode mode);

Integer size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ default void activate() {
default void deactivate() {

}

default ProcessInstancesFactory processInstancesFactory() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public ProcessInstance<BpmnVariables> createInstance() {
return new BpmnProcessInstance(this, createModel(), this.createProcessRuntime());
}

@Override
public ProcessInstance<BpmnVariables> createInstance(String businessKey, Model m) {
BpmnVariables vars = new BpmnVariables();
vars.fromMap(m.toMap());
return createInstance(businessKey, vars);
}

@Override
public ProcessInstance<BpmnVariables> createInstance(String businessKey, BpmnVariables variables) {
BpmnVariables variablesModel = createModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ private ProcessInstance startProcess(String processId, Map<String, Object> param
KogitoProcessInstance processInstance = createProcessInstance(processId, parameters);
if (processInstance != null) {
processInstanceManager.addProcessInstance(processInstance);
runtimeContext.setupParameters((org.jbpm.process.instance.ProcessInstance) processInstance, parameters);
return kogitoProcessRuntime.startProcessInstance(processInstance.getStringId(), trigger, agendaFilter);
}
return null;
Expand All @@ -164,6 +165,7 @@ public KogitoProcessInstance startProcess(String processId, CorrelationKey corre
KogitoProcessInstance processInstance = createProcessInstance(processId, correlationKey, parameters);
if (processInstance != null) {
processInstanceManager.addProcessInstance(processInstance);
runtimeContext.setupParameters((org.jbpm.process.instance.ProcessInstance) processInstance, parameters);
return (KogitoProcessInstance) startProcessInstance(processInstance.getId());
}
return null;
Expand All @@ -188,7 +190,6 @@ public KogitoProcessInstance createProcessInstance(String processId, Correlation
private org.jbpm.process.instance.ProcessInstance createProcessInstance(Process process, CorrelationKey correlationKey, Map<String, Object> parameters) {
org.jbpm.process.instance.ProcessInstance pi = runtimeContext.createProcessInstance(process, correlationKey);
pi.setKnowledgeRuntime(knowledgeRuntime);
runtimeContext.setupParameters(pi, parameters);
return pi;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.function.Function;

import org.drools.core.process.instance.WorkItem;
import org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemHandler;
import org.jbpm.process.instance.impl.workitem.Abort;
import org.jbpm.process.instance.impl.workitem.Active;
import org.jbpm.process.instance.impl.workitem.Complete;
Expand Down Expand Up @@ -205,7 +206,9 @@ private void transitionWorkItem(InternalKogitoWorkItem workItem, Transition<?> t
workItem.setResults((Map<String, Object>) transition.data());
workItem.setPhaseId(Complete.ID);
workItem.setPhaseStatus(Complete.STATUS);
completePhase.apply(workItem, transition);
if (handler instanceof HumanTaskWorkItemHandler) {
completePhase.apply(workItem, transition);
}
internalCompleteWorkItem(workItem);
}
processInstance.signalEvent("workItemTransition", transition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
public class RuleSetNode extends StateBasedNode implements ContextContainer,
Mappable {

public RuleSetNode() {
// do nothing
}

public static abstract class RuleType implements Serializable {

private static final String UNIT_RULEFLOW_PREFIX = "unit:";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ public void setProcessInstancesFactory(ProcessInstancesFactory processInstancesF
this.processInstancesFactory = processInstancesFactory;
}

public ProcessInstancesFactory getProcessInstancesFactory() {
return processInstancesFactory;
}

public EventListener eventListener() {
return completionEventListener;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ public void start(String trigger, String referenceId) {
getProcessRuntime().getProcessInstanceManager().addProcessInstance(this.processInstance);
this.id = processInstance.getStringId();
addCompletionEventListener();

for (Entry<String, Object> entry : variables.toMap().entrySet()) {
processInstance().setVariable(entry.getKey(), entry.getValue());
}
KogitoProcessInstance processInstance = getProcessRuntime().getKogitoProcessRuntime().startProcessInstance(this.id, trigger);
addToUnitOfWork(pi -> ((MutableProcessInstances<T>) process.instances()).create(pi.id(), pi));
unbind(variables, processInstance.getVariables());
Expand Down
22 changes: 22 additions & 0 deletions kogito-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,28 @@
<artifactId>kogito-spring-boot-test-utils</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-junit5-extension</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-junit5-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-junit5-runtime-java</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-junit5-persistence-fs</artifactId>
<version>${project.version}</version>
</dependency>

</dependencies>

</dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
import org.kie.kogito.codegen.api.template.InvalidTemplateException;
import org.kie.kogito.codegen.api.template.TemplatedGenerator;
import org.kie.kogito.codegen.core.AbstractApplicationSection;
import org.kie.kogito.process.ProcessInstancesFactory;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier.Keyword;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.BodyDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.LambdaExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
Expand All @@ -40,6 +44,7 @@
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.IfStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.UnknownType;

import static com.github.javaparser.ast.NodeList.nodeList;
Expand All @@ -49,7 +54,6 @@ public class ProcessContainerGenerator extends AbstractApplicationSection {
public static final String SECTION_CLASS_NAME = "Processes";

private final List<ProcessGenerator> processes;
private final List<BodyDeclaration<?>> factoryMethods;

private BlockStmt byProcessIdBody = new BlockStmt();
private BlockStmt processesBody = new BlockStmt();
Expand All @@ -58,11 +62,11 @@ public class ProcessContainerGenerator extends AbstractApplicationSection {
public ProcessContainerGenerator(KogitoBuildContext context) {
super(context, SECTION_CLASS_NAME);
this.processes = new ArrayList<>();
this.factoryMethods = new ArrayList<>();

this.templatedGenerator = TemplatedGenerator.builder()
.withTargetTypeName(SECTION_CLASS_NAME)
.build(context, "ProcessContainer");

}

public void addProcess(ProcessGenerator p) {
Expand All @@ -74,6 +78,9 @@ public void addProcessToApplication(ProcessGenerator r) {
ObjectCreationExpr newProcess = new ObjectCreationExpr()
.setType(r.targetCanonicalName())
.addArgument("application");
if (context.getAddonsConfig().usePersistence() && !context.hasDI()) {
newProcess.addArgument("processInstancesFactory");
}
MethodCallExpr expr = new MethodCallExpr(newProcess, "configure");
MethodCallExpr method = new MethodCallExpr(new NameExpr("mappedProcesses"), "computeIfAbsent",
nodeList(new StringLiteralExpr(r.processId()),
Expand All @@ -90,6 +97,27 @@ public CompilationUnit compilationUnit() {
CompilationUnit compilationUnit = templatedGenerator.compilationUnitOrThrow("Invalid Template: No CompilationUnit");

registerProcessesExplicitly(compilationUnit);
if (context.getAddonsConfig().usePersistence() && !context.hasDI()) {
ClassOrInterfaceDeclaration clazz = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).get();

Expression expression = new ObjectCreationExpr(null, new ClassOrInterfaceType(null, "org.kie.kogito.persistence.KogitoProcessInstancesFactoryImpl"), NodeList.nodeList());
FieldDeclaration pathField = new FieldDeclaration().addVariable(new VariableDeclarator()
.setType(new ClassOrInterfaceType(null, ProcessInstancesFactory.class.getCanonicalName()))
.setName("processInstancesFactory")
.setInitializer(expression));

clazz.addMember(pathField);

BlockStmt getMethodBody = new BlockStmt();
getMethodBody.addStatement(new ReturnStmt(new NameExpr("processInstancesFactory")));

MethodDeclaration getMethod = new MethodDeclaration()
.addModifier(Keyword.PUBLIC)
.setName("processInstancesFactory")
.setType("org.kie.kogito.process.ProcessInstancesFactory")
.setBody(getMethodBody);
clazz.addMember(getMethod);
}
return compilationUnit;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.AssignExpr;
import com.github.javaparser.ast.expr.AssignExpr.Operator;
import com.github.javaparser.ast.expr.BooleanLiteralExpr;
import com.github.javaparser.ast.expr.ConditionalExpr;
import com.github.javaparser.ast.expr.EnclosedExpr;
Expand Down Expand Up @@ -394,28 +396,48 @@ protected Collection<GeneratedFile> fileSystemBasedPersistence() {
.addExtendedType(KOGITO_PROCESS_INSTANCE_FACTORY_PACKAGE);

Optional<GeneratedFile> generatedClientFile = Optional.empty();

if (context().hasDI()) {
context().getDependencyInjectionAnnotator().withApplicationComponent(persistenceProviderClazz);
}

FieldDeclaration pathField = new FieldDeclaration().addVariable(new VariableDeclarator()
.setType(new ClassOrInterfaceType(null, new SimpleName(Optional.class.getCanonicalName()), NodeList.nodeList(new ClassOrInterfaceType(null, String.class.getCanonicalName()))))
.setName(PATH_NAME));
FieldDeclaration pathField = new FieldDeclaration().addVariable(new VariableDeclarator()
.setType(new ClassOrInterfaceType(null, new SimpleName(Optional.class.getCanonicalName()), NodeList.nodeList(new ClassOrInterfaceType(null, String.class.getCanonicalName()))))
.setName(PATH_NAME));

if (context().hasDI()) {
context().getDependencyInjectionAnnotator().withConfigInjection(pathField, KOGITO_PERSISTENCE_FS_PATH_PROP);
// allow to inject path for the file system storage
BlockStmt pathMethodBody = new BlockStmt();
pathMethodBody.addStatement(new ReturnStmt(new MethodCallExpr(new NameExpr(PATH_NAME), OR_ELSE).addArgument(new StringLiteralExpr("/tmp"))));
}

MethodDeclaration pathMethod = new MethodDeclaration()
.addModifier(Keyword.PUBLIC)
.setName(PATH_NAME)
.setType(String.class)
.setBody(pathMethodBody);
BlockStmt setPathMethodBody = new BlockStmt();
setPathMethodBody.addStatement(new AssignExpr(new NameExpr(PATH_NAME),
new MethodCallExpr(new NameExpr("java.util.Optional"), "of").addArgument("storagePath"), Operator.ASSIGN));

ClassOrInterfaceType type = new ClassOrInterfaceType(null, "java.lang.String");
Parameter param = new Parameter(type, "storagePath");
MethodDeclaration setPathMethod = new MethodDeclaration()
.addModifier(Keyword.PUBLIC)
.setName("setPath")
.setType("void")
.addParameter(param)
.setBody(setPathMethodBody);

// allow to inject path for the file system storage
BlockStmt pathMethodBody = new BlockStmt();
pathMethodBody.addStatement(new ReturnStmt(new MethodCallExpr(new NameExpr(PATH_NAME), OR_ELSE).addArgument(new StringLiteralExpr("/tmp"))));

MethodDeclaration pathMethod = new MethodDeclaration()
.addModifier(Keyword.PUBLIC)
.setName(PATH_NAME)
.setType(String.class)
.setBody(pathMethodBody);

persistenceProviderClazz.addMember(pathField);
persistenceProviderClazz.addMember(pathMethod);
persistenceProviderClazz.addMember(setPathMethod);
generatedClientFile = generatePersistenceProviderClazz(persistenceProviderClazz,
new CompilationUnit(KOGITO_PROCESS_INSTANCE_PACKAGE).addType(persistenceProviderClazz));

persistenceProviderClazz.addMember(pathField);
persistenceProviderClazz.addMember(pathMethod);
generatedClientFile = generatePersistenceProviderClazz(persistenceProviderClazz,
new CompilationUnit(KOGITO_PROCESS_INSTANCE_PACKAGE).addType(persistenceProviderClazz));
}
Collection<GeneratedFile> generatedFiles = protobufBasedPersistence();
generatedClientFile.ifPresent(generatedFiles::add);

Expand Down
51 changes: 51 additions & 0 deletions kogito-tck/kogito-junit-examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-tck</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>kogito-junit-examples</artifactId>

<name>Kogito Test Compatibilty Kit Test Cases Example</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kogito.junit.runtime>standalone</kogito.junit.runtime>
</properties>
<dependencies>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-junit5-extension</artifactId>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-junit5-core</artifactId>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-junit5-runtime-java</artifactId>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-junit5-persistence-fs</artifactId>
</dependency>
</dependencies>

</project>
Loading

0 comments on commit 328c8ab

Please sign in to comment.