Skip to content

Commit

Permalink
Handle Supplier<JsonNode> for EventPayloadToJsonStringSerializer (#3722
Browse files Browse the repository at this point in the history
…) (#3723)
  • Loading branch information
arthware committed Aug 29, 2023
1 parent 4ed4436 commit 55b421f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.time.Duration;
import java.util.List;
import java.util.function.Supplier;

import javax.jms.Message;
import javax.jms.TextMessage;
Expand All @@ -39,6 +40,7 @@
import org.springframework.jms.core.JmsTemplate;
import org.springframework.test.context.TestPropertySource;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@CmmnJmsEventTest
Expand All @@ -59,6 +61,9 @@ public class CaseWithEventRegistryTest {
@Autowired
protected JmsTemplate jmsTemplate;

@Autowired
ObjectMapper objectMapper;

@Test
@CmmnDeployment(resources = { "org/flowable/eventregistry/integrationtest/startCaseWithEvent.cmmn",
"org/flowable/eventregistry/integrationtest/one.event",
Expand Down Expand Up @@ -176,7 +181,45 @@ public void testSendEventTask() throws Exception {
}
}
}


@Test
@CmmnDeployment(resources = { "org/flowable/eventregistry/integrationtest/testSendEventTaskWithJson.cmmn",
"org/flowable/eventregistry/integrationtest/oneJson.event",
"org/flowable/eventregistry/integrationtest/one-outbound.channel" })
public void testSendEventTaskWithJsonPayload() throws Exception {
try {
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("firstname", "Kermit");
objectNode.put("lastname", "The Frog");

Supplier<ObjectNode> supplier = () -> objectMapper.createObjectNode()
.put("firstname", "Annie Sue")
.put("lastname", "Pig");

CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("testSendEvent")
.transientVariable("myJsonVariable", objectNode)
.transientVariable("myJsonSupplierVariable", supplier)
.start();

Task task = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).singleResult();
assertThat(task).isNotNull();

Message message = jmsTemplate.receive("test-outbound-queue");
TextMessage textMessage = (TextMessage) message;
assertThatJson(textMessage.getText())
.isEqualTo("{"
+ " jsonPayload1: {firstname: 'Kermit',lastname: 'The Frog'},"
+ " jsonPayload2: {firstname: 'Annie Sue',lastname: 'Pig'}"
+ "}");

} finally {
List<EventDeployment> eventDeployments = getEventRepositoryService().createDeploymentQuery().list();
for (EventDeployment eventDeployment : eventDeployments) {
getEventRepositoryService().deleteDeployment(eventDeployment.getId());
}
}
}

@Test
@CmmnDeployment(resources = { "org/flowable/eventregistry/integrationtest/startCaseWithEvent.cmmn",
"org/flowable/eventregistry/integrationtest/one.event",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"key": "one",
"name": "My first Event",
"payload": [
{
"name": "jsonPayload1",
"type": "json"
},
{
"name": "jsonPayload2",
"type": "json"
}

]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/CMMN/20151109/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flowable="http://flowable.org/cmmn" xmlns:cmmndi="http://www.omg.org/spec/CMMN/20151109/CMMNDI" xmlns:dc="http://www.omg.org/spec/CMMN/20151109/DC" xmlns:di="http://www.omg.org/spec/CMMN/20151109/DI" xmlns:design="http://flowable.org/design" targetNamespace="http://flowable.org/cmmn">
<case id="testSendEvent" name="testSendEvent" flowable:initiatorVariableName="initiator" flowable:candidateStarterGroups="flowableUser">
<casePlanModel id="onecaseplanmodel1" name="Case plan model" flowable:formFieldValidation="false">
<planItem id="planItem1" name="Send event task" definitionRef="sendEventTask1"></planItem>
<planItem id="planItem2" name="Human task" definitionRef="humanTask1">
<entryCriterion id="entryCriterion1" sentryRef="sentry1"></entryCriterion>
</planItem>
<sentry id="sentry1">
<planItemOnPart id="sentryOnPart1" sourceRef="planItem1">
<standardEvent>complete</standardEvent>
</planItemOnPart>
</sentry>
<task id="sendEventTask1" name="Send event task" flowable:type="send-event">
<extensionElements>
<flowable:eventType>one</flowable:eventType>
<flowable:channelKey>one-outbound</flowable:channelKey>
<flowable:eventInParameter source="test" target="headerProperty1" />
<flowable:eventInParameter source="${myJsonVariable}" target="jsonPayload1" />
<flowable:eventInParameter source="${myJsonSupplierVariable}" target="jsonPayload2" />
</extensionElements>
</task>
<humanTask id="humanTask1" />
</casePlanModel>
</case>
</definitions>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.flowable.eventregistry.impl.serialization;

import java.util.Collection;
import java.util.function.Supplier;

import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
Expand Down Expand Up @@ -96,19 +97,25 @@ public String serialize(EventInstance eventInstance) {
}

} else if (EventPayloadTypes.JSON.equals(definitionType)) {

if (payloadInstanceValue instanceof JsonNode) {
objectNode.set(payloadInstance.getDefinitionName(), (JsonNode) payloadInstanceValue);
} else if (payloadInstanceValue instanceof String) {
JsonNode jsonNode = null;
Object jsonValue = payloadInstanceValue;
if (payloadInstanceValue instanceof Supplier<?>) {
Object suppliedValue = ((Supplier<?>) payloadInstanceValue).get();
if (suppliedValue instanceof JsonNode) {
jsonValue = suppliedValue;
}
}
if (jsonValue instanceof JsonNode) {
objectNode.set(payloadInstance.getDefinitionName(), (JsonNode) jsonValue);
} else if (jsonValue instanceof String) {
JsonNode jsonNode;
try {
jsonNode = objectMapper.readTree((String) payloadInstanceValue);
jsonNode = objectMapper.readTree((String) jsonValue);
} catch (JsonProcessingException e) {
throw new FlowableIllegalArgumentException("Could not read json event payload", e);
}
objectNode.set(payloadInstance.getDefinitionName(), jsonNode);
} else {
throw new FlowableIllegalArgumentException("Cannot convert event payload " + payloadInstanceValue + " to type 'json'");
throw new FlowableIllegalArgumentException("Cannot convert event payload " + jsonValue + " to type 'json'");
}

} else {
Expand Down

0 comments on commit 55b421f

Please sign in to comment.