Skip to content

Instantly share code, notes, and snippets.

View berndruecker's full-sized avatar

Bernd Ruecker berndruecker

View GitHub Profile
@berndruecker
berndruecker / Order.java
Last active March 13, 2017 06:17
Order entity with state information
class Order {
String id;
Customer customer;
List<OrderItem> items;
static enum GoodsDeliveryStatus {
NOTHING_DONE,
GOODS_RESERVED,
GOODS_PICKED
}
class OrderSaga {
String orderId;
static enum GoodsDeliveryStatus {
NOTHING_DONE,
GOODS_RESERVED,
GOODS_PICKED
}
boolean paymentReceived = false;
GoodsDeliveryStatus deliveryStatus = GoodsDeliveryStatus.NOTHING_DONE;
if (event.equals("order placed")) {
issueCommand("do payment");
}
engine.getRepositoryService().createDeployment() //
.addModelInstance("order.bpmn", Bpmn.createProcess("order").executable() //
.startEvent()
.serviceTask().name("A")
.parallelGateway("fork1")
.serviceTask().name("B")
.parallelGateway("join2")
.moveToNode("fork1")
.serviceTask().name("C")
.parallelGateway("fork2")
public boolean handleEvent(String type, String eventName, String transactionId, JsonObject event) {
VariableMap correlationKeys = getCorrelationKeys(event);
MessageCorrelationBuilder correlation = engine.getRuntimeService().createMessageCorrelation(eventName);
ExecutionQuery query = engine.getRuntimeService().createExecutionQuery().messageEventSubscriptionName(eventName);
for (String key : correlationKeys.keySet()) {
correlation.processInstanceVariableEquals(key, correlationKeys.get(key));
query.processVariableValueEquals(key, correlationKeys.get(key));
}
deploymentBuilder
.addModelInstance("order.bpmn", Bpmn.createExecutableProcess("Order")
.startEvent()
.businessRuleTask().name("Determine risk of fraud").camundaDecisionRef("RiskyOrder")
.camundaResultVariable("riskyOrder").camundaMapDecisionResult("singleEntry")
.serviceTask().name("Do payment").camundaClass(DoPaymentAdapter.class).camundaAsyncBefore()
.sendTask() .name("Initiate delivery").camundaClass(InitiateDeliveryAdapter.class)
.receiveTask().name("Wait for delivery").message("MessageDeliveryDone")
.endEvent()
.done()
@Test
@Deployment(resources = { "Order.bpmn", "RiskyOrder.dmn" })
public void testHappyPath() {
when(orderFlow.waitsAtUserTask("UserTask_ApproveOrder")) //
.thenReturn((task) -> task.complete(Variables.putValue("approved", true)));
mockRestServer
.expect(requestTo("https://api.stripe.com"))
// normal mocking...
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE, //
classes = TestApplication.class, //
properties = { //
"camunda.bpm.job-execution.enabled=false", //
"camunda.bpm.auto-deployment-enabled=false"})
@Deployment(resources = {"..."})
public class SomeScenarioTest {
//...
public class RetrievePaymentAdapter implements JavaDelegate {
public void execute(ActivityExecution ctx) {
// prepare message content, you can load variables from persistent flow context
ctx.getVariable("orderId");
// ...
// Send command
publishCommand("DoPayment", payload);
// ant tell the engine to wait for the next event
public class RetrievePaymentRestAdapter implements JavaDelegate {
@Inject private OrderRepository orderRepository; // e.g. using Spring
@Inject private RestTemplate rest; // e.g. using Spring
public void execute(DelegateExecution context) throws Exception {
// Input mapping
String orderId = context.getVariable("orderId");
Order o = orderRepository.findOrder(orderId);
CreateChargeRequest request = new CreateChargeRequest();
request.setAmount( o.getAmount() );