use of org.alfresco.rest.api.tests.client.PublicApiException 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);
}
}
use of org.alfresco.rest.api.tests.client.PublicApiException 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);
}
}
use of org.alfresco.rest.api.tests.client.PublicApiException 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.api.tests.client.PublicApiException in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testGetTaskModel.
@Test
public void testGetTaskModel() throws Exception {
RequestContext requestContext = initApiClientWithTestUser();
String otherPerson = getOtherPersonInNetwork(requestContext.getRunAsUser(), requestContext.getNetworkId()).getId();
RequestContext otherContext = new RequestContext(requestContext.getNetworkId(), otherPerson);
String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
RequestContext adminContext = new RequestContext(requestContext.getNetworkId(), tenantAdmin);
ProcessInstance processInstance = startAdhocProcess(requestContext.getRunAsUser(), requestContext.getNetworkId(), null);
try {
Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
assertNotNull(task);
JSONObject model = publicApiClient.tasksClient().findTaskFormModel(task.getId());
assertNotNull(model);
JSONArray entries = (JSONArray) model.get("entries");
assertNotNull(entries);
// Add all entries to a map, to make lookup easier
Map<String, JSONObject> modelFieldsByName = createEntryMap(entries);
testModelEntries(modelFieldsByName);
// get task form model with admin
publicApiClient.setRequestContext(adminContext);
model = publicApiClient.tasksClient().findTaskFormModel(task.getId());
assertNotNull(model);
entries = (JSONArray) model.get("entries");
assertNotNull(entries);
modelFieldsByName = createEntryMap(entries);
testModelEntries(modelFieldsByName);
// get task form model with non involved user
publicApiClient.setRequestContext(otherContext);
try {
publicApiClient.tasksClient().findTaskFormModel(task.getId());
} catch (PublicApiException e) {
assertEquals(403, e.getHttpResponse().getStatusCode());
}
// get task form model with invalid task id
publicApiClient.setRequestContext(requestContext);
try {
publicApiClient.tasksClient().findTaskFormModel("fakeid");
} catch (PublicApiException e) {
assertEquals(404, e.getHttpResponse().getStatusCode());
}
} finally {
cleanupProcessInstance(processInstance);
}
}
use of org.alfresco.rest.api.tests.client.PublicApiException in project alfresco-remote-api by Alfresco.
the class TaskWorkflowApiTest method testGetUnexistingTaskById.
@Test
public void testGetUnexistingTaskById() throws Exception {
initApiClientWithTestUser();
TasksClient tasksClient = publicApiClient.tasksClient();
try {
tasksClient.findTaskById("unexisting");
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());
}
}
Aggregations