use of org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testResolveTask.
@Test
@SuppressWarnings("unchecked")
public void testResolveTask() throws Exception {
RequestContext requestContext = initApiClientWithTestUser();
String user = requestContext.getRunAsUser();
String initiator = getOtherPersonInNetwork(requestContext.getRunAsUser(), requestContext.getNetworkId()).getId();
ProcessInstance processInstance = startAdhocProcess(initiator, requestContext.getNetworkId(), null);
try {
Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
TasksClient tasksClient = publicApiClient.tasksClient();
// Resolving as non-assignee/owner/initiator/admin should result in an error
JSONObject taskBody = new JSONObject();
taskBody.put("state", "resolved");
taskBody.put("assignee", initiator);
List<String> selectedFields = new ArrayList<String>();
selectedFields.addAll(Arrays.asList(new String[] { "state", "assignee" }));
try {
tasksClient.updateTask(task.getId(), taskBody, selectedFields);
fail("Exception expected");
} catch (PublicApiException expected) {
assertEquals(HttpStatus.FORBIDDEN.value(), expected.getHttpResponse().getStatusCode());
assertErrorSummary("Permission was denied", expected.getHttpResponse());
}
// Resolving as assignee
task.delegate(user);
activitiProcessEngine.getTaskService().saveTask(task);
taskBody.put("state", "resolved");
taskBody.put("assignee", initiator);
selectedFields = new ArrayList<String>();
selectedFields.addAll(Arrays.asList(new String[] { "state", "assignee" }));
JSONObject result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertEquals("resolved", result.get("state"));
assertEquals(initiator, result.get("assignee"));
task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertEquals(DelegationState.RESOLVED, task.getDelegationState());
assertEquals(initiator, task.getAssignee());
HistoricTaskInstance historyTask = activitiProcessEngine.getHistoryService().createHistoricTaskInstanceQuery().taskId(task.getId()).includeProcessVariables().includeTaskLocalVariables().singleResult();
assertNotNull("The outcome should not be null for resolved task.", historyTask.getTaskLocalVariables().get("bpm_outcome"));
// Resolving as owner
task.setDelegationState(null);
task.setOwner(requestContext.getRunAsUser());
task.setAssignee(null);
activitiProcessEngine.getTaskService().saveTask(task);
result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertEquals("resolved", result.get("state"));
assertEquals(user, result.get("assignee"));
task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertEquals(DelegationState.RESOLVED, task.getDelegationState());
assertEquals(user, task.getAssignee());
// Resolving as process initiator
task.setDelegationState(null);
task.setOwner(null);
task.setAssignee(null);
activitiProcessEngine.getTaskService().saveTask(task);
requestContext.setRunAsUser(initiator);
taskBody.put("assignee", user);
result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertEquals("resolved", result.get("state"));
task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertEquals(DelegationState.RESOLVED, task.getDelegationState());
// Resolving as administrator
String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
task.setDelegationState(null);
task.setOwner(initiator);
task.setAssignee(null);
activitiProcessEngine.getTaskService().saveTask(task);
task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
requestContext.setRunAsUser(tenantAdmin);
taskBody.put("assignee", user);
result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertEquals("resolved", result.get("state"));
assertEquals(initiator, result.get("assignee"));
task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertEquals(DelegationState.RESOLVED, task.getDelegationState());
assertEquals(initiator, task.getAssignee());
} finally {
cleanupProcessInstance(processInstance);
}
}
use of org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testUpdateTaskVariablesNoExplicitType.
@Test
@SuppressWarnings("unchecked")
public void testUpdateTaskVariablesNoExplicitType() throws Exception {
RequestContext requestContext = initApiClientWithTestUser();
ProcessInstance processInstance = startAdhocProcess(requestContext.getRunAsUser(), requestContext.getNetworkId(), null);
try {
Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertNotNull(task);
Map<String, Object> actualLocalVariables = activitiProcessEngine.getTaskService().getVariablesLocal(task.getId());
Map<String, Object> actualGlobalVariables = activitiProcessEngine.getRuntimeService().getVariables(processInstance.getId());
assertEquals(5, actualGlobalVariables.size());
assertEquals(8, actualLocalVariables.size());
// Raw number value
JSONObject variableBody = new JSONObject();
variableBody.put("name", "newVariable");
variableBody.put("value", 1234);
variableBody.put("scope", "global");
TasksClient tasksClient = publicApiClient.tasksClient();
JSONObject resultEntry = tasksClient.updateTaskVariable(task.getId(), "newVariable", variableBody);
assertNotNull(resultEntry);
JSONObject result = (JSONObject) resultEntry.get("entry");
assertEquals("newVariable", result.get("name"));
assertEquals(1234L, result.get("value"));
assertEquals("d:int", result.get("type"));
assertEquals("global", result.get("scope"));
assertEquals(1234, activitiProcessEngine.getRuntimeService().getVariable(processInstance.getId(), "newVariable"));
// Raw boolean value
variableBody = new JSONObject();
variableBody.put("name", "newVariable");
variableBody.put("value", Boolean.TRUE);
variableBody.put("scope", "local");
resultEntry = tasksClient.updateTaskVariable(task.getId(), "newVariable", variableBody);
assertNotNull(resultEntry);
result = (JSONObject) resultEntry.get("entry");
assertEquals("newVariable", result.get("name"));
assertEquals(Boolean.TRUE, result.get("value"));
assertEquals("d:boolean", result.get("type"));
assertEquals("local", result.get("scope"));
assertEquals(Boolean.TRUE, activitiProcessEngine.getTaskService().getVariable(task.getId(), "newVariable"));
// Raw string value
variableBody = new JSONObject();
variableBody.put("name", "newVariable");
variableBody.put("value", "test value");
variableBody.put("scope", "global");
resultEntry = tasksClient.updateTaskVariable(task.getId(), "newVariable", variableBody);
assertNotNull(resultEntry);
result = (JSONObject) resultEntry.get("entry");
assertEquals("newVariable", result.get("name"));
assertEquals("test value", result.get("value"));
assertEquals("d:text", result.get("type"));
assertEquals("global", result.get("scope"));
assertEquals("test value", activitiProcessEngine.getRuntimeService().getVariable(processInstance.getId(), "newVariable"));
} finally {
cleanupProcessInstance(processInstance);
}
}
use of org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testClaimTask.
@Test
@SuppressWarnings("unchecked")
public void testClaimTask() throws Exception {
RequestContext requestContext = initApiClientWithTestUser();
String initiator = getOtherPersonInNetwork(requestContext.getRunAsUser(), requestContext.getNetworkId()).getId();
ProcessInstance processInstance = startAdhocProcess(initiator, requestContext.getNetworkId(), null);
try {
Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
TasksClient tasksClient = publicApiClient.tasksClient();
// Claiming the task when NOT part of candidate-group results in an error
JSONObject taskBody = new JSONObject();
taskBody.put("state", "claimed");
List<String> selectedFields = new ArrayList<String>();
selectedFields.addAll(Arrays.asList(new String[] { "state", "assignee" }));
try {
tasksClient.updateTask(task.getId(), taskBody, selectedFields);
fail("Exception expected");
} catch (PublicApiException expected) {
assertEquals(HttpStatus.FORBIDDEN.value(), expected.getHttpResponse().getStatusCode());
assertErrorSummary("Permission was denied", expected.getHttpResponse());
}
// Set candidate for task, but keep assignee
List<MemberOfSite> memberships = getTestFixture().getNetwork(requestContext.getNetworkId()).getSiteMemberships(requestContext.getRunAsUser());
assertTrue(memberships.size() > 0);
MemberOfSite memberOfSite = memberships.get(0);
String group = "GROUP_site_" + memberOfSite.getSiteId() + "_" + memberOfSite.getRole().name();
activitiProcessEngine.getTaskService().addCandidateGroup(task.getId(), group);
// Claiming the task when part of candidate-group but another person has this task assigned results in conflict
try {
tasksClient.updateTask(task.getId(), taskBody, selectedFields);
fail("Exception expected");
} catch (PublicApiException expected) {
assertEquals(HttpStatus.CONFLICT.value(), expected.getHttpResponse().getStatusCode());
assertErrorSummary("The task is already claimed by another user.", expected.getHttpResponse());
}
// Claiming the task when part of candidate-group and NO assignee is currenlty set should work
activitiProcessEngine.getTaskService().setAssignee(task.getId(), null);
taskBody = new JSONObject();
taskBody.put("state", "claimed");
JSONObject result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertNotNull(result);
assertEquals(requestContext.getRunAsUser(), result.get("assignee"));
assertEquals(requestContext.getRunAsUser(), activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
// Re-claiming the same task with the current assignee shouldn't be a problem
result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertNotNull(result);
assertEquals(requestContext.getRunAsUser(), result.get("assignee"));
assertEquals(requestContext.getRunAsUser(), activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
// Claiming as a candidateUser should also work
activitiProcessEngine.getTaskService().setAssignee(task.getId(), null);
activitiProcessEngine.getTaskService().deleteGroupIdentityLink(task.getId(), group, IdentityLinkType.CANDIDATE);
activitiProcessEngine.getTaskService().addCandidateUser(task.getId(), requestContext.getRunAsUser());
result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertNotNull(result);
assertEquals(requestContext.getRunAsUser(), result.get("assignee"));
assertEquals(requestContext.getRunAsUser(), activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
// Claiming as a task owner should also work
activitiProcessEngine.getTaskService().setAssignee(task.getId(), null);
activitiProcessEngine.getTaskService().setOwner(task.getId(), requestContext.getRunAsUser());
activitiProcessEngine.getTaskService().deleteUserIdentityLink(task.getId(), requestContext.getRunAsUser(), IdentityLinkType.CANDIDATE);
result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertNotNull(result);
assertEquals(requestContext.getRunAsUser(), result.get("assignee"));
assertEquals(requestContext.getRunAsUser(), activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
// Claiming as admin should work
String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
publicApiClient.setRequestContext(new RequestContext(TenantUtil.DEFAULT_TENANT, tenantAdmin));
activitiProcessEngine.getTaskService().setAssignee(task.getId(), null);
activitiProcessEngine.getTaskService().deleteUserIdentityLink(task.getId(), requestContext.getRunAsUser(), IdentityLinkType.CANDIDATE);
result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertNotNull(result);
assertEquals(tenantAdmin, result.get("assignee"));
assertEquals(tenantAdmin, activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
} finally {
cleanupProcessInstance(processInstance);
}
}
use of org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testGetTasksWithPagingAndVariablesLimit.
@Test
public void testGetTasksWithPagingAndVariablesLimit() throws Exception {
RequestContext requestContext = initApiClientWithTestUser();
List<ProcessInstance> startedProcesses = new ArrayList<ProcessInstance>();
// system.workflow.engine.activiti.taskvariableslimit is set to 200 in test-resources/alfresco-global.properties
try {
// MNT-16040: Create tasks with a number of variables just below the taskvariableslimit and test that skipCount is working as expected.
int numberOfTasks = 15;
for (int i = 0; i < numberOfTasks; i++) {
startedProcesses.add(startAdhocProcess(requestContext.getRunAsUser(), requestContext.getNetworkId(), null));
}
TaskService taskService = activitiProcessEngine.getTaskService();
List<Task> taskList = new ArrayList<Task>();
for (int i = 0; i < numberOfTasks; i++) {
Task task = taskService.createTaskQuery().processInstanceId(startedProcesses.get(i).getProcessInstanceId()).singleResult();
taskService.setPriority(task.getId(), (i + 1) * 10);
// Add an extra variable to the task, there are other 12 added, so a total of 13 variables for each task.
taskService.setVariableLocal(task.getId(), "test1", "test1");
taskList.add(task);
}
TasksClient tasksClient = publicApiClient.tasksClient();
// Test without skipCount
Map<String, String> params = new HashMap<String, String>();
params.put("where", "(includeProcessVariables = true AND includeTaskVariables = true)");
JSONObject taskListJSONObject = tasksClient.findTasks(params);
assertNotNull(taskListJSONObject);
JSONObject paginationJSON = (JSONObject) taskListJSONObject.get("pagination");
assertEquals(15l, paginationJSON.get("count"));
assertEquals(15l, paginationJSON.get("totalItems"));
assertEquals(0l, paginationJSON.get("skipCount"));
assertEquals(false, paginationJSON.get("hasMoreItems"));
JSONArray jsonEntries = (JSONArray) taskListJSONObject.get("entries");
assertEquals(15, jsonEntries.size());
// Test with skipCount
params.clear();
params.put("skipCount", "5");
params.put("where", "(includeProcessVariables = true AND includeTaskVariables = true)");
taskListJSONObject = tasksClient.findTasks(params);
assertNotNull(taskListJSONObject);
paginationJSON = (JSONObject) taskListJSONObject.get("pagination");
assertEquals(10l, paginationJSON.get("count"));
assertEquals(15l, paginationJSON.get("totalItems"));
assertEquals(5l, paginationJSON.get("skipCount"));
assertEquals(false, paginationJSON.get("hasMoreItems"));
jsonEntries = (JSONArray) taskListJSONObject.get("entries");
assertEquals(10, jsonEntries.size());
params.clear();
params.put("maxItems", "10");
params.put("where", "(includeProcessVariables = true AND includeTaskVariables = true)");
taskListJSONObject = tasksClient.findTasks(params);
assertNotNull(taskListJSONObject);
paginationJSON = (JSONObject) taskListJSONObject.get("pagination");
assertEquals(10l, paginationJSON.get("count"));
assertEquals(15l, paginationJSON.get("totalItems"));
assertEquals(0l, paginationJSON.get("skipCount"));
assertEquals(true, paginationJSON.get("hasMoreItems"));
jsonEntries = (JSONArray) taskListJSONObject.get("entries");
assertEquals(10, jsonEntries.size());
} finally {
cleanupProcessInstance(startedProcesses.toArray(new ProcessInstance[] {}));
}
}
use of org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testUnClaimTask.
@Test
@SuppressWarnings("unchecked")
public void testUnClaimTask() throws Exception {
RequestContext requestContext = initApiClientWithTestUser();
String user = requestContext.getRunAsUser();
String initiator = getOtherPersonInNetwork(requestContext.getRunAsUser(), requestContext.getNetworkId()).getId();
ProcessInstance processInstance = startAdhocProcess(initiator, requestContext.getNetworkId(), null);
try {
Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
TasksClient tasksClient = publicApiClient.tasksClient();
// Unclaiming the task when NOT assignee, owner, initiator or admin results in error
JSONObject taskBody = new JSONObject();
taskBody.put("state", "unclaimed");
List<String> selectedFields = new ArrayList<String>();
selectedFields.addAll(Arrays.asList(new String[] { "state" }));
try {
tasksClient.updateTask(task.getId(), taskBody, selectedFields);
fail("Exception expected");
} catch (PublicApiException expected) {
assertEquals(HttpStatus.FORBIDDEN.value(), expected.getHttpResponse().getStatusCode());
assertErrorSummary("Permission was denied", expected.getHttpResponse());
}
// Unclaiming as process initiator
requestContext.setRunAsUser(initiator);
activitiProcessEngine.getTaskService().setAssignee(task.getId(), null);
JSONObject result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertNull(result.get("assignee"));
assertNull(activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
// Unclaiming as assignee
activitiProcessEngine.getTaskService().setAssignee(task.getId(), user);
requestContext.setRunAsUser(user);
assertNotNull(activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertNull(result.get("assignee"));
assertNull(activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
// Unclaim as owner
activitiProcessEngine.getTaskService().setOwner(task.getId(), user);
activitiProcessEngine.getTaskService().setAssignee(task.getId(), initiator);
assertNotNull(activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertNull(result.get("assignee"));
assertNull(activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
// Unclaim as admin
String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
publicApiClient.setRequestContext(new RequestContext(TenantUtil.DEFAULT_TENANT, tenantAdmin));
activitiProcessEngine.getTaskService().setAssignee(task.getId(), initiator);
activitiProcessEngine.getTaskService().deleteUserIdentityLink(task.getId(), requestContext.getRunAsUser(), IdentityLinkType.CANDIDATE);
assertNotNull(activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertNull(result.get("assignee"));
assertNull(activitiProcessEngine.getTaskService().createTaskQuery().taskId(task.getId()).singleResult().getAssignee());
} finally {
cleanupProcessInstance(processInstance);
}
}
Aggregations