From 9d58e0ea6a36e9af6fb55e7478e22fef6da81660 Mon Sep 17 00:00:00 2001 From: Patrick Schalk Date: Fri, 1 Sep 2017 15:17:25 +0200 Subject: [PATCH] WiP #42 --- .../extension/mockito/ProcessExpressions.java | 6 ++ .../mockito/process/ProcessMock.java | 9 +-- .../mockito/process/SubProcessMock.java | 38 ++++++++++++ .../EmbeddedSubprocessMockExample.java | 62 +++++++++++++++++++ .../mockito/SubprocessMockExample.java | 17 +++-- 5 files changed, 119 insertions(+), 13 deletions(-) create mode 100644 src/main/java/org/camunda/bpm/extension/mockito/process/SubProcessMock.java create mode 100644 src/test/java/org/camunda/bpm/extension/mockito/EmbeddedSubprocessMockExample.java diff --git a/src/main/java/org/camunda/bpm/extension/mockito/ProcessExpressions.java b/src/main/java/org/camunda/bpm/extension/mockito/ProcessExpressions.java index d615bf9a..87a9fef0 100644 --- a/src/main/java/org/camunda/bpm/extension/mockito/ProcessExpressions.java +++ b/src/main/java/org/camunda/bpm/extension/mockito/ProcessExpressions.java @@ -1,6 +1,8 @@ package org.camunda.bpm.extension.mockito; +import org.camunda.bpm.engine.test.ProcessEngineRule; import org.camunda.bpm.extension.mockito.process.ProcessMock; +import org.camunda.bpm.extension.mockito.process.SubProcessMock; public class ProcessExpressions { @@ -10,4 +12,8 @@ private ProcessExpressions() { public static ProcessMock registerSubProcessMock(String processId) { return new ProcessMock(processId); } + + public static ProcessMock registerEmbeddedSubProcessMock(final ProcessEngineRule rule, final String processDefinitionKey, final String subProcessModelId) { + return new SubProcessMock(rule, processDefinitionKey, subProcessModelId); + } } diff --git a/src/main/java/org/camunda/bpm/extension/mockito/process/ProcessMock.java b/src/main/java/org/camunda/bpm/extension/mockito/process/ProcessMock.java index 3155b9c1..a29f3b28 100644 --- a/src/main/java/org/camunda/bpm/extension/mockito/process/ProcessMock.java +++ b/src/main/java/org/camunda/bpm/extension/mockito/process/ProcessMock.java @@ -9,7 +9,7 @@ import org.camunda.bpm.model.bpmn.Bpmn; import org.camunda.bpm.model.bpmn.builder.AbstractFlowNodeBuilder; -import java.util.*; +import java.util.UUID; import java.util.function.Consumer; import static org.camunda.bpm.engine.variable.Variables.createVariables; @@ -17,8 +17,8 @@ public class ProcessMock { - private String processId; - private AbstractFlowNodeBuilder flowNodeBuilder; + protected String processId; + protected AbstractFlowNodeBuilder flowNodeBuilder; public ProcessMock(String processId) { this.processId = processId; @@ -49,7 +49,8 @@ public ProcessMock onExecutionDo(final Consumer consumer) { } public ProcessMock onExecutionDo(final String serviceId, final Consumer consumer) { - flowNodeBuilder = flowNodeBuilder.serviceTask(serviceId) + flowNodeBuilder = flowNodeBuilder + .serviceTask(serviceId) .camundaDelegateExpression("${id}".replace("id", serviceId)); registerInstance(serviceId, (JavaDelegate)execution -> { diff --git a/src/main/java/org/camunda/bpm/extension/mockito/process/SubProcessMock.java b/src/main/java/org/camunda/bpm/extension/mockito/process/SubProcessMock.java new file mode 100644 index 00000000..a223f4c2 --- /dev/null +++ b/src/main/java/org/camunda/bpm/extension/mockito/process/SubProcessMock.java @@ -0,0 +1,38 @@ +package org.camunda.bpm.extension.mockito.process; + +import org.camunda.bpm.engine.repository.ProcessDefinition; +import org.camunda.bpm.engine.test.ProcessEngineRule; +import org.camunda.bpm.model.bpmn.Bpmn; +import org.camunda.bpm.model.bpmn.BpmnModelInstance; + +public class SubProcessMock extends ProcessMock { + + private final ProcessEngineRule rule; + private final String processDefinitionKey; + + public SubProcessMock(final ProcessEngineRule rule, final String processDefinitionKey, final String subProcessModelId) { + super(processDefinitionKey); + this.rule = rule; + this.processDefinitionKey = processDefinitionKey; + + this.extendGivenProcess(subProcessModelId); + } + + private void extendGivenProcess(String subProcessModelId) { + final ProcessDefinition processDefinition = this.rule.getRepositoryService() + .createProcessDefinitionQuery() + .processDefinitionKey(processDefinitionKey) + .latestVersion().singleResult(); + + final BpmnModelInstance bpmnModelInstance = this.rule.getRepositoryService() + .getBpmnModelInstance(processDefinition.getId()); + + System.out.println(Bpmn.convertToString(bpmnModelInstance)); + + flowNodeBuilder = bpmnModelInstance.getModelElementById(subProcessModelId); + + + // bpmnModelInstance. + } + +} diff --git a/src/test/java/org/camunda/bpm/extension/mockito/EmbeddedSubprocessMockExample.java b/src/test/java/org/camunda/bpm/extension/mockito/EmbeddedSubprocessMockExample.java new file mode 100644 index 00000000..3b71a4f2 --- /dev/null +++ b/src/test/java/org/camunda/bpm/extension/mockito/EmbeddedSubprocessMockExample.java @@ -0,0 +1,62 @@ +package org.camunda.bpm.extension.mockito; + +import org.camunda.bpm.engine.runtime.ProcessInstance; +import org.camunda.bpm.engine.test.ProcessEngineRule; +import org.camunda.bpm.extension.mockito.function.DeployProcess; +import org.camunda.bpm.model.bpmn.Bpmn; +import org.camunda.bpm.model.bpmn.BpmnModelInstance; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import java.util.Map; + +import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareAssertions.assertThat; +import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareTests.runtimeService; +import static org.camunda.bpm.extension.mockito.MostUsefulProcessEngineConfiguration.mostUsefulProcessEngineConfiguration; +import static org.camunda.bpm.extension.mockito.ProcessExpressions.registerEmbeddedSubProcessMock; + +public class EmbeddedSubprocessMockExample { + + public static final String PROCESS_ID = "myProcess"; + public static final String SUB_PROCESS_ID = "mySubProcess"; + public static final String SUB_PROCESS2_ID = "mySubProcess2"; + + @Rule + public final ProcessEngineRule rule = new ProcessEngineRule(mostUsefulProcessEngineConfiguration().buildProcessEngine()); + + @Before + public void setUp() throws Exception { + final BpmnModelInstance processWithSubProcess = Bpmn.createExecutableProcess(PROCESS_ID) + .startEvent("start") + .subProcess(SUB_PROCESS_ID) + .embeddedSubProcess() + .startEvent() + .endEvent() + .subProcessDone() + .userTask("user_task") + .endEvent("end") + .done(); + + DeployProcess.INSTANCE.apply(rule, processWithSubProcess, PROCESS_ID); + } + + @Test + public void register_embedded_subprocess_mock_addVar() throws Exception { + registerEmbeddedSubProcessMock(rule, PROCESS_ID, SUB_PROCESS_ID) + .onExecutionAddVariable("foo", "bar") + .deploy(rule); + + final ProcessInstance processInstance = startProcess(PROCESS_ID); + assertThat(processInstance).isWaitingAt("user_task"); + //TODO doesn't work with current camunda-bpm-assert version (1.*) and our assertj version (3.*) + //assertThat(processInstance).hasVariables("foo", "bar"); + final Map variables = runtimeService().getVariables(processInstance.getId()); + assertThat(variables).hasSize(1); + assertThat(variables).containsEntry("foo", "bar"); + } + + private ProcessInstance startProcess(final String key) { + return rule.getRuntimeService().startProcessInstanceByKey(key); + } +} diff --git a/src/test/java/org/camunda/bpm/extension/mockito/SubprocessMockExample.java b/src/test/java/org/camunda/bpm/extension/mockito/SubprocessMockExample.java index 6fbcd83e..660b9b2e 100644 --- a/src/test/java/org/camunda/bpm/extension/mockito/SubprocessMockExample.java +++ b/src/test/java/org/camunda/bpm/extension/mockito/SubprocessMockExample.java @@ -47,6 +47,7 @@ public void register_subprocess_mock_addVar() throws Exception { .deploy(rule); final ProcessInstance processInstance = startProcess(PROCESS_ID); + assertThat(processInstance).isWaitingAt("user_task"); //TODO doesn't work with current camunda-bpm-assert version (1.*) and our assertj version (3.*) //assertThat(processInstance).hasVariables("foo", "bar"); final Map variables = runtimeService().getVariables(processInstance.getId()); @@ -57,12 +58,11 @@ public void register_subprocess_mock_addVar() throws Exception { @Test public void register_subprocess_mock_withOwnConsumer() throws Exception { registerSubProcessMock(SUB_PROCESS_ID) - .onExecutionDo(execution -> { - execution.setVariable("foo", "bar"); - }) + .onExecutionDo(execution -> execution.setVariable("foo", "bar")) .deploy(rule); final ProcessInstance processInstance = startProcess(PROCESS_ID); + assertThat(processInstance).isWaitingAt("user_task"); //TODO doesn't work with current camunda-bpm-assert version (1.*) and our assertj version (3.*) //assertThat(processInstance).hasVariables("foo", "bar"); final Map variables = runtimeService().getVariables(processInstance.getId()); @@ -70,12 +70,6 @@ public void register_subprocess_mock_withOwnConsumer() throws Exception { assertThat(variables).containsEntry("foo", "bar"); } - private ProcessInstance startProcess(final String key) { - final ProcessInstance processInstance = rule.getRuntimeService().startProcessInstanceByKey(key); - assertThat(processInstance).isWaitingAt("user_task"); - return processInstance; - } - @Test public void register_subprocesses_mocks_withVariables() throws Exception { final BpmnModelInstance processWithSubProcess = Bpmn.createExecutableProcess(PROCESS_ID) @@ -100,6 +94,7 @@ public void register_subprocesses_mocks_withVariables() throws Exception { .deploy(rule); final ProcessInstance processInstance = startProcess(PROCESS_ID); + assertThat(processInstance).isWaitingAt("user_task"); //TODO doesn't work with current camunda-bpm-assert version (1.*) and our assertj version (3.*) //assertThat(processInstance).hasVariables("foo", "bar"); final Map variables = runtimeService().getVariables(processInstance.getId()); @@ -107,4 +102,8 @@ public void register_subprocesses_mocks_withVariables() throws Exception { assertThat(variables).containsEntry("foo", "bar"); assertThat(variables).containsEntry("bar", "foo"); } + + private ProcessInstance startProcess(final String key) { + return rule.getRuntimeService().startProcessInstanceByKey(key); + } }