From ccb8401b946e140f9aa6e52b71570a946ee3666d Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 20 Jul 2021 17:54:41 +0300 Subject: [PATCH] Introduce the ability to inject context into @QuarkusIntegrationTest tests Resolves: #18854 --- .../src/main/resources/application.properties | 1 - .../panache/MongoDbRestDataPanacheIT.java | 14 +++++- .../panache/MongoDbRestDataPanacheTest.java | 3 -- integration-tests/oidc/pom.xml | 5 +++ ...BearerTokenAuthorizationInGraalITCase.java | 15 ++++++- .../test/junit/IntegrationTestUtil.java | 2 +- .../test/junit/NativeTestExtension.java | 6 +-- .../test/junit/QuarkusIntegrationTest.java | 14 ++++++ .../QuarkusIntegrationTestExtension.java | 45 ++++++++++++++++++- 9 files changed, 91 insertions(+), 14 deletions(-) diff --git a/integration-tests/mongodb-rest-data-panache/src/main/resources/application.properties b/integration-tests/mongodb-rest-data-panache/src/main/resources/application.properties index d7e6302e19151..1662de97aef85 100644 --- a/integration-tests/mongodb-rest-data-panache/src/main/resources/application.properties +++ b/integration-tests/mongodb-rest-data-panache/src/main/resources/application.properties @@ -1,3 +1,2 @@ -quarkus.mongodb.connection-string=mongodb://localhost:27017 quarkus.mongodb.write-concern.journal=false quarkus.mongodb.database=test diff --git a/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheIT.java b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheIT.java index 659d1b9449771..361c3b47e022e 100644 --- a/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheIT.java +++ b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheIT.java @@ -1,11 +1,21 @@ package io.quarkus.it.mongodb.rest.data.panache; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; -import io.quarkus.test.junit.NativeImageTest; +import io.quarkus.test.junit.QuarkusIntegrationTest; -@NativeImageTest +@QuarkusIntegrationTest @DisabledOnOs(OS.WINDOWS) class MongoDbRestDataPanacheIT extends MongoDbRestDataPanacheTest { + + QuarkusIntegrationTest.Context context; + + @Test + public void testDevServicesProperties() { + assertThat(context.devServicesProperties()).hasSize(1).containsKey("quarkus.mongodb.connection-string"); + } } diff --git a/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java index f857c7d8b3776..05417efbf3b55 100644 --- a/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java +++ b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java @@ -18,13 +18,10 @@ import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; -import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.mongodb.MongoTestResource; import io.restassured.response.Response; @QuarkusTest -@QuarkusTestResource(MongoTestResource.class) @DisabledOnOs(OS.WINDOWS) class MongoDbRestDataPanacheTest { diff --git a/integration-tests/oidc/pom.xml b/integration-tests/oidc/pom.xml index c10218ddb9c03..4b3a75df11b50 100644 --- a/integration-tests/oidc/pom.xml +++ b/integration-tests/oidc/pom.xml @@ -48,6 +48,11 @@ awaitility test + + org.assertj + assertj-core + test + io.quarkus diff --git a/integration-tests/oidc/src/test/java/io/quarkus/it/keycloak/BearerTokenAuthorizationInGraalITCase.java b/integration-tests/oidc/src/test/java/io/quarkus/it/keycloak/BearerTokenAuthorizationInGraalITCase.java index 4decbe21df340..310b4d34be043 100644 --- a/integration-tests/oidc/src/test/java/io/quarkus/it/keycloak/BearerTokenAuthorizationInGraalITCase.java +++ b/integration-tests/oidc/src/test/java/io/quarkus/it/keycloak/BearerTokenAuthorizationInGraalITCase.java @@ -1,7 +1,18 @@ package io.quarkus.it.keycloak; -import io.quarkus.test.junit.NativeImageTest; +import static org.assertj.core.api.Assertions.assertThat; -@NativeImageTest +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusIntegrationTest; + +@QuarkusIntegrationTest public class BearerTokenAuthorizationInGraalITCase extends BearerTokenAuthorizationTest { + + QuarkusIntegrationTest.Context context; + + @Test + public void testDevServicesProperties() { + assertThat(context.devServicesProperties()).isEmpty(); + } } diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java index 8a8ca5c185f18..d4c4f79fad7e0 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/IntegrationTestUtil.java @@ -186,7 +186,7 @@ static void startLauncher(ArtifactLauncher launcher, Map additio } } - static Map handleDevDb(ExtensionContext context) throws Exception { + static Map handleDevServices(ExtensionContext context) throws Exception { Class requiredTestClass = context.getRequiredTestClass(); Path testClassLocation = getTestClassesLocation(requiredTestClass); final Path appClassLocation = getAppClassLocationForTestLocation(testClassLocation.toString()); diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeTestExtension.java index b6e259009f122..a1b4f7e6ccae6 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/NativeTestExtension.java @@ -7,7 +7,7 @@ import static io.quarkus.test.junit.IntegrationTestUtil.ensureNoInjectAnnotationIsUsed; import static io.quarkus.test.junit.IntegrationTestUtil.getAdditionalTestResources; import static io.quarkus.test.junit.IntegrationTestUtil.getSysPropsToRestore; -import static io.quarkus.test.junit.IntegrationTestUtil.handleDevDb; +import static io.quarkus.test.junit.IntegrationTestUtil.handleDevServices; import static io.quarkus.test.junit.IntegrationTestUtil.startLauncher; import java.util.HashMap; @@ -110,7 +110,7 @@ private IntegrationTestExtensionState ensureStarted(ExtensionContext extensionCo private IntegrationTestExtensionState doNativeStart(ExtensionContext context, Class profile) throws Throwable { - Map devDbProps = handleDevDb(context); + Map devServicesProps = handleDevServices(context); quarkusTestProfile = profile; currentJUnitTestClass = context.getRequiredTestClass(); TestResourceManager testResourceManager = null; @@ -128,7 +128,7 @@ private IntegrationTestExtensionState doNativeStart(ExtensionContext context, Cl hasPerTestResources = testResourceManager.hasPerTestResources(); Map additionalProperties = new HashMap<>(testProfileAndProperties.properties); - additionalProperties.putAll(devDbProps); + additionalProperties.putAll(devServicesProps); Map resourceManagerProps = testResourceManager.start(); Map old = new HashMap<>(); for (Map.Entry i : resourceManagerProps.entrySet()) { diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTest.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTest.java index 25af85954ec70..8651516b5657a 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTest.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTest.java @@ -4,6 +4,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import java.util.Map; import org.junit.jupiter.api.extension.ExtendWith; @@ -31,4 +32,17 @@ @ExtendWith({ DisabledOnIntegrationTestCondition.class, QuarkusTestExtension.class, QuarkusIntegrationTestExtension.class }) @Retention(RetentionPolicy.RUNTIME) public @interface QuarkusIntegrationTest { + + /** + * If used as a field of class annotated with {@link QuarkusIntegrationTest}, the field is populated + * with an implementation that allows accessing contextual test information + */ + interface Context { + + /** + * Returns a map containing all the properties creates by potentially launched dev services. + * If no dev services where launched, the map will be empty. + */ + Map devServicesProperties(); + } } diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java index e24544487317d..0b1af5f2fc697 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusIntegrationTestExtension.java @@ -3,6 +3,7 @@ import static io.quarkus.test.junit.IntegrationTestUtil.*; import static io.quarkus.test.junit.IntegrationTestUtil.ensureNoInjectAnnotationIsUsed; +import java.lang.reflect.Field; import java.nio.file.Path; import java.util.Collections; import java.util.HashMap; @@ -41,6 +42,8 @@ public class QuarkusIntegrationTestExtension private static Class currentJUnitTestClass; private static boolean hasPerTestResources; + private static Map devServicesProps; + @Override public void afterEach(ExtensionContext context) throws Exception { if (!failedBoot) { @@ -102,7 +105,7 @@ private IntegrationTestExtensionState ensureStarted(ExtensionContext extensionCo private IntegrationTestExtensionState doProcessStart(Properties quarkusArtifactProperties, Class profile, ExtensionContext context) throws Throwable { - Map devDbProps = handleDevDb(context); + devServicesProps = handleDevServices(context); quarkusTestProfile = profile; currentJUnitTestClass = context.getRequiredTestClass(); TestResourceManager testResourceManager = null; @@ -119,7 +122,7 @@ private IntegrationTestExtensionState doProcessStart(Properties quarkusArtifactP hasPerTestResources = testResourceManager.hasPerTestResources(); Map additionalProperties = new HashMap<>(testProfileAndProperties.properties); - additionalProperties.putAll(devDbProps); + additionalProperties.putAll(devServicesProps); Map resourceManagerProps = testResourceManager.start(); Map old = new HashMap<>(); for (Map.Entry i : resourceManagerProps.entrySet()) { @@ -189,6 +192,30 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex ensureStarted(context); if (!failedBoot) { doProcessTestInstance(testInstance, context); + injectTestContext(testInstance); + } + } + + private void injectTestContext(Object testInstance) { + Class c = testInstance.getClass(); + while (c != Object.class) { + for (Field f : c.getDeclaredFields()) { + if (f.getType().equals(QuarkusIntegrationTest.Context.class)) { + try { + Map devServicesPropsCopy = devServicesProps.isEmpty() ? Collections.emptyMap() + : Collections.unmodifiableMap(devServicesProps); + QuarkusIntegrationTest.Context testContext = new DefaultQuarkusIntegrationTestContext( + devServicesPropsCopy); + f.setAccessible(true); + f.set(testInstance, testContext); + return; + } catch (Exception e) { + throw new RuntimeException("Unable to set field '" + f.getName() + + "' with the proper test context", e); + } + } + } + c = c.getSuperclass(); } } @@ -229,4 +256,18 @@ public Class testClass() { return requiredTestClass; } } + + private static class DefaultQuarkusIntegrationTestContext implements QuarkusIntegrationTest.Context { + + private final Map map; + + private DefaultQuarkusIntegrationTestContext(Map map) { + this.map = map; + } + + @Override + public Map devServicesProperties() { + return map; + } + } }