Skip to content

Commit

Permalink
Use mongo with textcontainers in ITests #1005
Browse files Browse the repository at this point in the history
  • Loading branch information
p-wunderlich committed Jul 18, 2024
1 parent c9b2a7f commit a914606
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 245 deletions.
11 changes: 8 additions & 3 deletions view/mongo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo.spring30x</artifactId>
<version>4.11.0</version>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.tngtech.jgiven</groupId>
<artifactId>jgiven-junit5</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
package io.holunda.polyflow.view.mongo.service

import io.holunda.polyflow.view.mongo.utils.MongoLauncher
import org.junit.jupiter.api.AfterAll
import io.holunda.polyflow.view.mongo.TaskPoolMongoViewConfiguration
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeAll
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.TestPropertySource
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest
import org.springframework.test.context.*
import org.testcontainers.containers.MongoDBContainer
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers

@TestPropertySource(
properties = [
"polyflow.view.mongo.changeTrackingMode=CHANGE_STREAM",
"spring.data.mongodb.database=TaskPoolMongoServiceChangeStreamChangeTrackingITest"
]
)
@ActiveProfiles("itest-replicated")
@Testcontainers
@DataMongoTest
@ContextConfiguration(classes = [TaskPoolMongoViewConfiguration::class])
class PolyflowMongoServiceChangeStreamChangeTrackingITest : PolyflowMongoServiceITestBase() {
companion object {
private val mongo = MongoLauncher.MongoInstance(true, "TaskPoolMongoServiceChangeStreamChangeTrackingITest")

@BeforeAll
@Container
@JvmStatic
fun initMongo() {
mongo.init()
}
var mongoDBContainer: MongoDBContainer = MongoDBContainer("mongo:4.4.2").withSharding()

@AfterAll
@DynamicPropertySource
@JvmStatic
fun stop() {
mongo.stop()
fun setProperties(registry: DynamicPropertyRegistry) {
registry.add("spring.data.mongodb.uri") { mongoDBContainer.replicaSetUrl }
}
}

@AfterEach
fun clearMongo() {
mongo.clear()
mongoDBContainer.clear()
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
package io.holunda.polyflow.view.mongo.service

import io.holunda.polyflow.view.mongo.utils.MongoLauncher
import org.junit.jupiter.api.AfterAll
import com.mongodb.client.MongoClients
import io.holunda.polyflow.view.mongo.TaskPoolMongoViewConfiguration
import org.axonframework.extensions.mongo.MongoTemplate
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeAll
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.TestPropertySource
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest
import org.springframework.test.context.*
import org.testcontainers.containers.MongoDBContainer
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers


@TestPropertySource(
properties = [
"polyflow.view.mongo.changeTrackingMode=EVENT_HANDLER",
"spring.data.mongodb.database=TaskPoolMongoServiceEventHandlerChangeTrackingITest"
]
)
@ActiveProfiles("itest-standalone")
@Testcontainers
@DataMongoTest
@ContextConfiguration(classes = [TaskPoolMongoViewConfiguration::class])
class PolyflowMongoServiceEventHandlerChangeTrackingITest : PolyflowMongoServiceITestBase() {
companion object {
private val mongo = MongoLauncher.MongoInstance(false, "TaskPoolMongoServiceEventHandlerChangeTrackingITest")

@BeforeAll
@Container
@JvmStatic
fun initMongo() {
mongo.init()
}
var mongoDBContainer: MongoDBContainer = MongoDBContainer("mongo:4.4.2")

@AfterAll
@DynamicPropertySource
@JvmStatic
fun stop() {
mongo.stop()
fun setProperties(registry: DynamicPropertyRegistry) {
registry.add("spring.data.mongodb.uri") { mongoDBContainer.replicaSetUrl }
}
}

@Autowired
var mongoTemplate: MongoTemplate? = null

@AfterEach
fun clearMongo() {
mongo.clear()
mongoDBContainer.clear()
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.holunda.polyflow.view.mongo.service

import com.mongodb.client.MongoClients
import com.tngtech.jgiven.integration.spring.junit5.SpringScenarioTest
import com.tngtech.jgiven.junit5.JGivenExtension
import io.holunda.camunda.taskpool.api.business.*
Expand All @@ -9,20 +10,18 @@ import io.holunda.polyflow.view.ProtocolEntry
import io.holunda.polyflow.view.Task
import io.holunda.polyflow.view.TaskWithDataEntries
import io.holunda.polyflow.view.auth.User
import io.holunda.polyflow.view.mongo.PolyflowMongoTestApplication
import io.holunda.polyflow.view.query.data.DataEntriesForUserQuery
import io.holunda.polyflow.view.query.task.*
import org.camunda.bpm.engine.variable.VariableMap
import org.camunda.bpm.engine.variable.Variables
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.annotation.DirtiesContext
import org.testcontainers.containers.MongoDBContainer
import java.time.OffsetDateTime
import java.util.*

@ExtendWith(JGivenExtension::class)
@SpringBootTest(classes = [PolyflowMongoTestApplication::class])
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
abstract class PolyflowMongoServiceITestBase : SpringScenarioTest<PolyflowGivenStage<*>, PolyflowWhenStage<*>, PolyflowThenStage<*>>() {

Expand Down Expand Up @@ -685,3 +684,13 @@ data class TestDataEntryData(
}

private fun Task.withDataEntries(dataEntries: List<DataEntry> = listOf()) = TaskWithDataEntries(this, dataEntries)

/**
* Clear client and db.
*/
fun MongoDBContainer.clear() {
MongoClients.create(this.connectionString).use {
val database = it.getDatabase("test")
database.drop()
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
package io.holunda.polyflow.view.mongo.task

import io.holunda.polyflow.view.mongo.PolyflowMongoTestApplication
import io.holunda.polyflow.view.mongo.utils.MongoLauncher
import io.holunda.polyflow.view.mongo.TaskPoolMongoViewConfiguration
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.*
import org.junit.jupiter.api.extension.ExtendWith
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.autoconfigure.data.mongo.DataMongoTest
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.springframework.test.context.TestPropertySource
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.testcontainers.containers.MongoDBContainer
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers
import reactor.core.publisher.Mono
import java.time.Instant
import java.util.*


@TestPropertySource(
properties = [
"polyflow.view.mongo.changeTrackingMode=EVENT_HANDLER",
"spring.data.mongodb.database=TaskRepositoryExtensionImplITest"
]
)
@SpringBootTest(classes = [PolyflowMongoTestApplication::class])
@Testcontainers
@DataMongoTest
@ActiveProfiles("itest-standalone")
@ExtendWith(SpringExtension::class)
@ContextConfiguration(classes = [TaskPoolMongoViewConfiguration::class])
class TaskRepositoryExtensionImplITest {

companion object {
Expand All @@ -39,19 +44,16 @@ class TaskRepositoryExtensionImplITest {
const val PRIORITY_TWO = 90
const val PRIORITY_THREE = 100

private val mongo = MongoLauncher.MongoInstance(false, "TaskRepositoryExtensionImplITest")

@BeforeAll
@Container
@JvmStatic
fun initMongo() {
mongo.init()
}
var mongoDBContainer: MongoDBContainer = MongoDBContainer("mongo:4.4.2")

@AfterAll
@DynamicPropertySource
@JvmStatic
fun stop() {
mongo.stop()
fun setProperties(registry: DynamicPropertyRegistry) {
registry.add("spring.data.mongodb.uri") { mongoDBContainer.replicaSetUrl }
}

}

@BeforeEach
Expand All @@ -62,12 +64,6 @@ class TaskRepositoryExtensionImplITest {
@Autowired
lateinit var taskRepository: TaskRepository

@AfterEach
fun clearMongo() {
mongo.clear()
}


@Test
fun finds_by_everything() {
val documents = prepareDocuments(task().build())
Expand Down
Loading

0 comments on commit a914606

Please sign in to comment.