From d3c384ee1d07781f91f4b380c6e85f7071423154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pere=20Fern=C3=A1ndez?= Date: Mon, 22 Jan 2024 18:34:52 +0100 Subject: [PATCH 1/2] incubator-kie-issues#845:Add property checks `ProtobufProcessInstanceReader` to avoid setting incorrect empty values --- .../impl/ProtobufProcessInstanceReader.java | 64 +++++++--- .../process/ProcessInstanceMarshallTest.java | 113 ++++++++++++++++++ 2 files changed, 161 insertions(+), 16 deletions(-) diff --git a/jbpm/process-serialization-protobuf/src/main/java/org/kie/kogito/serialization/process/impl/ProtobufProcessInstanceReader.java b/jbpm/process-serialization-protobuf/src/main/java/org/kie/kogito/serialization/process/impl/ProtobufProcessInstanceReader.java index a7581dc555e..a182931c155 100644 --- a/jbpm/process-serialization-protobuf/src/main/java/org/kie/kogito/serialization/process/impl/ProtobufProcessInstanceReader.java +++ b/jbpm/process-serialization-protobuf/src/main/java/org/kie/kogito/serialization/process/impl/ProtobufProcessInstanceReader.java @@ -130,34 +130,66 @@ private RuleFlowProcessInstance buildWorkflow(KogitoProcessInstanceProtobuf.Proc processInstance.setProcessId(processInstanceProtobuf.getProcessId()); processInstance.setState(processInstanceProtobuf.getState()); processInstance.setSignalCompletion(processInstanceProtobuf.getSignalCompletion()); - processInstance.setStartDate(new Date(processInstanceProtobuf.getStartDate())); - processInstance.setDescription(processInstanceProtobuf.getDescription()); - processInstance.setDeploymentId(processInstanceProtobuf.getDeploymentId()); + if (processInstanceProtobuf.hasStartDate()) { + processInstance.setStartDate(new Date(processInstanceProtobuf.getStartDate())); + } + + if (processInstanceProtobuf.hasDescription()) { + processInstance.setDescription(processInstanceProtobuf.getDescription()); + } + + if (processInstanceProtobuf.hasDeploymentId()) { + processInstance.setDeploymentId(processInstanceProtobuf.getDeploymentId()); + } for (String completedNodeId : processInstanceProtobuf.getCompletedNodeIdsList()) { processInstance.addCompletedNodeId(completedNodeId); } - processInstance.setCorrelationKey(processInstanceProtobuf.getBusinessKey()); + if (processInstanceProtobuf.hasBusinessKey()) { + processInstance.setCorrelationKey(processInstanceProtobuf.getBusinessKey()); + } + + if (processInstanceProtobuf.hasSla()) { + SLAContext slaContext = processInstanceProtobuf.getSla(); + if (slaContext.getSlaDueDate() > 0) { + processInstance.internalSetSlaDueDate(new Date(slaContext.getSlaDueDate())); + } + + if (slaContext.hasSlaTimerId()) { + processInstance.internalSetSlaTimerId(slaContext.getSlaTimerId()); + } + if (slaContext.hasSlaCompliance()) { + processInstance.internalSetSlaCompliance(slaContext.getSlaCompliance()); + } + } - SLAContext slaContext = processInstanceProtobuf.getSla(); - if (slaContext.getSlaDueDate() > 0) { - processInstance.internalSetSlaDueDate(new Date(slaContext.getSlaDueDate())); + if (processInstanceProtobuf.hasCancelTimerId()) { + processInstance.internalSetCancelTimerId(processInstanceProtobuf.getCancelTimerId()); } - processInstance.internalSetSlaTimerId(slaContext.getSlaTimerId()); - processInstance.internalSetSlaCompliance(slaContext.getSlaCompliance()); - processInstance.internalSetCancelTimerId(processInstanceProtobuf.getCancelTimerId()); + if (processInstanceProtobuf.hasParentProcessInstanceId()) { + processInstance.setParentProcessInstanceId(processInstanceProtobuf.getParentProcessInstanceId()); + } + if (processInstanceProtobuf.hasRootProcessInstanceId()) { + processInstance.setRootProcessInstanceId(processInstanceProtobuf.getRootProcessInstanceId()); + } + if (processInstanceProtobuf.hasRootProcessId()) { + processInstance.setRootProcessId(processInstanceProtobuf.getRootProcessId()); + } - processInstance.setParentProcessInstanceId(processInstanceProtobuf.getParentProcessInstanceId()); - processInstance.setRootProcessInstanceId(processInstanceProtobuf.getRootProcessInstanceId()); - processInstance.setRootProcessId(processInstanceProtobuf.getRootProcessId()); + if (processInstanceProtobuf.hasErrorNodeId()) { + processInstance.internalSetErrorNodeId(processInstanceProtobuf.getErrorNodeId()); + } - processInstance.internalSetErrorNodeId(processInstanceProtobuf.getErrorNodeId()); - processInstance.internalSetErrorMessage(processInstanceProtobuf.getErrorMessage()); + if (processInstanceProtobuf.hasErrorMessage()) { + processInstance.internalSetErrorMessage(processInstanceProtobuf.getErrorMessage()); + } - processInstance.setReferenceId(processInstanceProtobuf.getReferenceId()); + if (processInstanceProtobuf.hasReferenceId()) { + processInstance.setReferenceId(processInstanceProtobuf.getReferenceId()); + } if (processInstanceProtobuf.getSwimlaneContextCount() > 0) { SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) processInstance.getContextInstance(SwimlaneContext.SWIMLANE_SCOPE); diff --git a/jbpm/process-serialization-protobuf/src/test/java/org/kie/kogito/serialization/process/ProcessInstanceMarshallTest.java b/jbpm/process-serialization-protobuf/src/test/java/org/kie/kogito/serialization/process/ProcessInstanceMarshallTest.java index 884187c641e..3ed1dd80346 100644 --- a/jbpm/process-serialization-protobuf/src/test/java/org/kie/kogito/serialization/process/ProcessInstanceMarshallTest.java +++ b/jbpm/process-serialization-protobuf/src/test/java/org/kie/kogito/serialization/process/ProcessInstanceMarshallTest.java @@ -38,11 +38,24 @@ import java.util.stream.Stream; import org.jbpm.process.core.context.variable.Variable; +import org.jbpm.process.core.context.variable.VariableScope; +import org.jbpm.process.instance.context.variable.VariableScopeInstance; +import org.jbpm.ruleflow.instance.RuleFlowProcessInstance; +import org.jbpm.workflow.core.Node; +import org.jbpm.workflow.core.impl.ConnectionImpl; +import org.jbpm.workflow.core.impl.WorkflowProcessImpl; +import org.jbpm.workflow.core.node.EndNode; +import org.jbpm.workflow.core.node.HumanTaskNode; +import org.jbpm.workflow.core.node.StartNode; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.NullSource; +import org.kie.kogito.process.impl.AbstractProcess; import org.kie.kogito.serialization.process.impl.ProtobufMarshallerReaderContext; +import org.kie.kogito.serialization.process.impl.ProtobufProcessInstanceReader; +import org.kie.kogito.serialization.process.impl.ProtobufProcessInstanceWriter; import org.kie.kogito.serialization.process.impl.ProtobufProcessMarshallerWriteContext; import org.kie.kogito.serialization.process.impl.ProtobufVariableReader; import org.kie.kogito.serialization.process.impl.ProtobufVariableWriter; @@ -52,8 +65,52 @@ import static java.util.Collections.singletonMap; import static org.assertj.core.api.Assertions.assertThat; +import static org.kie.kogito.internal.process.runtime.KogitoWorkflowProcess.RULEFLOW_TYPE; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ProcessInstanceMarshallTest { + private static final String PROCESS_DESCRIPTION = "The description"; + private static final String PROCESS_INSTANCE_ID = "1"; + private static final String ROOT_PROCESS_ID = "rootProcess"; + private static final String ROOT_PROCESS_INSTANCE_ID = "0"; + private static final String PARENT_PROCESS_ID = "2"; + + private static AbstractProcess process; + + private static WorkflowProcessImpl workflow; + + @BeforeAll + public static void init() { + workflow = new WorkflowProcessImpl(); + + workflow.setId("processId"); + workflow.setVersion("1.0"); + workflow.setType(RULEFLOW_TYPE); + + Node endNode = new EndNode(); + endNode.setId(1); + endNode.setName("end node"); + + Node taskNode = new HumanTaskNode(); + taskNode.setId(2); + taskNode.setName("human task"); + new ConnectionImpl(taskNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE); + + Node startNode = new StartNode(); + startNode.setId(0); + startNode.setName("start node"); + new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, taskNode, Node.CONNECTION_DEFAULT_TYPE); + + workflow.addNode(startNode); + workflow.addNode(taskNode); + workflow.addNode(endNode); + + process = mock(AbstractProcess.class); + + when(process.get()).thenReturn(workflow); + } + public static class MarshableObject implements Serializable { @@ -85,6 +142,31 @@ public boolean equals(Object obj) { } } + + private static Stream testProcessInstanceMarshalling() { + return Stream.of(Arguments.of(buildInstance(false)), + (Arguments.of(buildInstance(true)))); + } + + private static RuleFlowProcessInstance buildInstance(boolean orphan) { + RuleFlowProcessInstance instance = new RuleFlowProcessInstance(); + instance.setId(PROCESS_INSTANCE_ID); + instance.setStartDate(new Date()); + instance.setDescription(PROCESS_DESCRIPTION); + + if(!orphan) { + instance.setRootProcessInstanceId(ROOT_PROCESS_INSTANCE_ID); + instance.setRootProcessId(ROOT_PROCESS_ID); + instance.setParentProcessInstanceId(PARENT_PROCESS_ID); + } + + instance.setProcess(workflow); + instance.setContextInstance(VariableScope.VARIABLE_SCOPE, new VariableScopeInstance()); + + return instance; + } + + private static Stream testRoundTrip() throws Exception { return Stream.of( Arguments.of(1), @@ -109,6 +191,37 @@ private static Stream testRoundTrip() throws Exception { ); } + @ParameterizedTest + @MethodSource + public void testProcessInstanceMarshalling(RuleFlowProcessInstance toMarshall) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ProtobufProcessMarshallerWriteContext ctxOut = new ProtobufProcessMarshallerWriteContext(out); + ctxOut.set(MarshallerContextName.OBJECT_MARSHALLING_STRATEGIES, defaultStrategies()); + + ProtobufProcessInstanceWriter writer = new ProtobufProcessInstanceWriter(ctxOut); + + writer.writeProcessInstance(toMarshall, out); + + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + ProtobufMarshallerReaderContext ctxIn = new ProtobufMarshallerReaderContext(in); + ctxIn.set(MarshallerContextName.OBJECT_MARSHALLING_STRATEGIES, defaultStrategies()); + ctxIn.set(MarshallerContextName.MARSHALLER_PROCESS, process); + ProtobufProcessInstanceReader reader = new ProtobufProcessInstanceReader(ctxIn); + RuleFlowProcessInstance unmarshalled = reader.read(in); + + assertThat(unmarshalled) + .hasFieldOrPropertyWithValue("id", toMarshall.getId()) + .hasFieldOrPropertyWithValue("state", toMarshall.getState()) + .hasFieldOrPropertyWithValue("startDate", toMarshall.getStartDate()) + .hasFieldOrPropertyWithValue("processId", toMarshall.getProcessId()) + .hasFieldOrPropertyWithValue("processVersion", toMarshall.getProcessVersion()) + .hasFieldOrPropertyWithValue("description", toMarshall.getDescription()) + .hasFieldOrPropertyWithValue("rootProcessInstanceId", toMarshall.getRootProcessInstanceId()) + .hasFieldOrPropertyWithValue("rootProcessId", toMarshall.getRootProcessId()) + .hasFieldOrPropertyWithValue("parentProcessInstanceId", toMarshall.getParentProcessInstanceId()) + .hasFieldOrPropertyWithValue("process", toMarshall.getProcess()); + } + @ParameterizedTest @MethodSource @NullSource From 24e643aff1179a506de667a1966ec3bc3b16ca25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pere=20Fern=C3=A1ndez?= Date: Tue, 23 Jan 2024 09:29:40 +0100 Subject: [PATCH 2/2] - reformat --- .../serialization/process/ProcessInstanceMarshallTest.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/jbpm/process-serialization-protobuf/src/test/java/org/kie/kogito/serialization/process/ProcessInstanceMarshallTest.java b/jbpm/process-serialization-protobuf/src/test/java/org/kie/kogito/serialization/process/ProcessInstanceMarshallTest.java index 3ed1dd80346..a3af7111980 100644 --- a/jbpm/process-serialization-protobuf/src/test/java/org/kie/kogito/serialization/process/ProcessInstanceMarshallTest.java +++ b/jbpm/process-serialization-protobuf/src/test/java/org/kie/kogito/serialization/process/ProcessInstanceMarshallTest.java @@ -111,7 +111,6 @@ public static void init() { when(process.get()).thenReturn(workflow); } - public static class MarshableObject implements Serializable { private static final long serialVersionUID = 1481370154514125687L; @@ -142,7 +141,6 @@ public boolean equals(Object obj) { } } - private static Stream testProcessInstanceMarshalling() { return Stream.of(Arguments.of(buildInstance(false)), (Arguments.of(buildInstance(true)))); @@ -154,7 +152,7 @@ private static RuleFlowProcessInstance buildInstance(boolean orphan) { instance.setStartDate(new Date()); instance.setDescription(PROCESS_DESCRIPTION); - if(!orphan) { + if (!orphan) { instance.setRootProcessInstanceId(ROOT_PROCESS_INSTANCE_ID); instance.setRootProcessId(ROOT_PROCESS_ID); instance.setParentProcessInstanceId(PARENT_PROCESS_ID); @@ -166,7 +164,6 @@ private static RuleFlowProcessInstance buildInstance(boolean orphan) { return instance; } - private static Stream testRoundTrip() throws Exception { return Stream.of( Arguments.of(1),