CreateWorkflowInstanceProcessor implements CommandProcessor<WorkflowInstanceCreationRecord> { @Override public boolean onCommand( final TypedRecord<WorkflowInstanceCreationRecord> command, final CommandControl<WorkflowInstanceCreationRecord> controller, final TypedStreamWriter streamWriter) { final WorkflowInstanceCreationRecord record = command.getValue(); final DeployedWorkflow workflow = getWorkflow(record, controller); if (workflow == null || !isValidWorkflow(controller, workflow)) { return true; } final long workflowInstanceKey = keyGenerator.nextKey(); if (!setVariablesFromDocument(controller, record, workflow.getKey(), workflowInstanceKey)) { return true; } final ElementInstance workflowInstance = createElementInstance(workflow, workflowInstanceKey); streamWriter.appendFollowUpEvent( workflowInstanceKey, WorkflowInstanceIntent.ELEMENT_ACTIVATING, workflowInstance.getValue()); record .setWorkflowInstanceKey(workflowInstanceKey) .setBpmnProcessId(workflow.getBpmnProcessId()) .setVersion(workflow.getVersion()) .setWorkflowKey(workflow.getKey()); controller.accept(WorkflowInstanceCreationIntent.CREATED, record); return true; } CreateWorkflowInstanceProcessor(
      final WorkflowState workflowState,
      final ElementInstanceState elementInstanceState,
      final VariablesState variablesState,
      final KeyGenerator keyGenerator); @Override boolean onCommand(
      final TypedRecord<WorkflowInstanceCreationRecord> command,
      final CommandControl<WorkflowInstanceCreationRecord> controller,
      final TypedStreamWriter streamWriter);  }
@Test public void shouldRejectIfNoBpmnProcessIdOrKeyGiven() { final TypedRecord<WorkflowInstanceCreationRecord> command = newCommand(WorkflowInstanceCreationRecord.class); processor.onCommand(command, controller, streamWriter); refuteAccepted(); assertRejected(RejectionType.INVALID_ARGUMENT); }
@Test public void shouldRejectIfNoWorkflowFoundByKey() { final TypedRecord<WorkflowInstanceCreationRecord> command = newCommand(WorkflowInstanceCreationRecord.class); command.getValue().setWorkflowKey(keyGenerator.nextKey()); processor.onCommand(command, controller, streamWriter); refuteAccepted(); assertRejected(RejectionType.NOT_FOUND); }
@Test public void shouldRejectIfNoWorkflowFoundByProcessId() { final TypedRecord<WorkflowInstanceCreationRecord> command = newCommand(WorkflowInstanceCreationRecord.class); command.getValue().setBpmnProcessId("workflow"); processor.onCommand(command, controller, streamWriter); refuteAccepted(); assertRejected(RejectionType.NOT_FOUND); }
@Test public void shouldRejectIfNoWorkflowFoundByProcessIdAndVersion() { final TypedRecord<WorkflowInstanceCreationRecord> command = newCommand(WorkflowInstanceCreationRecord.class); command.getValue().setBpmnProcessId("workflow").setVersion(1); processor.onCommand(command, controller, streamWriter); refuteAccepted(); assertRejected(RejectionType.NOT_FOUND); }
@Test public void shouldRejectIfWorkflowHasNoNoneStartEvent() { final BpmnModelInstance process = Bpmn.createExecutableProcess() .startEvent() .message(m -> m.name("message").zeebeCorrelationKeyExpression("key")) .endEvent() .done(); final DeployedWorkflow workflow = deployNewWorkflow(process); final TypedRecord<WorkflowInstanceCreationRecord> command = newCommand(WorkflowInstanceCreationRecord.class); command.getValue().setBpmnProcessId(workflow.getBpmnProcessId()); processor.onCommand(command, controller, streamWriter); refuteAccepted(); assertRejected(RejectionType.INVALID_STATE); }
@Test public void shouldRejectIfVariablesIsAnInvalidDocument() { final DeployedWorkflow workflow = deployNewWorkflow(); final TypedRecord<WorkflowInstanceCreationRecord> command = newCommand(WorkflowInstanceCreationRecord.class); final MutableDirectBuffer badDocument = new UnsafeBuffer(MsgPackUtil.asMsgPack("{ 'foo': 1 }")); command.getValue().setBpmnProcessId(workflow.getBpmnProcessId()).setVariables(badDocument); badDocument.putByte(0, (byte) 0); processor.onCommand(command, controller, streamWriter); refuteAccepted(); assertRejected(RejectionType.INVALID_ARGUMENT); }
@Test public void shouldActivateElementInstance() { final DirectBuffer variables = MsgPackUtil.asMsgPack(Maps.of(entry("foo", "bar"), entry("baz", "boz"))); final DeployedWorkflow workflow = deployNewWorkflow(); final TypedRecord<WorkflowInstanceCreationRecord> command = newCommand(WorkflowInstanceCreationRecord.class); command.getValue().setBpmnProcessId(workflow.getBpmnProcessId()).setVariables(variables); processor.onCommand(command, controller, streamWriter); final WorkflowInstanceCreationRecord acceptedRecord = getAcceptedRecord(WorkflowInstanceCreationIntent.CREATED); final long instanceKey = acceptedRecord.getWorkflowInstanceKey(); final ElementInstance instance = elementInstanceState.getInstance(instanceKey); final WorkflowInstanceRecord expectedElementActivatingRecord = newExpectedElementActivatingRecord(workflow, instanceKey); assertThat(instance.getState()).isEqualTo(WorkflowInstanceIntent.ELEMENT_ACTIVATING); assertThat(instance.getValue()).isEqualTo(expectedElementActivatingRecord); verifyElementActivatingPublished(instanceKey, instance); }
@Test public void shouldSetVariablesFromDocument() { final Map<String, Object> document = Maps.of(entry("foo", "bar"), entry("baz", "boz")); final DeployedWorkflow workflow = deployNewWorkflow(); final TypedRecord<WorkflowInstanceCreationRecord> command = newCommand(WorkflowInstanceCreationRecord.class); command .getValue() .setBpmnProcessId(workflow.getBpmnProcessId()) .setVariables(MsgPackUtil.asMsgPack(document)); processor.onCommand(command, controller, streamWriter); final WorkflowInstanceCreationRecord acceptedRecord = getAcceptedRecord(WorkflowInstanceCreationIntent.CREATED); final long scopeKey = acceptedRecord.getWorkflowInstanceKey(); MsgPackUtil.assertEquality( variablesState.getVariableLocal(scopeKey, BufferUtil.wrapString("foo")), "\"bar\""); MsgPackUtil.assertEquality( variablesState.getVariableLocal(scopeKey, BufferUtil.wrapString("baz")), "\"boz\""); }
@Test public void shouldAcceptAndUpdateKey() { final DeployedWorkflow workflow = deployNewWorkflow(); final TypedRecord<WorkflowInstanceCreationRecord> command = newCommand(WorkflowInstanceCreationRecord.class); command.getValue().setBpmnProcessId(workflow.getBpmnProcessId()); processor.onCommand(command, controller, streamWriter); final WorkflowInstanceCreationRecord acceptedRecord = getAcceptedRecord(WorkflowInstanceCreationIntent.CREATED); assertThat(acceptedRecord.getWorkflowKey()).isEqualTo(workflow.getKey()); }
@Test public void shouldAcceptAndUpdateVersion() { final DeployedWorkflow workflow = deployNewWorkflow(); final TypedRecord<WorkflowInstanceCreationRecord> command = newCommand(WorkflowInstanceCreationRecord.class); command.getValue().setBpmnProcessId(workflow.getBpmnProcessId()); processor.onCommand(command, controller, streamWriter); final WorkflowInstanceCreationRecord acceptedRecord = getAcceptedRecord(WorkflowInstanceCreationIntent.CREATED); assertThat(acceptedRecord.getVersion()).isEqualTo(workflow.getVersion()); }
@Test public void shouldAcceptAndUpdateProcessId() { final DeployedWorkflow workflow = deployNewWorkflow(); final TypedRecord<WorkflowInstanceCreationRecord> command = newCommand(WorkflowInstanceCreationRecord.class); command.getValue().setWorkflowKey(workflow.getKey()); processor.onCommand(command, controller, streamWriter); final WorkflowInstanceCreationRecord acceptedRecord = getAcceptedRecord(WorkflowInstanceCreationIntent.CREATED); assertThat(acceptedRecord.getBpmnProcessIdBuffer()).isEqualTo(workflow.getBpmnProcessId()); }