-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: JUnit extension injects Zeebe client
If the test class has a field with the type of the Zeebe client then the JUnit extension injects a Zeebe client for the current test run. The injection can be used to start an external job worker or a process application for the test run.
- Loading branch information
Showing
5 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
junit-extension/src/main/kotlin/io/zeebe/bpmnspec/junit/SpecRunnerInjectCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.zeebe.bpmnspec.junit | ||
|
||
import io.camunda.zeebe.client.ZeebeClient | ||
import io.zeebe.bpmnspec.api.SpecTestRunnerContext | ||
import org.junit.jupiter.api.extension.BeforeEachCallback | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import org.junit.platform.commons.util.ExceptionUtils | ||
import org.junit.platform.commons.util.ReflectionUtils | ||
import java.lang.reflect.Field | ||
import kotlin.reflect.KClass | ||
|
||
class SpecRunnerInjectCallback : BeforeEachCallback { | ||
|
||
override fun beforeEach(context: ExtensionContext?) { | ||
context | ||
?.requiredTestInstances | ||
?.allInstances | ||
?.forEach { | ||
injectFields(context, it, it.javaClass) | ||
} | ||
} | ||
|
||
private fun injectFields(context: ExtensionContext, testInstance: Any, testClass: Class<*>) { | ||
|
||
val specTestRunnerContext = context | ||
.getStore(SpecRunnerProvider.extensionContextNamespace) | ||
.get(SpecRunnerProvider.extensionContextStoreKey, SpecTestRunnerContext::class.java) | ||
?: throw RuntimeException("Expect spec test runner context but not found") | ||
|
||
injectFields( | ||
specTestRunnerContext, | ||
testInstance, | ||
testClass, | ||
ZeebeClient::class | ||
) { it.getZeebeClient() } | ||
} | ||
|
||
private fun injectFields( | ||
specTestRunnerContext: SpecTestRunnerContext, | ||
testInstance: Any, | ||
testClass: Class<*>, | ||
fieldType: KClass<*>, | ||
fieldValue: (SpecTestRunnerContext) -> Any | ||
) { | ||
val fields = ReflectionUtils.findFields( | ||
testClass, | ||
{ field: Field -> ReflectionUtils.isNotStatic(field) && field.type == fieldType.java }, | ||
ReflectionUtils.HierarchyTraversalMode.TOP_DOWN | ||
) | ||
|
||
fields.forEach { field: Field -> | ||
try { | ||
ReflectionUtils.makeAccessible(field)[testInstance] = | ||
fieldValue(specTestRunnerContext) | ||
} catch (t: Throwable) { | ||
ExceptionUtils.throwAsUncheckedException(t) | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
junit-extension/src/test/kotlin/io/zeebe/bpmnspec/junit/BpmnSpecExtensionInjectionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.zeebe.bpmnspec.junit | ||
|
||
import io.camunda.zeebe.client.ZeebeClient | ||
import io.zeebe.bpmnspec.SpecRunner | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.params.ParameterizedTest | ||
|
||
@BpmnSpecRunner(verificationTimeout = "PT1S") | ||
class BpmnSpecExtensionInjectionTest(private val specRunner: SpecRunner) { | ||
|
||
private lateinit var zeebeClient: ZeebeClient | ||
|
||
@BeforeEach | ||
fun `start external worker`() { | ||
zeebeClient.newWorker() | ||
.jobType("a") | ||
.handler { client, job -> | ||
val valueOfX = job.variablesAsMap["x"] as Int | ||
val newValue = valueOfX + 1 | ||
|
||
client.newCompleteCommand(job.key) | ||
.variables(mapOf("x" to newValue)) | ||
.send() | ||
.join() | ||
} | ||
.open() | ||
} | ||
|
||
@ParameterizedTest | ||
@BpmnSpecSource(specResources = ["spec-with-external-worker.yaml"]) | ||
fun `should inject Zeebe client and complete process`(spec: BpmnSpecTestCase) { | ||
|
||
assertThat(zeebeClient) | ||
.describedAs("Zeebe client should be injected") | ||
.isNotNull() | ||
|
||
val testResult = | ||
specRunner.runSingleTestCase(resources = spec.resources, testcase = spec.testCase) | ||
|
||
assertThat(testResult.success) | ||
.describedAs(testResult.message) | ||
.isTrue() | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
junit-extension/src/test/resources/spec-with-external-worker.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
resources: | ||
- exclusive-gateway.bpmn | ||
|
||
testCases: | ||
- name: activate task B | ||
description: the external job worker should complete task A with 'x' > 5 | ||
actions: | ||
- action: create-instance | ||
args: | ||
bpmn_process_id: exclusive-gateway | ||
variables: '{"x":5}' | ||
|
||
verifications: | ||
- verification: element-instance-state | ||
args: | ||
element_name: B | ||
state: activated | ||
|
||
- name: activate task C | ||
description: the external job worker should complete task A with 'x' <= 5 | ||
actions: | ||
- action: create-instance | ||
args: | ||
bpmn_process_id: exclusive-gateway | ||
variables: '{"x":3}' | ||
|
||
verifications: | ||
- verification: element-instance-state | ||
args: | ||
element_name: C | ||
state: activated |