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));
 }
 
  • Constructor Details

    • AuthorizationTestExtension

      public AuthorizationTestExtension(ProcessEngineExtension processEngineExtension)
      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:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
    • enableAuthorization

      public void enableAuthorization(String userId)
      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

      public void start(AuthorizationScenario scenario)
      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

      public boolean assertScenario(AuthorizationScenario scenario)
      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

      protected Authorization createAuthorization(int type, Resource resource, String resourceId)
      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

      public void manageAuthorization(Authorization authorization)
      Registers an authorization for cleanup.
      Parameters:
      authorization - the authorization to manage.
    • createUserAndGroup

      public void createUserAndGroup(String userId, String groupId)
      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

      public static Collection<AuthorizationScenario[]> asParameters(AuthorizationScenario... scenarios)
    • init

      Returns a builder for creating an authorization scenario instance.
      Parameters:
      scenario - the authorization scenario.
      Returns:
      the builder.