Skip to content

Commit

Permalink
WiP #42
Browse files Browse the repository at this point in the history
  • Loading branch information
p-wunderlich committed Sep 1, 2017
1 parent d2c1b0d commit 9d58e0e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
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;
import static org.camunda.bpm.extension.mockito.Expressions.registerInstance;

public class ProcessMock {

private String processId;
private AbstractFlowNodeBuilder flowNodeBuilder;
protected String processId;
protected AbstractFlowNodeBuilder flowNodeBuilder;

public ProcessMock(String processId) {
this.processId = processId;
Expand Down Expand Up @@ -49,7 +49,8 @@ public ProcessMock onExecutionDo(final Consumer<DelegateExecution> consumer) {
}

public ProcessMock onExecutionDo(final String serviceId, final Consumer<DelegateExecution> consumer) {
flowNodeBuilder = flowNodeBuilder.serviceTask(serviceId)
flowNodeBuilder = flowNodeBuilder
.serviceTask(serviceId)
.camundaDelegateExpression("${id}".replace("id", serviceId));

registerInstance(serviceId, (JavaDelegate)execution -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
}

}
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> variables = runtimeService().getVariables(processInstance.getId());
Expand All @@ -57,25 +58,18 @@ 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<String, Object> variables = runtimeService().getVariables(processInstance.getId());
assertThat(variables).hasSize(1);
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)
Expand All @@ -100,11 +94,16 @@ 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<String, Object> variables = runtimeService().getVariables(processInstance.getId());
assertThat(variables).hasSize(2);
assertThat(variables).containsEntry("foo", "bar");
assertThat(variables).containsEntry("bar", "foo");
}

private ProcessInstance startProcess(final String key) {
return rule.getRuntimeService().startProcessInstanceByKey(key);
}
}

0 comments on commit 9d58e0e

Please sign in to comment.