Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gclaussn committed May 21, 2024
1 parent 5491011 commit 12e53d2
Show file tree
Hide file tree
Showing 36 changed files with 571 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,23 @@ public class CallActivityHandler {

private Consumer<String> processIdConsumer;

public CallActivityHandler(String elementId) {
if (elementId == null) {
throw new IllegalArgumentException("element ID is null");
}

element = new CallActivityElement();
element.id = elementId;
}

public CallActivityHandler(CallActivityElement element) {
if (element == null) {
throw new IllegalArgumentException("element is null");
}
if (element.id == null) {
throw new IllegalArgumentException("element ID is null");
}

this.element = element;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,23 @@ public class CustomMultiInstanceHandler {

private Boolean expectedSequential;

public CustomMultiInstanceHandler(String elementId) {
if (elementId == null) {
throw new IllegalArgumentException("element ID is null");
}

element = new MultiInstanceElement();
element.id = elementId;
}

public CustomMultiInstanceHandler(MultiInstanceElement element) {
if (element == null) {
throw new IllegalArgumentException("element is null");
}
if (element.id == null) {
throw new IllegalArgumentException("element ID is null");
}

this.element = element;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,23 @@ public class JobHandler {
private Consumer<Integer> retriesConsumer;
private Consumer<String> typeConsumer;

public JobHandler(String elementId) {
if (elementId == null) {
throw new IllegalArgumentException("element ID is null");
}

element = new JobElement();
element.id = elementId;
}

public JobHandler(JobElement element) {
if (element == null) {
throw new IllegalArgumentException("element is null");
}
if (element.id == null) {
throw new IllegalArgumentException("element ID is null");
}

this.element = element;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,25 @@ public class MessageEventHandler {
private Consumer<String> correlationKeyConsumer;
private Consumer<String> messageNameConsumer;

public MessageEventHandler(String elementId) {
if (elementId == null) {
throw new IllegalArgumentException("element ID is null");
}

element = new MessageEventElement();
element.id = elementId;

correlate();
}

public MessageEventHandler(MessageEventElement element) {
if (element == null) {
throw new IllegalArgumentException("element is null");
}
if (element.id == null) {
throw new IllegalArgumentException("element ID is null");
}

this.element = element;

correlate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,25 @@ public class SignalEventHandler {

private Consumer<String> signalNameConsumer;

public SignalEventHandler(String elementId) {
if (elementId == null) {
throw new IllegalArgumentException("element ID is null");
}

element = new SignalEventElement();
element.id = elementId;

broadcast();
}

public SignalEventHandler(SignalEventElement element) {
if (element == null) {
throw new IllegalArgumentException("element is null");
}
if (element.id == null) {
throw new IllegalArgumentException("element ID is null");
}

this.element = element;

broadcast();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.camunda.community.bpmndt.api;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.camunda.community.bpmndt.api.TestCaseInstanceMemo.JobMemo;
import org.camunda.community.bpmndt.api.TestCaseInstanceMemo.MessageSubscriptionMemo;
Expand All @@ -21,7 +23,9 @@
import io.camunda.zeebe.protocol.record.ValueType;
import io.camunda.zeebe.protocol.record.intent.IncidentIntent;
import io.camunda.zeebe.protocol.record.intent.ProcessInstanceIntent;
import io.camunda.zeebe.protocol.record.value.BpmnElementType;
import io.camunda.zeebe.protocol.record.value.IncidentRecordValue;
import io.camunda.zeebe.protocol.record.value.ProcessInstanceRecordValue;

/**
* Link between a test case and its execution, utilizing a process instance that was instantiated by a {@link TestCaseExecutor} and handlers (e.g.
Expand Down Expand Up @@ -118,7 +122,7 @@ public void hasPassed(long processInstanceKey, String elementId) {
});

if (!hasPassed) {
var message = withIncidents("expected process instance %d to have passed BPMN element %s, but has not", processInstanceKey);
var message = withDetails("expected process instance %d to have passed BPMN element %s, but has not", processInstanceKey);
throw new AssertionError(String.format(message, processInstanceKey, elementId));
}
}
Expand All @@ -135,7 +139,7 @@ public void hasPassedMultiInstance(long processInstanceKey, String elementId) {
});

if (!hasPassed) {
var message = withIncidents("expected process instance %d to have passed BPMN multi instance element %s, but has not", processInstanceKey);
var message = withDetails("expected process instance %d to have passed BPMN multi instance element %s, but has not", processInstanceKey);
throw new AssertionError(String.format(message, processInstanceKey, elementId));
}
}
Expand All @@ -152,7 +156,7 @@ public void hasTerminated(long processInstanceKey, String bpmnElementId) {
});

if (!hasTerminated) {
var message = withIncidents("expected process instance %d to have terminated BPMN element %s, but has not", processInstanceKey);
var message = withDetails("expected process instance %d to have terminated BPMN element %s, but has not", processInstanceKey);
throw new AssertionError(String.format(message, processInstanceKey, bpmnElementId));
}
}
Expand All @@ -168,7 +172,7 @@ public void isCompleted(long processInstanceKey) {
});

if (!isCompleted) {
String message = withIncidents("expected process instance %d to be completed, but was not", processInstanceKey);
String message = withDetails("expected process instance %d to be completed, but was not", processInstanceKey);
throw new AssertionError(String.format(message, processInstanceKey));
}
}
Expand Down Expand Up @@ -208,21 +212,21 @@ ProcessInstanceMemo getCalledProcessInstance(long processInstanceKey, String cal
return select(memo -> {
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
var message = "process instance %d could not be found";
var message = withDetails("process instance %d could not be found", processInstanceKey);
throw new IllegalStateException(String.format(message, processInstanceKey));
}

var callActivity = processInstance.getElement(callActivityId);
if (callActivity == null) {
var message = "call activity %s of process instance %d could not be found";
var message = withDetails("call activity %s of process instance %d could not be found", processInstanceKey);
throw new IllegalStateException(String.format(message, callActivityId, processInstanceKey));
}

return memo.getProcessInstances().stream()
.filter(pi -> pi.parentElementInstanceKey == callActivity.key)
.findFirst()
.orElseThrow(() -> {
var message = "call activity %s of process instance %d has not called a process";
var message = withDetails("call activity %s of process instance %d has not called a process", processInstanceKey);
return new IllegalStateException(String.format(message, callActivityId, processInstanceKey));
});
});
Expand All @@ -232,13 +236,13 @@ JobMemo getJob(long processInstanceKey, String elementId) {
return select(memo -> {
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
var message = "process instance %d could not be found";
var message = withDetails("process instance %d could not be found", processInstanceKey);
throw new IllegalStateException(String.format(message, processInstanceKey));
}

var job = processInstance.getJob(elementId);
if (job == null) {
var message = "job %s could not be found";
var message = withDetails("job %s could not be found", processInstanceKey);
throw new IllegalStateException(String.format(message, elementId));
}

Expand All @@ -250,13 +254,13 @@ MessageSubscriptionMemo getMessageSubscription(long processInstanceKey, String e
return select(memo -> {
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
var message = "process instance %d could not be found";
var message = withDetails("process instance %d could not be found", processInstanceKey);
throw new IllegalStateException(String.format(message, processInstanceKey));
}

var messageSubscription = processInstance.getMessageSubscription(elementId);
if (messageSubscription == null) {
var message = "message subscription %s could not be found";
var message = withDetails("message subscription %s could not be found", processInstanceKey);
throw new IllegalStateException(String.format(message, elementId));
}

Expand All @@ -268,15 +272,15 @@ SignalSubscriptionMemo getSignalSubscription(long processInstanceKey, String ele
return select(memo -> {
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
var message = "process instance %d could not be found";
var message = withDetails("process instance %d could not be found", processInstanceKey);
throw new IllegalStateException(String.format(message, processInstanceKey));
}

return memo.getSignalSubscriptions().stream()
.filter(signalSubscription -> signalSubscription.catchEventId.equals(elementId))
.findFirst()
.orElseThrow(() -> {
var message = "element %s of process instance %d has no signal subscription";
var message = withDetails("element %s of process instance %d has no signal subscription", processInstanceKey);
return new IllegalStateException(String.format(message, elementId, processInstanceKey));
});
});
Expand All @@ -286,13 +290,13 @@ TimerMemo getTimer(long processInstanceKey, String elementId) {
return select(memo -> {
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
var message = "process instance %d could not be found";
var message = withDetails("process instance %d could not be found", processInstanceKey);
throw new IllegalStateException(String.format(message, processInstanceKey));
}

var timer = processInstance.getTimer(elementId);
if (timer == null) {
var message = "timer %s could not be found";
var message = withDetails("timer %s could not be found", processInstanceKey);
throw new IllegalStateException(String.format(message, elementId));
}

Expand Down Expand Up @@ -391,6 +395,10 @@ private boolean selectAndTest(Predicate<TestCaseInstanceMemo> predicate) {
return selectAndTestTask == null;
}

private String withDetails(String message, long processInstanceKey) {
return withIncidents(withWaitStates(message, processInstanceKey), processInstanceKey);
}

private String withIncidents(String message, long processInstanceKey) {
var incidents = new LinkedList<IncidentRecordValue>();

Expand Down Expand Up @@ -423,6 +431,44 @@ private String withIncidents(String message, long processInstanceKey) {
return messageBuilder.toString();
}

private String withWaitStates(String message, long processInstanceKey) {
var elements = new LinkedHashMap<String, ProcessInstanceIntent>();

var recordStream = RecordStream.of(engine.getRecordStreamSource());
for (Record<?> record : recordStream.records()) {
if (record.getValueType() != ValueType.PROCESS_INSTANCE) {
continue;
}

var recordValue = (ProcessInstanceRecordValue) record.getValue();
if (recordValue.getBpmnElementType() == BpmnElementType.PROCESS || recordValue.getProcessInstanceKey() != processInstanceKey) {
continue;
}

var state = (ProcessInstanceIntent) record.getIntent();

elements.put(recordValue.getElementId(), state);
}

var waitStates = elements.keySet().stream()
.filter(k -> elements.get(k) == ProcessInstanceIntent.ELEMENT_ACTIVATED)
.collect(Collectors.toList());

if (waitStates.isEmpty()) {
return message;
}

var messageBuilder = new StringBuilder(message);
messageBuilder.append("\nfound active elements:");

for (String waitState : waitStates) {
messageBuilder.append("\n - ");
messageBuilder.append(waitState);
}

return messageBuilder.toString();
}

private static class SelectTask<T> {

final Function<TestCaseInstanceMemo, T> selector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,23 @@ public class TimerEventHandler {
private Consumer<LocalDateTime> timeDateConsumer;
private Consumer<Duration> timeDurationConsumer;

public TimerEventHandler(String elementId) {
if (elementId == null) {
throw new IllegalArgumentException("element ID is null");
}

element = new TimerEventElement();
element.id = elementId;
}

public TimerEventHandler(TimerEventElement element) {
if (element == null) {
throw new IllegalArgumentException("element is null");
}
if (element.id == null) {
throw new IllegalArgumentException("element ID is null");
}

this.element = element;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,25 @@ public class UserTaskHandler {
private Consumer<String> followUpDateConsumer;
private Consumer<String> formKeyConsumer;

public UserTaskHandler(String elementId) {
if (elementId == null) {
throw new IllegalArgumentException("element ID is null");
}

element = new UserTaskElement();
element.id = elementId;

complete();
}

public UserTaskHandler(UserTaskElement element) {
if (element == null) {
throw new IllegalArgumentException("element is null");
}
if (element.id == null) {
throw new IllegalArgumentException("element ID is null");
}

this.element = element;

complete();
Expand Down
Loading

0 comments on commit 12e53d2

Please sign in to comment.