Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gclaussn committed May 12, 2024
1 parent f4471fc commit a2912ed
Show file tree
Hide file tree
Showing 39 changed files with 422 additions and 259 deletions.
36 changes: 25 additions & 11 deletions impl-8/src/main/java/org/camunda/community/bpmndt/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@

import org.camunda.community.bpmndt.api.AbstractJUnit5TestCase;
import org.camunda.community.bpmndt.api.AbstractTestCase;
import org.camunda.community.bpmndt.api.CallActivityHandler;
import org.camunda.community.bpmndt.api.CustomMultiInstanceHandler;
import org.camunda.community.bpmndt.api.JobHandler;
import org.camunda.community.bpmndt.api.MessageEventHandler;
import org.camunda.community.bpmndt.api.SignalEventHandler;
import org.camunda.community.bpmndt.api.TestCaseExecutor;
import org.camunda.community.bpmndt.api.TestCaseInstance;
import org.camunda.community.bpmndt.api.TestCaseInstanceElement;
import org.camunda.community.bpmndt.api.TestCaseInstanceMemo;
import org.camunda.community.bpmndt.api.TimerEventHandler;
import org.camunda.community.bpmndt.api.UserTaskHandler;
import org.camunda.community.bpmndt.cmd.BuildTestCaseContext;
import org.camunda.community.bpmndt.cmd.CollectBpmnFiles;
import org.camunda.community.bpmndt.cmd.DeleteTestSources;
import org.camunda.community.bpmndt.cmd.GenerateSimulateSubProcessResource;
import org.camunda.community.bpmndt.cmd.GenerateTestCase;
import org.camunda.community.bpmndt.cmd.WriteJavaFile;
import org.camunda.community.bpmndt.cmd.WriteJavaType;
Expand Down Expand Up @@ -47,7 +53,7 @@ public void generate(GeneratorContext ctx) {
var bpmnFiles = new CollectBpmnFiles().apply(ctx.getMainResourcePath());
for (Path bpmnFile : bpmnFiles) {
var relativePath = ctx.getMainResourcePath().relativize(bpmnFile).toString().replace('\\', '/');
LOGGER.info("Found BPMN file: {}", relativePath);
LOGGER.info("found BPMN file: {}", relativePath);
}

// generate test cases for each BPMN file
Expand All @@ -57,19 +63,22 @@ public void generate(GeneratorContext ctx) {
generateTestCases(ctx, bpmnFile);
}

// generate simulate sub process resource class
new GenerateSimulateSubProcessResource(result).accept(ctx);

LOGGER.info("");

var writeJavaFile = new WriteJavaFile(ctx);

// write test cases
LOGGER.info("Writing test cases");
LOGGER.info("writing test cases");
result.getFiles().forEach(writeJavaFile);

if (!result.getAdditionalFiles().isEmpty()) {
LOGGER.info("");

// write additional classes
LOGGER.info("Writing additional classes");
LOGGER.info("writing additional classes");
result.getAdditionalFiles().forEach(writeJavaFile);
}

Expand All @@ -79,17 +88,22 @@ public void generate(GeneratorContext ctx) {

apiClasses.add(AbstractJUnit5TestCase.class);
apiClasses.add(AbstractTestCase.class);
apiClasses.add(CallActivityHandler.class);
apiClasses.add(CustomMultiInstanceHandler.class);
apiClasses.add(JobHandler.class);
apiClasses.add(MessageEventHandler.class);
apiClasses.add(SignalEventHandler.class);
apiClasses.add(TestCaseExecutor.class);
apiClasses.add(TestCaseInstance.class);
apiClasses.add(TestCaseInstanceElement.class);
apiClasses.add(TestCaseInstanceMemo.class);
apiClasses.add(TimerEventHandler.class);
apiClasses.add(UserTaskHandler.class);

var writeJavaType = new WriteJavaType(ctx);

// write API classes
LOGGER.info("Writing API classes");
LOGGER.info("writing API classes");
apiClasses.forEach(writeJavaType);
}

Expand All @@ -98,15 +112,15 @@ protected void generateTestCases(GeneratorContext gCtx, Path bpmnFile) {
var testCases = TestCases.of(bpmnFile);

for (String processId : testCases.getProcessIds()) {
LOGGER.info("Process: {}", processId);
LOGGER.info("process: {}", processId);

generateTestCases(gCtx, bpmnFile, testCases.get(processId));
}
}

protected void generateTestCases(GeneratorContext gCtx, Path bpmnFile, List<TestCase> testCases) {
if (testCases.isEmpty()) {
LOGGER.info("No test cases defined");
LOGGER.info("no test cases defined");
return;
}

Expand All @@ -118,27 +132,27 @@ protected void generateTestCases(GeneratorContext gCtx, Path bpmnFile, List<Test

// check for invalid test cases
if (testCase.hasEmptyPath()) {
LOGGER.error("Test case #{} has an empty path", i + 1);
LOGGER.error("test case #{} has an empty path", i + 1);
continue;
}
if (testCase.hasIncompletePath()) {
LOGGER.error("Test case #{} has an incomplete path", i + 1);
LOGGER.error("test case #{} has an incomplete path", i + 1);
continue;
}
if (testCase.hasInvalidPath()) {
LOGGER.error("Test case #{} has an invalid path - invalid element IDs: {}", i + 1, testCase.getInvalidElementIds());
LOGGER.error("test case #{} has an invalid path - invalid element IDs: {}", i + 1, testCase.getInvalidElementIds());
continue;
}

var ctx = buildTestCaseContext.apply(testCases.get(i));

// check for duplicate test case names
if (ctx.hasDuplicateName()) {
LOGGER.warn("Skipping test case #{}: name '{}' must be unique", i + 1, ctx.getName());
LOGGER.warn("skipping test case #{}: name '{}' must be unique", i + 1, ctx.getName());
continue;
}

LOGGER.info("Generating test case '{}'", ctx.getName());
LOGGER.info("generating test case '{}'", ctx.getName());
generate.accept(ctx);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ protected String getBpmnResourceName() {
*/
protected String getSimulateSubProcessResource() {
try (InputStream resource = this.getClass().getResourceAsStream("/simulate-sub-process.bpmn")) {
return new BufferedReader(new InputStreamReader(Objects.requireNonNull(resource)))
.lines()
.collect(Collectors.joining("\n"));
return new BufferedReader(new InputStreamReader(Objects.requireNonNull(resource))).lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
throw new RuntimeException("failed to read simulate sub process BPMN resource", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class OutboundConnectorHandler {

public OutboundConnectorHandler() {

// TODO
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void execute(long processInstanceKey) {
}

if (!exists) {
throw new IllegalArgumentException(String.format("no process instance with key %d found", processInstanceKey));
throw new IllegalArgumentException(String.format("process instance %d could not be found", processInstanceKey));
}

try (ZeebeClient client = createClient()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,43 @@ public ZeebeTestEngine getEngine() {
return engine;
}

public void hasPassed(long processInstanceKey, String bpmnElementId) {
public void hasPassed(long processInstanceKey, String elementId) {
boolean hasPassed = selectAndTest(memo -> {
var processInstance = memo.processInstances.get(processInstanceKey);
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
return false;
}

var element = processInstance.getElement(bpmnElementId);
var element = processInstance.getElement(elementId);
return element != null && element.state == ProcessInstanceIntent.ELEMENT_COMPLETED;
});

if (!hasPassed) {
var message = withIncidents("expected process instance %d to have passed BPMN element %s, but has not", processInstanceKey);
throw new AssertionError(String.format(message, processInstanceKey, bpmnElementId));
throw new AssertionError(String.format(message, processInstanceKey, elementId));
}
}

public void hasPassedMultiInstance(long processInstanceKey, String elementId) {
boolean hasPassed = selectAndTest(memo -> {
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
return false;
}

var element = processInstance.getMultiInstance(elementId);
return element != null && element.state == ProcessInstanceIntent.ELEMENT_COMPLETED;
});

if (!hasPassed) {
var message = withIncidents("expected process instance %d to have passed BPMN multi instance element %s, but has not", processInstanceKey);
throw new AssertionError(String.format(message, processInstanceKey, elementId));
}
}

public void hasTerminated(long processInstanceKey, String bpmnElementId) {
boolean hasTerminated = selectAndTest(memo -> {
var processInstance = memo.processInstances.get(processInstanceKey);
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
return false;
}
Expand All @@ -140,7 +157,7 @@ public void hasTerminated(long processInstanceKey, String bpmnElementId) {

public void isCompleted(long processInstanceKey) {
boolean isCompleted = selectAndTest(memo -> {
var processInstance = memo.processInstances.get(processInstanceKey);
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
return false;
}
Expand All @@ -154,14 +171,14 @@ public void isCompleted(long processInstanceKey) {
}
}

public void isWaitingAt(long processInstanceKey, String bpmnElementId) {
public void isWaitingAt(long processInstanceKey, String elementId) {
boolean isWaitingAt = selectAndTest(memo -> {
var processInstance = memo.processInstances.get(processInstanceKey);
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
return false;
}

var element = processInstance.getElement(bpmnElementId);
var element = processInstance.getElement(elementId);
if (element == null) {
return false;
}
Expand All @@ -171,7 +188,7 @@ public void isWaitingAt(long processInstanceKey, String bpmnElementId) {

// special check for elements with a task definition
// needed because a job worker could already have completed or terminated the related job
var job = processInstance.getJob(bpmnElementId);
var job = processInstance.getJob(elementId);
if (job == null) {
return false;
}
Expand All @@ -181,13 +198,13 @@ public void isWaitingAt(long processInstanceKey, String bpmnElementId) {

if (!isWaitingAt) {
var message = withIncidents("expected process instance %d to be waiting at BPMN element %s, but was not", processInstanceKey);
throw new AssertionError(String.format(message, processInstanceKey, bpmnElementId));
throw new AssertionError(String.format(message, processInstanceKey, elementId));
}
}

ProcessInstanceMemo getCalledProcessInstance(long processInstanceKey, String callActivityId) {
return select(memo -> {
var processInstance = memo.processInstances.get(processInstanceKey);
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
var message = "process instance %d could not be found";
throw new IllegalStateException(String.format(message, processInstanceKey));
Expand All @@ -199,7 +216,7 @@ ProcessInstanceMemo getCalledProcessInstance(long processInstanceKey, String cal
throw new IllegalStateException(String.format(message, callActivityId, processInstanceKey));
}

return memo.processInstances.values().stream()
return memo.getProcessInstances().stream()
.filter(pi -> pi.parentElementInstanceKey == callActivity.key)
.findFirst()
.orElseThrow(() -> {
Expand All @@ -209,78 +226,78 @@ ProcessInstanceMemo getCalledProcessInstance(long processInstanceKey, String cal
});
}

JobMemo getJob(long processInstanceKey, String bpmnElementId) {
JobMemo getJob(long processInstanceKey, String elementId) {
return select(memo -> {
var processInstance = memo.processInstances.get(processInstanceKey);
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
var message = "process instance %d could not be found";
throw new IllegalStateException(String.format(message, processInstanceKey));
}

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

return job;
});
}

MessageSubscriptionMemo getMessageSubscription(long processInstanceKey, String bpmnElementId) {
MessageSubscriptionMemo getMessageSubscription(long processInstanceKey, String elementId) {
return select(memo -> {
var processInstance = memo.processInstances.get(processInstanceKey);
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
var message = "process instance %d could not be found";
throw new IllegalStateException(String.format(message, processInstanceKey));
}

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

return messageSubscription;
});
}

SignalSubscriptionMemo getSignalSubscription(long processInstanceKey, String bpmnElementId) {
SignalSubscriptionMemo getSignalSubscription(long processInstanceKey, String elementId) {
return select(memo -> {
var processInstance = memo.processInstances.get(processInstanceKey);
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
var message = "process instance %d could not be found";
throw new IllegalStateException(String.format(message, processInstanceKey));
}

var element = processInstance.getElement(bpmnElementId);
var element = processInstance.getElement(elementId);
if (element == null || element.state != ProcessInstanceIntent.ELEMENT_ACTIVATED) {
var message = "element %s of process instance %d has not been activated";
throw new IllegalStateException(String.format(message, bpmnElementId, processInstanceKey));
throw new IllegalStateException(String.format(message, elementId, processInstanceKey));
}

return memo.signalSubscriptions.stream()
return memo.getSignalSubscriptions().stream()
.filter(signalSubscription -> signalSubscription.catchEventInstanceKey == element.key)
.findFirst()
.orElseThrow(() -> {
var message = "element %s of process instance %d has no signal subscription";
return new IllegalStateException(String.format(message, bpmnElementId, processInstanceKey));
return new IllegalStateException(String.format(message, elementId, processInstanceKey));
});
});
}

TimerMemo getTimer(long processInstanceKey, String bpmnElementId) {
TimerMemo getTimer(long processInstanceKey, String elementId) {
return select(memo -> {
var processInstance = memo.processInstances.get(processInstanceKey);
var processInstance = memo.getProcessInstance(processInstanceKey);
if (processInstance == null) {
var message = "process instance %d could not be found";
throw new IllegalStateException(String.format(message, processInstanceKey));
}

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

return timer;
Expand Down
Loading

0 comments on commit a2912ed

Please sign in to comment.