Search in sources :

Example 81 with RequestContext

use of org.alfresco.rest.api.tests.client.RequestContext 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);
    }
}
Also used : PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) Task(org.activiti.engine.task.Task) JSONObject(org.json.simple.JSONObject) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) ArrayList(java.util.ArrayList) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 82 with RequestContext

use of org.alfresco.rest.api.tests.client.RequestContext in project alfresco-remote-api by Alfresco.

the class TaskWorkflowApiTest method testSetOutcome.

@Test
@SuppressWarnings("unchecked")
public void testSetOutcome() throws Exception {
    RequestContext requestContext = initApiClientWithTestUser();
    ProcessInfo processInf = startReviewPooledProcess(requestContext);
    Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInf.getId()).singleResult();
    TasksClient tasksClient = publicApiClient.tasksClient();
    activitiProcessEngine.getTaskService().saveTask(task);
    Map<String, String> params = new HashMap<String, String>();
    params.put("select", "state,variables");
    HttpResponse response = tasksClient.update("tasks", task.getId(), null, null, "{\"state\":\"completed\",\"variables\":[{\"name\":\"wf_reviewOutcome\",\"value\":\"Approve\",\"scope\":\"local\"},{\"name\":\"bpm_comment\",\"value\":\"approved by me\",\"scope\":\"local\"}]}", params, "Failed to update task", 200);
    HistoricTaskInstance historyTask = activitiProcessEngine.getHistoryService().createHistoricTaskInstanceQuery().taskId(task.getId()).includeProcessVariables().includeTaskLocalVariables().singleResult();
    String outcome = (String) historyTask.getTaskLocalVariables().get("bpm_outcome");
    assertEquals("Approve", outcome);
}
Also used : Task(org.activiti.engine.task.Task) HistoricTaskInstance(org.activiti.engine.history.HistoricTaskInstance) HashMap(java.util.HashMap) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) ProcessInfo(org.alfresco.rest.workflow.api.model.ProcessInfo) Test(org.junit.Test)

Example 83 with RequestContext

use of org.alfresco.rest.api.tests.client.RequestContext in project alfresco-remote-api by Alfresco.

the class TaskWorkflowApiTest method testDeleteTaskVariable.

@Test
public void testDeleteTaskVariable() 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);
        activitiProcessEngine.getTaskService().setVariableLocal(task.getId(), "myVariable", "This is a variable");
        activitiProcessEngine.getTaskService().setVariableLocal(task.getId(), "overlappingVariable", "Value set in task");
        // Delete a task-variable
        TasksClient tasksClient = publicApiClient.tasksClient();
        tasksClient.deleteTaskVariable(task.getId(), "myVariable");
        assertFalse(activitiProcessEngine.getTaskService().hasVariableLocal(task.getId(), "myVariable"));
        // Delete a task-variable that has the same name as a global process-variable - which should remain untouched after delete
        tasksClient.deleteTaskVariable(task.getId(), "overlappingVariable");
        assertFalse(activitiProcessEngine.getTaskService().hasVariableLocal(task.getId(), "overlappingVariable"));
        assertTrue(activitiProcessEngine.getRuntimeService().hasVariable(processInstance.getId(), "overlappingVariable"));
        assertEquals("Value set in process", activitiProcessEngine.getRuntimeService().getVariable(processInstance.getId(), "overlappingVariable"));
    } finally {
        cleanupProcessInstance(processInstance);
    }
}
Also used : Task(org.activiti.engine.task.Task) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 84 with RequestContext

use of org.alfresco.rest.api.tests.client.RequestContext in project alfresco-remote-api by Alfresco.

the class TaskWorkflowApiTest method testGetTasksWithSorting.

@Test
public void testGetTasksWithSorting() 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));
        }
        List<Task> taskList = new ArrayList<Task>();
        for (int i = 0; i < numberOfTasks; i++) {
            Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(startedProcesses.get(i).getProcessInstanceId()).singleResult();
            activitiProcessEngine.getTaskService().setPriority(task.getId(), (i + 1) * 10);
            taskList.add(task);
        }
        // set last task priority to 1
        activitiProcessEngine.getTaskService().setPriority(taskList.get(numberOfTasks - 1).getId(), 1);
        TasksClient tasksClient = publicApiClient.tasksClient();
        // Test with existing processDefinitionId
        Map<String, String> params = new HashMap<String, String>();
        params.put("where", "(processDefinitionId = '" + startedProcesses.get(0).getProcessDefinitionId() + "')");
        params.put("orderBy", "priority ASC");
        JSONObject tasksResponseJSON = tasksClient.findTasks(params);
        JSONObject paginationJSON = (JSONObject) tasksResponseJSON.get("pagination");
        assertEquals(6l, paginationJSON.get("count"));
        assertEquals(6l, paginationJSON.get("totalItems"));
        JSONArray tasksListJSON = (JSONArray) tasksResponseJSON.get("entries");
        assertEquals(6, tasksListJSON.size());
        JSONObject taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(0)).get("entry");
        assertEquals(taskList.get(numberOfTasks - 1).getId(), taskJSON.get("id"));
        taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(1)).get("entry");
        assertEquals(taskList.get(0).getId(), taskJSON.get("id"));
        taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(2)).get("entry");
        assertEquals(taskList.get(1).getId(), taskJSON.get("id"));
        taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(3)).get("entry");
        assertEquals(taskList.get(2).getId(), taskJSON.get("id"));
        taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(4)).get("entry");
        assertEquals(taskList.get(3).getId(), taskJSON.get("id"));
        taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(5)).get("entry");
        assertEquals(taskList.get(4).getId(), taskJSON.get("id"));
        params.put("orderBy", "priority DESC");
        tasksResponseJSON = tasksClient.findTasks(params);
        paginationJSON = (JSONObject) tasksResponseJSON.get("pagination");
        assertEquals(6l, paginationJSON.get("count"));
        assertEquals(6l, paginationJSON.get("totalItems"));
        tasksListJSON = (JSONArray) tasksResponseJSON.get("entries");
        assertEquals(6, tasksListJSON.size());
        taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(0)).get("entry");
        assertEquals(taskList.get(4).getId(), taskJSON.get("id"));
        taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(1)).get("entry");
        assertEquals(taskList.get(3).getId(), taskJSON.get("id"));
        taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(2)).get("entry");
        assertEquals(taskList.get(2).getId(), taskJSON.get("id"));
        taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(3)).get("entry");
        assertEquals(taskList.get(1).getId(), taskJSON.get("id"));
        taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(4)).get("entry");
        assertEquals(taskList.get(0).getId(), taskJSON.get("id"));
        taskJSON = (JSONObject) ((JSONObject) tasksListJSON.get(5)).get("entry");
        assertEquals(taskList.get(numberOfTasks - 1).getId(), taskJSON.get("id"));
        params.put("orderBy", "dueAt DESC");
        tasksResponseJSON = tasksClient.findTasks(params);
        tasksListJSON = (JSONArray) tasksResponseJSON.get("entries");
        assertEquals(6, tasksListJSON.size());
    } finally {
        cleanupProcessInstance(startedProcesses.toArray(new ProcessInstance[] {}));
    }
}
Also used : Task(org.activiti.engine.task.Task) HashMap(java.util.HashMap) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 85 with RequestContext

use of org.alfresco.rest.api.tests.client.RequestContext in project alfresco-remote-api by Alfresco.

the class TaskWorkflowApiTest method testChangeDueDate.

@Test
@SuppressWarnings("unchecked")
public void testChangeDueDate() throws Exception {
    RequestContext requestContext = initApiClientWithTestUser();
    ProcessInstance processInstance = startAdhocProcess(requestContext.getRunAsUser(), requestContext.getNetworkId(), null);
    try {
        Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        TasksClient tasksClient = publicApiClient.tasksClient();
        JSONObject taskObject = tasksClient.findTaskById(task.getId());
        assertNull(taskObject.get("dueAt"));
        List<String> selectedFields = new ArrayList<String>();
        selectedFields.addAll(Arrays.asList(new String[] { "name", "description", "dueAt", "priority", "assignee", "owner" }));
        // set due date
        JSONObject taskBody = new JSONObject();
        String dueAt = formatDate(new Date());
        taskBody.put("dueAt", dueAt);
        JSONObject result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
        assertNotNull(result.get("dueAt"));
        taskObject = tasksClient.findTaskById(task.getId());
        assertNotNull(taskObject.get("dueAt"));
        taskBody = new JSONObject();
        taskBody.put("dueAt", taskObject.get("dueAt"));
        result = tasksClient.updateTask(task.getId(), taskBody, selectedFields);
        assertNotNull(result.get("dueAt"));
        taskObject = tasksClient.findTaskById(task.getId());
        assertNotNull(taskObject.get("dueAt"));
        JSONObject variableBody = new JSONObject();
        variableBody.put("name", "bpm_workflowDueDate");
        variableBody.put("value", formatDate(new Date()));
        variableBody.put("type", "d:date");
        variableBody.put("scope", "global");
        tasksClient.updateTaskVariable(task.getId(), "bpm_workflowDueDate", variableBody);
    } finally {
        cleanupProcessInstance(processInstance);
    }
}
Also used : Task(org.activiti.engine.task.Task) JSONObject(org.json.simple.JSONObject) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) ArrayList(java.util.ArrayList) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Date(java.util.Date) Test(org.junit.Test)

Aggregations

RequestContext (org.alfresco.rest.api.tests.client.RequestContext)185 Test (org.junit.Test)174 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)102 JSONObject (org.json.simple.JSONObject)67 HashMap (java.util.HashMap)58 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)52 ArrayList (java.util.ArrayList)46 NodeRef (org.alfresco.service.cmr.repository.NodeRef)45 Task (org.activiti.engine.task.Task)44 TasksClient (org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient)39 TestSite (org.alfresco.rest.api.tests.RepoService.TestSite)38 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)36 ProcessInfo (org.alfresco.rest.workflow.api.model.ProcessInfo)36 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)33 VersionableAspectTest (org.alfresco.repo.version.VersionableAspectTest)26 Person (org.alfresco.rest.api.tests.client.data.Person)25 JSONArray (org.json.simple.JSONArray)25 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)24 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)24 List (java.util.List)23