Package org.keycloak.test
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");
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classFluentTestsHelper.ClientData
-
Field Summary
Fields Modifier and Type Field Description protected StringaccessTokenprotected StringadminClientprotected StringadminPasswordprotected StringadminRealmprotected StringadminUserNameprotected Map<String,FluentTestsHelper.ClientData>createdClientsstatic StringDEFAULT_ADMIN_CLIENTstatic StringDEFAULT_ADMIN_PASSWORDstatic StringDEFAULT_ADMIN_REALMstatic StringDEFAULT_ADMIN_USERNAMEstatic StringDEFAULT_KEYCLOAK_URLstatic StringDEFAULT_TEST_REALMstatic StringDEFAULT_USER_ROLEprotected booleanisInitializedprotected Keycloakkeycloakprotected StringkeycloakBaseUrlprotected StringtestRealm
-
Constructor Summary
Constructors Constructor Description FluentTestsHelper()Creates a new helper instance.FluentTestsHelper(String adminUserName, String adminPassword)Creates a new helper instance.FluentTestsHelper(String keycloakBaseUrl, String adminUserName, String adminPassword, String adminRealm, String adminClient, String testRealm)Creates a new helper instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FluentTestsHelperassignRoleWithUser(String userName, String roleName)Associates a user with a role.voidclose()FluentTestsHelpercreateClient(org.keycloak.representations.idm.ClientRepresentation clientRepresentation)Creates a new client based on its representation.FluentTestsHelpercreateDirectGrantClient(String clientId)Creates a direct grant client.protected KeycloakcreateKeycloakInstance(String keycloakBaseUrl, String realm, String username, String password, String clientId)FluentTestsHelpercreateTestRealm(org.keycloak.representations.idm.RealmRepresentation realmRepresentation)Creates a test realm.FluentTestsHelpercreateTestUser(String username, String password)Creates a test user.FluentTestsHelperdeleteClient(String clientId)Deletes a client previously created by this helper.FluentTestsHelperdeleteRealm(String realmName)Deletes a realm.FluentTestsHelperdeleteRole(String roleName)Deletes a role.FluentTestsHelperdeleteTestRealm()Deletes the test realm.FluentTestsHelperdeleteTestUser(String userName)Deletes a user.protected StringgenerateInitialAccessToken()StringgetAdminClientId()StringgetAdminPassword()StringgetAdminRealmName()StringgetAdminUserName()StringgetCreatedId(javax.ws.rs.core.Response response)StringgetKeycloakBaseUrl()KeycloakgetKeycloakInstance()For more complex test scenariosStringgetTestRealmName()RealmResourcegetTestRealmResource()StringgetToken()FluentTestsHelperimportTestRealm(File realmJsonPath)FluentTestsHelperimportTestRealm(InputStream stream)Import a test realm.FluentTestsHelperimportTestRealm(String realmJsonPath)FluentTestsHelperinit()Initialization method.booleanisInitialized()booleanreturnsForbidden(String endpoint)Checks if a given endpoint returns Forbidden HTTP Code.booleantestGetWithAuth(String endpoint, String token)Checks if given endpoint returns successfully with supplied token.
-
-
-
Field Detail
-
DEFAULT_KEYCLOAK_URL
public static final String DEFAULT_KEYCLOAK_URL
- See Also:
- Constant Field Values
-
DEFAULT_ADMIN_USERNAME
public static final String DEFAULT_ADMIN_USERNAME
- See Also:
- Constant Field Values
-
DEFAULT_ADMIN_PASSWORD
public static final String DEFAULT_ADMIN_PASSWORD
- See Also:
- Constant Field Values
-
DEFAULT_ADMIN_REALM
public static final String DEFAULT_ADMIN_REALM
- See Also:
- Constant Field Values
-
DEFAULT_ADMIN_CLIENT
public static final String DEFAULT_ADMIN_CLIENT
- See Also:
- Constant Field Values
-
DEFAULT_TEST_REALM
public static final String DEFAULT_TEST_REALM
- See Also:
- Constant Field Values
-
DEFAULT_USER_ROLE
public static final String DEFAULT_USER_ROLE
- See Also:
- Constant Field Values
-
keycloakBaseUrl
protected final String keycloakBaseUrl
-
adminUserName
protected final String adminUserName
-
adminPassword
protected final String adminPassword
-
adminClient
protected final String adminClient
-
adminRealm
protected final String adminRealm
-
testRealm
protected String testRealm
-
keycloak
protected Keycloak keycloak
-
accessToken
protected String accessToken
-
isInitialized
protected volatile boolean isInitialized
-
createdClients
protected Map<String,FluentTestsHelper.ClientData> createdClients
-
-
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
-
init
public FluentTestsHelper init()
Initialization method.- Returns:
this
-
isInitialized
public boolean isInitialized()
- Returns:
- Returns
trueif 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:
ClientRegistrationExceptioncom.fasterxml.jackson.core.JsonProcessingException
-
createDirectGrantClient
public FluentTestsHelper createDirectGrantClient(String clientId) throws ClientRegistrationException, com.fasterxml.jackson.core.JsonProcessingException
Creates a direct grant client.- Throws:
ClientRegistrationExceptioncom.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(InputStream)
-
importTestRealm
public FluentTestsHelper importTestRealm(File realmJsonPath) throws IOException
- Throws:
IOException- See Also:
importTestRealm(InputStream)
-
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 theAuthorizationheader.- Returns:
trueif 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:
trueif 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
-