8000 is-45364 Proposal for getClientApi() helper method by robson90 · Pull Request #47527 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

is-45364 Proposal for getClientApi() helper method - #47527

Closed
robson90 wants to merge 1 commit into
keycloak:mainfrom
robson90:is-45364-get-client-api
Closed

robson90 wants to merge 1 commit into
keycloak:mainfrom
robson90:is-45364-get-client-api

Conversation

@robson90
@robson90 robson90 commented Mar 27, 2026 •
Copy link
Copy Markdown
Contributor

As discussed here: #46973 (comment)

Here is my proposal for the helper method.

This was the only option, to still have the option to overwrite the Injection in the child class.

Abstract class:

    protected abstract Keycloak getAdminClient();
    protected abstract ManagedRealm getTestRealm();

    public ClientApi getClientApi(String clientId) {
        return getAdminClient().clients(getTestRealm().getName()).v2().client(clientId);
    }

Child class:

......
    @InjectRealm(config = NoAccessRealmConfig.class)
    ManagedRealm testRealm;

    @Override
    protected Keycloak getAdminClient() {
        return this.adminClient;
    }

    @Override
    protected ManagedRealm getTestRealm() {
        return this.testRealm;
    }

    @Test
    public void getClient() {
        var client = getClientApi("account").getClient();
        assertEquals("account", client.getClientId());
    }

CC: @mabartos && @vmuzikar

Signed-off-by: Robin Meese <39960884+robson90@users.noreply.github.com>
@mabartos
mabartos commented Mar 27, 2026 •
Copy link
Copy Markdown
Member

@robson90 Or even something like this in the parent class?

    @InjectAdminClient
    Keycloak adminClient;

    public ClientApi getClientApi(Keycloak adminClient, String realmName, String clientId) {
        return adminClient.clients(realmName).v2().client(clientId);
    }

    public ClientApi getClientApi(String realmName, String clientId) {
        return getClientApi(adminClient, realmName, clientId);
    }

    public ClientApi getClientApi(String clientId) {
        return getClientApi(getRealmName(), clientId);
    }

The majority of the test might use only one adminClient and one realm, but sometimes, there are additional requirements. So, the getClientApi("<clientId>") should be used the most, and for some cases, using the other ones.

@robson90
Copy link
Copy Markdown
Contributor Author

@mabartos works for me, will adjust it.

I just want to point out, it won't be possible to Inject with specific properties, for example:

    @InjectRealm(config = NoAccessRealmConfig.class)
    ManagedRealm testRealm;

Right now that is only the case for the ManagedRealm, as the AdminClient has no config, as far as I have seen.

@mabartos
Copy link
Copy Markdown
Member

Right now that is only the case for the ManagedRealm, as the AdminClient has no config, as far as I have seen.

@robson90 It's only for tests, so we can adjust how we want if we need some kind of functionality. But even with this managed realm, you can then specify the realm name as the testRealm.getName(). Or override the getRealmName() if necessary.

@robson90
Copy link
Copy Markdown
Contributor Author

Yeah works for me, thanks !

@robson90 robson90 closed this Mar 30, 2026
@robson90
robson90 deleted the is-45364-get-client-api branch March 30, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0