Skip to content

Commit

Permalink
flowable#3835 initial implementation for processes
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-grofcik committed Feb 2, 2024
1 parent 356c008 commit 0942eb0
Show file tree
Hide file tree
Showing 22 changed files with 1,213 additions and 1 deletion.
40 changes: 40 additions & 0 deletions modules/flowable-assertions/flowable-process-assertions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.flowable</groupId>
<artifactId>flowable-assertions</artifactId>
<version>7.1.0-SNAPSHOT</version>
</parent>

<artifactId>flowable-process-assertions</artifactId>

<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-engine</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.flowable.assertions.process;

import org.assertj.core.api.Assertions;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;

/**
* @author martin.grofcik
*/
public class FlowableProcessAssertions extends Assertions {

public static ProcessInstanceAssert assertThat(ProcessInstance processInstance) {
return new ProcessInstanceAssert(processInstance);
}
public static HistoricProcessInstanceAssert assertThat(HistoricProcessInstance historicProcessInstance) {
return new HistoricProcessInstanceAssert(historicProcessInstance);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package org.flowable.assertions.process;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.ListAssert;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.identitylink.api.IdentityLink;
import org.flowable.identitylink.api.history.HistoricIdentityLink;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.variable.api.history.HistoricVariableInstance;

import static org.flowable.assertions.process.FlowableProcessAssertions.assertThat;
import static org.flowable.assertions.process.Utils.*;

/**
* @author martin.grofcik
*/

public class HistoricProcessInstanceAssert extends AbstractAssert<HistoricProcessInstanceAssert, HistoricProcessInstance> {

protected final ProcessServicesProvider processServicesProvider;

protected HistoricProcessInstanceAssert(ProcessEngine processEngine, HistoricProcessInstance historicProcessInstance) {
super(historicProcessInstance, HistoricProcessInstanceAssert.class);
processServicesProvider = ProcessServicesProvider.of(processEngine);
}

protected HistoricProcessInstanceAssert(HistoricProcessInstance historicProcessInstance) {
this(getProcessEngine(), historicProcessInstance);
}

/**
* Assert <b>historic</b> activities ordered by activity instance start time.
*
* @return Assertion of {@link HistoricActivityInstance} list.
*/
public ListAssert<HistoricActivityInstance> activities() {
processExistsInHistory();

return assertThat(processServicesProvider.getHistoryService().createHistoricActivityInstanceQuery().processInstanceId(actual.getId())
.orderByHistoricActivityInstanceStartTime().desc().list());
}

/**
* Assert <b>historic</b> process instance exists in the history and is finished.
*
* @return Historic process instance assertion.
*/
public HistoricProcessInstanceAssert isFinished() {
processExistsInHistory();

if (processServicesProvider.getHistoryService().createHistoricProcessInstanceQuery().finished().processInstanceId(actual.getId()).count() != 1) {
failWithMessage(getProcessDescription(actual)+" to be finished, but is running in history.");
}

return this;
}

public ListAssert<HistoricVariableInstance> variables() {
processExistsInHistory();

return assertThat(processServicesProvider.getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(actual.getId()).orderByVariableName().asc().list());
}

/**
* Assert that process instance has variable in <b>history</b>.
*
* @param variableName variable to check.
* @return Historic process instance assertion
*/

public HistoricProcessInstanceAssert hasVariable(String variableName) {
processExistsInHistory();

if (processServicesProvider.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(actual.getId()).variableExists(variableName).count() != 1) {
failWithMessage(getProcessDescription(actual)+" has variable <%s> but variable does not exist in history.", variableName);
}

return this;
}

/**
* Assert that process instance does not have variable in <b>history</b>.
* @param variableName variable to check
* @return Historic process instance assertion
*/
public HistoricProcessInstanceAssert doesNotHaveVariable(String variableName) {
processExistsInHistory();

if (processServicesProvider.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(actual.getId()).variableExists(variableName).count() != 0) {
failWithMessage(getProcessDescription(actual)+" does not have variable <%s> but variable exists in history.", variableName);
}

return this;
}

/**
* Assert that process instance has variable in <b>history</b> with value equals to expectedValue.
*
* @param variableName variable to check.
* @param expectedValue expected variable value.
* @return Historic process instance assertion
*/
public HistoricProcessInstanceAssert hasVariableWithValue(String variableName, Object expectedValue) {
processExistsInHistory();
hasVariable(variableName);

HistoricVariableInstance actualVariable = processServicesProvider.getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(actual.getId()).variableName(variableName).singleResult();
Assertions.assertThat(actualVariable.getValue()).isEqualTo(expectedValue);
return this;
}

/**
* Assert list of <b>historic</b> identity links without ordering.
*
* @return Assertion of #{@link IdentityLink} list.
*/
public ListAssert<HistoricIdentityLink> identityLinks() {
processExistsInHistory();

return assertThat(processServicesProvider.getHistoryService().getHistoricIdentityLinksForProcessInstance(actual.getId()));
}

/**
* Assert list of user tasks in the <b>history</b> ordered by the task name ascending.
* Process, Task variables and identityLinks are included.
*
* @return Assertion of {@link HistoricTaskInstance} list.
*/
public ListAssert<HistoricTaskInstance> userTasks() {
processExistsInHistory();

return assertThat(processServicesProvider.getHistoryService().createHistoricTaskInstanceQuery().processInstanceId(actual.getId()).orderByTaskName().asc()
.includeProcessVariables().includeIdentityLinks().includeTaskLocalVariables().list());
}

private void processExistsInHistory() {
isNotNull();
isInHistory();
}

private void isInHistory() {
if (processServicesProvider.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(actual.getId()).count() != 1) {
failWithMessage(getProcessDescription(actual)+"> exists in history but process instance not found.");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package org.flowable.assertions.process;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.ListAssert;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.runtime.ActivityInstance;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.eventsubscription.api.EventSubscription;
import org.flowable.identitylink.api.IdentityLink;
import org.flowable.task.api.Task;
import org.flowable.variable.api.persistence.entity.VariableInstance;

import static org.flowable.assertions.process.FlowableProcessAssertions.assertThat;
import static org.flowable.assertions.process.Utils.*;
/**
* @author martin.grofcik
*/
public class ProcessInstanceAssert extends AbstractAssert<ProcessInstanceAssert, ProcessInstance> {
protected final ProcessServicesProvider processServicesProvider;

protected ProcessInstanceAssert(ProcessEngine processEngine, ProcessInstance processInstance) {
super(processInstance, ProcessInstanceAssert.class);
processServicesProvider = ProcessServicesProvider.of(processEngine);
}

protected ProcessInstanceAssert(ProcessInstance processInstance) {
this(getProcessEngine(), processInstance);
}

/**
* Assert that process instance exists in <b>runtime</b>.
*
* @return Process instance assert.
*/
public ProcessInstanceAssert isRunning() {
isNotNull();

if (processServicesProvider.getRuntimeService().createProcessInstanceQuery().processInstanceId(actual.getId()).count() < 1) {
failWithMessage(getProcessDescription(actual)+" to be running but is not.", actual.getId());
}
return this;
}

/**
* Assert that process instance has variable in <b>runtime</b>.
*
* @param variableName variable to check.
* @return Process instance assertion
*/
public ProcessInstanceAssert hasVariable(String variableName) {
isNotNull();

if (processServicesProvider.getRuntimeService().createProcessInstanceQuery().processInstanceId(actual.getId()).variableExists(variableName).count() != 1) {
failWithMessage(getProcessDescription(actual)+" has variable <%s> but variable does not exist.", variableName);
}

return this;
}

/**
* Assert that process instance has variable in <b>runtime</b> with value equals to expectedValue.
*
* @param variableName variable to check.
* @param expectedValue expected variable value.
* @return Process instance assertion
*/
public ProcessInstanceAssert hasVariableWithValue(String variableName, Object expectedValue) {
isNotNull();
hasVariable(variableName);

VariableInstance actualVariable = processServicesProvider.getRuntimeService().createVariableInstanceQuery().processInstanceId(actual.getId()).variableName(variableName).singleResult();
Assertions.assertThat(actualVariable.getValue()).isEqualTo(expectedValue);
return this;
}

/**
* Assert that process instance does not have variable in <b>runtime</b>.
* @param variableName variable to check
* @return Process instance assertion
*/
public ProcessInstanceAssert doesNotHaveVariable(String variableName) {
isNotNull();

if (processServicesProvider.getRuntimeService().createProcessInstanceQuery().processInstanceId(actual.getId()).variableExists(variableName).count() != 0) {
failWithMessage(getProcessDescription(actual)+" does not have variable <%s> but variable exists.", variableName);
}

return this;
}

/**
* Assert that process instance does on exist in <b>runtime</b>.
*
* @return Process instance assertion
*/
public ProcessInstanceAssert doesNotExist() {
isNotNull();

if (processServicesProvider.getRuntimeService().createProcessInstanceQuery().processInstanceId(actual.getId()).count() != 0) {
failWithMessage(getProcessDescription(actual)+" is finished but instance exists in runtime.");
}

return this;
}

/**
* @return Historic process instance assertion
*/
public HistoricProcessInstanceAssert inHistory() {
return assertThat(processServicesProvider.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(actual.getId()).singleResult());
}

/**
* Assert list of <b>runtime</b> process instance activities ordered by activity start time.
*
* @return Assertion of #{@link ActivityInstance} list.
*/
public ListAssert<ActivityInstance> activities() {
isNotNull();

return assertThat(processServicesProvider.getRuntimeService().createActivityInstanceQuery().processInstanceId(actual.getId()).orderByActivityInstanceStartTime().asc().list());
}

/**
* Assert list of <b>runtime</b> execution instances without ordering.
*
* @return Assertion of #{@link Execution} list.
*/
public ListAssert<Execution> executions() {
isNotNull();

return assertThat(processServicesProvider.getRuntimeService().createExecutionQuery().processInstanceId(actual.getId()).list());
}

/**
* Assert list of <b>runtime</b> variable instances ordered by variable name ascending.
*
* @return Assertion of #{@link VariableInstance} list.
*/
public ListAssert<VariableInstance> variables() {
isNotNull();

return assertThat(processServicesProvider.getRuntimeService().createVariableInstanceQuery().processInstanceId(actual.getId()).orderByVariableName().asc().list());
}

/**
* Assert list of <b>runtime</b> identity links without ordering.
*
* @return Assertion of #{@link IdentityLink} list.
*/
public ListAssert<IdentityLink> identityLinks() {
isNotNull();

return assertThat(processServicesProvider.getRuntimeService().getIdentityLinksForProcessInstance(actual.getId()));
}

/**
* Assert list of user tasks in the <b>runtime</b> ordered by the task name ascending.
*
* @return Assertion of {@link Task} list.
*/
public ListAssert<Task> userTasks() {
isNotNull();

return assertThat(getTaskService().createTaskQuery().processInstanceId(actual.getId()).orderByTaskName().asc()
.includeProcessVariables().includeIdentityLinks().includeTaskLocalVariables().list());
}

/**
* Assert list of event subscriptions in the <b>runtime</b> ordered by the event name ascending.
*
* @return Assertion of {@link EventSubscription} list.
*/

public ListAssert<EventSubscription> eventSubscription() {
isNotNull();

return assertThat(processServicesProvider.getRuntimeService().createEventSubscriptionQuery().processInstanceId(actual.getId()).orderByEventName().asc().list());
}
}
Loading

0 comments on commit 0942eb0

Please sign in to comment.