Skip to content

Commit

Permalink
merge: #12072
Browse files Browse the repository at this point in the history
12072: [Backport stable/8.0] Add QA tests that verify CreateProcessInstanceWithResult responses r=korthout a=backport-action

# Description
Backport of #11993 to `stable/8.0`.

relates to #11848

Co-authored-by: Nico Korthout <nico.korthout@camunda.com>
  • Loading branch information
zeebe-bors-camunda[bot] and korthout authored Mar 20, 2023
2 parents c4faa42 + 42c1141 commit 21a7eaf
Showing 1 changed file with 231 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
import io.camunda.zeebe.client.api.ZeebeFuture;
import io.camunda.zeebe.client.api.command.ClientException;
import io.camunda.zeebe.client.api.response.ActivateJobsResponse;
import io.camunda.zeebe.client.api.response.ActivatedJob;
import io.camunda.zeebe.client.api.response.ProcessInstanceResult;
import io.camunda.zeebe.it.util.GrpcClientRule;
import io.camunda.zeebe.model.bpmn.Bpmn;
import io.camunda.zeebe.model.bpmn.BpmnModelInstance;
import io.camunda.zeebe.protocol.record.intent.IncidentIntent;
import io.camunda.zeebe.protocol.record.intent.JobIntent;
import io.camunda.zeebe.test.util.BrokerClassRuleHelper;
import io.camunda.zeebe.test.util.collection.Maps;
import io.camunda.zeebe.test.util.record.RecordingExporter;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.ClassRule;
Expand Down Expand Up @@ -166,6 +169,234 @@ public void shouldCreateProcessInstanceAwaitResultsWithFetchVariables() {
assertThat(result.getVariablesAsMap()).containsExactly(entry("y", "bar"));
}

@Test
public void shouldRespondResultWhenCompletedByPublishedMessage() {
// given
final var client = CLIENT_RULE.getClient();
client
.newDeployResourceCommand()
.addProcessModel(
Bpmn.createExecutableProcess(processId)
.startEvent()
.intermediateCatchEvent()
.message(message -> message.name("a").zeebeCorrelationKeyExpression("key"))
.endEvent()
.done(),
"process.bpmn")
.send()
.join();

final ZeebeFuture<ProcessInstanceResult> processInstanceResult =
client
.newCreateInstanceCommand()
.bpmnProcessId(processId)
.latestVersion()
.variables(Map.of("key", "key-1"))
.withResult()
.send();

// when
client
.newPublishMessageCommand()
.messageName("a")
.correlationKey("key-1")
.variables(Map.of("message", "correlated"))
.send()
.join();

// then
assertThat(processInstanceResult.join().getVariablesAsMap())
.containsEntry("message", "correlated");
}

@Test
public void shouldRespondResultWhenCompletedByCompletedJob() {
// given
final var client = CLIENT_RULE.getClient();
client
.newDeployResourceCommand()
.addProcessModel(
Bpmn.createExecutableProcess(processId)
.startEvent()
.serviceTask("task", t -> t.zeebeJobType("task"))
.endEvent()
.done(),
"process.bpmn")
.send()
.join();

final ZeebeFuture<ProcessInstanceResult> processInstanceResult =
client
.newCreateInstanceCommand()
.bpmnProcessId(processId)
.latestVersion()
.withResult()
.send();

final List<ActivatedJob> jobs =
client
.newActivateJobsCommand()
.jobType("task")
.maxJobsToActivate(1)
.send()
.join()
.getJobs();
assertThat(jobs).hasSize(1);

// when
jobs.forEach(
job -> client.newCompleteCommand(job).variables(Map.of("job", "completed")).send().join());

// then
assertThat(processInstanceResult.join().getVariablesAsMap()).containsEntry("job", "completed");
}

@Test
public void shouldRespondResultWhenCompletedByThrownError() {
// given
final var client = CLIENT_RULE.getClient();
client
.newDeployResourceCommand()
.addProcessModel(
Bpmn.createExecutableProcess(processId)
.startEvent()
.serviceTask("task", t -> t.zeebeJobType("task"))
.boundaryEvent("error", e -> e.errorEventDefinition().error("error"))
.zeebeOutputExpression("error", "error") // error variables are not propagated
.endEvent()
.moveToActivity("task")
.endEvent()
.done(),
"process.bpmn")
.send()
.join();

final ZeebeFuture<ProcessInstanceResult> processInstanceResult =
client
.newCreateInstanceCommand()
.bpmnProcessId(processId)
.latestVersion()
.withResult()
.send();

final List<ActivatedJob> jobs =
client
.newActivateJobsCommand()
.jobType("task")
.maxJobsToActivate(1)
.send()
.join()
.getJobs();
assertThat(jobs).hasSize(1);

// when
jobs.forEach(
job ->
client
.newThrowErrorCommand(job)
.errorCode("error")
.errorMessage("throwing an error")
.variables(Map.of("error", "thrown"))
.send()
.join());

// then
assertThat(processInstanceResult.join().getVariablesAsMap()).containsEntry("error", "thrown");
}

@Test
public void shouldRespondResultWhenCompletedByResolvedIncident() {
// given
final var client = CLIENT_RULE.getClient();
client
.newDeployResourceCommand()
.addProcessModel(
Bpmn.createExecutableProcess(processId)
.startEvent("start_event_with_output_mapping")
.zeebeOutputExpression("missing_variable", "output_variable")
.endEvent()
.done(),
"process.bpmn")
.send()
.join();

final ZeebeFuture<ProcessInstanceResult> processInstanceResult =
client
.newCreateInstanceCommand()
.bpmnProcessId(processId)
.latestVersion()
.withResult()
.send();

final var incident =
RecordingExporter.incidentRecords(IncidentIntent.CREATED)
.withElementId("start_event_with_output_mapping")
.getFirst();

// when
client
.newSetVariablesCommand(incident.getValue().getElementInstanceKey())
.variables(Map.of("missing_variable", "incident resolved"))
.send()
.join();
client.newResolveIncidentCommand(incident.getKey()).send().join();

// then
assertThat(processInstanceResult.join().getVariablesAsMap())
.containsEntry("missing_variable", "incident resolved");
}

@Test
public void shouldRespondResultWhenCompletedByModifiedProcessInstance() {
// given
final var client = CLIENT_RULE.getClient();
client
.newDeployResourceCommand()
.addProcessModel(
Bpmn.createExecutableProcess(processId)
.startEvent()
.serviceTask("task", t -> t.zeebeJobType("task"))
.endEvent("end_event")
.done(),
"process.bpmn")
.send()
.join();

final ZeebeFuture<ProcessInstanceResult> processInstanceResult =
client
.newCreateInstanceCommand()
.bpmnProcessId(processId)
.latestVersion()
.withResult()
.send();

final List<ActivatedJob> jobs =
client
.newActivateJobsCommand()
.jobType("task")
.maxJobsToActivate(1)
.send()
.join()
.getJobs();
assertThat(jobs).hasSize(1);

// when
jobs.forEach(
job ->
client
.newModifyProcessInstanceCommand(job.getProcessInstanceKey())
.terminateElement(job.getElementInstanceKey())
.and()
.activateElement("end_event")
.withVariables(Map.of("process instance", "modified"))
.send()
.join());

// then
assertThat(processInstanceResult.join().getVariablesAsMap())
.containsEntry("process instance", "modified");
}

private ZeebeFuture<ProcessInstanceResult> createProcessInstanceWithVariables(
final Map<String, Object> variables) {
return CLIENT_RULE
Expand Down

0 comments on commit 21a7eaf

Please sign in to comment.