Search in sources :

Example 16 with PublicApiException

use of org.alfresco.rest.api.tests.client.PublicApiException 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 17 with PublicApiException

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

the class TaskWorkflowApiTest method testGetTaskVariablesAuthentication.

@Test
public void testGetTaskVariablesAuthentication() throws Exception {
    RequestContext requestContext = initApiClientWithTestUser();
    String initiator = getOtherPersonInNetwork(requestContext.getRunAsUser(), requestContext.getNetworkId()).getId();
    // Start process by one user and try to access the task variables as the task assignee instead of the process
    // initiator to see if the assignee is authorized to get the task
    ProcessInstance processInstance = startAdhocProcess(initiator, requestContext.getNetworkId(), null);
    try {
        Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        assertNotNull(task);
        TasksClient tasksClient = publicApiClient.tasksClient();
        // Try accessing task variables when NOT involved in the task
        try {
            tasksClient.findTaskVariables(task.getId());
            fail("Exception expected");
        } catch (PublicApiException expected) {
            assertEquals(HttpStatus.FORBIDDEN.value(), expected.getHttpResponse().getStatusCode());
            assertErrorSummary("Permission was denied", expected.getHttpResponse());
        }
        // Set assignee, task variables should be accessible now
        activitiProcessEngine.getTaskService().setAssignee(task.getId(), requestContext.getRunAsUser());
        JSONObject jsonObject = tasksClient.findTaskVariables(task.getId());
        assertNotNull(jsonObject);
        // Fetching task variables as admin should be possible
        String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
        publicApiClient.setRequestContext(new RequestContext(TenantUtil.DEFAULT_TENANT, tenantAdmin));
        jsonObject = tasksClient.findTaskVariables(task.getId());
        assertNotNull(jsonObject);
        // Fetching the task variables as a admin from another tenant shouldn't be possible
        TestNetwork anotherNetwork = getOtherNetwork(requestContext.getNetworkId());
        tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + anotherNetwork.getId();
        publicApiClient.setRequestContext(new RequestContext(TenantUtil.DEFAULT_TENANT, tenantAdmin));
        try {
            tasksClient.findTaskVariables(task.getId());
            fail("Exception expected");
        } catch (PublicApiException expected) {
            assertEquals(HttpStatus.FORBIDDEN.value(), expected.getHttpResponse().getStatusCode());
            assertErrorSummary("Permission was denied", expected.getHttpResponse());
        }
    } 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) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 18 with PublicApiException

use of org.alfresco.rest.api.tests.client.PublicApiException 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);
    }
}
Also used : PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) Task(org.activiti.engine.task.Task) HistoricTaskInstance(org.activiti.engine.history.HistoricTaskInstance) 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 19 with PublicApiException

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

the class TaskWorkflowApiTest method testDeleteTaskItem.

@Test
@SuppressWarnings("unchecked")
public void testDeleteTaskItem() throws Exception {
    final 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);
    // Create test-document and add to package
    NodeRef[] docNodeRefs = createTestDocuments(requestContext);
    ProcessInfo processInfo = startAdhocProcess(requestContext, docNodeRefs);
    final Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processInfo.getId()).singleResult();
    assertNotNull(task);
    activitiProcessEngine.getTaskService().setAssignee(task.getId(), null);
    try {
        TasksClient tasksClient = publicApiClient.tasksClient();
        tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
        try {
            tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // delete item as admin
        JSONObject createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        JSONObject result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        JSONObject itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        publicApiClient.setRequestContext(adminContext);
        tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
        try {
            tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // delete item with candidate user
        createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        activitiProcessEngine.getTaskService().addCandidateUser(task.getId(), otherPerson);
        publicApiClient.setRequestContext(otherContext);
        tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
        try {
            tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // delete item with not involved user
        createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        activitiProcessEngine.getTaskService().deleteCandidateUser(task.getId(), otherPerson);
        publicApiClient.setRequestContext(otherContext);
        try {
            tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(403, e.getHttpResponse().getStatusCode());
        }
        // delete item with user from candidate group with no assignee
        List<MemberOfSite> memberships = getTestFixture().getNetwork(otherContext.getNetworkId()).getSiteMemberships(otherContext.getRunAsUser());
        assertTrue(memberships.size() > 0);
        MemberOfSite memberOfSite = memberships.get(0);
        String group = "GROUP_site_" + memberOfSite.getSiteId() + "_" + memberOfSite.getRole().name();
        activitiProcessEngine.getTaskService().deleteCandidateUser(task.getId(), otherContext.getRunAsUser());
        activitiProcessEngine.getTaskService().addCandidateGroup(task.getId(), group);
        publicApiClient.setRequestContext(otherContext);
        createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
        try {
            tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // delete item with user from candidate group with assignee
        activitiProcessEngine.getTaskService().setAssignee(task.getId(), requestContext.getRunAsUser());
        publicApiClient.setRequestContext(requestContext);
        createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        publicApiClient.setRequestContext(otherContext);
        try {
            tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(403, e.getHttpResponse().getStatusCode());
        }
        publicApiClient.setRequestContext(requestContext);
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
        try {
            tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // invalid task id
        publicApiClient.setRequestContext(requestContext);
        try {
            tasksClient.deleteTaskItem("fakeid", docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // invalid item id
        try {
            tasksClient.deleteTaskItem(task.getId(), "fakeid");
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
        // delete item from completed task
        createItemObject = new JSONObject();
        createItemObject.put("id", docNodeRefs[0].getId());
        result = tasksClient.addTaskItem(task.getId(), createItemObject.toJSONString());
        assertNotNull(result);
        assertEquals(docNodeRefs[0].getId(), result.get("id"));
        itemJSON = tasksClient.findTaskItem(task.getId(), docNodeRefs[0].getId());
        assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                activitiProcessEngine.getTaskService().complete(task.getId());
                return null;
            }
        }, requestContext.getRunAsUser(), requestContext.getNetworkId());
        try {
            tasksClient.deleteTaskItem(task.getId(), docNodeRefs[0].getId());
            fail("Expected exception");
        } catch (PublicApiException e) {
            assertEquals(404, e.getHttpResponse().getStatusCode());
        }
    } finally {
        cleanupProcessInstance(processInfo.getId());
    }
}
Also used : Task(org.activiti.engine.task.Task) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) MemberOfSite(org.alfresco.rest.api.tests.client.data.MemberOfSite) ProcessInfo(org.alfresco.rest.workflow.api.model.ProcessInfo) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.simple.JSONObject) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 20 with PublicApiException

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

the class TaskWorkflowApiTest method testUpdateUnexistingTask.

@Test
@SuppressWarnings("unchecked")
public void testUpdateUnexistingTask() throws Exception {
    initApiClientWithTestUser();
    TasksClient tasksClient = publicApiClient.tasksClient();
    try {
        JSONObject taskBody = new JSONObject();
        taskBody.put("name", "Updated name");
        List<String> selectedFields = new ArrayList<String>();
        selectedFields.addAll(Arrays.asList(new String[] { "name" }));
        tasksClient.updateTask("unexisting", taskBody, selectedFields);
        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());
    }
}
Also used : PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) JSONObject(org.json.simple.JSONObject) TasksClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)81 Test (org.junit.Test)76 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)67 JSONObject (org.json.simple.JSONObject)40 ArrayList (java.util.ArrayList)24 Task (org.activiti.engine.task.Task)23 ProcessInfo (org.alfresco.rest.workflow.api.model.ProcessInfo)23 TasksClient (org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient)21 ProcessesClient (org.alfresco.rest.workflow.api.tests.WorkflowApiClient.ProcessesClient)19 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)18 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)16 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)16 TestSite (org.alfresco.rest.api.tests.RepoService.TestSite)15 NodeRef (org.alfresco.service.cmr.repository.NodeRef)15 HashMap (java.util.HashMap)14 List (java.util.List)13 ListResponse (org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse)13 JSONArray (org.json.simple.JSONArray)13 TenantRunAsWork (org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork)11 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)9