Class 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");
        

    • Constructor Detail

      • 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 Detail

      • isInitialized

        public boolean isInitialized()
        Returns:
        Returns true if this helper has been initialized.
      • 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
      • 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​(javax.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()