BaseJavaDelegate implements JavaDelegate { protected void setSecurityContext(ApplicationUser applicationUser) { userNamespaceAuthorizationHelper.buildNamespaceAuthorizations(applicationUser); SecurityContextHolder.getContext().setAuthentication(new PreAuthenticatedAuthenticationToken( new SecurityUserWrapper(applicationUser.getUserId(), "", true, true, true, true, Collections.emptyList(), applicationUser), null)); }  abstract void executeImpl(DelegateExecution execution); @Override final void execute(DelegateExecution execution); void setJsonResponseAsWorkflowVariable(Object responseObject, DelegateExecution execution); void setJsonResponseAsWorkflowVariable(Object responseObject, String executionId, String activitiId); boolean isSpringInitialized(); void setSpringInitialized(boolean springInitialized); static final String VARIABLE_JSON_RESPONSE; }
@Test public void testSetSecurityContext() throws Exception { String jobDefinitionNamespace = "jobDefinitionNamespace"; String jobDefinitionName = "jobDefinitionName"; String processDefinitionId = "processDefinitionId"; String processDefinitionKey = String.format("%s.%s", jobDefinitionNamespace, jobDefinitionName); String updatedBy = "updatedBy"; JobDefinitionEntity jobDefinitionEntity = new JobDefinitionEntity(); jobDefinitionEntity.setUpdatedBy(updatedBy); when(delegateExecution.getProcessDefinitionId()).thenReturn(processDefinitionId); ProcessDefinition processDefinition = mock(ProcessDefinition.class); when(processDefinition.getKey()).thenReturn(processDefinitionKey); when(activitiService.getProcessDefinitionById(any())).thenReturn(processDefinition); when(jobDefinitionDaoHelper.getJobDefinitionEntity(any(), any())).thenReturn(jobDefinitionEntity); SecurityContextHolder.clearContext(); baseJavaDelegate.setSecurityContext(baseJavaDelegate.getApplicationUser(delegateExecution)); InOrder inOrder = inOrder(activitiService, jobDefinitionDaoHelper, userNamespaceAuthorizationHelper); inOrder.verify(activitiService).getProcessDefinitionById(processDefinitionId); inOrder.verify(jobDefinitionDaoHelper).getJobDefinitionEntity(jobDefinitionNamespace, jobDefinitionName); inOrder.verify(userNamespaceAuthorizationHelper).buildNamespaceAuthorizations(applicationUserUserIdEq(updatedBy)); inOrder.verifyNoMoreInteractions(); verifyNoMoreInteractions(activitiService, jobDefinitionDaoHelper, userNamespaceAuthorizationHelper); Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); assertAuthenticationUserIdEquals(updatedBy, authentication); }