Search in sources :

Example 86 with RequestContext

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[] {}));
    }
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 87 with RequestContext

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);
    }
}
Also used : PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) 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 88 with RequestContext

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

Example 89 with RequestContext

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());
    }
}
Also used : Task(org.activiti.engine.task.Task) JSONObject(org.json.simple.JSONObject) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) JSONArray(org.json.simple.JSONArray) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) ProcessInfo(org.alfresco.rest.workflow.api.model.ProcessInfo) Test(org.junit.Test)

Example 90 with RequestContext

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);
    }
}
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)

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