Skip to content

Commit

Permalink
test(engine): Add Tests for Different Type Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
psavidis committed Jun 12, 2024
1 parent ffbcef2 commit 964c9d4
Showing 1 changed file with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import org.camunda.bpm.engine.variable.value.ObjectValue;
import org.junit.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class VariableInstanceTest extends PluggableProcessEngineTest {

@Test
@Deployment(resources={"org/camunda/bpm/engine/test/api/runtime/oneTaskProcess.bpmn20.xml"})
public void shouldUpdateVariableStateOnVariableTypeChange() {
@Deployment(resources = {"org/camunda/bpm/engine/test/api/runtime/oneTaskProcess.bpmn20.xml"})
public void shouldUpdateVariableStateOnVariableTypeChangeObjectToLong() {
// given

var processInstance = startProcessInstanceWithObjectVariable("oneTaskProcess",
Expand Down Expand Up @@ -43,7 +45,7 @@ public void shouldUpdateVariableStateOnVariableTypeChange() {
.singleResult();

// then the changed integer type should be reflected in the variable entity row appropriately
assertThat(variable.getByteArrayValue()).isNull(); // byte array fields should note exist for an integer field
assertThat(variable.getByteArrayValue()).isNull(); // byte array fields should not exist for an integer field
assertThat(variable.getByteArrayValueId()).isNull();

assertThat(variable.getDoubleValue()).isNull();
Expand All @@ -52,6 +54,93 @@ public void shouldUpdateVariableStateOnVariableTypeChange() {
assertThat(variable.getTextValue2()).isNull();
}

@Test
@Deployment(resources = {"org/camunda/bpm/engine/test/api/runtime/oneTaskProcess.bpmn20.xml"})
public void shouldUpdateVariableStateOnVariableTypeChangeDoubleToObject() {
// given
var processInstance = startProcessInstanceWithVariable("oneTaskProcess",
"variableA", 43.0);

VariableInstanceEntity variable = (VariableInstanceEntity) runtimeService.createVariableInstanceQuery()
.processInstanceIdIn(processInstance.getId())
.singleResult();

// Double variable state is populated
assertThat(variable.getDoubleValue()).isEqualTo(43.0);
assertThat(variable.getLongValue()).isNull();
assertThat(variable.getTextValue()).isNull();
assertThat(variable.getTextValue2()).isNull();
assertThat(variable.getValue()).isEqualTo(43.0);

// The other type fields will be null
assertThat(variable.getByteArrayValue()).isNull();
assertThat(variable.getByteArrayValueId()).isNull();

// when the type is updated from double to object
setVariableWithObjectValue(processInstance.getId(), "variableA", "43.0"); // object value

variable = (VariableInstanceEntity) runtimeService.createVariableInstanceQuery()
.processInstanceIdIn(processInstance.getId())
.singleResult();

// then the changed object type should be reflected in the variable entity row appropriately
assertThat(variable.getByteArrayValue()).isNotNull(); // byte array fields should note exist for an integer field
assertThat(variable.getByteArrayValueId()).isNotNull();

assertThat(variable.getDoubleValue()).isNull();
assertThat(variable.getLongValue()).isNull();
assertThat(variable.getTextValue()).isNull();
assertThat(variable.getTextValue2()).isEqualTo("java.lang.String");
}

@Test
@Deployment(resources = {"org/camunda/bpm/engine/test/api/runtime/oneTaskProcess.bpmn20.xml"})
public void shouldUpdateVariableStateOnVariableTypeChangeStringToObject() {
// given
var processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess",
"variableA", Map.of("variableA", "This is a string value")
);

VariableInstanceEntity variable = (VariableInstanceEntity) runtimeService.createVariableInstanceQuery()
.processInstanceIdIn(processInstance.getId())
.singleResult();

// Double variable state is populated
assertThat(variable.getDoubleValue()).isNull();
assertThat(variable.getLongValue()).isNull();
assertThat(variable.getTextValue()).isEqualTo("This is a string value");
assertThat(variable.getTextValue2()).isNull();
assertThat(variable.getValue()).isEqualTo("This is a string value");

// The other type fields will be null
assertThat(variable.getByteArrayValue()).isNull();
assertThat(variable.getByteArrayValueId()).isNull();

// when the type is updated from double to object
setVariableWithObjectValue(processInstance.getId(), "variableA", "43.0"); // object value

variable = (VariableInstanceEntity) runtimeService.createVariableInstanceQuery()
.processInstanceIdIn(processInstance.getId())
.singleResult();

// then the changed object type should be reflected in the variable entity row appropriately
assertThat(variable.getByteArrayValue()).isNotNull(); // byte array fields should note exist for an integer field
assertThat(variable.getByteArrayValueId()).isNotNull();

assertThat(variable.getDoubleValue()).isNull();
assertThat(variable.getLongValue()).isNull();
assertThat(variable.getTextValue()).isNull();
assertThat(variable.getTextValue2()).isEqualTo("java.lang.String");
}

private ProcessInstance startProcessInstanceWithVariable(String processDefinitionKey, String variableName, Object variableValue) {
VariableMap variables = Variables.createVariables()
.putValue(variableName, variableValue);

String businessKey = processDefinitionKey; // for simplicity’s sake, same businessKey with processDefinitionKey
return runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
}

private ProcessInstance startProcessInstanceWithObjectVariable(String processDefinitionKey, String variableName, Object variableValue) {
ObjectValue value = Variables.objectValue(variableValue)
.serializationDataFormat(Variables.SerializationDataFormats.JAVA)
Expand All @@ -63,4 +152,12 @@ private ProcessInstance startProcessInstanceWithObjectVariable(String processDef
String businessKey = processDefinitionKey; // for simplicity’s sake, same businessKey with processDefinitionKey
return runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
}

private void setVariableWithObjectValue(String executionId, String variableName, Object variableValue) {
ObjectValue value = Variables.objectValue(variableValue)
.serializationDataFormat(Variables.SerializationDataFormats.JAVA)
.create();

runtimeService.setVariable(executionId, variableName, value);
}
}

0 comments on commit 964c9d4

Please sign in to comment.