public static void assertObjectValueSerializedJava(ObjectValue typedValue, Object value) { assertEquals(Variables.SerializationDataFormats.JAVA.getName(), typedValue.getSerializationDataFormat()); try { // validate this is the base 64 encoded string representation of the serialized value of the java object String valueSerialized = typedValue.getValueSerialized(); byte[] decodedObject = Base64.decodeBase64(valueSerialized.getBytes(Charset.forName("UTF-8"))); ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(decodedObject)); assertEquals(value, objectInputStream.readObject()); } catch(IOException e) { throw new RuntimeException(e); } catch(ClassNotFoundException e) { throw new RuntimeException(e); } }
protected String getObjectTypeName(ObjectValue value, ValueFields valueFields) { String objectTypeName = value.getObjectTypeName(); if (objectTypeName == null && !value.isDeserialized() && value.getValueSerialized() != null) { throw new ProcessEngineException("Cannot write serialized value for variable '" + valueFields.getName() + "': no 'objectTypeName' provided for non-null value."); } // update type name if the object is deserialized if (value.isDeserialized() && value.getValue() != null) { objectTypeName = getTypeNameForDeserialized(value.getValue()); } return objectTypeName; }
protected String getObjectTypeName(ObjectValue value, ValueFields valueFields) { String objectTypeName = value.getObjectTypeName(); if (objectTypeName == null && !value.isDeserialized() && value.getValueSerialized() != null) { throw new ProcessEngineException("Cannot write serialized value for variable '" + valueFields.getName() + "': no 'objectTypeName' provided for non-null value."); } // update type name if the object is deserialized if (value.isDeserialized() && value.getValue() != null) { objectTypeName = getTypeNameForDeserialized(value.getValue()); } return objectTypeName; }
@Test public void getHistoricVariableByteArrayUpdates() { // given BpmnModelInstance simpleDefinition = Bpmn.createExecutableProcess("process") .startEvent() .endEvent() .done(); testHelper.deploy(simpleDefinition); List<String> serializable = new ArrayList<String>(); serializable.add("one"); serializable.add("two"); serializable.add("three"); Map<String, Object> variables = new HashMap<String, Object>(); variables.put("var", serializable); runtimeService.startProcessInstanceByKey("process", variables); runtimeService.startProcessInstanceByKey("process", variables); runtimeService.startProcessInstanceByKey("process", variables); runtimeService.startProcessInstanceByKey("process", variables); // when List<HistoricVariableUpdate> historicVariableUpdates = optimizeService.getHistoricVariableUpdates(new Date(1L), null, 10); // then assertThat(historicVariableUpdates.size(), is(4)); for (HistoricVariableUpdate variableUpdate : historicVariableUpdates) { ObjectValue typedValue = (ObjectValue) variableUpdate.getTypedValue(); assertThat(typedValue.isDeserialized(), is(false)); assertThat(typedValue.getValueSerialized(), notNullValue()); } }
@Test public void testPaLocalJodaConfiguration() throws JsonProcessingException, IOException { // given a process instance final ProcessInstance pi = runtimeService.startProcessInstanceByKey("testProcess"); // when setting a variable in the context of a process application Date date = new Date(JodaJsonSerializable.ONE_DAY_IN_MILLIS * 10); // 10th of January 1970 JodaJsonSerializable jsonSerializable = new JodaJsonSerializable(new DateTime(date.getTime())); 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(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode actualJsonTree = objectMapper.readTree(serializedValue); JsonNode expectedJsonTree = objectMapper.readTree(expectedSerializedValue); // JsonNode#equals makes a deep comparison Assert.assertEquals(expectedJsonTree, actualJsonTree); }
public static MockObjectValue fromObjectValue(ObjectValue objectValue) { MockObjectValue result = new MockObjectValue(objectValue.getValue()); result.isDeserialized = objectValue.isDeserialized(); result.objectTypeName = objectValue.getObjectTypeName(); result.serializationDataFormat = objectValue.getSerializationDataFormat(); result.serializedValue = objectValue.getValueSerialized(); result.type = objectValue.getType(); return result; }
/** * 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 Date date = new Date(JsonSerializable.ONE_DAY_IN_MILLIS * 10); // 10th of January 1970 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); }
assertNotNull(variableTyped); assertFalse(variableTyped.isDeserialized()); assertEquals(serializedObject, variableTyped.getValueSerialized()); assertEquals(FailingJavaSerializable.class.getName(), variableTyped.getObjectTypeName()); assertEquals(SerializationDataFormats.JAVA.getName(), variableTyped.getSerializationDataFormat());
assertNotNull(variableTyped); assertFalse(variableTyped.isDeserialized()); assertEquals(serializedObject, variableTyped.getValueSerialized()); assertEquals(FailingJavaSerializable.class.getName(), variableTyped.getObjectTypeName()); assertEquals(SerializationDataFormats.JAVA.getName(), variableTyped.getSerializationDataFormat());
public static void assertObjectValueSerializedNull(ObjectValue typedValue) { assertNotNull(typedValue); assertFalse(typedValue.isDeserialized()); assertNotNull(typedValue.getSerializationDataFormat()); assertNull(typedValue.getValueSerialized()); assertNull(typedValue.getObjectTypeName()); }
/** * 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 Date date = new Date(JsonSerializable.ONE_DAY_IN_MILLIS * 10); // 10th of January 1970 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); }
public void testDisableCustomObjectDeserialization() { // given Task newTask = taskService.newTask(); taskService.saveTask(newTask); Map<String, Object> variables = new HashMap<String, Object>(); variables.put("customSerializable", new CustomSerializable()); variables.put("failingSerializable", new FailingSerializable()); taskService.setVariables(newTask.getId(), variables); // when List<HistoricVariableInstance> variableInstances = historyService.createHistoricVariableInstanceQuery() .disableCustomObjectDeserialization() .list(); // then assertEquals(2, variableInstances.size()); for (HistoricVariableInstance variableInstance : variableInstances) { assertNull(variableInstance.getErrorMessage()); ObjectValue typedValue = (ObjectValue) variableInstance.getTypedValue(); assertNotNull(typedValue); assertFalse(typedValue.isDeserialized()); // cannot access the deserialized value try { typedValue.getValue(); } catch(IllegalStateException e) { assertTextPresent("Object is not deserialized", e.getMessage()); } assertNotNull(typedValue.getValueSerialized()); } taskService.deleteTask(newTask.getId(), true); }
CoreMatchers.containsString("Object is not deserialized")); assertNotNull(typedValue.getValueSerialized());
assertTextPresent("Object is not deserialized", e.getMessage()); assertNotNull(typedValue.getValueSerialized());
assertTextPresent("Object is not deserialized", e.getMessage()); assertNotNull(typedValue.getValueSerialized());
public static void assertObjectValueDeserializedNull(ObjectValue typedValue) { assertNotNull(typedValue); assertTrue(typedValue.isDeserialized()); assertNotNull(typedValue.getSerializationDataFormat()); assertNull(typedValue.getValue()); assertNull(typedValue.getValueSerialized()); assertNull(typedValue.getObjectType()); assertNull(typedValue.getObjectTypeName()); }
@Deployment(resources = {"org/camunda/bpm/engine/test/api/form/FormsProcess.bpmn20.xml"}) @Test public void testSubmitStartFormDataTypedVariables() { String procDefId = repositoryService.createProcessDefinitionQuery().singleResult().getId(); String stringValue = "some string"; String serializedValue = "some value"; ProcessInstance processInstance = formService.submitStartForm(procDefId, createVariables() .putValueTyped("boolean", booleanValue(null)) .putValueTyped("string", stringValue(stringValue)) .putValueTyped("serializedObject", serializedObjectValue(serializedValue) .objectTypeName(String.class.getName()) .serializationDataFormat(Variables.SerializationDataFormats.JAVA) .create()) .putValueTyped("object", objectValue(serializedValue).create())); VariableMap variables = runtimeService.getVariablesTyped(processInstance.getId(), false); assertEquals(booleanValue(null), variables.getValueTyped("boolean")); assertEquals(stringValue(stringValue), variables.getValueTyped("string")); assertNotNull(variables.<ObjectValue>getValueTyped("serializedObject").getValueSerialized()); assertNotNull(variables.<ObjectValue>getValueTyped("object").getValueSerialized()); }
@Test public void testCanMigrateWithObjectVariableThatFailsOnDeserialization() { // given ProcessDefinition sourceProcessDefinition = testHelper.deployAndGetDefinition(ProcessModels.ONE_TASK_PROCESS); ProcessDefinition targetProcessDefinition = testHelper.deployAndGetDefinition(ProcessModels.ONE_TASK_PROCESS); MigrationPlan migrationPlan = rule.getRuntimeService() .createMigrationPlan(sourceProcessDefinition.getId(), targetProcessDefinition.getId()) .mapActivities("userTask", "userTask") .build(); ProcessInstance processInstance = rule.getRuntimeService().startProcessInstanceById(sourceProcessDefinition.getId()); ObjectValue objectValue = Variables .serializedObjectValue("does/not/deserialize") .serializationDataFormat(SerializationDataFormats.JAVA) .objectTypeName("and.this.is.a.nonexisting.Class") .create(); runtimeService.setVariable( processInstance.getId(), "var", objectValue); // when testHelper.migrateProcessInstance(migrationPlan, processInstance); // then ObjectValue migratedValue = runtimeService.getVariableTyped(processInstance.getId(), "var", false); Assert.assertEquals(objectValue.getValueSerialized(), migratedValue.getValueSerialized()); Assert.assertEquals(objectValue.getObjectTypeName(), migratedValue.getObjectTypeName()); }
@Deployment(resources = {"org/camunda/bpm/engine/test/api/form/FormsProcess.bpmn20.xml"}) @Test public void testSubmitTaskFormDataTypedVariables() { String procDefId = repositoryService.createProcessDefinitionQuery().singleResult().getId(); ProcessInstance processInstance = formService.submitStartForm(procDefId, createVariables()); Task task = taskService.createTaskQuery().singleResult(); String stringValue = "some string"; String serializedValue = "some value"; formService.submitTaskForm(task.getId(), createVariables() .putValueTyped("boolean", booleanValue(null)) .putValueTyped("string", stringValue(stringValue)) .putValueTyped("serializedObject", serializedObjectValue(serializedValue) .objectTypeName(String.class.getName()) .serializationDataFormat(Variables.SerializationDataFormats.JAVA) .create()) .putValueTyped("object", objectValue(serializedValue).create())); VariableMap variables = runtimeService.getVariablesTyped(processInstance.getId(), false); assertEquals(booleanValue(null), variables.getValueTyped("boolean")); assertEquals(stringValue(stringValue), variables.getValueTyped("string")); assertNotNull(variables.<ObjectValue>getValueTyped("serializedObject").getValueSerialized()); assertNotNull(variables.<ObjectValue>getValueTyped("object").getValueSerialized()); }
@Test @Deployment(resources = ONE_TASK_PROCESS) public void testSetJavaObjectNullSerializedObjectTypeName() throws Exception { ProcessInstance instance = runtimeService.startProcessInstanceByKey("oneTaskProcess"); String typeName = "some.type.Name"; // set null value as "serialized" object runtimeService.setVariable(instance.getId(), "nullObject", serializedObjectValue() .serializationDataFormat(JAVA_DATA_FORMAT) .objectTypeName(typeName) // This time an objectTypeName is provided .create()); // get null value via untyped api assertNull(runtimeService.getVariable(instance.getId(), "nullObject")); // get null via typed api ObjectValue deserializedTypedValue = runtimeService.getVariableTyped(instance.getId(), "nullObject"); assertNotNull(deserializedTypedValue); assertTrue(deserializedTypedValue.isDeserialized()); assertEquals(JAVA_DATA_FORMAT, deserializedTypedValue.getSerializationDataFormat()); assertNull(deserializedTypedValue.getValue()); assertNull(deserializedTypedValue.getValueSerialized()); assertNull(deserializedTypedValue.getObjectType()); assertEquals(typeName, deserializedTypedValue.getObjectTypeName()); ObjectValue serializedTypedValue = runtimeService.getVariableTyped(instance.getId(), "nullObject", false); assertNotNull(serializedTypedValue); assertFalse(serializedTypedValue.isDeserialized()); assertEquals(JAVA_DATA_FORMAT, serializedTypedValue.getSerializationDataFormat()); assertNull(serializedTypedValue.getValueSerialized()); assertEquals(typeName, serializedTypedValue.getObjectTypeName()); }