@@ -4,77 +4,31 @@ import com.hivemq.client.mqtt.mqtt5.datatypes.Mqtt5UserProperties
44import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish
55import org.junit.jupiter.api.AfterAll
66import org.junit.jupiter.api.BeforeAll
7+ import org.junit.jupiter.api.TestInstance
78import org.junit.jupiter.api.Timeout
8- import org.testcontainers.containers.BindMode
9- import org.testcontainers.containers.GenericContainer
9+ import org.openbase.jul.communication.config.CommunicatorConfig
10+ import org.openbase.jul.communication.mqtt.test.MqttBrokerManager
1011import org.testcontainers.junit.jupiter.Testcontainers
11- import org.testcontainers.utility.DockerImageName
12- import java.time.Duration
13- import kotlin.io.path.absolute
14- import kotlin.io.path.deleteIfExists
15- import kotlin.io.path.writeLines
1612
17- @Testcontainers
18- abstract class AbstractIntegrationTest {
13+ @TestInstance( TestInstance . Lifecycle . PER_CLASS )
14+ open class AbstractIntegrationTest {
1915
20- // the companion object makes sure that the container is started once before all tests instead of restarting for every test
21- companion object {
22-
23- private const val port: Int = 1883
24-
25- private val mosquittoConfig = kotlin.io.path.createTempFile(prefix = " mosquitto_" , suffix = " .conf" )
26- private lateinit var broker: MqttBrokerContainer
27-
28- private var usageCounter = 0
29- private val lock = Any ()
30-
31- @JvmStatic
32- @BeforeAll
33- @Timeout(30 )
34- fun setup () {
35- synchronized(lock) {
36- if (usageCounter == 0 ) {
37- mosquittoConfig.writeLines(
38- listOf (
39- " allow_anonymous true" ,
40- " listener 1883"
41- )
42- )
43-
44- broker = MqttBrokerContainer ()
45- .withExposedPorts(port)
46- .withFileSystemBind(
47- mosquittoConfig.absolute().toString(),
48- " /mosquitto/config/mosquitto.conf" ,
49- BindMode .READ_ONLY
50- )
51- broker.withStartupTimeout(Duration .ofSeconds(30 )).start()
52- }
53- usageCounter++
54- }
55- }
56-
57- @JvmStatic
58- @AfterAll
59- @Timeout(30 )
60- fun cleanup () {
61- synchronized(lock) {
62- usageCounter--
63- if (usageCounter == 0 ) {
64- SharedMqttClient .waitForShutdown()
65- broker.stop()
66- mosquittoConfig.deleteIfExists()
67- }
68- }
69- }
16+ @BeforeAll
17+ @Timeout(30 )
18+ fun setupMqtt () {
19+ MqttBrokerManager .setupMqtt(this ::class .java.simpleName)
7020 }
7121
72- protected val brokerHost: String get() = broker.host
22+ protected val brokerHost: String? get() = MqttBrokerManager . broker? .host
7323
74- protected val brokerPort: Int get() = broker.firstMappedPort
75- }
24+ protected val brokerPort: Int? get() = MqttBrokerManager .broker?.firstMappedPort
7625
77- class MqttBrokerContainer : GenericContainer <MqttBrokerContainer >(DockerImageName .parse("eclipse-mosquitto"))
26+ protected val config
27+ get() = CommunicatorConfig (
28+ hostname = brokerHost ? : error(" Host not defined!" ),
29+ port = brokerPort ? : error(" Port not defined!" ),
30+ )
31+ }
7832
7933fun Mqtt5Publish.clearTimestamp () = let {
8034 this .extend().userProperties(Mqtt5UserProperties .of()).build()
0 commit comments