Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ interface ExternalTaskProcessFixture : ProcessModelFixture {
* @return end-event identifier
*/
fun endElementId(): String

/**
* @return BPMN error code handled by the external task's boundary event
*/
fun bpmnErrorCode(): String

/**
* @return end-event identifier reached when the BPMN error is handled
*/
fun bpmnErrorEndElementId(): String
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,28 @@ abstract class ExternalTaskCompatibilitySuite :
processIsFinished()
}
}

/**
* Verifies that completing an external task with a BPMN error follows the model's error path.
*/
@Test
fun `adapter completes an external task with a BPMN error`() {
val fixture = externalTaskProcessFixture()
given {
processIsDeployed(fixture.classpathResource())
processIsStartedByDefinition(fixture.definitionKey(), restrictions = fixture.definitionRestrictions())
}

whenever {
externalTaskIsCompletedWithError(
fixture.externalTaskDescriptionKey(),
fixture.bpmnErrorCode(),
)
}

then {
processHasPassed(fixture.bpmnErrorEndElementId())
processIsFinished()
}
}
}
2 changes: 2 additions & 0 deletions docs/testing-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ Once a task is selected, use one of these action methods:
| `selectedUserTaskIsAssignedTo(assignee)` | Change the selected user task's assignee. |
| `externalTaskIsCompleted(payload)` | Complete the selected external task. |
| `externalTaskIsCompleted(taskDescriptionKey, payload, restrictions, predicate)` | Select and complete an external task in one step. |
| `externalTaskIsCompletedWithError(errorCode, payload, errorMessage)` | Complete the selected external task by throwing a BPMN error. |
| `externalTaskIsCompletedWithError(taskDescriptionKey, errorCode, payload, errorMessage, restrictions, predicate)` | Select and complete an external task by throwing a BPMN error in one step. |
| `externalTaskHasFailed(reason, retries)` | Fail the selected external task with the remaining retry count. |

### Payload methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import dev.bpmcrafters.processengineapi.process.StartProcessByDefinitionCmd
import dev.bpmcrafters.processengineapi.process.StartProcessByMessageCmd
import dev.bpmcrafters.processengineapi.task.ChangeAssignmentModifyTaskCmd.AssignTaskCmd
import dev.bpmcrafters.processengineapi.task.ChangePayloadModifyTaskCmd.UpdatePayloadTaskCmd
import dev.bpmcrafters.processengineapi.task.CompleteTaskByErrorCmd
import dev.bpmcrafters.processengineapi.task.CompleteTaskCmd
import dev.bpmcrafters.processengineapi.task.FailTaskCmd
import dev.bpmcrafters.processengineapi.task.TaskInformation
Expand Down Expand Up @@ -246,6 +247,50 @@ abstract class ActionStage<SUBTYPE : ActionStage<SUBTYPE>> : ProcessStage<SUBTYP
externalTaskIsCompleted(payload)
}

/**
* Completes the currently selected external task with a BPMN error.
* @param errorCode BPMN error code to throw.
* @param payload task payload supplied with the error.
* @param errorMessage optional error details.
* @return this concrete action stage for Java/JGiven chaining.
*/
@JvmOverloads
@Description("selected external task is completed with BPMN error \$errorCode")
open fun externalTaskIsCompletedWithError(
@Quoted errorCode: String,
@Hidden payload: Map<String, Any?> = emptyMap(),
@Quoted errorMessage: String? = null,
): SUBTYPE = step {
runtimeApis().serviceTaskCompletionApi().completeTaskByError(
CompleteTaskByErrorCmd(requireCurrentTask().taskId, errorCode, errorMessage, payload)
).get()
refreshProcessInformation()
}

/**
* Selects and completes one matching external task with a BPMN error.
* @param taskDescriptionKey external-task description key.
* @param errorCode BPMN error code to throw.
* @param payload task payload supplied with the error.
* @param errorMessage optional error details.
* @param restrictions task-selection restrictions.
* @param predicate optional post-restriction task filter.
* @return this concrete action stage for Java/JGiven chaining.
*/
@JvmOverloads
@Description("external task \$taskDescriptionKey is completed with BPMN error \$errorCode")
open fun externalTaskIsCompletedWithError(
@Quoted taskDescriptionKey: String,
@Quoted errorCode: String,
@Hidden payload: Map<String, Any?> = emptyMap(),
@Quoted errorMessage: String? = null,
@Hidden restrictions: Map<String, String> = builder().build(),
@Hidden predicate: Predicate<TaskInformation>? = null,
): SUBTYPE = step {
externalTaskIsSelected(taskDescriptionKey, restrictions, predicate)
externalTaskIsCompletedWithError(errorCode, payload, errorMessage)
}

/**
* Fails the selected external task.
* @param reason engine-visible failure reason.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import dev.bpmcrafters.processengineapi.deploy.DeployBundleCommand;
import dev.bpmcrafters.processengineapi.deploy.DeploymentApi;
import dev.bpmcrafters.processengineapi.deploy.DeploymentInformation;
import dev.bpmcrafters.processengineapi.task.CompleteTaskByErrorCmd;
import dev.bpmcrafters.processengineapi.task.ServiceTaskCompletionApi;
import dev.bpmcrafters.processengineapi.task.TaskInformation;
import dev.bpmcrafters.processengineapi.testing.api.ProcessTestAssertApi;
import dev.bpmcrafters.processengineapi.testing.api.ProcessTestContext;
import dev.bpmcrafters.processengineapi.testing.api.ProcessTestQueryApi;
import dev.bpmcrafters.processengineapi.testing.api.ProcessTestRuntimeApis;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -25,6 +28,8 @@ class ActionStageJavaApiTest {
private DeploymentApi deploymentApi;
private DeploymentInformation deployment;
private ProcessTestAssertApi assertApi;
private ProcessTestQueryApi queryApi;
private ServiceTaskCompletionApi serviceTaskCompletionApi;
private TaskInformation task;

@BeforeEach
Expand All @@ -34,14 +39,21 @@ void setUp() {
deploymentApi = mock(DeploymentApi.class);
deployment = mock(DeploymentInformation.class);
assertApi = mock(ProcessTestAssertApi.class);
task = mock(TaskInformation.class);
queryApi = mock(ProcessTestQueryApi.class);
serviceTaskCompletionApi = mock(ServiceTaskCompletionApi.class);
task = new TaskInformation("external-task-id", Map.of());

when(context.runtime()).thenReturn(runtimeApis);
when(runtimeApis.deploymentApi()).thenReturn(deploymentApi);
when(deploymentApi.deploy(any(DeployBundleCommand.class)))
.thenReturn(CompletableFuture.completedFuture(deployment));
when(context.assertions()).thenReturn(assertApi);
when(context.query()).thenReturn(queryApi);
when(runtimeApis.serviceTaskCompletionApi()).thenReturn(serviceTaskCompletionApi);
when(assertApi.userTaskIsSelected("approve", Map.of(), null)).thenReturn(task);
when(assertApi.externalTaskIsSelected("validate", Map.of(), null)).thenReturn(task);
when(serviceTaskCompletionApi.completeTaskByError(any(CompleteTaskByErrorCmd.class)))
.thenReturn(CompletableFuture.completedFuture(null));
}

@Test
Expand Down Expand Up @@ -71,7 +83,52 @@ void selectionStepDelegatesToTheAssertApiAndReturnsTheConcreteJavaStage() {
verifyNoMoreInteractions(context, assertApi);
}

@Test
void bpmnErrorCompletionDelegatesToTheRuntimeApiAndRefreshesTheCurrentProcess() {
JavaActionStage stage = new JavaActionStage();
stage.processTestContext(context);
stage.selectExternalTask("instance-id", task);

JavaActionStage returnedStage = stage.externalTaskIsCompletedWithError(
"BUSINESS_ERROR", Map.of("reason", "invalid"), "Business validation failed"
);

assertSame(stage, returnedStage);
verify(serviceTaskCompletionApi).completeTaskByError(argThat(command ->
command.getTaskId().equals("external-task-id")
&& command.getErrorCode().equals("BUSINESS_ERROR")
&& command.getErrorMessage().equals("Business validation failed")
&& command.get().equals(Map.of("reason", "invalid"))
));
verify(queryApi).getProcessInformation("instance-id");
}

@Test
void bpmnErrorCompletionCanSelectTheExternalTaskBeforeCompletingIt() {
JavaActionStage stage = new JavaActionStage();
stage.processTestContext(context);
stage.selectCurrentProcess("instance-id");

stage.externalTaskIsCompletedWithError("validate", "BUSINESS_ERROR");

verify(assertApi).externalTaskIsSelected("validate", Map.of(), null);
verify(serviceTaskCompletionApi).completeTaskByError(argThat(command ->
command.getTaskId().equals("external-task-id")
&& command.getErrorCode().equals("BUSINESS_ERROR")
&& command.getErrorMessage() == null
&& command.get().isEmpty()
));
}

private static final class JavaActionStage extends ActionStage<JavaActionStage> {
void selectExternalTask(String instanceId, TaskInformation task) {
selectCurrentProcess(instanceId);
this.taskInformation = task;
}

void selectCurrentProcess(String instanceId) {
this.instanceId = instanceId;
}
}

private static final class JavaAssertStage extends AssertStage<JavaAssertStage> {
Expand Down