TaskRuntimeHelper { public org.activiti.engine.task.Task getInternalTaskWithChecks(String taskId) { String authenticatedUserId = getAuthenticatedUser(); if (authenticatedUserId != null && !authenticatedUserId.isEmpty() && securityManager != null) { List<String> userRoles = securityManager.getAuthenticatedUserRoles(); List<String> userGroups = securityManager.getAuthenticatedUserGroups(); org.activiti.engine.task.Task task = taskService.createTaskQuery() .or() .taskCandidateOrAssigned(authenticatedUserId, userGroups) .taskOwner(authenticatedUserId) .endOr() .taskId(taskId) .singleResult(); if (task == null) { throw new NotFoundException("Unable to find task for the given id: " + taskId + " for user: " + authenticatedUserId + " (with groups: " + userGroups + " & with roles: " + userRoles + ")"); } return task; } throw new IllegalStateException("There is no authenticated user, we need a user authenticated to find tasks"); } TaskRuntimeHelper(TaskService taskService,
                             APITaskConverter taskConverter,
                             SecurityManager securityManager,
                             TaskVariablesPayloadValidator taskVariablesValidator); Task applyUpdateTaskPayload(boolean isAdmin, UpdateTaskPayload updateTaskPayload); org.activiti.engine.task.Task getInternalTaskWithChecks(String taskId); void assertHasAccessToTask(String taskId); org.activiti.engine.task.Task getInternalTask(String taskId); Map<String, org.activiti.engine.impl.persistence.entity.VariableInstance> getInternalTaskVariables(String taskId); void createVariable(boolean isAdmin,
                               CreateTaskVariablePayload createTaskVariablePayload); void updateVariable(boolean isAdmin,
                               UpdateTaskVariablePayload updateTaskVariablePayload); void handleCompleteTaskPayload(CompleteTaskPayload completeTaskPayload); void handleSaveTaskPayload(SaveTaskPayload saveTaskPayload);  }
@Test public void getInternalTaskWithChecksShouldReturnMatchinTaskFromTaskQuery() { List<String> groups = singletonList("doctor"); given(securityManager.getAuthenticatedUserGroups()).willReturn(groups); TaskQuery taskQuery = mock(TaskQuery.class); given(taskQuery.taskCandidateOrAssigned(AUTHENTICATED_USER, groups)).willReturn(taskQuery); given(taskQuery.taskOwner(AUTHENTICATED_USER)).willReturn(taskQuery); given(taskQuery.or()).willReturn(taskQuery); given(taskQuery.endOr()).willReturn(taskQuery); given(taskQuery.taskId("taskId")).willReturn(taskQuery); Task internalTask = mock(Task.class); given(taskQuery.singleResult()).willReturn(internalTask); given(taskService.createTaskQuery()).willReturn(taskQuery); Task retrievedTask = taskRuntimeHelper.getInternalTaskWithChecks("taskId"); assertThat(retrievedTask).isEqualTo(internalTask); }
@Test public void getInternalTaskWithChecksShouldThrowNotFoundExceptionWhenNoTaskIsFound() { List<String> groups = singletonList("doctor"); given(securityManager.getAuthenticatedUserGroups()).willReturn(groups); TaskQuery taskQuery = mock(TaskQuery.class); given(taskQuery.taskCandidateOrAssigned(AUTHENTICATED_USER, groups)).willReturn(taskQuery); given(taskQuery.taskOwner(AUTHENTICATED_USER)).willReturn(taskQuery); given(taskQuery.or()).willReturn(taskQuery); given(taskQuery.endOr()).willReturn(taskQuery); given(taskQuery.taskId("taskId")).willReturn(taskQuery); given(taskQuery.singleResult()).willReturn(null); given(taskService.createTaskQuery()).willReturn(taskQuery); Throwable thrown = catchThrowable(() -> taskRuntimeHelper.getInternalTaskWithChecks("taskId")); assertThat(thrown) .isInstanceOf(NotFoundException.class) .hasMessageStartingWith("Unable to find task for the given id:"); }
@Test public void getInternalTaskWithChecksShouldThrowExceptionIfAuthenticatedUserIsNotSet() { given(securityManager.getAuthenticatedUserId()).willReturn(null); Throwable thrown = catchThrowable(() -> taskRuntimeHelper.getInternalTaskWithChecks("taskId")); assertThat(thrown) .isInstanceOf(IllegalStateException.class) .hasMessage("There is no authenticated user, we need a user authenticated to find tasks"); }