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 f110cc4905753..4da90e8867b46 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 @@ -1,8 +1,15 @@ package io.quarkus.test.junit; -import static io.quarkus.test.junit.IntegrationTestUtil.*; +import static io.quarkus.test.junit.IntegrationTestUtil.determineBuildOutputDirectory; +import static io.quarkus.test.junit.IntegrationTestUtil.determineTestProfileAndProperties; +import static io.quarkus.test.junit.IntegrationTestUtil.doProcessTestInstance; import static io.quarkus.test.junit.IntegrationTestUtil.ensureNoInjectAnnotationIsUsed; +import static io.quarkus.test.junit.IntegrationTestUtil.getSysPropsToRestore; +import static io.quarkus.test.junit.IntegrationTestUtil.handleDevServices; +import static io.quarkus.test.junit.IntegrationTestUtil.readQuarkusArtifactProperties; +import static io.quarkus.test.junit.IntegrationTestUtil.startLauncher; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.nio.file.Path; import java.util.Collections; @@ -208,23 +215,42 @@ private void injectTestContext(Object testInstance) { 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); + f.set(testInstance, createTestContext()); return; } catch (Exception e) { throw new RuntimeException("Unable to set field '" + f.getName() + "' with the proper test context", e); } + } else { + Constructor testContextAware = null; + try { + testContextAware = f.getType().getConstructor(QuarkusIntegrationTest.Context.class); + } catch (Exception t) { + continue; + } + if (testContextAware != null) { + try { + Object testContextAwareInstance = testContextAware.newInstance(createTestContext()); + f.setAccessible(true); + f.set(testInstance, testContextAwareInstance); + } catch (Exception e) { + throw new RuntimeException("Unable to set field '" + f.getName() + + "' with the proper test context", e); + } + } } } c = c.getSuperclass(); } } + private QuarkusIntegrationTest.Context createTestContext() { + Map devServicesPropsCopy = devServicesProps.isEmpty() ? Collections.emptyMap() + : Collections.unmodifiableMap(devServicesProps); + return new DefaultQuarkusIntegrationTestContext(devServicesPropsCopy); + } + private void throwBootFailureException() { if (firstException != null) { Throwable throwable = firstException; diff --git a/test-framework/keycloak-server/pom.xml b/test-framework/keycloak-server/pom.xml index f127d4b7de105..7ad759bd581c8 100644 --- a/test-framework/keycloak-server/pom.xml +++ b/test-framework/keycloak-server/pom.xml @@ -39,6 +39,10 @@ io.quarkus quarkus-test-common + + io.quarkus + quarkus-junit5 + diff --git a/test-framework/keycloak-server/src/main/java/io/quarkus/test/keycloak/server/KeycloakTestAdmin.java b/test-framework/keycloak-server/src/main/java/io/quarkus/test/keycloak/server/KeycloakTestAdmin.java new file mode 100644 index 0000000000000..91261146315b7 --- /dev/null +++ b/test-framework/keycloak-server/src/main/java/io/quarkus/test/keycloak/server/KeycloakTestAdmin.java @@ -0,0 +1,79 @@ +package io.quarkus.test.keycloak.server; + +import org.eclipse.microprofile.config.ConfigProvider; +import org.keycloak.representations.AccessTokenResponse; + +import io.quarkus.runtime.configuration.ConfigurationException; +import io.quarkus.test.junit.QuarkusIntegrationTest; +import io.restassured.RestAssured; + +public class KeycloakTestAdmin { + + private final static String AUTH_SERVER_URL_PROP = "quarkus.oidc.auth-server-url"; + private final static String CLIENT_ID_PROP = "quarkus.oidc.client-id"; + private final static String CLIENT_SECRET_PROP = "quarkus.oidc.credentials.secret"; + + static { + RestAssured.useRelaxedHTTPSValidation(); + } + + private QuarkusIntegrationTest.Context testContext; + + public KeycloakTestAdmin() { + + } + + public KeycloakTestAdmin(QuarkusIntegrationTest.Context testContext) { + this.testContext = testContext; + } + + public String getAccessToken(String userName) { + return getAccessToken(userName, getClientId()); + } + + public String getAccessToken(String userName, String clientId) { + return getAccessToken(userName, userName, clientId); + } + + public String getAccessToken(String userName, String userSecret, String clientId) { + return getAccessToken(userName, userSecret, clientId, getClientSecret()); + } + + public String getAccessToken(String userName, String userSecret, String clientId, String clientSecret) { + return RestAssured.given().param("grant_type", "password") + .param("username", userName) + .param("password", userSecret) + .param("client_id", clientId) + .param("client_secret", clientSecret) + .when() + .post(getAuthServerUrl() + "/protocol/openid-connect/token") + .as(AccessTokenResponse.class).getToken(); + } + + private String getClientId() { + return getPropertyValue(CLIENT_ID_PROP, "quarkus-app"); + } + + private String getClientSecret() { + return getPropertyValue(CLIENT_SECRET_PROP, "secret"); + } + + private String getAuthServerUrl() { + String authServerUrl = getPropertyValue(AUTH_SERVER_URL_PROP, null); + if (authServerUrl == null) { + throw new ConfigurationException(AUTH_SERVER_URL_PROP + " is not configured"); + } + return authServerUrl; + } + + private String getPropertyValue(String prop, String defaultValue) { + return ConfigProvider.getConfig().getOptionalValue(prop, String.class) + .orElseGet(() -> getDevProperty(prop, defaultValue)); + } + + private String getDevProperty(String prop, String defaultValue) { + String value = testContext == null ? null : testContext.devServicesProperties().get(prop); + return value == null ? defaultValue : value; + } + +}