Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gclaussn committed May 25, 2024
1 parent 9d2cf5b commit 98eba67
Show file tree
Hide file tree
Showing 36 changed files with 800 additions and 180 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package org.camunda.community.bpmndt;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.camunda.community.bpmndt.model.BpmnElement;
import org.camunda.community.bpmndt.model.BpmnElementScope;
import org.camunda.community.bpmndt.model.TestCase;

public class TestCaseContext {

private final List<BpmnElement> multiInstances = new LinkedList<>();
private final List<BpmnElementScope> multiInstanceScopes = new LinkedList<>();
private final Map<String, GeneratorStrategy> strategies = new HashMap<>();
private final List<BpmnElement> multiInstances = new ArrayList<>();
private final List<BpmnElementScope> multiInstanceScopes = new ArrayList<>();
private final List<GeneratorStrategy> strategies = new ArrayList<>();

private String className;
private boolean duplicateName;
Expand All @@ -31,7 +29,7 @@ public void addMultiInstanceScope(BpmnElementScope scope) {
}

public void addStrategy(GeneratorStrategy strategy) {
strategies.put(strategy.getElement().getId(), strategy);
strategies.add(strategy);
}

/**
Expand Down Expand Up @@ -74,13 +72,12 @@ public String getResourceName() {
}

/**
* Returns the strategy of the BPMN element with the given ID.
* Returns the strategies of all relevant BPMN elements and scopes.
*
* @param elementId ID of an existing element.
* @return The strategy.
* @return A list of strategies.
*/
public GeneratorStrategy getStrategy(String elementId) {
return strategies.get(elementId);
public List<GeneratorStrategy> getStrategies() {
return strategies;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ void apply(TestCaseInstance instance, long processInstanceKey) {
}
}

/**
* Customizes the handler, using the given {@link Consumer} function. This method can be used to apply a common customization needed for different test
* cases.
*
* <pre>
* tc.handleMultiInstance().customize(this::prepare);
* </pre>
*
* @param customizer A function that accepts a {@link CustomMultiInstanceHandler}.
* @return The handler.
*/
public CustomMultiInstanceHandler customize(Consumer<CustomMultiInstanceHandler> customizer) {
if (customizer != null) {
customizer.accept(this);
}
return this;
}

/**
* Executes a custom action that handles the multi instance or the multi instance scope.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ void apply(TestCaseInstance instance, long processInstanceKey) {
}
}

/**
* Executes an action that completes the job, when the process instance is waiting at the corresponding element, using specified variables.
*
* @see #withVariable(String, Object)
* @see #withVariables(Object)
* @see #withVariableMap(Map)
*/
public void complete() {
action = this::complete;
}

/**
* Customizes the handler, using the given {@link Consumer} function. This method can be used to apply a common customization needed for different test
* cases.
Expand All @@ -108,17 +119,6 @@ public JobHandler customize(Consumer<JobHandler> customizer) {
return this;
}

/**
* Executes an action that completes the job, when the process instance is waiting at the corresponding element, using specified variables.
*
* @see #withVariable(String, Object)
* @see #withVariables(Object)
* @see #withVariableMap(Map)
*/
public void execute() {
action = this::execute;
}

/**
* Executes a custom action that handles the job, when the process instance is waiting at the corresponding element.
*
Expand All @@ -132,14 +132,6 @@ public void execute(BiConsumer<ZeebeClient, Long> action) {
this.action = action;
}

void execute(ZeebeClient client, long jobKey) {
if (variables != null) {
client.newCompleteCommand(jobKey).variables(variables).send().join();
} else {
client.newCompleteCommand(jobKey).variables(variableMap).send().join();
}
}

/**
* Throws an BPMN error using the given error code and message as well as the specified variables.
* <p>
Expand All @@ -155,16 +147,6 @@ public void throwBpmnError(String errorCode, String errorMessage) {
this.action = this::throwBpmnError;
}

void throwBpmnError(ZeebeClient client, long jobKey) {
var throwErrorCommandStep2 = client.newThrowErrorCommand(jobKey).errorCode(errorCode).errorMessage(errorMessage);

if (variables != null) {
throwErrorCommandStep2.variables(variables).send().join();
} else {
throwErrorCommandStep2.variables(variableMap).send().join();
}
}

/**
* Verifies the job's waiting state.
* <p>
Expand Down Expand Up @@ -258,7 +240,7 @@ public void waitForBoundaryEvent() {
* @param name The name of the variable.
* @param value The variable's value.
* @return The handler.
* @see #execute()
* @see #complete()
*/
public JobHandler withVariable(String name, Object value) {
if (variables != null) {
Expand All @@ -273,7 +255,7 @@ public JobHandler withVariable(String name, Object value) {
*
* @param variables The variables as POJO.
* @return The handler.
* @see #execute()
* @see #complete()
*/
public JobHandler withVariables(Object variables) {
if (!variableMap.isEmpty()) {
Expand All @@ -288,7 +270,7 @@ public JobHandler withVariables(Object variables) {
*
* @param variableMap A map of variables.
* @return The handler.
* @see #execute()
* @see #complete()
*/
public JobHandler withVariableMap(Map<String, Object> variableMap) {
if (variables != null) {
Expand All @@ -297,4 +279,22 @@ public JobHandler withVariableMap(Map<String, Object> variableMap) {
this.variableMap.putAll(variableMap);
return this;
}

void complete(ZeebeClient client, long jobKey) {
if (variables != null) {
client.newCompleteCommand(jobKey).variables(variables).send().join();
} else {
client.newCompleteCommand(jobKey).variables(variableMap).send().join();
}
}

void throwBpmnError(ZeebeClient client, long jobKey) {
var throwErrorCommandStep2 = client.newThrowErrorCommand(jobKey).errorCode(errorCode).errorMessage(errorMessage);

if (variables != null) {
throwErrorCommandStep2.variables(variables).send().join();
} else {
throwErrorCommandStep2.variables(variableMap).send().join();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,6 @@ public void correlate() {
action = this::correlate;
}

/**
* Correlates the message using a custom action, when the process instance is waiting at the corresponding element.
*
* @param action A specific action that implements the {@link Correlation} or accepts a {@link ZeebeClient}, the message name and the correlation key.
* @see ZeebeClient#newPublishMessageCommand()
*/
public void correlate(Correlation action) {
if (action == null) {
throw new IllegalArgumentException("action is null");
}
this.action = action;
}

void correlate(ZeebeClient client, String messageName, String correlationKey) {
var publishMessageCommandStep3 = client.newPublishMessageCommand()
.messageName(messageName)
.correlationKey(correlationKey);

if (variables != null) {
publishMessageCommandStep3.variables(variables).send().join();
} else {
publishMessageCommandStep3.variables(variableMap).send().join();
}
}

/**
* Customizes the handler, using the given {@link Consumer} function. This method can be used to apply a common customization needed for different test
* cases.
Expand All @@ -140,6 +115,19 @@ public MessageEventHandler customize(Consumer<MessageEventHandler> customizer) {
return this;
}

/**
* Correlates the message using a custom action, when the process instance is waiting at the corresponding element.
*
* @param action A specific action that implements the {@link Correlation} or accepts a {@link ZeebeClient}, the message name and the correlation key.
* @see ZeebeClient#newPublishMessageCommand()
*/
public void execute(Correlation action) {
if (action == null) {
throw new IllegalArgumentException("action is null");
}
this.action = action;
}

/**
* Verifies the message event's waiting state.
*
Expand Down Expand Up @@ -263,6 +251,18 @@ public MessageEventHandler withVariableMap(Map<String, Object> variableMap) {
return this;
}

void correlate(ZeebeClient client, String messageName, String correlationKey) {
var publishMessageCommandStep3 = client.newPublishMessageCommand()
.messageName(messageName)
.correlationKey(correlationKey);

if (variables != null) {
publishMessageCommandStep3.variables(variables).send().join();
} else {
publishMessageCommandStep3.variables(variableMap).send().join();
}
}

/**
* Interface for custom actions that need to correlate a message.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,6 @@ public void broadcast() {
action = this::broadcast;
}

/**
* Broadcasts a signal using a custom action, when the process instance is waiting at the corresponding element.
*
* @param action A specific action that accepts a {@link ZeebeClient} and the signal name.
* @see ZeebeClient#newBroadcastSignalCommand()
*/
public void broadcast(BiConsumer<ZeebeClient, String> action) {
if (action == null) {
throw new IllegalArgumentException("action is null");
}
this.action = action;
}

void broadcast(ZeebeClient client, String signalName) {
client.newBroadcastSignalCommand().signalName(signalName).send().join();
}

/**
* Customizes the handler, using the given {@link Consumer} function. This method can be used to apply a common customization needed for different test
* cases.
Expand All @@ -110,6 +93,19 @@ public SignalEventHandler customize(Consumer<SignalEventHandler> customizer) {
return this;
}

/**
* Broadcasts a signal using a custom action, when the process instance is waiting at the corresponding element.
*
* @param action A specific action that accepts a {@link ZeebeClient} and the signal name.
* @see ZeebeClient#newBroadcastSignalCommand()
*/
public void execute(BiConsumer<ZeebeClient, String> action) {
if (action == null) {
throw new IllegalArgumentException("action is null");
}
this.action = action;
}

/**
* Verifies the signal event's waiting state.
*
Expand Down Expand Up @@ -153,4 +149,8 @@ public SignalEventHandler verifySignalNameExpression(Consumer<String> signalName
this.signalNameExpressionConsumer = signalNameExpressionConsumer;
return this;
}

void broadcast(ZeebeClient client, String signalName) {
client.newBroadcastSignalCommand().signalName(signalName).send().join();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ public void hasTerminated(long processInstanceKey, String bpmnElementId) {
}
}

public void hasTerminatedMultiInstance(long processInstanceKey, String elementId) {
boolean hasTerminated = selectAndTest(memo -> {
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
return false;
}

var element = processInstance.getMultiInstance(elementId);
return element != null && element.state == ProcessInstanceIntent.ELEMENT_TERMINATED;
});

if (!hasTerminated) {
var message = withDetails("expected process instance %d to have terminated BPMN multi instance element %s, but has not", processInstanceKey);
throw new AssertionError(String.format(message, processInstanceKey, elementId));
}
}

public void isCompleted(long processInstanceKey) {
boolean isCompleted = selectAndTest(memo -> {
var processInstance = memo.getProcessInstance(processInstanceKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public TimerEventHandler verifyTimeDurationExpression(Consumer<String> timeDurat
* @param creationDate The timer's creation date.
* @return The duration rounded to seconds.
*/
protected Duration toDuration(long dueDate, long creationDate) {
Duration toDuration(long dueDate, long creationDate) {
long millis = Math.round((dueDate - creationDate + 999) / 1000 * 1000);
return Duration.ofMillis(millis);
}
Expand Down
Loading

0 comments on commit 98eba67

Please sign in to comment.