Class AuthorizationTestExtension
java.lang.Object
org.operaton.bpm.engine.test.junit5.authorization.AuthorizationTestExtension
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback,org.junit.jupiter.api.extension.BeforeEachCallback,org.junit.jupiter.api.extension.Extension
public class AuthorizationTestExtension
extends Object
implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
JUnit 5 extension that replaces the legacy JUnit 4 AuthorizationTestRule.
This extension provides an API for starting an authorization scenario, asserting its outcome, and managing authorizations, users, and groups.
Usage Example:
@RegisterExtension
static ProcessEngineExtension engineExtension = ProcessEngineExtension.builder().build();
@RegisterExtension
AuthorizationTestExtension authExtension = new AuthorizationTestExtension(engineExtension);
// In a test method:
@Test
public void testSomething() {
authExtension.createGrantAuthorization(Resources.AUTHORIZATION, "*", "testUser", Permissions.CREATE);
authExtension.start(myScenario);
// ... perform operations ...
assertTrue(authExtension.assertScenario(myScenario));
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for fluent construction of an authorization scenario instance. -
Constructor Summary
ConstructorsConstructorDescriptionAuthorizationTestExtension(ProcessEngineExtension processEngineExtension) Constructs the extension with the given ProcessEngine. -
Method Summary
Modifier and TypeMethodDescriptionvoidafterEach(org.junit.jupiter.api.extension.ExtensionContext context) static Collection<AuthorizationScenario[]> asParameters(AuthorizationScenario... scenarios) booleanassertScenario(AuthorizationScenario scenario) Asserts that the given authorization scenario has executed as expected.voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context) protected AuthorizationcreateAuthorization(int type, Resource resource, String resourceId) Helper method to create a new authorization.voidcreateGrantAuthorization(Resource resource, String resourceId, String userId, Permission... permissions) Creates a grant authorization.voidcreateRevokeAuthorization(Resource resource, String resourceId, String userId, Permission... permissions) Creates a revoke authorization.voidcreateUserAndGroup(String userId, String groupId) Creates a new user and group and registers them for cleanup.voidDeletes all managed authorizations.voidvoidDisables authorization and clears authentication.voidenableAuthorization(String userId) Enables authorization and (optionally) sets the authenticated user.init(AuthorizationScenario scenario) Returns a builder for creating an authorization scenario instance.voidmanageAuthorization(Authorization authorization) Registers an authorization for cleanup.booleanReturns true if the authorization scenario failed (an exception was thrown).booleanReturns true if the authorization scenario succeeded (no exception was thrown).voidstart(AuthorizationScenario scenario) Starts an authorization scenario with default resource bindings.voidStarts an authorization scenario with the given user and resource bindings.
-
Constructor Details
-
AuthorizationTestExtension
Constructs the extension with the given ProcessEngine.- Parameters:
processEngineExtension- extension from which to get process engine instance to use
-
-
Method Details
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback
-
enableAuthorization
Enables authorization and (optionally) sets the authenticated user.- Parameters:
userId- the user ID to authenticate (can be null)
-
disableAuthorization
public void disableAuthorization()Disables authorization and clears authentication. -
start
Starts an authorization scenario with default resource bindings.- Parameters:
scenario- the authorization scenario to start.
-
start
public void start(AuthorizationScenario scenario, String userId, Map<String, String> resourceBindings) Starts an authorization scenario with the given user and resource bindings.- Parameters:
scenario- the authorization scenario to start.userId- the user ID to set as authenticated.resourceBindings- the resource bindings for the scenario.
-
assertScenario
Asserts that the given authorization scenario has executed as expected.- Parameters:
scenario- the scenario to assert.- Returns:
- true if no exception was thrown (scenario succeeded), false otherwise.
-
scenarioSucceeded
public boolean scenarioSucceeded()Returns true if the authorization scenario succeeded (no exception was thrown). -
scenarioFailed
public boolean scenarioFailed()Returns true if the authorization scenario failed (an exception was thrown). -
createGrantAuthorization
public void createGrantAuthorization(Resource resource, String resourceId, String userId, Permission... permissions) Creates a grant authorization.- Parameters:
resource- the resource type.resourceId- the resource id (can be null).userId- the user id.permissions- the permissions to grant.
-
createRevokeAuthorization
public void createRevokeAuthorization(Resource resource, String resourceId, String userId, Permission... permissions) Creates a revoke authorization.- Parameters:
resource- the resource type.resourceId- the resource id (can be null).userId- the user id.permissions- the permissions to revoke.
-
createAuthorization
Helper method to create a new authorization.- Parameters:
type- the type of authorization (grant or revoke).resource- the resource type.resourceId- the resource id (can be null).- Returns:
- the new authorization.
-
manageAuthorization
Registers an authorization for cleanup.- Parameters:
authorization- the authorization to manage.
-
createUserAndGroup
Creates a new user and group and registers them for cleanup.- Parameters:
userId- the user id.groupId- the group id.
-
deleteManagedAuthorizations
public void deleteManagedAuthorizations()Deletes all managed authorizations. -
deleteUsersAndGroups
public void deleteUsersAndGroups() -
asParameters
-
init
public AuthorizationTestExtension.AuthorizationScenarioInstanceBuilder init(AuthorizationScenario scenario) Returns a builder for creating an authorization scenario instance.- Parameters:
scenario- the authorization scenario.- Returns:
- the builder.
-