Skip to content

Commit

Permalink
feat(clients/java): add service method to set variables
Browse files Browse the repository at this point in the history
related to CAM-13774, closes camunda#1603
  • Loading branch information
tahaarian authored Sep 10, 2021
1 parent d9112ec commit f41329c
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public void shouldInvokeHandler() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand Down Expand Up @@ -126,8 +126,8 @@ public void shouldCompleteTask() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -138,6 +138,68 @@ public void shouldCompleteTask() {
assertThat(task.getTaskDefinitionKey()).isEqualTo(USER_TASK_ID);
}

@Test
public void shouldSetVariablesByProcessInstanceId() {
// given
String variableName = "progress";
Integer variableValue = 10;

RecordingExternalTaskHandler handler = new RecordingExternalTaskHandler((task, client) -> {
Map<String, Object> variables = new HashMap<>();
variables.put(variableName, variableValue);

client.setVariables(task.getProcessInstanceId(), variables);
client.complete(task, variables);
});

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());

VariableInstanceDto variable = engineRule.getVariableByProcessInstanceId(processInstance.getId());
assertThat(variable).isNotNull();
assertThat(variable.getProcessInstanceId()).isEqualTo(processInstance.getId());

assertThat(variable.getName()).isEqualTo(variableName);
assertThat(variable.getValue()).isEqualTo(variableValue);

}

@Test
public void shouldSetVariablesByExternalTask() {
// given
String variableName = "progress";
Integer variableValue = 10;

RecordingExternalTaskHandler handler = new RecordingExternalTaskHandler((task, client) -> {
Map<String, Object> variables = new HashMap<>();
variables.put(variableName, variableValue);

client.setVariables(task, variables);
client.complete(task, variables);
});

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());

VariableInstanceDto variable = engineRule.getVariableByProcessInstanceId(processInstance.getId());
assertThat(variable).isNotNull();
assertThat(variable.getProcessInstanceId()).isEqualTo(processInstance.getId());

assertThat(variable.getName()).isEqualTo(variableName);
assertThat(variable.getValue()).isEqualTo(variableValue);

}

@Test
public void shouldCompleteWithVariables() {
// given
Expand All @@ -152,8 +214,8 @@ public void shouldCompleteWithVariables() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand Down Expand Up @@ -183,8 +245,8 @@ public void shouldCompleteWithLocalVariables() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand Down Expand Up @@ -220,8 +282,8 @@ public void shouldCompleteWithVariablesAndLocalVariables() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand Down Expand Up @@ -257,8 +319,8 @@ public void shouldCompleteWithTransientVariables() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -281,8 +343,8 @@ public void shouldCompleteById() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -303,8 +365,8 @@ public void shouldLock() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -323,8 +385,8 @@ public void shouldLockById() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -342,8 +404,8 @@ public void shouldExtendLock() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -363,8 +425,8 @@ public void shouldExtendLockById() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -387,8 +449,8 @@ public void shouldUnlockTask() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> handler.getHandledTasks().size() >= 2);
Expand All @@ -407,8 +469,8 @@ public void shouldInvokeHandleBpmnError() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -432,8 +494,8 @@ public void shouldInvokeHandleBpmnErrorWithVariables() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -460,8 +522,8 @@ public void shouldInvokeHandleBpmnErrorWithErrorMessage() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -485,8 +547,8 @@ public void shouldInvokeHandleBpmnErrorById() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -506,8 +568,8 @@ public void shouldInvokeHandleFailure() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -527,9 +589,9 @@ public void shouldInvokeHandleFailureWithRetries() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.lockDuration(1000)
.handler(handler)
.open();
.lockDuration(1000)
.handler(handler)
.open();


clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -554,8 +616,8 @@ public void shouldInvokeHandleFailureById() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand Down Expand Up @@ -583,8 +645,8 @@ public void shouldFailWithVariables() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand Down Expand Up @@ -618,8 +680,8 @@ public void shouldFailWithLocalVariables() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand Down Expand Up @@ -659,8 +721,8 @@ public void shouldFailWithVariablesAndLocalVariables() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand Down Expand Up @@ -690,8 +752,8 @@ public void shouldCheckExecutionId() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
List<ExternalTask> handledTasks = handler.getHandledTasks();
Expand All @@ -711,8 +773,8 @@ public void shouldCheckTenantId() {

// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand All @@ -728,8 +790,8 @@ public void shouldCheckTaskPriority() {
RecordingExternalTaskHandler handler = new RecordingExternalTaskHandler();
// when
client.subscribe(EXTERNAL_TASK_TOPIC_FOO)
.handler(handler)
.open();
.handler(handler)
.open();

// then
clientRule.waitForFetchAndLockUntil(() -> !handler.getHandledTasks().isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.camunda.bpm.client.task.impl.dto.ExtendLockRequestDto;
import org.camunda.bpm.client.task.impl.dto.FailureRequestDto;
import org.camunda.bpm.client.task.impl.dto.LockRequestDto;
import org.camunda.bpm.client.task.impl.dto.SetVariablesRequestDto;
import org.camunda.bpm.client.topic.impl.dto.FetchAndLockRequestDto;
import org.camunda.bpm.client.topic.impl.dto.TopicRequestDto;
import org.camunda.bpm.client.variable.impl.TypedValueField;
Expand All @@ -38,11 +39,13 @@
public class EngineClient {

protected static final String EXTERNAL_TASK_RESOURCE_PATH = "/external-task";
protected static final String EXTERNAL_TASK__PROCESS_RESOURCE_PATH = "/process-instance";
protected static final String FETCH_AND_LOCK_RESOURCE_PATH = EXTERNAL_TASK_RESOURCE_PATH + "/fetchAndLock";
public static final String ID_PATH_PARAM = "{id}";
protected static final String ID_RESOURCE_PATH = EXTERNAL_TASK_RESOURCE_PATH + "/" + ID_PATH_PARAM;
public static final String LOCK_RESOURCE_PATH = ID_RESOURCE_PATH + "/lock";
public static final String EXTEND_LOCK_RESOURCE_PATH = ID_RESOURCE_PATH + "/extendLock";
public static final String SET_VARIABLES_RESOURCE_PATH = EXTERNAL_TASK__PROCESS_RESOURCE_PATH + "/" + ID_PATH_PARAM + "/variables";
public static final String UNLOCK_RESOURCE_PATH = ID_RESOURCE_PATH + "/unlock";
public static final String COMPLETE_RESOURCE_PATH = ID_RESOURCE_PATH + "/complete";
public static final String FAILURE_RESOURCE_PATH = ID_RESOURCE_PATH + "/failure";
Expand Down Expand Up @@ -104,6 +107,15 @@ public void complete(String taskId, Map<String, Object> variables, Map<String, O
engineInteraction.postRequest(resourceUrl, payload, Void.class);
}

public void setVariables(String proccessId,Map<String, Object> variables) throws EngineClientException {
Map<String, TypedValueField> typedValueDtoMap = typedValues.serializeVariables(variables);
SetVariablesRequestDto payload = new SetVariablesRequestDto(workerId, typedValueDtoMap);
String resourcePath = SET_VARIABLES_RESOURCE_PATH.replace("{id}", proccessId);
String resourceUrl = baseUrl + resourcePath;
engineInteraction.postRequest(resourceUrl, payload, Void.class);
}


public void failure(String taskId, String errorMessage, String errorDetails, int retries, long retryTimeout, Map<String, Object> variables, Map<String, Object> localVariables) throws EngineClientException {
Map<String, TypedValueField> typedValueDtoMap = typedValues.serializeVariables(variables);
Map<String, TypedValueField> localTypedValueDtoMap = typedValues.serializeVariables(localVariables);
Expand Down Expand Up @@ -131,8 +143,8 @@ public void extendLock(String taskId, long newDuration) throws EngineClientExcep

public byte[] getLocalBinaryVariable(String variableName, String processInstanceId) throws EngineClientException {
String resourcePath = baseUrl + GET_LOCAL_BINARY_VARIABLE
.replace(ID_PATH_PARAM, processInstanceId)
.replace(NAME_PATH_PARAM, variableName);
.replace(ID_PATH_PARAM, processInstanceId)
.replace(NAME_PATH_PARAM, variableName);

return engineInteraction.getRequest(resourcePath);
}
Expand Down
Loading

0 comments on commit f41329c

Please sign in to comment.