Class FluentTestsHelper

java.lang.Object
org.keycloak.test.FluentTestsHelper
All Implemented Interfaces:
Closeable, AutoCloseable

public class FluentTestsHelper extends Object implements Closeable
A helper class that makes creating tests a bit easier.

Usage example:


    new FluentTestsHelper()
        .init()
        .createDirectGrantClient("direct-grant-client")
        .deleteClient("direct-grant-client")
        .createTestUser("seb", "seb")
        .assignRoleWithUser("seb", "user")
        .deleteTestUser("seb")
        .deleteRole("user");
    

  • Field Details

  • Constructor Details

    • FluentTestsHelper

      public FluentTestsHelper()
      Creates a new helper instance.
    • FluentTestsHelper

      public FluentTestsHelper(String adminUserName, String adminPassword)
      Creates a new helper instance.
      Parameters:
      adminUserName - Admin username.
      adminPassword - Admin password.
    • FluentTestsHelper

      public FluentTestsHelper(String keycloakBaseUrl, String adminUserName, String adminPassword, String adminRealm, String adminClient, String testRealm)
      Creates a new helper instance.
      Parameters:
      keycloakBaseUrl - Full keycloak URL.
      adminUserName - Admin username.
      adminPassword - Admin password.
      adminRealm - Master realm name.
      adminClient - Admin Client name.
      testRealm - new instance.
  • Method Details

    • init

      public FluentTestsHelper init()
      Initialization method.
      Returns:
      this
    • isInitialized

      public boolean isInitialized()
      Returns:
      Returns true if this helper has been initialized.
    • createKeycloakInstance

      protected Keycloak createKeycloakInstance(String keycloakBaseUrl, String realm, String username, String password, String clientId)
    • getKeycloakInstance

      public Keycloak getKeycloakInstance()
      For more complex test scenarios
      Returns:
      Keycloak Client instance
    • generateInitialAccessToken

      protected String generateInitialAccessToken()
    • createClient

      public FluentTestsHelper createClient(org.keycloak.representations.idm.ClientRepresentation clientRepresentation) throws ClientRegistrationException, com.fasterxml.jackson.core.JsonProcessingException
      Creates a new client based on its representation.
      Parameters:
      clientRepresentation - Client data.
      Returns:
      this
      Throws:
      ClientRegistrationException
      com.fasterxml.jackson.core.JsonProcessingException
    • createDirectGrantClient

      public FluentTestsHelper createDirectGrantClient(String clientId) throws ClientRegistrationException, com.fasterxml.jackson.core.JsonProcessingException
      Creates a direct grant client.
      Throws:
      ClientRegistrationException
      com.fasterxml.jackson.core.JsonProcessingException
    • deleteClient

      public FluentTestsHelper deleteClient(String clientId) throws ClientRegistrationException
      Deletes a client previously created by this helper. This will throw an error if you try to delete an arbitrary client.
      Parameters:
      clientId - Client id to be deleted.
      Returns:
      this
      Throws:
      ClientRegistrationException - Thrown when client registration error occurs.
    • importTestRealm

      public FluentTestsHelper importTestRealm(String realmJsonPath) throws IOException
      Throws:
      IOException
      See Also:
    • importTestRealm

      public FluentTestsHelper importTestRealm(File realmJsonPath) throws IOException
      Throws:
      IOException
      See Also:
    • importTestRealm

      public FluentTestsHelper importTestRealm(InputStream stream) throws IOException
      Import a test realm.
      Parameters:
      stream - A stream representing a JSON file with an exported realm.
      Returns:
      this
      Throws:
      IOException - Thrown in case of parsing error.
    • createTestRealm

      public FluentTestsHelper createTestRealm(org.keycloak.representations.idm.RealmRepresentation realmRepresentation)
      Creates a test realm.
      Parameters:
      realmRepresentation - A test realm representation.
      Returns:
      this
    • deleteRealm

      public FluentTestsHelper deleteRealm(String realmName)
      Deletes a realm.
      Parameters:
      realmName - Realm to be deleted.
      Returns:
      this
    • deleteTestRealm

      public FluentTestsHelper deleteTestRealm()
      Deletes the test realm. Meant to be called after testing has finished.
      Returns:
      this
    • createTestUser

      public FluentTestsHelper createTestUser(String username, String password)
      Creates a test user.
      Parameters:
      username - A username to be created.
      password - A password for a user.
      Returns:
      this
    • assignRoleWithUser

      public FluentTestsHelper assignRoleWithUser(String userName, String roleName)
      Associates a user with a role. This method also creates a role if that is missing.
      Parameters:
      userName - A username to be associated with a role.
      roleName - A role to be associated with a user name.
      Returns:
      this
    • deleteRole

      public FluentTestsHelper deleteRole(String roleName)
      Deletes a role.
      Parameters:
      roleName - A Role name to be deleted.
      Returns:
      this
    • deleteTestUser

      public FluentTestsHelper deleteTestUser(String userName)
      Deletes a user.
      Parameters:
      userName - A Username to be deleted.
      Returns:
      this
    • getCreatedId

      public String getCreatedId(jakarta.ws.rs.core.Response response)
      Parameters:
      response -
      Returns:
      ID of the created record
    • testGetWithAuth

      public boolean testGetWithAuth(String endpoint, String token) throws IOException
      Checks if given endpoint returns successfully with supplied token.
      Parameters:
      endpoint - Endpoint to be evaluated,
      token - Token that will be passed into the Authorization header.
      Returns:
      true if the endpoint returns forbidden.
      Throws:
      IOException - Thrown by the underlying HTTP Client implementation
    • returnsForbidden

      public boolean returnsForbidden(String endpoint) throws IOException
      Checks if a given endpoint returns Forbidden HTTP Code.
      Parameters:
      endpoint - Endpoint to be evaluated,
      Returns:
      true if the endpoint returns forbidden.
      Throws:
      IOException - Thrown by the underlying HTTP Client implementation
    • getToken

      public String getToken()
      Returns:
      Returns an Access Token.
    • getKeycloakBaseUrl

      public String getKeycloakBaseUrl()
    • getAdminUserName

      public String getAdminUserName()
    • getAdminPassword

      public String getAdminPassword()
    • getAdminClientId

      public String getAdminClientId()
    • getAdminRealmName

      public String getAdminRealmName()
    • getTestRealmName

      public String getTestRealmName()
    • getTestRealmResource

      public RealmResource getTestRealmResource()
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable