diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/OAuthClient.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/OAuthClient.java index 538ccd29c3..a7d3949e8b 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/OAuthClient.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/OAuthClient.java @@ -1192,8 +1192,8 @@ public class OAuthClient { private JSONWebKeySet getRealmKeys(String realm) { String certUrl = baseUrl + "/realms/" + realm + "/protocol/openid-connect/certs"; - try { - return SimpleHttp.doGet(certUrl, httpClient.get()).asJson(JSONWebKeySet.class); + try (CloseableHttpClient client = httpClient.get()){ + return SimpleHttp.doGet(certUrl, client).asJson(JSONWebKeySet.class); } catch (IOException e) { throw new RuntimeException("Failed to retrieve keys", e); } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/CrossRealmPermissionsTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/CrossRealmPermissionsTest.java index 7ab95805a4..fe46395b85 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/CrossRealmPermissionsTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/CrossRealmPermissionsTest.java @@ -17,6 +17,7 @@ package org.keycloak.testsuite.admin; +import org.junit.AfterClass; import org.junit.Test; import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.resource.RealmResource; @@ -48,6 +49,9 @@ public class CrossRealmPermissionsTest extends AbstractKeycloakTest { private static final String REALM_NAME = "crossrealm-test"; private static final String REALM2_NAME = "crossrealm2-test"; + private static Keycloak adminClient1; + private static Keycloak adminClient2; + private RealmResource realm1; private RealmResource realm2; @@ -62,7 +66,8 @@ public class CrossRealmPermissionsTest extends AbstractKeycloakTest { .addPassword("password")); testRealms.add(builder.build()); - realm1 = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", REALM_NAME, AdminRoles.REALM_ADMIN, "password", "test-client", "secret", TLSUtils.initializeTLS()).realm(REALM_NAME); + adminClient1 = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", REALM_NAME, AdminRoles.REALM_ADMIN, "password", "test-client", "secret", TLSUtils.initializeTLS()); + realm1 = adminClient1.realm(REALM_NAME); builder = RealmBuilder.create().name(REALM2_NAME).testMail(); builder.client(ClientBuilder.create().clientId("test-client").publicClient().directAccessGrants()); @@ -74,9 +79,18 @@ public class CrossRealmPermissionsTest extends AbstractKeycloakTest { testRealms.add(builder.build()); - realm2 = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", REALM2_NAME, AdminRoles.REALM_ADMIN, "password", "test-client", "secret", TLSUtils.initializeTLS()).realm(REALM2_NAME); + adminClient2 = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", REALM2_NAME, AdminRoles.REALM_ADMIN, "password", "test-client", "secret", TLSUtils.initializeTLS()); + realm2 = adminClient2.realm(REALM2_NAME); } + + @AfterClass + public static void afterClass() { + adminClient1.close(); + adminClient2.close(); + } + + @Test public void users() { UserRepresentation user = UserBuilder.create().username("randomuser-" + Time.currentTimeMillis()).build(); diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/FineGrainAdminUnitTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/FineGrainAdminUnitTest.java index 29f912881d..4b9f733b02 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/FineGrainAdminUnitTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/FineGrainAdminUnitTest.java @@ -893,10 +893,10 @@ public class FineGrainAdminUnitTest extends AbstractKeycloakTest { String exchanged = oauth.doTokenExchange("master", token, "admin-cli", "kcinit", "password").getAccessToken(); Assert.assertNotNull(exchanged); - Keycloak client = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", - AuthRealm.MASTER, Constants.ADMIN_CLI_CLIENT_ID, exchanged, TLSUtils.initializeTLS()); - - Assert.assertNotNull(client.realm("master").roles().get("offline_access")); + try (Keycloak client = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", + AuthRealm.MASTER, Constants.ADMIN_CLI_CLIENT_ID, exchanged, TLSUtils.initializeTLS())) { + Assert.assertNotNull(client.realm("master").roles().get("offline_access")); + } } @Test @@ -949,42 +949,45 @@ public class FineGrainAdminUnitTest extends AbstractKeycloakTest { } }); - Keycloak client = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", - "test", "customer-a-manager", "password", Constants.ADMIN_CLI_CLIENT_ID, TLSUtils.initializeTLS()); + try (Keycloak client = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", + "test", "customer-a-manager", "password", Constants.ADMIN_CLI_CLIENT_ID, TLSUtils.initializeTLS())) { - List result = client.realm("test").users().search(null, "test", null, null, -1, 20); + List result = client.realm("test").users().search(null, "test", null, null, -1, 20); - Assert.assertEquals(20, result.size()); - Assert.assertThat(result, Matchers.everyItem(Matchers.hasProperty("username", Matchers.startsWith("b")))); + Assert.assertEquals(20, result.size()); + Assert.assertThat(result, Matchers.everyItem(Matchers.hasProperty("username", Matchers.startsWith("b")))); - result = client.realm("test").users().search(null, "test", null, null, 20, 40); + result = client.realm("test").users().search(null, "test", null, null, 20, 40); - Assert.assertEquals(0, result.size()); + Assert.assertEquals(0, result.size()); + } - client = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", - "test", "regular-admin-user", "password", Constants.ADMIN_CLI_CLIENT_ID, TLSUtils.initializeTLS()); + try (Keycloak client = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", + "test", "regular-admin-user", "password", Constants.ADMIN_CLI_CLIENT_ID, TLSUtils.initializeTLS())) { - result = client.realm("test").users().search(null, "test", null, null, -1, 20); + List result = client.realm("test").users().search(null, "test", null, null, -1, 20); - Assert.assertEquals(20, result.size()); - Assert.assertThat(result, Matchers.everyItem(Matchers.hasProperty("username", Matchers.startsWith("a")))); + Assert.assertEquals(20, result.size()); + Assert.assertThat(result, Matchers.everyItem(Matchers.hasProperty("username", Matchers.startsWith("a")))); - client.realm("test").users().search(null, null, null, null, -1, -1); + client.realm("test").users().search(null, null, null, null, -1, -1); - Assert.assertEquals(20, result.size()); - Assert.assertThat(result, Matchers.everyItem(Matchers.hasProperty("username", Matchers.startsWith("a")))); + Assert.assertEquals(20, result.size()); + Assert.assertThat(result, Matchers.everyItem(Matchers.hasProperty("username", Matchers.startsWith("a")))); + } - client = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", - "test", "customer-a-manager", "password", Constants.ADMIN_CLI_CLIENT_ID, TLSUtils.initializeTLS()); + try (Keycloak client = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", + "test", "customer-a-manager", "password", Constants.ADMIN_CLI_CLIENT_ID, TLSUtils.initializeTLS())) { - result = client.realm("test").users().search(null, null, null, null, -1, 20); + List result = client.realm("test").users().search(null, null, null, null, -1, 20); - Assert.assertEquals(20, result.size()); - Assert.assertThat(result, Matchers.everyItem(Matchers.hasProperty("username", Matchers.startsWith("b")))); + Assert.assertEquals(20, result.size()); + Assert.assertThat(result, Matchers.everyItem(Matchers.hasProperty("username", Matchers.startsWith("b")))); - result = client.realm("test").users().search("a", -1, 20, false); + result = client.realm("test").users().search("a", -1, 20, false); - Assert.assertEquals(0, result.size()); + Assert.assertEquals(0, result.size()); + } } } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/concurrency/AbstractConcurrencyTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/concurrency/AbstractConcurrencyTest.java index e115777bad..ed96f767df 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/concurrency/AbstractConcurrencyTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/concurrency/AbstractConcurrencyTest.java @@ -25,9 +25,12 @@ import org.keycloak.testsuite.AbstractKeycloakTest; import org.keycloak.testsuite.AbstractTestRealmKeycloakTest; import org.keycloak.testsuite.arquillian.undertow.TLSUtils; +import java.util.Collections; +import java.util.HashSet; import java.util.LinkedList; import java.util.Collection; import java.util.List; +import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutorService; @@ -78,11 +81,18 @@ public abstract class AbstractConcurrencyTest extends AbstractTestRealmKeycloakT Collection> tasks = new LinkedList<>(); Collection failures = new ConcurrentLinkedQueue<>(); final List> runnablesToTasks = new LinkedList<>(); + + // Track all used admin clients, so they can be closed after the test + Set usedKeycloaks = Collections.synchronizedSet(new HashSet<>()); + for (KeycloakRunnable runnable : runnables) { runnablesToTasks.add(() -> { int arrayIndex = currentThreadIndex.getAndIncrement() % numThreads; try { - runnable.run(arrayIndex % numThreads, keycloaks.get(), keycloaks.get().realm(REALM_NAME)); + Keycloak keycloak = keycloaks.get(); + usedKeycloaks.add(keycloak); + + runnable.run(arrayIndex % numThreads, keycloak, keycloak.realm(REALM_NAME)); } catch (Throwable ex) { failures.add(ex); } @@ -99,6 +109,14 @@ public abstract class AbstractConcurrencyTest extends AbstractTestRealmKeycloakT service.awaitTermination(3, TimeUnit.MINUTES); } catch (InterruptedException ex) { throw new RuntimeException(ex); + } finally { + for (Keycloak keycloak : usedKeycloaks) { + try { + keycloak.close(); + } catch (Exception e) { + failures.add(e); + } + } } if (! failures.isEmpty()) { diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/GroupTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/GroupTest.java index 800b778132..63c8367793 100755 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/GroupTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/GroupTest.java @@ -569,11 +569,12 @@ public class GroupTest extends AbstractGroupTest { RoleMappingResource mappings = realm.users().get(userId).roles(); mappings.realmLevel().add(Collections.singletonList(adminRole)); - Keycloak userClient = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", - realmName, userName, "pwd", Constants.ADMIN_CLI_CLIENT_ID, TLSUtils.initializeTLS()); + try (Keycloak userClient = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", + realmName, userName, "pwd", Constants.ADMIN_CLI_CLIENT_ID, TLSUtils.initializeTLS())) { - assertThat(userClient.realms().findAll(), // Any admin operation will do - not(empty())); + assertThat(userClient.realms().findAll(), // Any admin operation will do + not(empty())); + } } /** diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/GroupNamePolicyTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/GroupNamePolicyTest.java index b831f775d6..3668877067 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/GroupNamePolicyTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/GroupNamePolicyTest.java @@ -28,6 +28,8 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; +import javax.ws.rs.core.Response; + import org.junit.Before; import org.junit.Test; import org.keycloak.admin.client.resource.AuthorizationResource; @@ -195,7 +197,8 @@ public class GroupNamePolicyTest extends AbstractAuthzTest { policy.setGroupsClaim("groups"); policy.addGroupPath(groupPath, extendChildren); - getClient().authorization().policies().group().create(policy); + Response response = getClient().authorization().policies().group().create(policy); + response.close(); } private void createResourcePermission(String name, String resource, String... policies) { @@ -205,19 +208,21 @@ public class GroupNamePolicyTest extends AbstractAuthzTest { permission.addResource(resource); permission.addPolicy(policies); - getClient().authorization().permissions().resource().create(permission); + Response response = getClient().authorization().permissions().resource().create(permission); + response.close(); } private void createResource(String name) { AuthorizationResource authorization = getClient().authorization(); ResourceRepresentation resource = new ResourceRepresentation(name); - authorization.resources().create(resource); + Response response = authorization.resources().create(resource); + response.close(); } private RealmResource getRealm() { try { - return AdminClientUtil.createAdminClient().realm("authz-test"); + return getAdminClient().realm("authz-test"); } catch (Exception e) { throw new RuntimeException("Failed to create admin client"); } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/GroupPathPolicyTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/GroupPathPolicyTest.java index 77ce401b99..25bdcdf893 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/GroupPathPolicyTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/GroupPathPolicyTest.java @@ -28,6 +28,8 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; +import javax.ws.rs.core.Response; + import org.junit.Before; import org.junit.Test; import org.keycloak.admin.client.resource.AuthorizationResource; @@ -179,7 +181,8 @@ public class GroupPathPolicyTest extends AbstractAuthzTest { policy.setGroupsClaim("groups"); policy.addGroupPath(groupPath, extendChildren); - getClient().authorization().policies().group().create(policy); + Response response = getClient().authorization().policies().group().create(policy); + response.close(); } private void createResourcePermission(String name, String resource, String... policies) { @@ -189,19 +192,21 @@ public class GroupPathPolicyTest extends AbstractAuthzTest { permission.addResource(resource); permission.addPolicy(policies); - getClient().authorization().permissions().resource().create(permission); + Response response = getClient().authorization().permissions().resource().create(permission); + response.close(); } private void createResource(String name) { AuthorizationResource authorization = getClient().authorization(); ResourceRepresentation resource = new ResourceRepresentation(name); - authorization.resources().create(resource); + Response response = authorization.resources().create(resource); + response.close(); } private RealmResource getRealm() { try { - return AdminClientUtil.createAdminClient().realm("authz-test"); + return getAdminClient().realm("authz-test"); } catch (Exception e) { throw new RuntimeException("Failed to create admin client"); } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/RolePolicyTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/RolePolicyTest.java index 05ce1e9fd6..25fc4fa0cd 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/RolePolicyTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/authz/RolePolicyTest.java @@ -23,6 +23,8 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; +import javax.ws.rs.core.Response; + import org.junit.Before; import org.junit.Test; import org.keycloak.admin.client.resource.AuthorizationResource; @@ -177,7 +179,8 @@ public class RolePolicyTest extends AbstractAuthzTest { policy.addRole(role); } - getClient().authorization().policies().role().create(policy); + Response response = getClient().authorization().policies().role().create(policy); + response.close(); } private void createResourcePermission(String name, String resource, String... policies) { @@ -187,19 +190,21 @@ public class RolePolicyTest extends AbstractAuthzTest { permission.addResource(resource); permission.addPolicy(policies); - getClient().authorization().permissions().resource().create(permission); + Response response = getClient().authorization().permissions().resource().create(permission); + response.close(); } private void createResource(String name) { AuthorizationResource authorization = getClient().authorization(); ResourceRepresentation resource = new ResourceRepresentation(name); - authorization.resources().create(resource); + Response response = authorization.resources().create(resource); + response.close(); } private RealmResource getRealm() { try { - return AdminClientUtil.createAdminClient().realm("authz-test"); + return getAdminClient().realm("authz-test"); } catch (Exception e) { throw new RuntimeException("Failed to create admin client"); } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/cluster/AbstractClusterTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/cluster/AbstractClusterTest.java index 1fab946c86..9700557d65 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/cluster/AbstractClusterTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/cluster/AbstractClusterTest.java @@ -2,6 +2,7 @@ package org.keycloak.testsuite.cluster; import org.jboss.arquillian.container.test.api.ContainerController; import org.jboss.arquillian.test.api.ArquillianResource; +import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.keycloak.admin.client.Keycloak; @@ -38,9 +39,9 @@ public abstract class AbstractClusterTest extends AbstractKeycloakTest { @ArquillianResource protected ContainerController controller; - protected Map backendAdminClients = new HashMap<>(); + protected static Map backendAdminClients = new HashMap<>(); - protected Map backendTestingClients = new HashMap<>(); + protected static Map backendTestingClients = new HashMap<>(); private int currentFailNodeIndex = 0; @@ -165,6 +166,16 @@ public abstract class AbstractClusterTest extends AbstractKeycloakTest { ContainerAssume.assumeClusteredContainer(); } + @AfterClass + public static void closeClients() { + backendAdminClients.values().forEach(Keycloak::close); + backendAdminClients.clear(); + + backendTestingClients.values().forEach(KeycloakTestingClient::close); + backendTestingClients.clear(); + + } + @Before public void beforeClusterTest() { failback(); diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/url/FixedHostnameTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/url/FixedHostnameTest.java index 988b2a7c58..76067a8aec 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/url/FixedHostnameTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/url/FixedHostnameTest.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.jboss.arquillian.container.test.api.ContainerController; import org.jboss.arquillian.test.api.ArquillianResource; +import org.junit.After; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; @@ -42,8 +43,6 @@ public class FixedHostnameTest extends AbstractKeycloakTest { private String authServerUrl; - private Keycloak testAdminClient; - @Override public void addTestRealms(List testRealms) { RealmRepresentation realm = loadJson(getClass().getResourceAsStream("/testrealm.json"), RealmRepresentation.class); @@ -62,11 +61,10 @@ public class FixedHostnameTest extends AbstractKeycloakTest { public void fixedHostname() throws Exception { authServerUrl = oauth.AUTH_SERVER_ROOT; oauth.baseUrl(authServerUrl); - testAdminClient = AdminClientUtil.createAdminClient(suiteContext.isAdapterCompatTesting(), AuthServerTestEnricher.getAuthServerContextRoot()); oauth.clientId("direct-grant"); - try { + try (Keycloak testAdminClient = AdminClientUtil.createAdminClient(suiteContext.isAdapterCompatTesting(), AuthServerTestEnricher.getAuthServerContextRoot())) { assertWellKnown("test", AUTH_SERVER_SCHEME + "://localhost:" + AUTH_SERVER_PORT); configureFixedHostname(-1, -1, false); @@ -77,8 +75,8 @@ public class FixedHostnameTest extends AbstractKeycloakTest { assertTokenIssuer("test", AUTH_SERVER_SCHEME + "://keycloak.127.0.0.1.nip.io:" + AUTH_SERVER_PORT); assertTokenIssuer("hostname", AUTH_SERVER_SCHEME + "://custom-domain.127.0.0.1.nip.io:" + AUTH_SERVER_PORT); - assertInitialAccessTokenFromMasterRealm("test", AUTH_SERVER_SCHEME + "://keycloak.127.0.0.1.nip.io:" + AUTH_SERVER_PORT); - assertInitialAccessTokenFromMasterRealm("hostname", AUTH_SERVER_SCHEME + "://custom-domain.127.0.0.1.nip.io:" + AUTH_SERVER_PORT); + assertInitialAccessTokenFromMasterRealm(testAdminClient,"test", AUTH_SERVER_SCHEME + "://keycloak.127.0.0.1.nip.io:" + AUTH_SERVER_PORT); + assertInitialAccessTokenFromMasterRealm(testAdminClient,"hostname", AUTH_SERVER_SCHEME + "://custom-domain.127.0.0.1.nip.io:" + AUTH_SERVER_PORT); } finally { clearFixedHostname(); } @@ -89,11 +87,10 @@ public class FixedHostnameTest extends AbstractKeycloakTest { // Make sure request are always sent with http authServerUrl = "http://localhost:8180/auth"; oauth.baseUrl(authServerUrl); - testAdminClient = AdminClientUtil.createAdminClient(suiteContext.isAdapterCompatTesting(), "http://localhost:8180"); oauth.clientId("direct-grant"); - try { + try (Keycloak testAdminClient = AdminClientUtil.createAdminClient(suiteContext.isAdapterCompatTesting(), "http://localhost:8180")) { assertWellKnown("test", "http://localhost:8180"); configureFixedHostname(80, -1, false); @@ -104,8 +101,8 @@ public class FixedHostnameTest extends AbstractKeycloakTest { assertTokenIssuer("test", "http://keycloak.127.0.0.1.nip.io"); assertTokenIssuer("hostname", "http://custom-domain.127.0.0.1.nip.io"); - assertInitialAccessTokenFromMasterRealm("test", "http://keycloak.127.0.0.1.nip.io"); - assertInitialAccessTokenFromMasterRealm("hostname", "http://custom-domain.127.0.0.1.nip.io"); + assertInitialAccessTokenFromMasterRealm(testAdminClient,"test", "http://keycloak.127.0.0.1.nip.io"); + assertInitialAccessTokenFromMasterRealm(testAdminClient,"hostname", "http://custom-domain.127.0.0.1.nip.io"); } finally { clearFixedHostname(); } @@ -116,11 +113,10 @@ public class FixedHostnameTest extends AbstractKeycloakTest { // Make sure request are always sent with http authServerUrl = "http://localhost:8180/auth"; oauth.baseUrl(authServerUrl); - testAdminClient = AdminClientUtil.createAdminClient(suiteContext.isAdapterCompatTesting(), "http://localhost:8180"); oauth.clientId("direct-grant"); - try { + try (Keycloak testAdminClient = AdminClientUtil.createAdminClient(suiteContext.isAdapterCompatTesting(), "http://localhost:8180")) { assertWellKnown("test", "http://localhost:8180"); configureFixedHostname(-1, 443, true); @@ -131,14 +127,14 @@ public class FixedHostnameTest extends AbstractKeycloakTest { assertTokenIssuer("test", "https://keycloak.127.0.0.1.nip.io"); assertTokenIssuer("hostname", "https://custom-domain.127.0.0.1.nip.io"); - assertInitialAccessTokenFromMasterRealm("test", "https://keycloak.127.0.0.1.nip.io"); - assertInitialAccessTokenFromMasterRealm("hostname", "https://custom-domain.127.0.0.1.nip.io"); + assertInitialAccessTokenFromMasterRealm(testAdminClient, "test", "https://keycloak.127.0.0.1.nip.io"); + assertInitialAccessTokenFromMasterRealm(testAdminClient, "hostname", "https://custom-domain.127.0.0.1.nip.io"); } finally { clearFixedHostname(); } } - private void assertInitialAccessTokenFromMasterRealm(String realm, String expectedBaseUrl) throws JWSInputException, ClientRegistrationException { + private void assertInitialAccessTokenFromMasterRealm(Keycloak testAdminClient, String realm, String expectedBaseUrl) throws JWSInputException, ClientRegistrationException { ClientInitialAccessCreatePresentation rep = new ClientInitialAccessCreatePresentation(); rep.setCount(1); rep.setExpiration(10000); diff --git a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/broker/OIDCKeycloakServerBrokerWithConsentTest.java b/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/broker/OIDCKeycloakServerBrokerWithConsentTest.java index b2ecd4145b..acee4ed099 100644 --- a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/broker/OIDCKeycloakServerBrokerWithConsentTest.java +++ b/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/broker/OIDCKeycloakServerBrokerWithConsentTest.java @@ -17,6 +17,7 @@ package org.keycloak.testsuite.broker; +import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.ClassRule; @@ -33,7 +34,6 @@ import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.services.managers.RealmManager; import org.keycloak.testsuite.KeycloakServer; import org.keycloak.testsuite.rule.AbstractKeycloakRule; -import org.openqa.selenium.NoSuchElementException; import java.util.List; @@ -97,6 +97,12 @@ public class OIDCKeycloakServerBrokerWithConsentTest extends AbstractIdentityPro realmWithBroker.update(realmRep); } + @AfterClass + public static void after() { + keycloak1.close(); + keycloak2.close(); + } + @Override protected String getProviderId() { diff --git a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/BrokenUserStorageTest.java b/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/BrokenUserStorageTest.java index 6d7ea34742..c7b3a8025f 100644 --- a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/BrokenUserStorageTest.java +++ b/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/federation/storage/BrokenUserStorageTest.java @@ -113,21 +113,22 @@ public class BrokenUserStorageTest { // make sure we can list components and delete provider as this is an admin console operation - Keycloak keycloakAdmin = Keycloak.getInstance(AUTH_SERVER_URL, "master", "admin", "admin", Constants.ADMIN_CLI_CLIENT_ID); - RealmResource master = keycloakAdmin.realms().realm("master"); - List components = master.components().query(masterId, UserStorageProvider.class.getName()); - boolean found = false; - for (ComponentRepresentation rep : components) { - if (rep.getName().equals("bad-provider-id")) { - found = true; + try (Keycloak keycloakAdmin = Keycloak.getInstance(AUTH_SERVER_URL, "master", "admin", "admin", Constants.ADMIN_CLI_CLIENT_ID)) { + RealmResource master = keycloakAdmin.realms().realm("master"); + List components = master.components().query(masterId, UserStorageProvider.class.getName()); + boolean found = false; + for (ComponentRepresentation rep : components) { + if (rep.getName().equals("bad-provider-id")) { + found = true; + } } + Assert.assertTrue(found); + + master.components().component(component.getId()).remove(); + + List components2 = master.components().query(masterId, UserStorageProvider.class.getName()); + Assert.assertEquals(components.size() - 1, components2.size()); } - Assert.assertTrue(found); - - master.components().component(component.getId()).remove(); - - List components2 = master.components().query(masterId, UserStorageProvider.class.getName()); - Assert.assertEquals(components.size() - 1, components2.size()); } @After diff --git a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/helper/adapter/AdapterTestStrategy.java b/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/helper/adapter/AdapterTestStrategy.java index c35ddab895..e6d3200164 100755 --- a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/helper/adapter/AdapterTestStrategy.java +++ b/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/helper/adapter/AdapterTestStrategy.java @@ -202,18 +202,20 @@ public class AdapterTestStrategy extends ExternalResource { Assert.assertTrue(pageSource.contains("iPhone") && pageSource.contains("iPad")); // View stats - List> stats = Keycloak.getInstance("http://localhost:8081/auth", "master", "admin", "admin", Constants.ADMIN_CLI_CLIENT_ID).realm("demo").getClientSessionStats(); - Map customerPortalStats = null; - Map productPortalStats = null; - for (Map s : stats) { - if (s.get("clientId").equals("customer-portal")) { - customerPortalStats = s; - } else if (s.get("clientId").equals("product-portal")) { - productPortalStats = s; + try (Keycloak adminClient = Keycloak.getInstance("http://localhost:8081/auth", "master", "admin", "admin", Constants.ADMIN_CLI_CLIENT_ID)) { + List> stats = adminClient.realm("demo").getClientSessionStats(); + Map customerPortalStats = null; + Map productPortalStats = null; + for (Map s : stats) { + if (s.get("clientId").equals("customer-portal")) { + customerPortalStats = s; + } else if (s.get("clientId").equals("product-portal")) { + productPortalStats = s; + } } + Assert.assertEquals(1, Integer.parseInt(customerPortalStats.get("active"))); + Assert.assertEquals(1, Integer.parseInt(productPortalStats.get("active"))); } - Assert.assertEquals(1, Integer.parseInt(customerPortalStats.get("active"))); - Assert.assertEquals(1, Integer.parseInt(productPortalStats.get("active"))); // test logout String logoutUri = OIDCLoginProtocolService.logoutUrl(UriBuilder.fromUri(AUTH_SERVER_URL)) @@ -603,15 +605,16 @@ public class AdapterTestStrategy extends ExternalResource { loginAndCheckSession(driver, loginPage); // logout mposolda with admin client - Keycloak keycloakAdmin = Keycloak.getInstance(AUTH_SERVER_URL, "master", "admin", "admin", Constants.ADMIN_CLI_CLIENT_ID); - UserRepresentation mposolda = keycloakAdmin.realm("demo").users().search("mposolda", null, null, null, null, null).get(0); - keycloakAdmin.realm("demo").users().get(mposolda.getId()).logout(); + try (Keycloak keycloakAdmin = Keycloak.getInstance(AUTH_SERVER_URL, "master", "admin", "admin", Constants.ADMIN_CLI_CLIENT_ID)) { + UserRepresentation mposolda = keycloakAdmin.realm("demo").users().search("mposolda", null, null, null, null, null).get(0); + keycloakAdmin.realm("demo").users().get(mposolda.getId()).logout(); - // bburke should be still logged with original httpSession in our browser window - driver.navigate().to(APP_SERVER_BASE_URL + "/session-portal"); - Assert.assertEquals(driver.getCurrentUrl(), APP_SERVER_BASE_URL + "/session-portal" + slash); - String pageSource = driver.getPageSource(); - Assert.assertTrue(pageSource.contains("Counter=3")); + // bburke should be still logged with original httpSession in our browser window + driver.navigate().to(APP_SERVER_BASE_URL + "/session-portal"); + Assert.assertEquals(driver.getCurrentUrl(), APP_SERVER_BASE_URL + "/session-portal" + slash); + String pageSource = driver.getPageSource(); + Assert.assertTrue(pageSource.contains("Counter=3")); + } } /**