Skip to content

Commit

Permalink
feat(engine): set variables into new scope triggered by correlation (c…
Browse files Browse the repository at this point in the history
…amunda#3973)

* temporary pass the payload to the process instance execution
  * or set variables as local for intermediate event message
  * or set variables for start event message (not part of event subprocess) in AbstractCorrelateMessageCmd
* add setDelayedPayloadToNewScope to PvmExecutionImpl, PvmAtomicOperationCancelActivity, PvmAtomicOperationCreateConcurrentExecution to set the payload for the right scope
* REST API + OpenAPI
  * clean up obsolete comments in tests

camunda#2266
  • Loading branch information
yanavasileva authored Nov 27, 2023
1 parent 9f3e0d0 commit cb15a42
Show file tree
Hide file tree
Showing 20 changed files with 750 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,17 @@
type = "object"
additionalProperties = true
dto = "VariableValueDto"
desc = "A map of local variables that is injected into the triggered execution or process instance after the
message has been delivered. Each key is a variable name and each value a JSON variable value object
desc = "A map of local variables that is injected into the execution waiting on the message.
Each key is a variable name and each value a JSON variable value object
with the following properties."/>

<@lib.property
name = "processVariablesToTriggeredScope"
type = "object"
additionalProperties = true
dto = "VariableValueDto"
desc = "A map of variables that is injected into the new scope triggered by message correlation.
Each key is a variable name and each value a JSON variable value object
with the following properties."/>

<@lib.property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CorrelationMessageDto {
private Map<String, VariableValueDto> localCorrelationKeys;
private Map<String, VariableValueDto> processVariables;
private Map<String, VariableValueDto> processVariablesLocal;
private Map<String, VariableValueDto> processVariablesToTriggeredScope;
private String tenantId;
private boolean withoutTenantId;
private String processInstanceId;
Expand Down Expand Up @@ -84,6 +85,14 @@ public void setProcessVariablesLocal(Map<String, VariableValueDto> processVariab
this.processVariablesLocal = processVariablesLocal;
}

public Map<String, VariableValueDto> getProcessVariablesToTriggeredScope() {
return processVariablesToTriggeredScope;
}

public void setProcessVariablesToTriggeredScope(Map<String, VariableValueDto> processVariablesToTriggeredScope) {
this.processVariablesToTriggeredScope = processVariablesToTriggeredScope;
}

public boolean isAll() {
return all;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ protected MessageCorrelationBuilder createMessageCorrelationBuilder(CorrelationM
Map<String, Object> localCorrelationKeys = VariableValueDto.toMap(messageDto.getLocalCorrelationKeys(), processEngine, objectMapper);
Map<String, Object> processVariables = VariableValueDto.toMap(messageDto.getProcessVariables(), processEngine, objectMapper);
Map<String, Object> processVariablesLocal = VariableValueDto.toMap(messageDto.getProcessVariablesLocal(), processEngine, objectMapper);
Map<String, Object> processVariablesToTriggeredScope = VariableValueDto.toMap(messageDto.getProcessVariablesToTriggeredScope(), processEngine, objectMapper);

MessageCorrelationBuilder builder = runtimeService
.createMessageCorrelation(messageDto.getMessageName());
Expand All @@ -134,6 +135,9 @@ protected MessageCorrelationBuilder createMessageCorrelationBuilder(CorrelationM
if (processVariablesLocal != null) {
builder.setVariablesLocal(processVariablesLocal);
}
if (processVariablesToTriggeredScope != null) {
builder.setVariablesToTriggeredScope(processVariablesToTriggeredScope);
}
if (messageDto.getBusinessKey() != null) {
builder.processInstanceBusinessKey(messageDto.getBusinessKey());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public void setupMocks() {
when(messageCorrelationBuilderMock.processInstanceVariableEquals(anyString(), any())).thenReturn(messageCorrelationBuilderMock);
when(messageCorrelationBuilderMock.setVariables(Mockito.any())).thenReturn(messageCorrelationBuilderMock);
when(messageCorrelationBuilderMock.setVariable(anyString(), any())).thenReturn(messageCorrelationBuilderMock);
when(messageCorrelationBuilderMock.setVariablesLocal(Mockito.any())).thenReturn(messageCorrelationBuilderMock);
when(messageCorrelationBuilderMock.setVariableLocal(anyString(), any())).thenReturn(messageCorrelationBuilderMock);
when(messageCorrelationBuilderMock.setVariablesToTriggeredScope(Mockito.any())).thenReturn(messageCorrelationBuilderMock);
when(messageCorrelationBuilderMock.setVariableToTriggeredScope(anyString(), any())).thenReturn(messageCorrelationBuilderMock);

executionResult = MockProvider.createMessageCorrelationResult(MessageCorrelationResultType.Execution);
procInstanceResult = MockProvider.createMessageCorrelationResult(MessageCorrelationResultType.ProcessDefinition);
Expand All @@ -122,6 +126,7 @@ public void testFullMessageCorrelation() {
String businessKey = "aBusinessKey";
Map<String, Object> variables = VariablesBuilder.create().variable("aKey", "aValue").getVariables();
Map<String, Object> variablesLocal = VariablesBuilder.create().variable("aKeyLocal", "aValueLocal").getVariables();
Map<String, Object> variablesToTriggeredScope = VariablesBuilder.create().variable("aKeyToTriggeredScope", "aValueToTriggeredScope").getVariables();

Map<String, Object> correlationKeys = VariablesBuilder.create()
.variable("aKey", "aValue")
Expand All @@ -139,6 +144,7 @@ public void testFullMessageCorrelation() {
messageParameters.put("localCorrelationKeys", localCorrelationKeys);
messageParameters.put("processVariables", variables);
messageParameters.put("processVariablesLocal", variablesLocal);
messageParameters.put("processVariablesToTriggeredScope", variablesToTriggeredScope);
messageParameters.put("businessKey", businessKey);

given().contentType(POST_JSON_CONTENT_TYPE).body(messageParameters)
Expand All @@ -159,11 +165,14 @@ public void testFullMessageCorrelation() {
expectedVariables.put("aKey", "aValue");
Map<String, Object> expectedVariablesLocal = new HashMap<>();
expectedVariablesLocal.put("aKeyLocal", "aValueLocal");
Map<String, Object> expectedVariablesToTriggeredScope = new HashMap<>();
expectedVariablesToTriggeredScope.put("aKeyToTriggeredScope", "aValueToTriggeredScope");

verify(runtimeServiceMock).createMessageCorrelation(eq(messageName));
verify(messageCorrelationBuilderMock).processInstanceBusinessKey(eq(businessKey));
verify(messageCorrelationBuilderMock).setVariables(argThat(new EqualsMap(expectedVariables)));
verify(messageCorrelationBuilderMock).setVariablesLocal(argThat(new EqualsMap(expectedVariablesLocal)));
verify(messageCorrelationBuilderMock).setVariablesToTriggeredScope(argThat(new EqualsMap(expectedVariablesToTriggeredScope)));

for (Entry<String, Object> expectedKey : expectedCorrelationKeys.entrySet()) {
String name = expectedKey.getKey();
Expand All @@ -178,9 +187,6 @@ public void testFullMessageCorrelation() {
}

verify(messageCorrelationBuilderMock).correlateWithResult();

// verify(runtimeServiceMock).correlateMessage(eq(messageName), eq(businessKey),
// argThat(new EqualsMap(expectedCorrelationKeys)), argThat(new EqualsMap(expectedVariables)));
}

@Test
Expand Down Expand Up @@ -266,6 +272,7 @@ public void testFullMessageCorrelationAll() {
String businessKey = "aBusinessKey";
Map<String, Object> variables = VariablesBuilder.create().variable("aKey", "aValue").getVariables();
Map<String, Object> variablesLocal = VariablesBuilder.create().variable("aKeyLocal", "aValueLocal").getVariables();
Map<String, Object> variablesToTriggeredScope = VariablesBuilder.create().variable("aKeyToTriggeredScope", "aValueToTriggeredScope").getVariables();

Map<String, Object> correlationKeys = VariablesBuilder.create()
.variable("aKey", "aValue")
Expand All @@ -283,6 +290,7 @@ public void testFullMessageCorrelationAll() {
messageParameters.put("localCorrelationKeys", localCorrelationKeys);
messageParameters.put("processVariables", variables);
messageParameters.put("processVariablesLocal", variablesLocal);
messageParameters.put("processVariablesToTriggeredScope", variablesToTriggeredScope);
messageParameters.put("businessKey", businessKey);
messageParameters.put("all", true);

Expand All @@ -304,11 +312,14 @@ public void testFullMessageCorrelationAll() {
expectedVariables.put("aKey", "aValue");
Map<String, Object> expectedVariablesLocal = new HashMap<>();
expectedVariablesLocal.put("aKeyLocal", "aValueLocal");
Map<String, Object> expectedVariablesToTriggeredScope = new HashMap<>();
expectedVariablesToTriggeredScope.put("aKeyToTriggeredScope", "aValueToTriggeredScope");

verify(runtimeServiceMock).createMessageCorrelation(eq(messageName));
verify(messageCorrelationBuilderMock).processInstanceBusinessKey(eq(businessKey));
verify(messageCorrelationBuilderMock).setVariables(argThat(new EqualsMap(expectedVariables)));
verify(messageCorrelationBuilderMock).setVariablesLocal(argThat(new EqualsMap(expectedVariablesLocal)));
verify(messageCorrelationBuilderMock).setVariablesToTriggeredScope(argThat(new EqualsMap(expectedVariablesToTriggeredScope)));

for (Entry<String, Object> expectedKey : expectedCorrelationKeys.entrySet()) {
String name = expectedKey.getKey();
Expand All @@ -323,7 +334,6 @@ public void testFullMessageCorrelationAll() {
}

verify(messageCorrelationBuilderMock).correlateAllWithResult();

}

@Test
Expand Down Expand Up @@ -490,9 +500,6 @@ public void testMessageNameAndBusinessKeyCorrelation() {
.then().expect().statusCode(Status.NO_CONTENT.getStatusCode())
.when().post(MESSAGE_URL);

// verify(runtimeServiceMock).correlateMessage(eq(messageName), eq(businessKey),
// argThat(new EqualsMap(null)), argThat(new EqualsMap(null)));

verify(runtimeServiceMock).createMessageCorrelation(eq(messageName));
verify(messageCorrelationBuilderMock).processInstanceBusinessKey(eq(businessKey));
verify(messageCorrelationBuilderMock).correlateWithResult();
Expand Down Expand Up @@ -1078,6 +1085,72 @@ public void testFailingDueToUnparseableDateInProcessVariablesLocal() {
.when().post(MESSAGE_URL);
}

@Test
public void testFailingDueToUnparseableIntegerInProcessVariablesToTriggeredScope() {
String variableKey = "aVariableKey";
String variableValue = "1abc";
String variableType = "Integer";

Map<String, Object> variableToTriggeredScopeJson = VariablesBuilder.create().variable(variableKey, variableValue, variableType).getVariables();

String messageName = "aMessageName";

Map<String, Object> messageParameters = new HashMap<>();
messageParameters.put("messageName", messageName);
messageParameters.put("processVariablesToTriggeredScope", variableToTriggeredScopeJson);

given().contentType(POST_JSON_CONTENT_TYPE).body(messageParameters)
.then().expect().statusCode(Status.BAD_REQUEST.getStatusCode())
.body("type", equalTo(InvalidRequestException.class.getSimpleName()))
.body("message", equalTo("Cannot deliver message: "
+ ErrorMessageHelper.getExpectedFailingConversionMessage(variableValue, variableType, Integer.class)))
.when().post(MESSAGE_URL);
}


@Test
public void testFailingDueToNotSupportedTypeInProcessVariablesToTriggeredScope() {
String variableKey = "aVariableKey";
String variableValue = "1abc";
String variableType = "X";

Map<String, Object> variableToTriggeredScopeJson = VariablesBuilder.create().variable(variableKey, variableValue, variableType).getVariables();

String messageName = "aMessageName";

Map<String, Object> messageParameters = new HashMap<>();
messageParameters.put("messageName", messageName);
messageParameters.put("processVariablesToTriggeredScope", variableToTriggeredScopeJson);

given().contentType(POST_JSON_CONTENT_TYPE).body(messageParameters)
.then().expect().statusCode(Status.BAD_REQUEST.getStatusCode())
.body("type", equalTo(InvalidRequestException.class.getSimpleName()))
.body("message", equalTo("Cannot deliver message: Unsupported value type 'X'"))
.when().post(MESSAGE_URL);
}

@Test
public void testFailingDueToUnparseableDateInProcessVariablesToTriggeredScope() {
String variableKey = "aVariableKey";
String variableValue = "1abc";
String variableType = "Date";

Map<String, Object> variableToTriggeredScopeJson = VariablesBuilder.create().variable(variableKey, variableValue, variableType).getVariables();

String messageName = "aMessageName";

Map<String, Object> messageParameters = new HashMap<>();
messageParameters.put("messageName", messageName);
messageParameters.put("processVariablesToTriggeredScope", variableToTriggeredScopeJson);

given().contentType(POST_JSON_CONTENT_TYPE).body(messageParameters)
.then().expect().statusCode(Status.BAD_REQUEST.getStatusCode())
.body("type", equalTo(InvalidRequestException.class.getSimpleName()))
.body("message", equalTo("Cannot deliver message: "
+ ErrorMessageHelper.getExpectedFailingConversionMessage(variableValue, variableType, Date.class)))
.when().post(MESSAGE_URL);
}

@Test
public void testCorrelateThrowsAuthorizationException() {
String messageName = "aMessageName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class MessageCorrelationBuilderImpl implements MessageCorrelationBuilder
protected VariableMap correlationLocalVariables;
protected VariableMap payloadProcessInstanceVariables;
protected VariableMap payloadProcessInstanceVariablesLocal;
protected VariableMap payloadProcessInstanceVariablesToTriggeredScope;

protected String tenantId = null;
protected boolean isTenantIdSet = false;
Expand Down Expand Up @@ -158,6 +159,14 @@ public MessageCorrelationBuilder setVariableLocal(String variableName, Object va
return this;
}

@Override
public MessageCorrelationBuilder setVariableToTriggeredScope(String variableName, Object variableValue) {
ensureNotNull("variableName", variableName);
ensurePayloadProcessInstanceVariablesToTriggeredScopeInitialized();
payloadProcessInstanceVariablesToTriggeredScope.put(variableName, variableValue);
return this;
}

public MessageCorrelationBuilder setVariables(Map<String, Object> variables) {
if (variables != null) {
ensurePayloadProcessInstanceVariablesInitialized();
Expand All @@ -175,6 +184,15 @@ public MessageCorrelationBuilder setVariablesLocal(Map<String, Object> variables
return this;
}

@Override
public MessageCorrelationBuilder setVariablesToTriggeredScope(Map<String, Object> variables) {
if (variables != null) {
ensurePayloadProcessInstanceVariablesToTriggeredScopeInitialized();
payloadProcessInstanceVariablesToTriggeredScope.putAll(variables);
}
return this;
}

protected void ensurePayloadProcessInstanceVariablesInitialized() {
if (payloadProcessInstanceVariables == null) {
payloadProcessInstanceVariables = new VariableMapImpl();
Expand All @@ -187,6 +205,12 @@ protected void ensurePayloadProcessInstanceVariablesLocalInitialized() {
}
}

protected void ensurePayloadProcessInstanceVariablesToTriggeredScopeInitialized() {
if (payloadProcessInstanceVariablesToTriggeredScope == null) {
payloadProcessInstanceVariablesToTriggeredScope = new VariableMapImpl();
}
}

public MessageCorrelationBuilder tenantId(String tenantId) {
ensureNotNull(
"The tenant-id cannot be null. Use 'withoutTenantId()' if you want to correlate the message to a process definition or an execution which has no tenant-id.",
Expand Down Expand Up @@ -367,6 +391,10 @@ public VariableMap getPayloadProcessInstanceVariablesLocal() {
return payloadProcessInstanceVariablesLocal;
}

public VariableMap getPayloadProcessInstanceVariablesToTriggeredScope() {
return payloadProcessInstanceVariablesToTriggeredScope;
}

public boolean isExclusiveCorrelation() {
return isExclusiveCorrelation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void execute(ActivityExecution execution) throws Exception {

for (EventSubscriptionEntity signalEventSubscription : signalEventSubscriptions) {
if (isActiveEventSubscription(signalEventSubscription)) {
signalEventSubscription.eventReceived(variableMap, null, businessKey, signalDefinition.isAsync());
signalEventSubscription.eventReceived(variableMap, null, null, businessKey, signalDefinition.isAsync());
}
}
leave(execution);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ protected AbstractCorrelateMessageCmd(MessageCorrelationBuilderImpl builder, boo
protected void triggerExecution(CommandContext commandContext, CorrelationHandlerResult correlationResult) {
String executionId = correlationResult.getExecutionEntity().getId();

MessageEventReceivedCmd command = new MessageEventReceivedCmd(messageName, executionId, builder.getPayloadProcessInstanceVariables(), builder.getPayloadProcessInstanceVariablesLocal(), builder.isExclusiveCorrelation());
MessageEventReceivedCmd command = new MessageEventReceivedCmd(messageName,
executionId,
builder.getPayloadProcessInstanceVariables(),
builder.getPayloadProcessInstanceVariablesLocal(),
builder.getPayloadProcessInstanceVariablesToTriggeredScope(),
builder.isExclusiveCorrelation());
command.execute(commandContext);
}

Expand Down Expand Up @@ -132,6 +137,7 @@ protected VariableMap resolveStartVariables() {
VariableMap mergedVariables = Variables.createVariables();
mergedVariables.putAll(builder.getPayloadProcessInstanceVariables());
mergedVariables.putAll(builder.getPayloadProcessInstanceVariablesLocal());
mergedVariables.putAll(builder.getPayloadProcessInstanceVariablesToTriggeredScope());
return mergedVariables;
}

Expand Down
Loading

0 comments on commit cb15a42

Please sign in to comment.