Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gclaussn committed Mar 2, 2024
1 parent b6d8af9 commit 639c312
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.InputStream;

import io.camunda.zeebe.client.api.response.ProcessInstanceEvent;
import io.camunda.zeebe.process.test.api.ZeebeTestEngine;

/**
Expand Down Expand Up @@ -53,10 +54,10 @@ public TestCaseExecutor createExecutor(ZeebeTestEngine engine) {
/**
* Executes the test case.
*
* @param instance The test case instance to use.
* @param processInstanceKey The key of the process instance, created especially for the test case.
* @param instance The test case instance to use.
* @param processInstanceEvent The related process instance event, created during the start of the test case.
*/
protected abstract void execute(TestCaseInstance instance, long processInstanceKey);
protected abstract void execute(TestCaseInstance instance, ProcessInstanceEvent processInstanceEvent);

/**
* Returns an input stream that provides the BPMN resource with the process definition to be tested - either this method or {@link #getBpmnResourceName()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import org.camunda.community.bpmndt.platform8.api.TestCaseInstanceMemo.JobMemo;

import io.camunda.zeebe.client.api.response.ActivatedJob;
import io.camunda.zeebe.client.api.response.ProcessInstanceEvent;
import io.camunda.zeebe.client.api.worker.JobClient;
import io.camunda.zeebe.client.api.worker.JobWorker;
import io.camunda.zeebe.process.test.assertions.BpmnAssert;
import io.camunda.zeebe.process.test.assertions.ProcessInstanceAssert;

/**
* Fluent API to handle jobs that are handled via {@code JobHandler} by worker.
Expand All @@ -20,31 +23,44 @@ public class JobHandler {

private final Map<String, Object> variableMap = new HashMap<>();

private Consumer<ProcessInstanceAssert> verifier;
private io.camunda.zeebe.client.api.worker.JobHandler action;
private Object variables;

private String expectedEvaluatedType;
private Consumer<String> expectedEvaluatedTypeConsumer;
private String expectedType;
private Consumer<String> expectedTypeConsumer;

JobHandler(JobElement element) {
this.element = element;
}

void apply(TestCaseInstance instance, long processInstanceKey) {
void apply(TestCaseInstance instance, ProcessInstanceEvent processInstanceEvent) {
if (verifier != null) {
verifier.accept(BpmnAssert.assertThat(processInstanceEvent));
}

if (expectedType != null && !expectedType.equals(element.getType())) {
String message = "expected job %s to be of type %s, but was %s";
String message = "expected job %s to be of type '%s', but was '%s'";
throw new AssertionError(String.format(message, element.getId(), expectedType, element.getType()));
}
if (expectedTypeConsumer != null) {
expectedTypeConsumer.accept(element.getType());
}

JobMemo job = instance.getJob(processInstanceKey, element.getId());
JobMemo job = instance.getJob(processInstanceEvent, element.getId());
if (expectedEvaluatedType != null && !expectedEvaluatedType.equals(job.type)) {
String message = "expected job %s to be of evaluated type %s, but was %s";
String message = "expected job %s to be of evaluated type '%s', but was '%s'";
throw new AssertionError(String.format(message, element.getId(), expectedEvaluatedType, job.type));
}
if (expectedEvaluatedTypeConsumer != null) {
expectedEvaluatedTypeConsumer.accept(job.type);
}

if (action != null) {
try (JobWorker worker = instance.client.newWorker().jobType(job.type).handler(action).open()) {
instance.hasPassed(processInstanceKey, element.getId());
instance.hasPassed(processInstanceEvent, element.getId());
}
}
}
Expand Down Expand Up @@ -87,10 +103,29 @@ public void execute(io.camunda.zeebe.client.api.worker.JobHandler action) {
this.action = action;
}

void execute(JobClient client, ActivatedJob job) {
if (variables != null) {
client.newCompleteCommand(job).variables(variables).send();
} else {
client.newCompleteCommand(job).variables(variableMap).send();
}
}

/**
* Verifies that the job is of the given evaluated type.
* Verifies the job's waiting state.
*
* @param expectedEvaluatedType The expected type - an evaluated FEEL expression specified in the task definition.
* @param verifier Verifier that accepts an {@link ProcessInstanceAssert} instance.
* @return The handler.
*/
public JobHandler verify(Consumer<ProcessInstanceAssert> verifier) {
this.verifier = verifier;
return this;
}

/**
* Verifies that the job is of the given evaluated type (an evaluated FEEL expression specified in the task definition).
*
* @param expectedEvaluatedType The expected type.
* @return The handler.
*/
public JobHandler verifyEvaluatedType(String expectedEvaluatedType) {
Expand All @@ -99,16 +134,38 @@ public JobHandler verifyEvaluatedType(String expectedEvaluatedType) {
}

/**
* Verifies that the job is of the given type.
* Verifies that the job is of the given evaluated type (an evaluated FEEL expression specified in the task definition), using a consumer function.
*
* @param expectedType The expected type - a static value or FEEL expression specified in the task definition.
* @param expectedEvaluatedTypeConsumer A consumer asserting the evaluated type.
* @return The handler.
*/
public JobHandler verifyEvaluatedType(Consumer<String> expectedEvaluatedTypeConsumer) {
this.expectedEvaluatedTypeConsumer = expectedEvaluatedTypeConsumer;
return this;
}

/**
* Verifies that the job is of the given type (a static value or FEEL expression specified in the task definition).
*
* @param expectedType The expected type.
* @return The handler.
*/
public JobHandler verifyType(String expectedType) {
this.expectedType = expectedType;
return this;
}

/**
* Verifies that the job is of the given type (a static value or FEEL expression specified in the task definition), using a consumer function.
*
* @param expectedTypeConsumer A consumer asserting the type.
* @return The handler.
*/
public JobHandler verifyType(Consumer<String> expectedTypeConsumer) {
this.expectedTypeConsumer = expectedTypeConsumer;
return this;
}

/**
* Sets a variable that is used to complete the job.
*
Expand All @@ -129,7 +186,7 @@ public JobHandler withVariable(String name, Object value) {
* Sets an object as variables that is used to complete the job.
*
* @param variables The variables as POJO.
* @return The executor.
* @return The handler.
* @see #execute()
*/
public JobHandler withVariables(Object variables) {
Expand All @@ -144,7 +201,7 @@ public JobHandler withVariables(Object variables) {
* Sets variables that are used to complete the job.
*
* @param variableMap A map of variables.
* @return The executor.
* @return The handler.
* @see #execute()
*/
public JobHandler withVariableMap(Map<String, Object> variableMap) {
Expand All @@ -154,12 +211,4 @@ public JobHandler withVariableMap(Map<String, Object> variableMap) {
this.variableMap.putAll(variableMap);
return this;
}

void execute(JobClient client, ActivatedJob job) {
if (variables != null) {
client.newCompleteCommand(job).variables(variables).send();
} else {
client.newCompleteCommand(job).variables(variableMap).send();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ ZeebeClient createClient() {

void executeTestCase(ProcessInstanceEvent processInstanceEvent, ZeebeClient client) {
try (TestCaseInstance testCaseInstance = new TestCaseInstance(engine, client)) {
testCase.execute(testCaseInstance, processInstanceEvent.getProcessInstanceKey());
testCase.execute(testCaseInstance, processInstanceEvent);
}

if (verifier != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.camunda.community.bpmndt.platform8.api.TestCaseInstanceMemo.ProcessInstanceMemo;

import io.camunda.zeebe.client.ZeebeClient;
import io.camunda.zeebe.client.api.response.ProcessInstanceEvent;
import io.camunda.zeebe.process.test.api.ZeebeTestEngine;
import io.camunda.zeebe.process.test.filters.RecordStream;
import io.camunda.zeebe.protocol.record.Record;
Expand Down Expand Up @@ -59,17 +60,17 @@ public void close() {
}
}

public void apply(long processInstanceKey, JobHandler handler) {
handler.apply(this, processInstanceKey);
public void apply(ProcessInstanceEvent processInstanceEvent, JobHandler handler) {
handler.apply(this, processInstanceEvent);
}

public void apply(long processInstanceKey, UserTaskHandler handler) {
handler.apply(this, processInstanceKey);
public void apply(ProcessInstanceEvent processInstanceEvent, UserTaskHandler handler) {
handler.apply(this, processInstanceEvent);
}

public void hasPassed(long processInstanceKey, String bpmnElementId) {
public void hasPassed(ProcessInstanceEvent processInstanceEvent, String bpmnElementId) {
boolean hasPassed = selectAndTest(memo -> {
ProcessInstanceMemo processInstance = memo.processInstances.get(processInstanceKey);
ProcessInstanceMemo processInstance = memo.processInstances.get(processInstanceEvent.getProcessInstanceKey());
if (processInstance == null) {
return false;
}
Expand All @@ -80,13 +81,13 @@ public void hasPassed(long processInstanceKey, String bpmnElementId) {

if (!hasPassed) {
String message = "expected process instance %d to has passed BPMN element %s, but was not";
throw new AssertionError(String.format(message, processInstanceKey, bpmnElementId));
throw new AssertionError(String.format(message, processInstanceEvent.getProcessInstanceKey(), bpmnElementId));
}
}

public void isCompleted(long processInstanceKey) {
public void isCompleted(ProcessInstanceEvent processInstanceEvent) {
boolean isCompleted = selectAndTest(memo -> {
ProcessInstanceMemo processInstance = memo.processInstances.get(processInstanceKey);
ProcessInstanceMemo processInstance = memo.processInstances.get(processInstanceEvent.getProcessInstanceKey());
if (processInstance == null) {
return false;
}
Expand All @@ -96,13 +97,13 @@ public void isCompleted(long processInstanceKey) {

if (!isCompleted) {
String message = "expected process instance %d to be completed, but was not";
throw new AssertionError(String.format(message, processInstanceKey));
throw new AssertionError(String.format(message, processInstanceEvent.getProcessInstanceKey()));
}
}

public void isWaitingAt(long processInstanceKey, String bpmnElementId) {
public void isWaitingAt(ProcessInstanceEvent processInstanceEvent, String bpmnElementId) {
boolean isWaitingAt = selectAndTest(memo -> {
ProcessInstanceMemo processInstance = memo.processInstances.get(processInstanceKey);
ProcessInstanceMemo processInstance = memo.processInstances.get(processInstanceEvent.getProcessInstanceKey());
if (processInstance == null) {
return false;
}
Expand All @@ -117,16 +118,16 @@ public void isWaitingAt(long processInstanceKey, String bpmnElementId) {

if (!isWaitingAt) {
String message = "expected process instance %d to be waiting at %s, but was not";
throw new AssertionError(String.format(message, processInstanceKey, bpmnElementId));
throw new AssertionError(String.format(message, processInstanceEvent.getProcessInstanceKey(), bpmnElementId));
}
}

JobMemo getJob(long processInstanceKey, String bpmnElementId) {
JobMemo getJob(ProcessInstanceEvent processInstanceEvent, String bpmnElementId) {
return select(memo -> {
ProcessInstanceMemo processInstance = memo.processInstances.get(processInstanceKey);
ProcessInstanceMemo processInstance = memo.processInstances.get(processInstanceEvent.getProcessInstanceKey());
if (processInstance == null) {
String message = "process instance %d could not be found";
throw new IllegalStateException(String.format(message, processInstanceKey));
throw new IllegalStateException(String.format(message, processInstanceEvent.getProcessInstanceKey()));
}

JobMemo jobMemo = processInstance.jobs.get(bpmnElementId);
Expand Down
Loading

0 comments on commit 639c312

Please sign in to comment.