From 600a035e69b938f7896c1d6fc964625dbde94be6 Mon Sep 17 00:00:00 2001 From: Jonathan Lukas Date: Fri, 18 Oct 2024 14:12:15 +0200 Subject: [PATCH] new snippet: external-task-client-testing --- .../external-task-client-testing/README.md | 9 ++ snippets/external-task-client-testing/pom.xml | 90 +++++++++++++++++++ .../com/camunda/consulting/example/App.java | 11 +++ .../consulting/example/ExampleWorker.java | 23 +++++ .../src/main/resources/example-process.bpmn | 44 +++++++++ .../camunda/consulting/example/AppTest.java | 35 ++++++++ .../consulting/example/ExampleWorkerTest.java | 26 ++++++ .../consulting/example/ProcessUnitTest.java | 23 +++++ .../src/test/resources/application.yaml | 17 ++++ 9 files changed, 278 insertions(+) create mode 100644 snippets/external-task-client-testing/README.md create mode 100644 snippets/external-task-client-testing/pom.xml create mode 100644 snippets/external-task-client-testing/src/main/java/com/camunda/consulting/example/App.java create mode 100644 snippets/external-task-client-testing/src/main/java/com/camunda/consulting/example/ExampleWorker.java create mode 100644 snippets/external-task-client-testing/src/main/resources/example-process.bpmn create mode 100644 snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/AppTest.java create mode 100644 snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/ExampleWorkerTest.java create mode 100644 snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/ProcessUnitTest.java create mode 100644 snippets/external-task-client-testing/src/test/resources/application.yaml diff --git a/snippets/external-task-client-testing/README.md b/snippets/external-task-client-testing/README.md new file mode 100644 index 000000000..268f8984c --- /dev/null +++ b/snippets/external-task-client-testing/README.md @@ -0,0 +1,9 @@ +# External Task Worker Testing + +This example shows 3 ways to test external tasks: + +1. Test the worker: Mock an `ExternalTask` and `ExternalTaskService` and verify the interaction with these mocked objects. +2. Test the process: Run the engine, but disable the worker and complete external tasks with the java api. +3. Test everything together: Run the engine and the worker and test everything together. + +For all tests, we have the java apis at hand. \ No newline at end of file diff --git a/snippets/external-task-client-testing/pom.xml b/snippets/external-task-client-testing/pom.xml new file mode 100644 index 000000000..385840ea3 --- /dev/null +++ b/snippets/external-task-client-testing/pom.xml @@ -0,0 +1,90 @@ + + + 4.0.0 + + io.camunda + external-task-client-testing + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + 2.7.0 + + + + + + org.camunda.bpm + camunda-bom + 7.22.0 + import + pom + + + org.springframework.boot + spring-boot-dependencies + 3.3.4 + import + pom + + + org.camunda.community.process_test_coverage + camunda-process-test-coverage-starter-platform-7 + ${version.process-test-coverage-extension} + + + + + + + org.camunda.bpm.springboot + camunda-bpm-spring-boot-starter-external-task-client + + + org.springframework.boot + spring-boot-starter-test + test + + + org.camunda.bpm.springboot + camunda-bpm-spring-boot-starter-rest + test + + + org.camunda.bpm + camunda-bpm-junit5 + test + + + org.camunda.bpm + camunda-bpm-assert + test + + + org.springframework.boot + spring-boot-starter-jdbc + test + + + com.h2database + h2 + test + + + org.awaitility + awaitility + test + + + org.camunda.community.process_test_coverage + camunda-process-test-coverage-starter-platform-7 + test + + + + \ No newline at end of file diff --git a/snippets/external-task-client-testing/src/main/java/com/camunda/consulting/example/App.java b/snippets/external-task-client-testing/src/main/java/com/camunda/consulting/example/App.java new file mode 100644 index 000000000..42ac26bc8 --- /dev/null +++ b/snippets/external-task-client-testing/src/main/java/com/camunda/consulting/example/App.java @@ -0,0 +1,11 @@ +package com.camunda.consulting.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class App { + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } +} diff --git a/snippets/external-task-client-testing/src/main/java/com/camunda/consulting/example/ExampleWorker.java b/snippets/external-task-client-testing/src/main/java/com/camunda/consulting/example/ExampleWorker.java new file mode 100644 index 000000000..65ea79602 --- /dev/null +++ b/snippets/external-task-client-testing/src/main/java/com/camunda/consulting/example/ExampleWorker.java @@ -0,0 +1,23 @@ +package com.camunda.consulting.example; + +import org.camunda.bpm.client.spring.annotation.ExternalTaskSubscription; +import org.camunda.bpm.client.task.ExternalTask; +import org.camunda.bpm.client.task.ExternalTaskHandler; +import org.camunda.bpm.client.task.ExternalTaskService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +@Component +@Profile("!unitTest") +@ExternalTaskSubscription(topicName = "exampleWork") +public class ExampleWorker implements ExternalTaskHandler { + private static final Logger LOG = LoggerFactory.getLogger(ExampleWorker.class); + + @Override + public void execute(ExternalTask externalTask, ExternalTaskService externalTaskService) { + LOG.info("Executing exampleWork"); + externalTaskService.complete(externalTask); + } +} diff --git a/snippets/external-task-client-testing/src/main/resources/example-process.bpmn b/snippets/external-task-client-testing/src/main/resources/example-process.bpmn new file mode 100644 index 000000000..132670cec --- /dev/null +++ b/snippets/external-task-client-testing/src/main/resources/example-process.bpmn @@ -0,0 +1,44 @@ + + + + + Flow_1ecdqtr + + + + Flow_1ecdqtr + Flow_180z8iz + + + Flow_180z8iz + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/AppTest.java b/snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/AppTest.java new file mode 100644 index 000000000..3d32c68ce --- /dev/null +++ b/snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/AppTest.java @@ -0,0 +1,35 @@ +package com.camunda.consulting.example; + +import org.camunda.bpm.engine.ProcessEngines; +import org.camunda.bpm.engine.RuntimeService; +import org.camunda.bpm.engine.runtime.ProcessInstance; +import org.camunda.community.process_test_coverage.core.engine.ExcludeFromProcessCoverage; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; + +import java.time.Duration; + +import static org.awaitility.Awaitility.*; +import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareTests.*; + +/** + * Here, the interaction between the (remote) engine and running workers is tested + */ +@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) +public class AppTest { + + @Autowired + RuntimeService runtimeService; + + @Test + public void shouldRun() { + ProcessInstance exampleProcess = runtimeService.startProcessInstanceByKey("ExampleProcess"); + await() + .timeout(Duration.ofSeconds(60)) + .untilAsserted(() -> assertThat(exampleProcess).isEnded()); + } +} diff --git a/snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/ExampleWorkerTest.java b/snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/ExampleWorkerTest.java new file mode 100644 index 000000000..9ec0ddb26 --- /dev/null +++ b/snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/ExampleWorkerTest.java @@ -0,0 +1,26 @@ +package com.camunda.consulting.example; + +import org.camunda.bpm.client.task.ExternalTask; +import org.camunda.bpm.client.task.ExternalTaskService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.mockito.Mockito.*; + +@SpringBootTest(classes = ExampleWorker.class) +public class ExampleWorkerTest { + @Autowired + ExampleWorker exampleWorker; + + @Test + void shouldInvokeExampleWorker() { + // given + ExternalTask externalTask = mock(ExternalTask.class); + ExternalTaskService externalTaskService = mock(ExternalTaskService.class); + // when + exampleWorker.execute(externalTask, externalTaskService); + // then + verify(externalTaskService, times(1)).complete(externalTask); + } +} diff --git a/snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/ProcessUnitTest.java b/snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/ProcessUnitTest.java new file mode 100644 index 000000000..c738bf6a7 --- /dev/null +++ b/snippets/external-task-client-testing/src/test/java/com/camunda/consulting/example/ProcessUnitTest.java @@ -0,0 +1,23 @@ +package com.camunda.consulting.example; + +import org.camunda.bpm.engine.ProcessEngines; +import org.camunda.bpm.engine.runtime.ProcessInstance; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareTests.*; + +@SpringBootTest(properties = {"camunda.bpm.client.subscriptions.exampleWork.auto-open=false","camunda.bpm.job-execution.enabled=false"}) +@ActiveProfiles("unitTest") +public class ProcessUnitTest { + + @Test + public void shouldRunProcess() { + ProcessInstance exampleProcess = runtimeService().startProcessInstanceByKey("ExampleProcess"); + assertThat(exampleProcess).isWaitingAt(findId("Example work")); + complete(externalTask()); + assertThat(exampleProcess).isEnded(); + } +} diff --git a/snippets/external-task-client-testing/src/test/resources/application.yaml b/snippets/external-task-client-testing/src/test/resources/application.yaml new file mode 100644 index 000000000..a1ffc32b8 --- /dev/null +++ b/snippets/external-task-client-testing/src/test/resources/application.yaml @@ -0,0 +1,17 @@ +# This port is fixed, so it should not interfere with other ports +server: + port: 18081 +spring: + datasource: + url: jdbc:h2:mem:camunda +camunda: + bpm: + client: + base-url: http://localhost:${server.port}/engine-rest + disable-backoff-strategy: true +logging: + level: + org: + camunda: + bpm: + client: debug \ No newline at end of file