Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incubator-kie-issues#845: Add checks ProtobufProcessInstanceReader to avoid setting incorrect empty values on #3364

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,8 +65,51 @@

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 {

Expand Down Expand Up @@ -85,6 +141,29 @@ public boolean equals(Object obj) {
}
}

private static Stream<Arguments> 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<Arguments> testRoundTrip() throws Exception {
return Stream.of(
Arguments.of(1),
Expand All @@ -109,6 +188,37 @@ private static Stream<Arguments> 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
Expand Down