use of org.alfresco.rest.api.tests.client.RequestContext in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testGetTasksWithPaging.
@Test
public void testGetTasksWithPaging() throws Exception {
RequestContext requestContext = initApiClientWithTestUser();
// Start 6 processes
List<ProcessInstance> startedProcesses = new ArrayList<ProcessInstance>();
try {
int numberOfTasks = 6;
for (int i = 0; i < numberOfTasks; i++) {
startedProcesses.add(startAdhocProcess(requestContext.getRunAsUser(), requestContext.getNetworkId(), null));
}
String processDefinitionId = startedProcesses.get(0).getProcessDefinitionId();
TasksClient tasksClient = publicApiClient.tasksClient();
// Test with existing processDefinitionId
Map<String, String> params = new HashMap<String, String>();
params.put("where", "(processDefinitionId = '" + processDefinitionId + "' AND includeProcessVariables = true AND includeTaskVariables = true)");
JSONObject taskListJSONObject = tasksClient.findTasks(params);
assertNotNull(taskListJSONObject);
JSONObject paginationJSON = (JSONObject) taskListJSONObject.get("pagination");
assertEquals(6l, paginationJSON.get("count"));
assertEquals(6l, paginationJSON.get("totalItems"));
assertEquals(0l, paginationJSON.get("skipCount"));
assertEquals(false, paginationJSON.get("hasMoreItems"));
JSONArray jsonEntries = (JSONArray) taskListJSONObject.get("entries");
assertEquals(6, jsonEntries.size());
validateVariables((JSONObject) jsonEntries.get(0), requestContext);
// Test with existing processDefinitionId and max items
params.clear();
params.put("maxItems", "3");
params.put("where", "(processDefinitionId = '" + processDefinitionId + "' AND includeProcessVariables = true AND includeTaskVariables = true)");
taskListJSONObject = tasksClient.findTasks(params);
assertNotNull(taskListJSONObject);
paginationJSON = (JSONObject) taskListJSONObject.get("pagination");
assertEquals(3l, paginationJSON.get("count"));
assertEquals(6l, paginationJSON.get("totalItems"));
assertEquals(0l, paginationJSON.get("skipCount"));
assertEquals(true, paginationJSON.get("hasMoreItems"));
jsonEntries = (JSONArray) taskListJSONObject.get("entries");
assertEquals(3, jsonEntries.size());
validateVariables((JSONObject) jsonEntries.get(0), requestContext);
// Test with existing processDefinitionId and skip count
params.clear();
params.put("skipCount", "2");
params.put("where", "(processDefinitionId = '" + processDefinitionId + "' AND includeProcessVariables = true AND includeTaskVariables = true)");
taskListJSONObject = tasksClient.findTasks(params);
assertNotNull(taskListJSONObject);
paginationJSON = (JSONObject) taskListJSONObject.get("pagination");
assertEquals(4l, paginationJSON.get("count"));
assertEquals(6l, paginationJSON.get("totalItems"));
assertEquals(2l, paginationJSON.get("skipCount"));
assertEquals(false, paginationJSON.get("hasMoreItems"));
jsonEntries = (JSONArray) taskListJSONObject.get("entries");
assertEquals(4, jsonEntries.size());
// Test with existing processDefinitionId and max items and skip count
params.clear();
params.put("maxItems", "3");
params.put("skipCount", "2");
params.put("where", "(processDefinitionId = '" + processDefinitionId + "' AND includeProcessVariables = true AND includeTaskVariables = true)");
taskListJSONObject = tasksClient.findTasks(params);
assertNotNull(taskListJSONObject);
paginationJSON = (JSONObject) taskListJSONObject.get("pagination");
assertEquals(3l, paginationJSON.get("count"));
assertEquals(6l, paginationJSON.get("totalItems"));
assertEquals(2l, paginationJSON.get("skipCount"));
assertEquals(true, paginationJSON.get("hasMoreItems"));
jsonEntries = (JSONArray) taskListJSONObject.get("entries");
assertEquals(3, jsonEntries.size());
// Test with existing processDefinitionId and max items and skip count
params.clear();
params.put("maxItems", "3");
params.put("skipCount", "4");
params.put("where", "(processDefinitionId = '" + processDefinitionId + "' AND includeProcessVariables = true AND includeTaskVariables = true)");
taskListJSONObject = tasksClient.findTasks(params);
assertNotNull(taskListJSONObject);
paginationJSON = (JSONObject) taskListJSONObject.get("pagination");
assertEquals(2l, paginationJSON.get("count"));
assertEquals(6l, paginationJSON.get("totalItems"));
assertEquals(4l, paginationJSON.get("skipCount"));
assertEquals(false, paginationJSON.get("hasMoreItems"));
jsonEntries = (JSONArray) taskListJSONObject.get("entries");
assertEquals(2, jsonEntries.size());
} finally {
cleanupProcessInstance(startedProcesses.toArray(new ProcessInstance[] {}));
}
}
use of org.alfresco.rest.api.tests.client.RequestContext in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testDeleteTaskVariableExceptions.
@Test
public void testDeleteTaskVariableExceptions() throws Exception {
RequestContext requestContext = initApiClientWithTestUser();
ProcessInstance processInstance = startAdhocProcess(requestContext.getRunAsUser(), requestContext.getNetworkId(), null);
activitiProcessEngine.getRuntimeService().setVariable(processInstance.getId(), "overlappingVariable", "Value set in process");
try {
Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertNotNull(task);
TasksClient tasksClient = publicApiClient.tasksClient();
// Delete a variable on an unexisting task
try {
tasksClient.deleteTaskVariable("unexisting", "myVar");
fail("Exception expected");
} catch (PublicApiException expected) {
assertEquals(HttpStatus.NOT_FOUND.value(), expected.getHttpResponse().getStatusCode());
assertErrorSummary("The entity with id: unexisting was not found", expected.getHttpResponse());
}
// Delete an unexisting variable on an existing task
try {
tasksClient.deleteTaskVariable(task.getId(), "unexistingVarName");
fail("Exception expected");
} catch (PublicApiException expected) {
assertEquals(HttpStatus.NOT_FOUND.value(), expected.getHttpResponse().getStatusCode());
assertErrorSummary("The entity with id: unexistingVarName was not found", expected.getHttpResponse());
}
} finally {
cleanupProcessInstance(processInstance);
}
}
use of org.alfresco.rest.api.tests.client.RequestContext in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testGetTaskVariablesScoped.
@Test
public void testGetTaskVariablesScoped() 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());
TasksClient tasksClient = publicApiClient.tasksClient();
JSONObject variables = tasksClient.findTaskVariables(task.getId(), Collections.singletonMap("where", "(scope = local)"));
assertNotNull(variables);
JSONObject list = (JSONObject) variables.get("list");
assertNotNull(list);
JSONArray entries = (JSONArray) list.get("entries");
assertNotNull(entries);
// Check pagination object for size
JSONObject pagination = (JSONObject) list.get("pagination");
assertNotNull(pagination);
assertEquals(8L, pagination.get("count"));
assertEquals(8L, pagination.get("totalItems"));
assertEquals(0L, pagination.get("skipCount"));
assertFalse((Boolean) pagination.get("hasMoreItems"));
assertEquals(actualLocalVariables.size(), entries.size());
Set<String> expectedLocalVars = new HashSet<String>();
expectedLocalVars.addAll(actualLocalVariables.keySet());
for (int i = 0; i < entries.size(); i++) {
JSONObject entry = (JSONObject) ((JSONObject) entries.get(i)).get("entry");
assertNotNull(entry);
// Check if full entry is present with correct scope
assertEquals("local", entry.get("scope"));
assertNotNull(entry.get("name"));
assertNotNull(entry.get("type"));
if (!entry.get("name").equals("bpm_hiddenTransitions")) {
assertNotNull(entry.get("value"));
}
expectedLocalVars.remove(entry.get("name"));
}
assertEquals(0, expectedLocalVars.size());
// Now check the global scope
variables = tasksClient.findTaskVariables(task.getId(), Collections.singletonMap("where", "(scope = global)"));
assertNotNull(variables);
list = (JSONObject) variables.get("list");
assertNotNull(list);
entries = (JSONArray) list.get("entries");
assertNotNull(entries);
// Check pagination object for size
pagination = (JSONObject) list.get("pagination");
assertNotNull(pagination);
assertEquals(4L, pagination.get("count"));
assertEquals(4L, pagination.get("totalItems"));
assertEquals(0L, pagination.get("skipCount"));
assertFalse((Boolean) pagination.get("hasMoreItems"));
// Should contain one variable less than the actual variables in the engine, tenant-domain var is filtered out
assertEquals(actualGlobalVariables.size() - 1, entries.size());
Set<String> expectedGlobalVars = new HashSet<String>();
expectedGlobalVars.addAll(actualGlobalVariables.keySet());
expectedGlobalVars.remove(ActivitiConstants.VAR_TENANT_DOMAIN);
for (int i = 0; i < entries.size(); i++) {
JSONObject entry = (JSONObject) ((JSONObject) entries.get(i)).get("entry");
assertNotNull(entry);
// Check if full entry is present with correct scope
assertEquals("global", entry.get("scope"));
assertNotNull(entry.get("name"));
assertNotNull(entry.get("type"));
assertNotNull(entry.get("value"));
expectedGlobalVars.remove(entry.get("name"));
}
// Check if all variables are present
assertEquals(0, expectedGlobalVars.size());
} finally {
cleanupProcessInstance(processInstance);
}
}
use of org.alfresco.rest.api.tests.client.RequestContext in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testGetTaskVariablesReview.
@Test
public void testGetTaskVariablesReview() throws Exception {
RequestContext requestContext = initApiClientWithTestUser();
ProcessInfo processInstance = startParallelReviewProcess(requestContext);
try {
Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).taskAssignee(requestContext.getRunAsUser()).singleResult();
assertNotNull(task);
TasksClient tasksClient = publicApiClient.tasksClient();
JSONObject variables = tasksClient.findTaskVariables(task.getId());
assertNotNull(variables);
JSONObject list = (JSONObject) variables.get("list");
assertNotNull(list);
JSONArray entries = (JSONArray) list.get("entries");
assertNotNull(entries);
// Check pagination object for size
JSONObject pagination = (JSONObject) list.get("pagination");
assertNotNull(pagination);
assertEquals(42L, pagination.get("count"));
assertEquals(42L, pagination.get("totalItems"));
assertEquals(0L, pagination.get("skipCount"));
assertFalse((Boolean) pagination.get("hasMoreItems"));
} finally {
cleanupProcessInstance(processInstance.getId());
}
}
use of org.alfresco.rest.api.tests.client.RequestContext in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testDelegateTask.
@Test
@SuppressWarnings("unchecked")
public void testDelegateTask() 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();
// Delegating as non-assignee/owner/initiator/admin should result in an error
JSONObject taskBody = new JSONObject();
taskBody.put("state", "delegated");
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());
}
// Delegating (as assignee) and not passing in an asisgnee should result in an error
activitiProcessEngine.getTaskService().setAssignee(task.getId(), user);
taskBody = new JSONObject();
taskBody.put("state", "delegated");
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.BAD_REQUEST.value(), expected.getHttpResponse().getStatusCode());
assertErrorSummary("When delegating a task, assignee should be selected and provided in the request.", expected.getHttpResponse());
}
// Delegating as assignee
taskBody.put("state", "delegated");
taskBody.put("assignee", initiator);
selectedFields = new ArrayList<String>();
selectedFields.addAll(Arrays.asList(new String[] { "state", "assignee" }));
assertNull(task.getDelegationState());
JSONObject result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertEquals("delegated", result.get("state"));
assertEquals(initiator, result.get("assignee"));
assertEquals(requestContext.getRunAsUser(), result.get("owner"));
task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertEquals(DelegationState.PENDING, task.getDelegationState());
assertEquals(initiator, task.getAssignee());
assertEquals(requestContext.getRunAsUser(), task.getOwner());
// Delegating as owner
task.setDelegationState(null);
task.setOwner(requestContext.getRunAsUser());
task.setAssignee(null);
activitiProcessEngine.getTaskService().saveTask(task);
result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
assertEquals("delegated", result.get("state"));
assertEquals(initiator, result.get("assignee"));
assertEquals(requestContext.getRunAsUser(), result.get("owner"));
task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertEquals(DelegationState.PENDING, task.getDelegationState());
assertEquals(initiator, task.getAssignee());
assertEquals(requestContext.getRunAsUser(), task.getOwner());
// Delegating 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("delegated", result.get("state"));
assertEquals(user, result.get("assignee"));
assertEquals(initiator, result.get("owner"));
task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertEquals(DelegationState.PENDING, task.getDelegationState());
assertEquals(user, task.getAssignee());
assertEquals(initiator, task.getOwner());
// Delegating as administrator
String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
task.setDelegationState(null);
task.setOwner(null);
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("delegated", result.get("state"));
assertEquals(user, result.get("assignee"));
assertEquals(tenantAdmin, result.get("owner"));
task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertEquals(DelegationState.PENDING, task.getDelegationState());
assertEquals(user, task.getAssignee());
assertEquals(tenantAdmin, task.getOwner());
} finally {
cleanupProcessInstance(processInstance);
}
}
Aggregations