Search in sources :

Example 86 with ObjectValue

use of org.camunda.bpm.engine.variable.value.ObjectValue in project camunda-bpm-platform by camunda.

the class PaDataFormatConfiguratorTest method testPaLocalFormatDoesNotApply.

/**
 * Tests that the PA-local format does not apply if the value is set outside of the context
 * of the process application
 */
@Test
public void testPaLocalFormatDoesNotApply() throws JsonProcessingException, IOException {
    // given a process instance
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("testProcess");
    // when setting a variable without a process-application cotnext
    // 10th of January 1970
    Date date = new Date(JsonSerializable.ONE_DAY_IN_MILLIS * 10);
    JsonSerializable jsonSerializable = new JsonSerializable(date);
    runtimeService.setVariable(pi.getId(), "jsonSerializable", Variables.objectValue(jsonSerializable).serializationDataFormat(SerializationDataFormats.JSON).create());
    // then the global data format is applied
    ObjectValue objectValue = runtimeService.getVariableTyped(pi.getId(), "jsonSerializable", false);
    String serializedValue = objectValue.getValueSerialized();
    String expectedSerializedValue = jsonSerializable.toExpectedJsonString();
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode actualJsonTree = objectMapper.readTree(serializedValue);
    JsonNode expectedJsonTree = objectMapper.readTree(expectedSerializedValue);
    // JsonNode#equals makes a deep comparison
    Assert.assertEquals(expectedJsonTree, actualJsonTree);
}
Also used : ObjectValue(org.camunda.bpm.engine.variable.value.ObjectValue) JsonSerializable(org.camunda.bpm.integrationtest.functional.spin.dataformat.JsonSerializable) ProcessInstance(org.camunda.bpm.engine.runtime.ProcessInstance) JsonNode(com.fasterxml.jackson.databind.JsonNode) Date(java.util.Date) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AbstractFoxPlatformIntegrationTest(org.camunda.bpm.integrationtest.util.AbstractFoxPlatformIntegrationTest) Test(org.junit.Test)

Example 87 with ObjectValue

use of org.camunda.bpm.engine.variable.value.ObjectValue in project camunda-bpm-platform by camunda.

the class PaDataFormatConfiguratorTest method testPaLocalFormatApplies.

/**
 * Tests that the PA-local data format applies when a variable is set in
 * the context of it
 */
@Test
public void testPaLocalFormatApplies() throws JsonProcessingException, IOException {
    // given a process instance
    final ProcessInstance pi = runtimeService.startProcessInstanceByKey("testProcess");
    // when setting a variable in the context of a process application
    // 10th of January 1970
    Date date = new Date(JsonSerializable.ONE_DAY_IN_MILLIS * 10);
    JsonSerializable jsonSerializable = new JsonSerializable(date);
    try {
        ProcessApplicationContext.setCurrentProcessApplication(ReferenceStoringProcessApplication.INSTANCE);
        runtimeService.setVariable(pi.getId(), "jsonSerializable", Variables.objectValue(jsonSerializable).serializationDataFormat(SerializationDataFormats.JSON).create());
    } finally {
        ProcessApplicationContext.clear();
    }
    // then the process-application-local data format has been used to serialize the value
    ObjectValue objectValue = runtimeService.getVariableTyped(pi.getId(), "jsonSerializable", false);
    String serializedValue = objectValue.getValueSerialized();
    String expectedSerializedValue = jsonSerializable.toExpectedJsonString(JsonDataFormatConfigurator.DATE_FORMAT);
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode actualJsonTree = objectMapper.readTree(serializedValue);
    JsonNode expectedJsonTree = objectMapper.readTree(expectedSerializedValue);
    // JsonNode#equals makes a deep comparison
    Assert.assertEquals(expectedJsonTree, actualJsonTree);
}
Also used : ObjectValue(org.camunda.bpm.engine.variable.value.ObjectValue) JsonSerializable(org.camunda.bpm.integrationtest.functional.spin.dataformat.JsonSerializable) ProcessInstance(org.camunda.bpm.engine.runtime.ProcessInstance) JsonNode(com.fasterxml.jackson.databind.JsonNode) Date(java.util.Date) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AbstractFoxPlatformIntegrationTest(org.camunda.bpm.integrationtest.util.AbstractFoxPlatformIntegrationTest) Test(org.junit.Test)

Example 88 with ObjectValue

use of org.camunda.bpm.engine.variable.value.ObjectValue in project camunda-bpm-platform by camunda.

the class PaDataFormatConfiguratorTest method testTaskVariableImplicitObjectValueUpdate.

@Test
public void testTaskVariableImplicitObjectValueUpdate() throws JsonProcessingException, IOException {
    // given a process instance
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("implicitTaskVariableUpdate");
    Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
    // when setting a variable such that the process-application-local dataformat applies
    // 10th of January 1970
    Date date = new Date(JsonSerializable.ONE_DAY_IN_MILLIS * 10);
    JsonSerializable jsonSerializable = new JsonSerializable(date);
    try {
        ProcessApplicationContext.setCurrentProcessApplication(ReferenceStoringProcessApplication.INSTANCE);
        taskService.setVariableLocal(task.getId(), ImplicitObjectValueUpdateHandler.VARIABLE_NAME, Variables.objectValue(jsonSerializable).serializationDataFormat(SerializationDataFormats.JSON).create());
    } finally {
        ProcessApplicationContext.clear();
    }
    // and triggering an implicit update of the object value variable
    taskService.setAssignee(task.getId(), "foo");
    // then the process-application-local format was used for making the update
    ObjectValue objectValue = taskService.getVariableTyped(task.getId(), ImplicitObjectValueUpdateHandler.VARIABLE_NAME, false);
    ImplicitObjectValueUpdateHandler.addADay(jsonSerializable);
    String serializedValue = objectValue.getValueSerialized();
    String expectedSerializedValue = jsonSerializable.toExpectedJsonString(JsonDataFormatConfigurator.DATE_FORMAT);
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode actualJsonTree = objectMapper.readTree(serializedValue);
    JsonNode expectedJsonTree = objectMapper.readTree(expectedSerializedValue);
    // JsonNode#equals makes a deep comparison
    Assert.assertEquals(expectedJsonTree, actualJsonTree);
    // and it is also correct in the history
    HistoricVariableInstance historicObjectValue = historyService.createHistoricVariableInstanceQuery().processInstanceId(pi.getId()).variableName(ImplicitObjectValueUpdateHandler.VARIABLE_NAME).disableCustomObjectDeserialization().singleResult();
    serializedValue = ((ObjectValue) historicObjectValue.getTypedValue()).getValueSerialized();
    actualJsonTree = objectMapper.readTree(serializedValue);
    Assert.assertEquals(expectedJsonTree, actualJsonTree);
}
Also used : Task(org.camunda.bpm.engine.task.Task) ObjectValue(org.camunda.bpm.engine.variable.value.ObjectValue) JsonSerializable(org.camunda.bpm.integrationtest.functional.spin.dataformat.JsonSerializable) ProcessInstance(org.camunda.bpm.engine.runtime.ProcessInstance) JsonNode(com.fasterxml.jackson.databind.JsonNode) HistoricVariableInstance(org.camunda.bpm.engine.history.HistoricVariableInstance) Date(java.util.Date) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AbstractFoxPlatformIntegrationTest(org.camunda.bpm.integrationtest.util.AbstractFoxPlatformIntegrationTest) Test(org.junit.Test)

Example 89 with ObjectValue

use of org.camunda.bpm.engine.variable.value.ObjectValue in project camunda-bpm-platform by camunda.

the class ProcessInstantiationWithVariablesInReturnTest method testVariablesWithoutDeserialization.

private void testVariablesWithoutDeserialization(String processDefinitionKey) throws Exception {
    // given serializable variable
    JavaSerializable javaSerializable = new JavaSerializable("foo");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new ObjectOutputStream(baos).writeObject(javaSerializable);
    String serializedObject = StringUtil.fromBytes(Base64.encodeBase64(baos.toByteArray()), engineRule.getProcessEngine());
    // when execute process with serialized variable and wait state
    ProcessInstanceWithVariables procInstance = engineRule.getRuntimeService().createProcessInstanceByKey(processDefinitionKey).setVariable("serializedVar", serializedObjectValue(serializedObject).serializationDataFormat(Variables.SerializationDataFormats.JAVA).objectTypeName(JavaSerializable.class.getName()).create()).executeWithVariablesInReturn(false, false);
    // then returned instance contains serialized variable
    VariableMap map = procInstance.getVariables();
    assertNotNull(map);
    ObjectValue serializedVar = (ObjectValue) map.getValueTyped("serializedVar");
    assertFalse(serializedVar.isDeserialized());
    assertObjectValueSerializedJava(serializedVar, javaSerializable);
    // access on value should fail because variable is not deserialized
    try {
        serializedVar.getValue();
        Assert.fail("Deserialization should fail!");
    } catch (IllegalStateException ise) {
        assertTrue(ise.getMessage().equals("Object is not deserialized."));
    }
}
Also used : ObjectValue(org.camunda.bpm.engine.variable.value.ObjectValue) Variables.serializedObjectValue(org.camunda.bpm.engine.variable.Variables.serializedObjectValue) VariableMap(org.camunda.bpm.engine.variable.VariableMap) JavaSerializable(org.camunda.bpm.engine.test.api.variables.JavaSerializable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ProcessInstanceWithVariables(org.camunda.bpm.engine.runtime.ProcessInstanceWithVariables)

Example 90 with ObjectValue

use of org.camunda.bpm.engine.variable.value.ObjectValue in project camunda-bpm-platform by camunda.

the class MessageCorrelationTest method testExecutionCorrelationSetSerializedVariableValues.

@Deployment(resources = "org/camunda/bpm/engine/test/api/runtime/MessageCorrelationTest.testCatchingMessageEventCorrelation.bpmn20.xml")
@Test
public void testExecutionCorrelationSetSerializedVariableValues() throws IOException, ClassNotFoundException {
    // given
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("process");
    // when
    FailingJavaSerializable javaSerializable = new FailingJavaSerializable("foo");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new ObjectOutputStream(baos).writeObject(javaSerializable);
    String serializedObject = StringUtil.fromBytes(Base64.encodeBase64(baos.toByteArray()), engineRule.getProcessEngine());
    // then it is not possible to deserialize the object
    try {
        new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
    } catch (RuntimeException e) {
        testRule.assertTextPresent("Exception while deserializing object.", e.getMessage());
    }
    // but it can be set as a variable:
    runtimeService.createMessageCorrelation("newInvoiceMessage").setVariables(Variables.createVariables().putValueTyped("var", Variables.serializedObjectValue(serializedObject).objectTypeName(FailingJavaSerializable.class.getName()).serializationDataFormat(SerializationDataFormats.JAVA).create())).correlate();
    // then
    ObjectValue variableTyped = runtimeService.getVariableTyped(processInstance.getId(), "var", false);
    assertNotNull(variableTyped);
    assertFalse(variableTyped.isDeserialized());
    assertEquals(serializedObject, variableTyped.getValueSerialized());
    assertEquals(FailingJavaSerializable.class.getName(), variableTyped.getObjectTypeName());
    assertEquals(SerializationDataFormats.JAVA.getName(), variableTyped.getSerializationDataFormat());
}
Also used : ObjectValue(org.camunda.bpm.engine.variable.value.ObjectValue) ByteArrayInputStream(java.io.ByteArrayInputStream) FailingJavaSerializable(org.camunda.bpm.engine.test.api.variables.FailingJavaSerializable) ProcessInstance(org.camunda.bpm.engine.runtime.ProcessInstance) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) Deployment(org.camunda.bpm.engine.test.Deployment)

Aggregations

ObjectValue (org.camunda.bpm.engine.variable.value.ObjectValue)99 Test (org.junit.Test)58 ProcessInstance (org.camunda.bpm.engine.runtime.ProcessInstance)53 Deployment (org.camunda.bpm.engine.test.Deployment)46 Variables.serializedObjectValue (org.camunda.bpm.engine.variable.Variables.serializedObjectValue)36 MockObjectValue (org.camunda.bpm.engine.rest.helper.MockObjectValue)29 EqualsObjectValue (org.camunda.bpm.engine.rest.helper.variable.EqualsObjectValue)23 Matchers.containsString (org.hamcrest.Matchers.containsString)20 Matchers.anyString (org.mockito.Matchers.anyString)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ObjectOutputStream (java.io.ObjectOutputStream)9 AbstractFoxPlatformIntegrationTest (org.camunda.bpm.integrationtest.util.AbstractFoxPlatformIntegrationTest)8 Task (org.camunda.bpm.engine.task.Task)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ObjectInputStream (java.io.ObjectInputStream)6 HashMap (java.util.HashMap)6 HistoricVariableInstance (org.camunda.bpm.engine.history.HistoricVariableInstance)6 FailingJavaSerializable (org.camunda.bpm.engine.test.api.variables.FailingJavaSerializable)6 VariableMap (org.camunda.bpm.engine.variable.VariableMap)6 SerializedObjectValueBuilder (org.camunda.bpm.engine.variable.value.builder.SerializedObjectValueBuilder)6