8000 is-45364 migrate Client Api V2 tests to utilize adminClient by robson90 · Pull Request #47789 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

is-45364 migrate Client Api V2 tests to utilize adminClient - #47789

Merged
vmuzikar merged 2 commits into
keycloak:mainfrom
robson90:is-45364-migrate-tests-to-admin-client
May 5, 2026
Merged

vmuzikar merged 2 commits into
keycloak:mainfrom
robson90:is-45364-migrate-tests-to-admin-client

Conversation

@robson90
@robson90 robson90 commented Apr 7, 2026 •
Copy link
Copy Markdown
Contributor

Relates to: #45366

CC: @mabartos & @vmuzikar

EDIT:
Migrate Client API V2 Tests from http calls to using the Keycloak adminClient.
Original Issue: #45364
Related PR: #46973

Comment on lines +85 to +113
@InjectAdminClient(mode = InjectAdminClient.Mode.MANAGED_REALM,
user = "realm-admin",
client = "test-client",
ref = "realmAdmin")
Keycloak realmAdminAdminClient;

@InjectAdminClient(mode = InjectAdminClient.Mode.MANAGED_REALM,
user = "view-clients",
client = "test-client",
ref = "viewClients")
Keycloak viewClientsAdminClient;

@InjectAdminClient(mode = InjectAdminClient.Mode.MANAGED_REALM,
user = "query-clients",
client = "test-client",
ref = "queryClients")
Keycloak queryClientsAdminClient;

@InjectAdminClient(mode = InjectAdminClient.Mode.MANAGED_REALM,
user = "manage-clients",
client = "test-client",
ref = "manageClients")
Keycloak manageClientsAdminClient;

@InjectAdminClient(mode = InjectAdminClient.Mode.MANAGED_REALM,
user = "no-access",
client = "test-client",
ref = "noAccess")
Keycloak noAccessAdminClient;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear @mabartos , you got any better idea for handling the user specific tests ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's alright to have managed admin clients as this. Maybe @lhanusov or @vaceksimon have some ideas how to deal with numerous admin clients.

@robson90 For the abstract class, you could also overload the getClientsApi() like this:

public ClientsApi getClientsApi(Keycloak adminClient) {
        return adminClient.clients(getRealmName()).v2();
    }

And instead of using the:

try (var response = realmAdminAdminClient.clients(getRealmName()).v2().getClients()) {

Using:

try (var response = getClientsApi(realmAdminAdminClient).getClients()) {

But it's a nitpick.

@michalvavrik michalvavrik left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the PR description: Relates to: https://github.com/keycloak/keycloak/issues/45366, how exactly? Can you write better PR description or some commit message explaining changes in this PR, please? I really don't want to nitpick, but the PR title didn't help me much either.

@robson90
robson90 commented Apr 7, 2026
Copy link
Copy Markdown
Contributor Author

Dear @michalvavrik , Ive updated the description. Hope that helps.

For now I just wanted to gather some feedback for implementation in: ClientApiV2AuthorizationTest.java
As I didn't want to migrate all tests and rework everything afterwards.

@michalvavrik
Copy link
Copy Markdown
Member

Thank you @robson90 .

@michalvavrik michalvavrik left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through changes, it looks fine. I won't go into detailed review since this is still a draft and there is open question.

@mabartos mabartos left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robson90 I think it's a good way forward :) Thanks! Added some comments.

Comment on lines +85 to +113
@InjectAdminClient(mode = InjectAdminClient.Mode.MANAGED_REALM,
user = "realm-admin",
client = "test-client",
ref = "realmAdmin")
Keycloak realmAdminAdminClient;

@InjectAdminClient(mode = InjectAdminClient.Mode.MANAGED_REALM,
user = "view-clients",
client = "test-client",
ref = "viewClients")
Keycloak viewClientsAdminClient;

@InjectAdminClient(mode = InjectAdminClient.Mode.MANAGED_REALM,
user = "query-clients",
client = "test-client",
ref = "queryClients")
Keycloak queryClientsAdminClient;

@InjectAdminClient(mode = InjectAdminClient.Mode.MANAGED_REALM,
user = "manage-clients",
client = "test-client",
ref = "manageClients")
Keycloak manageClientsAdminClient;

@InjectAdminClient(mode = InjectAdminClient.Mode.MANAGED_REALM,
user = "no-access",
client = "test-client",
ref = "noAccess")
Keycloak noAccessAdminClient;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's alright to have managed admin clients as this. Maybe @lhanusov or @vaceksimon have some ideas how to deal with numerous admin clients.

@robson90 For the abstract class, you could also overload the getClientsApi() like this:

public ClientsApi getClientsApi(Keycloak adminClient) {
        return adminClient.clients(getRealmName()).v2();
    }

And instead of using the:

try (var response = realmAdminAdminClient.clients(getRealmName()).v2().getClients()) {

Using:

try (var response = getClientsApi(realmAdminAdminClient).getClients()) {

But it's a nitpick.

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Migrates Admin API v2 client-related integration tests from raw HTTP calls to using the generated v2 adminClient APIs, aligning tests with the new Java client work tracked in #45364.

Changes:

  • Introduce shared helper accessors in AbstractClientApiV2Test to obtain ClientsApi / ClientApi instances from the injected adminClient.
  • Update multiple test classes to use adminClient.clients(realm).v2() (and related helpers) instead of manual HTTP requests.
  • Adjust some tests to use realm-scoped injections (ManagedRealm) and JAX-RS exceptions (NotFoundException, ForbiddenException) rather than status-code assertions from HTTP responses.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
rest/admin-v2/tests/src/test/java/org/keycloak/tests/admin/client/v2/ClientApiV2Test.java Switches many client v2 tests to use ClientsApi / ClientApi helpers; introduces realm-name override.
rest/admin-v2/tests/src/test/java/org/keycloak/tests/admin/client/v2/ClientApiV2DisabledTest.java Converts disabled-feature test from HTTP GET to admin client call + NotFoundException assertion.
rest/admin-v2/tests/src/test/java/org/keycloak/tests/admin/client/v2/ClientApiV2AuthorizationTest.java Migrates parts of authorization tests (list/create) to use managed admin clients and v2 APIs.
rest/admin-v2/tests/src/test/java/org/keycloak/tests/admin/client/v2/AdminEventV2Test.java Migrates admin-event tests from HTTP calls to v2 admin client calls and switches event setup to a managed test realm.
rest/admin-v2/tests/src/test/java/org/keycloak/tests/admin/client/v2/AbstractClientApiV2Test.java Adds helper methods for constructing v2 API clients/URLs based on the test realm.

@robson90
robson90 force-pushed the is-45364-migrate-tests-to-admin-client branch 2 times, most recently from f7e46f1 to cd3190e Compare April 14, 2026 08:22
@robson90
robson90 requested a review from Copilot April 14, 2026 08:23
@robson90
robson90 marked this pull request as ready for review April 14, 2026 08:23
@robson90
robson90 requested review from a team as code owners April 14, 2026 08:23
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.

Comments suppressed due to low confidence (1)

rest/admin-v2/tests/src/test/java/org/keycloak/tests/admin/client/v2/AdminEventV2Test.java:32

  • After the migration to adminClient-based calls, the injected HTTP client (@InjectHttpClient CloseableHttpClient) and the masterRealm injection appear unused in this test. If the build enforces unused imports/fields, this can fail checkstyle/compilation; consider removing the now-unused imports and corresponding injected fields.
import org.keycloak.testframework.annotations.InjectHttpClient;
import org.keycloak.testframework.annotations.InjectRealm;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.realm.ManagedRealm;

@@ -1006,7 +1007,7 @@ private OIDCClientRepresentation getTestingFullClientRep() {
rep.setRoles(Set.of("view-consent", "manage-account"));
rep.setLoginFlows(Set.of(OIDCClientRepresentation.Flow.SERVICE_ACCOUNT));
// TODO when roles are not set and SA is enabled, the default role 'default-roles-master' for the SA is used for the master realm
rep.setServiceAccountRoles(Set.of("default-roles-%s".formatted(testRealm.getName())));
rep.setServiceAccountRoles(Set.of("default-roles-%s".formatted(testRealm.getName()).toLowerCase()));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear @mabartos , I ran into a problem here.

My realm name is "testRealm", then this fetching of default roles failed, as it was "default-roles-testRealm", which is wrong.

What do you think about adding a helper function to the ManagedRealm, something like get defaultRoleName ?

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 9 comments.

@robson90
robson90 force-pushed the is-45364-migrate-tests-to-admin-client branch from 7873982 to a149506 Compare April 20, 2026 10:01
@robson90
robson90 requested a review from Copilot April 20, 2026 10:10
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

@Pepo48 Pepo48 left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @robson90, I went through the PR and noticed that ClientApiV2AuthorizationTest still carries the old AdminClientFactory setup (adminClients map, setupClients(), CURRENT_USERS, createAdminClient()) even though all tests except getRealmAdmin() already use @InjectAdminClient. That's 7 redundant client instances per run.

I migrated getRealmAdmin() to use realmAdminAdminClient directly, which allowed removing the map, factory, CloseableHttpClient, and 9 unused imports. All 13 tests pass and it's a bit faster.

As I removed bunch of code I didn't want to clutter it here with several comments and instead I opened a PR against your branch - robson90#1.

There should not be a test parity gap but please check thoroughly and feel free to do the necessary changes. Thank you very much!

@Pepo48 Pepo48 left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InteropTest.adminClient shadows the inherited field - getClientsApi() and test methods end up using different instances. I suggest to remove the shadowing declaration and unused CloseableHttpClient.

That should match ClientPoliciesV2Test.

@Pepo48 Pepo48 left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that both resolve identically but since all the other tests use the parameterless getClientsApi(), I would keep it consistent.

@robson90
Copy link
Copy Markdown
Contributor Author

@Pepo48 thanks for the review, have inserted the requested changes !

@robson90
robson90 force-pushed the is-45364-migrate-tests-to-admin-client branch from 3590639 to 1eee363 Compare April 27, 2026 10:19
@robson90
robson90 requested a review from Pepo48 April 27, 2026 10:31
Pepo48
Pepo48 previously approved these changes Apr 27, 2026
@Pepo48 Pepo48 left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now, thanks @robson90!

@vmuzikar vmuzikar left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good (considering also @Pepo48's deeper review), thank you.

@robson90 Can you please rebase?

Signed-off-by: Robin Meese <39960884+robson90@users.noreply.github.com>
@robson90
robson90 force-pushed the is-45364-migrate-tests-to-admin-client branch from 1eee363 to 6d196cc Compare May 5, 2026 06:59
@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.forms.BruteForceTest#testNoFailureResetForPermanentLockout

Keycloak CI - Base IT (5)

org.opentest4j.AssertionFailedError: Expected error event ==> expected: <true> but was: <false>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertTrue.failNotTrue(AssertTrue.java:63)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:36)
...

Report flaky test

@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unreported flaky test detected, please review

…t AdminClientFactory setup

Signed-off-by: Peter Zaoral <pzaoral@redhat.com>

is-45364 fix PR comments

Signed-off-by: Robin Meese <39960884+robson90@users.noreply.github.com>
@robson90
robson90 force-pushed the is-45364-migrate-tests-to-admin-client branch from 6d196cc to 916c088 Compare May 5, 2026 08:26
@vmuzikar vmuzikar left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you.

@vmuzikar
vmuzikar merged commit 7894550 into keycloak:main May 5, 2026
86 checks passed
@stianst stianst mentioned this pull request Jun 1, 2026
sashyo pushed a commit to tide-foundation/keycloak-IGA that referenced this pull request Jul 20, 2026
…#47789)

* is-45364 migrate Client Api V2 tests to utilize adminClient

Signed-off-by: Robin Meese <39960884+robson90@users.noreply.github.com>

* Migrate getRealmAdmin() to use @InjectAdminClient and remove redundant AdminClientFactory setup

Signed-off-by: Peter Zaoral <pzaoral@redhat.com>

is-45364 fix PR comments

Signed-off-by: Robin Meese <39960884+robson90@users.noreply.github.com>

---------

Signed-off-by: Robin Meese <39960884+robson90@users.noreply.github.com>
Co-authored-by: Peter Zaoral <pzaoral@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

0