Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gclaussn committed Mar 12, 2024
1 parent 3aa5d76 commit 8abea3d
Show file tree
Hide file tree
Showing 38 changed files with 1,279 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class GradlePlugin implements Plugin<Project> {
private static final Logger LOGGER = LoggerFactory.getLogger(GradlePlugin.class);

/**
* Name of the test source set, which must contain "test" to indicate that the source set contains test code. Otherwise Eclipse will not recognize it!
* Name of the test source set, which must contain "test" to indicate that the source set contains test code. Otherwise, Eclipse will not recognize it!
*/
private static final String SOURCE_SET_NAME = "bpmndtTestCases";

Expand Down Expand Up @@ -62,8 +62,8 @@ public void apply(Project project) {
}).get();

// add generator task as test compile dependency
project.afterEvaluate(afterEvaluate -> {
afterEvaluate.getTasksByName(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME, false).forEach(task -> task.dependsOn(generatorTask));
});
project.afterEvaluate(afterEvaluate ->
afterEvaluate.getTasksByName(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME, false).forEach(task -> task.dependsOn(generatorTask))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Collection<Path> apply(GeneratorContextBase ctx) {
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.getFileName().toString().endsWith(Constants.BPMN_EXTENSION)) {
bpmnFiles.add(file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.impl.bpmn.behavior.CallActivityBehavior;
Expand Down Expand Up @@ -112,7 +113,7 @@ private void deploy(DeploymentBuilder deploymentBuilder, String deploymentName)
.filter(pd -> pd.getKey().equals(processDefinitionKey))
.map(ProcessDefinition::getId)
.findFirst()
.get();
.orElseThrow(() -> new NoSuchElementException(String.format("process definition with key '%s' could not be found", processDefinitionKey)));
}

/**
Expand Down Expand Up @@ -143,7 +144,7 @@ private BpmndtParseListener findParseListener() {
.filter((parseListener) -> (parseListener instanceof BpmndtParseListener))
.map(BpmndtParseListener.class::cast)
.findFirst()
.get();
.orElseThrow(() -> new NoSuchElementException("BpmndtParseListener could not be found in customPostBPMNParseListeners"));
}

public String getDeploymentId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,19 @@ public void accept(List<GeneratorStrategy> strategies, MethodSpec.Builder builde
strategy.applyHandlerAfter(builder);
}

if (activity.getType() == TestCaseActivityType.LINK_THROW) {
// since there is no activity for an intermediate link throw event
// a process instance will not pass and will never wait at such an activity
continue;
}

if (activity.hasPrevious(TestCaseActivityType.EVENT_BASED_GATEWAY)) {
// assert that event based gateway has been passed
ctx.getStrategy(activity.getPrevious().getId()).hasPassed(builder);
}

if (activity.getType() == TestCaseActivityType.EVENT_BASED_GATEWAY) {
strategy.isWaitingAt(builder);
} else if (activity.getType() == TestCaseActivityType.LINK_THROW) {
// since there is no activity for an intermediate link throw event
// a process instance will not pass and will never wait at such an activity
} else if ((activity.hasNext()) || activity.isProcessEnd()) {
strategy.hasPassed(builder);
} else if (activity.getType() == TestCaseActivityType.SCOPE) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
*/
public class DefaultStrategy implements GeneratorStrategy {

public static TypeName CALL_ACTIVITY = TypeName.get(CallActivityHandler.class);
public static TypeName EVENT = TypeName.get(EventHandler.class);
public static TypeName EXTERNAL_TASK = TypeName.get(ExternalTaskHandler.class);
public static TypeName JOB = TypeName.get(JobHandler.class);
public static TypeName OTHER = TypeName.get(Void.class);
public static TypeName USER_TASK = TypeName.get(UserTaskHandler.class);
public static final TypeName CALL_ACTIVITY = TypeName.get(CallActivityHandler.class);
public static final TypeName EVENT = TypeName.get(EventHandler.class);
public static final TypeName EXTERNAL_TASK = TypeName.get(ExternalTaskHandler.class);
public static final TypeName JOB = TypeName.get(JobHandler.class);
public static final TypeName OTHER = TypeName.get(Void.class);
public static final TypeName USER_TASK = TypeName.get(UserTaskHandler.class);

protected final TestCaseActivity activity;
protected final String literal;
Expand Down Expand Up @@ -227,7 +227,7 @@ public void setMultiInstanceParent(boolean multiInstanceParent) {
}

/**
* If the activity has the {@code asyncAfter} flag set, it must be handled. If it is the last activity and it does not end the process, the asynchronous
* If the activity has the {@code asyncAfter} flag set, it must be handled. If it is the last activity and does not end the process, the asynchronous
* continuation after should not be handled - the execution must wait!
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ void execute(JobClient client, ActivatedJob job) {
}

/**
* Throws an BPMN error using the given error message as well as specified variables.
* Throws an BPMN error using the given error message as well as the specified variables.
* <p>
* This action can be used, if there is no related job worker implementation yet.
* This action can be used, if there is no related registered job worker yet.
*/
public void throwBpmnError(String errorMessage) {
if (element.getErrorCode() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package org.camunda.community.bpmndt.platform8.api;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.camunda.community.bpmndt.platform8.api.TestCaseInstanceElement.MessageEventElement;
import org.camunda.community.bpmndt.platform8.api.TestCaseInstanceMemo.MessageSubscriptionMemo;

import io.camunda.zeebe.client.ZeebeClient;
import io.camunda.zeebe.client.api.command.PublishMessageCommandStep1.PublishMessageCommandStep3;
import io.camunda.zeebe.client.api.response.ProcessInstanceEvent;
import io.camunda.zeebe.process.test.assertions.BpmnAssert;
import io.camunda.zeebe.process.test.assertions.ProcessInstanceAssert;

public class MessageEventHandler {

private final MessageEventElement element;

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

private Consumer<ProcessInstanceAssert> verifier;
private BiConsumer<ZeebeClient, String> action;
private Object variables;

private Consumer<String> correlationKeyExpressionConsumer;

private String expectedCorrelationKey;
private String expectedMessageName;

private Consumer<String> correlationKeyConsumer;
private Consumer<String> messageNameConsumer;

public MessageEventHandler(MessageEventElement element) {
this.element = element;

correlate();
}

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

if (correlationKeyExpressionConsumer != null) {
correlationKeyExpressionConsumer.accept(element.getCorrelationKey());
}

MessageSubscriptionMemo messageSubscription = instance.getMessageSubscription(processInstanceEvent, element.getId());

if (expectedCorrelationKey != null && !expectedCorrelationKey.equals(messageSubscription.correlationKey)) {
String message = "expected message event %s to have correlation key '%s', but was '%s'";
throw new AssertionError(String.format(message, element.getId(), expectedCorrelationKey, messageSubscription.correlationKey));
}
if (correlationKeyConsumer != null) {
correlationKeyConsumer.accept(messageSubscription.correlationKey);
}

if (expectedMessageName != null && !expectedMessageName.equals(messageSubscription.correlationKey)) {
String message = "expected message event %s to have message name '%s', but was '%s'";
throw new AssertionError(String.format(message, element.getId(), expectedMessageName, messageSubscription.messageName));
}
if (messageNameConsumer != null) {
messageNameConsumer.accept(messageSubscription.messageName);
}

action.accept(instance.client, messageSubscription.correlationKey);

instance.hasPassed(processInstanceEvent, element.getId());
}

public void correlate() {
action = this::correlate;
}

public void correlate(BiConsumer<ZeebeClient, String> action) {
if (action == null) {
throw new IllegalArgumentException("action is null");
}
this.action = action;
}

void correlate(ZeebeClient client, String correlationKey) {
PublishMessageCommandStep3 publishMessageCommandStep3 = client.newPublishMessageCommand()
.messageName(element.getMessageName())
.correlationKey(element.getCorrelationKey());

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

/**
* 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.handleMessageCatchEvent().customize(this::prepare);
* </pre>
*
* @param customizer A function that accepts a {@link MessageEventHandler}.
* @return The handler.
*/
public MessageEventHandler customize(Consumer<MessageEventHandler> customizer) {
if (customizer != null) {
customizer.accept(this);
}
return this;
}

/**
* Verifies the message event's waiting state.
*
* @param verifier Verifier that accepts an {@link ProcessInstanceAssert} instance.
* @return The handler.
*/
public MessageEventHandler verify(Consumer<ProcessInstanceAssert> verifier) {
this.verifier = verifier;
return this;
}

/**
* Sets a variable that is used to correlate the message.
*
* @param name The name of the variable.
* @param value The variable's value.
* @return The handler.
* @see #correlate()
*/
public MessageEventHandler withVariable(String name, Object value) {
if (variables != null) {
throw new IllegalStateException("either use an object (POJO) as variables or a variable map");
}
variableMap.put(name, value);
return this;
}

/**
* Sets an object as variables that is used to correlate the message.
*
* @param variables The variables as POJO.
* @return The handler.
* @see #correlate()
*/
public MessageEventHandler withVariables(Object variables) {
if (!variableMap.isEmpty()) {
throw new IllegalStateException("either use an object (POJO) as variables or a variable map");
}
this.variables = variables;
return this;
}

/**
* Sets variables that are used to correlate the message.
*
* @param variableMap A map of variables.
* @return The handler.
* @see #correlate()
*/
public MessageEventHandler withVariableMap(Map<String, Object> variableMap) {
if (variables != null) {
throw new IllegalStateException("either use an object (POJO) as variables or a variable map");
}
this.variableMap.putAll(variableMap);
return this;
}
}
Loading

0 comments on commit 8abea3d

Please sign in to comment.