From a7779c6c5f7a6715295c720e20ec8139310b38e6 Mon Sep 17 00:00:00 2001 From: Tim Yates Date: Tue, 9 Jan 2024 16:53:34 +0000 Subject: [PATCH] Stop using micronaut-platform and plugins for jackson test --- gradle/libs.versions.toml | 2 - test-suite-jackson/build.gradle | 30 ++++++++------- .../src/main/resources/application.yml | 18 --------- .../example/micronaut/AbstractMQTTTest.java | 37 +++++++++++++++++++ .../example/micronaut/SubscriptionTest.java | 25 ++++++------- 5 files changed, 65 insertions(+), 47 deletions(-) delete mode 100644 test-suite-jackson/src/main/resources/application.yml create mode 100644 test-suite-jackson/src/test/java/example/micronaut/AbstractMQTTTest.java diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0d077438..a90b0899 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,6 @@ [versions] micronaut-docs = "2.0.0" micronaut = "4.1.11" -micronaut-platform = "4.1.6" micronaut-gradle-plugin = "4.1.2" groovy = "4.0.15" spock = "2.3-groovy-4.0" @@ -24,7 +23,6 @@ micronaut-logging = "1.1.2" [libraries] # Core micronaut-core = { module = 'io.micronaut:micronaut-core-bom', version.ref = 'micronaut' } -micronaut-platform = { module = 'io.micronaut.platform:micronaut-platform', version.ref = 'micronaut-platform' } micronaut-reactor = { module = "io.micronaut.reactor:micronaut-reactor-bom", version.ref = "micronaut-reactor" } micronaut-serde = { module = "io.micronaut.serde:micronaut-serde-bom", version.ref = "micronaut-serde" } micronaut-validation = { module = "io.micronaut.validation:micronaut-validation-bom", version.ref = "micronaut-validation" } diff --git a/test-suite-jackson/build.gradle b/test-suite-jackson/build.gradle index 615f18f8..3e3f65cf 100644 --- a/test-suite-jackson/build.gradle +++ b/test-suite-jackson/build.gradle @@ -1,29 +1,33 @@ plugins { - id("io.micronaut.library") - id("io.micronaut.test-resources") + id 'java-library' + id 'groovy' + id("io.micronaut.build.internal.mqtt-tests") } -version = "0.1" -group = "example.micronaut" - repositories { mavenCentral() } dependencies { + annotationProcessor(mn.micronaut.inject.java) + implementation(projects.micronautMqttv5) + implementation(mn.micronaut.inject) implementation(mn.micronaut.jackson.databind) - runtimeOnly(mn.snakeyaml) + runtimeOnly(mnLogging.logback.classic) + testAnnotationProcessor(mn.micronaut.inject.java) + + testImplementation(projects.testSuiteUtils) + testImplementation(libs.awaitility) + testImplementation(libs.junit.jupiter.api) + testImplementation(mnTest.micronaut.test.junit5) + + testRuntimeOnly(libs.junit.jupiter.engine) } -micronaut { - version.set(libs.versions.micronaut.platform) - testRuntime("junit5") - processing { - incremental(true) - annotations("example.micronaut.*") - } +tasks.named('test') { + useJUnitPlatform() } diff --git a/test-suite-jackson/src/main/resources/application.yml b/test-suite-jackson/src/main/resources/application.yml deleted file mode 100644 index 9afdfb5c..00000000 --- a/test-suite-jackson/src/main/resources/application.yml +++ /dev/null @@ -1,18 +0,0 @@ -#tag::mqtt[] -mqtt: - client: - server-uri: tcp://${mqtt.host}:${mqtt.port} - client-id: ${random.uuid} -#end::mqtt[] -#tag::test-resources[] -test-resources: - containers: - mosquitto: - image-name: eclipse-mosquitto - hostnames: - - mqtt.host - exposed-ports: - - mqtt.port: 1883 - ro-fs-bind: - - "src/test-resources/mosquitto.conf": /mosquitto/config/mosquitto.conf -#end::test-resources[] \ No newline at end of file diff --git a/test-suite-jackson/src/test/java/example/micronaut/AbstractMQTTTest.java b/test-suite-jackson/src/test/java/example/micronaut/AbstractMQTTTest.java new file mode 100644 index 00000000..2125d521 --- /dev/null +++ b/test-suite-jackson/src/test/java/example/micronaut/AbstractMQTTTest.java @@ -0,0 +1,37 @@ +package example.micronaut; + +import io.micronaut.context.ApplicationContext; +import org.testcontainers.containers.BindMode; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; +import org.testcontainers.utility.DockerImageName; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public abstract class AbstractMQTTTest { + + protected static GenericContainer mqttContainer = new GenericContainer(DockerImageName.parse("eclipse-mosquitto:1.6.12")) + .withExposedPorts(1883) + .waitingFor(new LogMessageWaitStrategy().withRegEx("(?s).*mosquitto version 1.6.12 running.*")) + .withClasspathResourceMapping("mosquitto.conf", + "/mosquitto/config/mosquitto.conf", + BindMode.READ_ONLY); + + static { + mqttContainer.start(); + } + + protected ApplicationContext startContext() { + return ApplicationContext.run(getConfiguration(), "test"); + } + + protected Map getConfiguration() { + Map config = new HashMap<>(); + config.put("mqtt.client.server-uri", "tcp://localhost:" + mqttContainer.getMappedPort(1883)); + config.put("mqtt.client.client-id", UUID.randomUUID().toString()); + config.put("spec.name", this.getClass().getSimpleName()); + return config; + } +} diff --git a/test-suite-jackson/src/test/java/example/micronaut/SubscriptionTest.java b/test-suite-jackson/src/test/java/example/micronaut/SubscriptionTest.java index a804371d..7a15b0dd 100644 --- a/test-suite-jackson/src/test/java/example/micronaut/SubscriptionTest.java +++ b/test-suite-jackson/src/test/java/example/micronaut/SubscriptionTest.java @@ -1,9 +1,8 @@ package example.micronaut; +import io.micronaut.context.ApplicationContext; import io.micronaut.mqtt.annotation.Topic; import io.micronaut.mqtt.annotation.v5.MqttPublisher; -import io.micronaut.test.extensions.junit5.annotation.MicronautTest; -import jakarta.inject.Inject; import org.junit.jupiter.api.Test; import java.util.concurrent.TimeUnit; @@ -11,22 +10,20 @@ import static org.awaitility.Awaitility.await; import static org.junit.jupiter.api.Assertions.assertEquals; -@MicronautTest -class SubscriptionTest { - - @Inject - SmellClient client; - - @Inject - SmellListener listener; +class SubscriptionTest extends AbstractMQTTTest { @Test void checkSubscriptionsAreReceived() { - client.publishLivingroomSmell(new Odour("cheesy")); + try(ApplicationContext applicationContext = startContext()) { + SmellClient client = applicationContext.getBean(SmellClient.class); + SmellListener listener = applicationContext.getBean(SmellListener.class); + + client.publishLivingroomSmell(new Odour("cheesy")); - await() - .atMost(5, TimeUnit.SECONDS) - .untilAsserted(() -> assertEquals("cheesy", listener.getSmell())); + await() + .atMost(5, TimeUnit.SECONDS) + .untilAsserted(() -> assertEquals("cheesy", listener.getSmell())); + } } @MqttPublisher