Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22 provide basic test coverage #40

Merged
merged 7 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import java.util.function.Supplier
* Supplier for the payload.
* @since 0.0.1
*/
interface PayloadSupplier : Supplier<Map<String, Any>>
fun interface PayloadSupplier : Supplier<Map<String, Any>>
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import java.util.function.Consumer
* Handler that is invoked if the task is deleted after the task has been assigned to the task handler.
* @since 0.0.1
*/
interface TaskTerminationHandler : Consumer<String>
fun interface TaskTerminationHandler : Consumer<String>
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ class InMemSubscriptionRepository : SubscriptionRepository {
}.keys.toList()
}

fun deleteAllTaskSubscriptions() {
subscriptions.clear()
activeSubscribedHandler.clear()
}

}
88 changes: 88 additions & 0 deletions engine-adapter/adapter-testing/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>dev.bpm-crafters.process-engine-api</groupId>
<artifactId>process-engine-api-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>process-engine-api-adapter-testing</artifactId>

<properties>
<h2.version>2.2.224</h2.version>
<camunda-platform-7-mockito.version>7.20.1</camunda-platform-7-mockito.version>
<mockk.version>1.13.10</mockk.version>
<jgiven.version>1.3.1</jgiven.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- For JGiven Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>com.tngtech.jgiven</groupId>
<artifactId>jgiven-spring-junit5</artifactId>
<version>${jgiven.version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>dev.bpm-crafters.process-engine-api</groupId>
<artifactId>process-engine-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk-jvm</artifactId>
<version>${mockk.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.tngtech.jgiven</groupId>
<artifactId>jgiven-junit5</artifactId>
<version>${jgiven.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.toolisticon.testing</groupId>
<artifactId>jgiven-kotlin</artifactId>
<version>${jgiven.version}.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package dev.bpmcrafters.processengineapi.test

import com.tngtech.jgiven.Stage
import com.tngtech.jgiven.annotation.ExpectedScenarioState
import com.tngtech.jgiven.annotation.ProvidedScenarioState
import dev.bpmcrafters.processengineapi.process.StartProcessByDefinitionCmd
import dev.bpmcrafters.processengineapi.process.StartProcessByMessageCmd
import dev.bpmcrafters.processengineapi.task.*
import io.mockk.mockk
import io.toolisticon.testing.jgiven.JGivenKotlinStage
import io.toolisticon.testing.jgiven.step
import mu.KLogging
import org.assertj.core.api.Assertions.assertThat

@JGivenKotlinStage
class BaseGivenWhenStage : Stage<BaseGivenWhenStage>() {

companion object : KLogging()

@ExpectedScenarioState
lateinit var processTestHelper: ProcessTestHelper

@ProvidedScenarioState
lateinit var instanceId: String

@ProvidedScenarioState
var userTaskId: String? = null

@ProvidedScenarioState
var externalTaskId: String? = null

@ProvidedScenarioState
lateinit var taskSubscription: TaskSubscription

fun `start process by definition`(definitionKey: String) = step {
instanceId = processTestHelper.getStartProcessApi().startProcess(
StartProcessByDefinitionCmd(
definitionKey = definitionKey,
payloadSupplier = { emptyMap() }
)
).get().instanceId
}

fun `start process by definition with payload`(definitionKey: String, singlePayload: Pair<String, Any>) = step {
instanceId = processTestHelper.getStartProcessApi().startProcess(
StartProcessByDefinitionCmd(
definitionKey = definitionKey,
payloadSupplier = { mapOf(singlePayload) }
)
).get().instanceId
}

fun `start process by message`(messageName: String) = step {
instanceId = processTestHelper.getStartProcessApi().startProcess(
StartProcessByMessageCmd(
messageName = messageName,
payloadSupplier = { emptyMap() }
)
).get().instanceId
}

fun `start process by message with payload`(messageName: String, singlePayload: Pair<String, Any>) = step {
instanceId = processTestHelper.getStartProcessApi().startProcess(
StartProcessByMessageCmd(
messageName = messageName,
payloadSupplier = { mapOf(singlePayload) }
)
).get().instanceId
}

fun `a active user task subscription`(taskDescriptionKey: String) = step {
taskSubscription = subscribeTask(TaskType.USER, taskDescriptionKey) { taskInformation, _ ->
run {
logger.info { "Got new task ${taskInformation.taskId}" }
userTaskId = taskInformation.taskId
}
}
}

fun `subscribe for tasks`() = step {
processTestHelper.subscribeForUserTasks()
}

fun `a active external task subscription`(taskDescriptionKey: String) = step {
taskSubscription = subscribeTask(TaskType.EXTERNAL, taskDescriptionKey) { taskInformation, _ -> externalTaskId = taskInformation.taskId }
}

fun `complete the user task`() = step {
assertThat(userTaskId).isNotEmpty()

processTestHelper.getUserTaskCompletionApi().completeTask(
CompleteTaskCmd(
taskId = userTaskId!!,
payloadSupplier = { emptyMap() }
)
)
}

fun `complete the external task`() = step {
assertThat(externalTaskId).isNotEmpty()

processTestHelper.getExternalTaskCompletionApi().completeTask(
CompleteTaskCmd(
taskId = externalTaskId!!,
payloadSupplier = { emptyMap() }
)
)
}

fun `unsubscribe user task subscription`() = step { unsubscribeTask() }

fun `unsubscribe external task subscription`() = step { unsubscribeTask() }

private fun subscribeTask(taskType: TaskType, taskDescriptionKey: String, taskHandler: TaskHandler) = processTestHelper.getTaskSubscriptionApi().subscribeForTask(
SubscribeForTaskCmd(
restrictions = emptyMap(),
taskType = taskType,
taskDescriptionKey = taskDescriptionKey,
action = taskHandler,
termination = {} // nothing to do
)
).get()

private fun unsubscribeTask() = processTestHelper.getTaskSubscriptionApi().unsubscribe(
UnsubscribeFromTaskCmd(
taskSubscription
)
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dev.bpmcrafters.processengineapi.test

import com.tngtech.jgiven.Stage
import com.tngtech.jgiven.annotation.ExpectedScenarioState
import io.toolisticon.testing.jgiven.JGivenKotlinStage
import io.toolisticon.testing.jgiven.step
import mu.KLogging
import org.assertj.core.api.Assertions.assertThat
import org.awaitility.Awaitility.await
import java.time.Duration
import java.util.concurrent.TimeUnit

@JGivenKotlinStage
class BaseThenStage : Stage<BaseThenStage>() {

companion object : KLogging()

@ExpectedScenarioState
lateinit var instanceId: String

@ExpectedScenarioState
var userTaskId: String? = null

@ExpectedScenarioState
var externalTaskId: String? = null

@ExpectedScenarioState
lateinit var processTestHelper: ProcessTestHelper

fun `we should have a running process`() = step {
val process = processTestHelper.getProcessInformation(instanceId)
assertThat(process).isNotNull()
}

fun `we should get notified about a new user task with pull strategy`() = step {
processTestHelper.triggerPullingUserTaskDeliveryManually()

await().untilAsserted { assertThat(userTaskId).isNotEmpty() }
}

fun `we should get notified about a new user task with subscribing strategy`() = step {
await().untilAsserted { assertThat(userTaskId).isNotEmpty() }
}

fun `we should not get notified about a new user task with pull strategy`() = step {
processTestHelper.triggerPullingUserTaskDeliveryManually()

await().untilAsserted { assertThat(userTaskId).isNull() }
}

fun `we should not get notified about a new user task with subscribing strategy`() = step {
await().untilAsserted { assertThat(userTaskId).isNull() }
}

fun `we should get notified about a new external task`() = step {
processTestHelper.triggerExternalTaskDeliveryManually()

await().untilAsserted { assertThat(externalTaskId).isNotEmpty() }
}

fun `we should not get notified about a new external task`() = step {
processTestHelper.triggerExternalTaskDeliveryManually()

await().untilAsserted { assertThat(externalTaskId).isNull() }
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.bpmcrafters.processengineapi.test

import com.tngtech.jgiven.annotation.ProvidedScenarioState
import com.tngtech.jgiven.junit5.ScenarioTest

abstract class JGivenBaseIntegrationTest(
@ProvidedScenarioState open val processTestHelper: ProcessTestHelper
) : ScenarioTest<BaseGivenWhenStage, BaseGivenWhenStage, BaseThenStage>()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.bpmcrafters.processengineapi.test

import com.tngtech.jgiven.annotation.ProvidedScenarioState
import com.tngtech.jgiven.integration.spring.junit5.SpringScenarioTest
import com.tngtech.jgiven.junit5.ScenarioTest

abstract class JGivenSpringBaseIntegrationTest(
@ProvidedScenarioState open val processTestHelper: ProcessTestHelper
) : SpringScenarioTest<BaseGivenWhenStage, BaseGivenWhenStage, BaseThenStage>()
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.bpmcrafters.processengineapi.test

import dev.bpmcrafters.processengineapi.process.ProcessInformation
import dev.bpmcrafters.processengineapi.process.StartProcessApi
import dev.bpmcrafters.processengineapi.task.ExternalTaskCompletionApi
import dev.bpmcrafters.processengineapi.task.TaskSubscriptionApi
import dev.bpmcrafters.processengineapi.task.UserTaskCompletionApi

interface ProcessTestHelper {

fun getStartProcessApi(): StartProcessApi

fun getTaskSubscriptionApi(): TaskSubscriptionApi

fun getUserTaskCompletionApi(): UserTaskCompletionApi

fun getExternalTaskCompletionApi(): ExternalTaskCompletionApi

fun triggerPullingUserTaskDeliveryManually()

fun subscribeForUserTasks()

fun triggerExternalTaskDeliveryManually()

fun getProcessInformation(instanceId: String): ProcessInformation

fun clearAllSubscriptions()

}
Loading