Search in sources :

Example 41 with RequestContext

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

the class ProcessWorkflowApiTest method testGetProcessTasks.

@Test
public void testGetProcessTasks() throws Exception {
    final RequestContext requestContext = initApiClientWithTestUser();
    String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
    final RequestContext adminContext = new RequestContext(requestContext.getNetworkId(), tenantAdmin);
    TestNetwork anotherNetwork = getOtherNetwork(requestContext.getNetworkId());
    tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + anotherNetwork.getId();
    final RequestContext otherContext = new RequestContext(anotherNetwork.getId(), tenantAdmin);
    String otherPerson = getOtherPersonInNetwork(requestContext.getRunAsUser(), requestContext.getNetworkId()).getId();
    RequestContext otherPersonContext = new RequestContext(requestContext.getNetworkId(), otherPerson);
    final ProcessInfo process1 = startAdhocProcess(requestContext, null);
    try {
        ProcessesClient processesClient = publicApiClient.processesClient();
        Map<String, String> paramMap = new HashMap<String, String>();
        JSONObject tasksJSON = processesClient.getTasks(process1.getId(), paramMap);
        assertNotNull(tasksJSON);
        JSONArray entriesJSON = (JSONArray) tasksJSON.get("entries");
        assertNotNull(entriesJSON);
        assertTrue(entriesJSON.size() == 1);
        JSONObject taskJSONObject = (JSONObject) ((JSONObject) entriesJSON.get(0)).get("entry");
        assertNotNull(taskJSONObject.get("id"));
        assertEquals(process1.getId(), taskJSONObject.get("processId"));
        assertEquals(process1.getProcessDefinitionId(), taskJSONObject.get("processDefinitionId"));
        assertEquals("adhocTask", taskJSONObject.get("activityDefinitionId"));
        assertEquals("Adhoc Task", taskJSONObject.get("name"));
        assertEquals(requestContext.getRunAsUser(), taskJSONObject.get("assignee"));
        assertEquals(2l, taskJSONObject.get("priority"));
        assertEquals("wf:adhocTask", taskJSONObject.get("formResourceKey"));
        assertNull(taskJSONObject.get("endedAt"));
        assertNull(taskJSONObject.get("durationInMs"));
        paramMap = new HashMap<String, String>();
        paramMap.put("status", "active");
        tasksJSON = processesClient.getTasks(process1.getId(), paramMap);
        assertNotNull(tasksJSON);
        entriesJSON = (JSONArray) tasksJSON.get("entries");
        assertNotNull(entriesJSON);
        assertTrue(entriesJSON.size() == 1);
        paramMap = new HashMap<String, String>();
        paramMap.put("status", "completed");
        tasksJSON = processesClient.getTasks(process1.getId(), paramMap);
        assertNotNull(tasksJSON);
        entriesJSON = (JSONArray) tasksJSON.get("entries");
        assertNotNull(entriesJSON);
        assertTrue(entriesJSON.size() == 0);
        paramMap = new HashMap<String, String>();
        try {
            processesClient.getTasks("fakeid", paramMap);
            fail("Exception expected");
        } catch (PublicApiException expected) {
            assertEquals(HttpStatus.NOT_FOUND.value(), expected.getHttpResponse().getStatusCode());
            assertErrorSummary("The entity with id: fakeid was not found", expected.getHttpResponse());
        }
        // get tasks with admin from the same tenant as the process initiator
        publicApiClient.setRequestContext(adminContext);
        paramMap = new HashMap<String, String>();
        tasksJSON = processesClient.getTasks(process1.getId(), paramMap);
        assertNotNull(tasksJSON);
        entriesJSON = (JSONArray) tasksJSON.get("entries");
        assertNotNull(entriesJSON);
        assertTrue(entriesJSON.size() == 1);
        // get tasks with admin from another tenant as the process initiator
        publicApiClient.setRequestContext(otherContext);
        paramMap = new HashMap<String, String>();
        try {
            tasksJSON = processesClient.getTasks(process1.getId(), paramMap);
            fail("forbidden expected");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.FORBIDDEN.value(), e.getHttpResponse().getStatusCode());
        }
        // get task with other not-involved person
        publicApiClient.setRequestContext(otherPersonContext);
        paramMap = new HashMap<String, String>();
        try {
            tasksJSON = processesClient.getTasks(process1.getId(), paramMap);
            fail("forbidden expected");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.FORBIDDEN.value(), e.getHttpResponse().getStatusCode());
        }
        // involve other person and get task
        final Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(process1.getId()).singleResult();
        activitiProcessEngine.getTaskService().addCandidateUser(task.getId(), otherPersonContext.getRunAsUser());
        publicApiClient.setRequestContext(otherPersonContext);
        paramMap = new HashMap<String, String>();
        tasksJSON = processesClient.getTasks(process1.getId(), paramMap);
        assertNotNull(tasksJSON);
        entriesJSON = (JSONArray) tasksJSON.get("entries");
        assertNotNull(entriesJSON);
        assertTrue(entriesJSON.size() == 1);
        // complete task and get tasks
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                activitiProcessEngine.getTaskService().complete(task.getId());
                return null;
            }
        }, requestContext.getRunAsUser(), requestContext.getNetworkId());
        publicApiClient.setRequestContext(requestContext);
        paramMap = new HashMap<String, String>();
        paramMap.put("status", "any");
        tasksJSON = processesClient.getTasks(process1.getId(), paramMap);
        assertNotNull(tasksJSON);
        entriesJSON = (JSONArray) tasksJSON.get("entries");
        assertNotNull(entriesJSON);
        assertTrue(entriesJSON.size() == 2);
        publicApiClient.setRequestContext(otherPersonContext);
        paramMap = new HashMap<String, String>();
        paramMap.put("status", "any");
        tasksJSON = processesClient.getTasks(process1.getId(), paramMap);
        assertNotNull(tasksJSON);
        entriesJSON = (JSONArray) tasksJSON.get("entries");
        assertNotNull(entriesJSON);
        assertTrue(entriesJSON.size() == 2);
    } finally {
        cleanupProcessInstance(process1.getId());
    }
}
Also used : ProcessesClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.ProcessesClient) Task(org.activiti.engine.task.Task) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) ProcessInfo(org.alfresco.rest.workflow.api.model.ProcessInfo) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) JSONObject(org.json.simple.JSONObject) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 42 with RequestContext

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

the class ProcessWorkflowApiTest method testCreateProcessInstanceForPooledReview.

@Test
public void testCreateProcessInstanceForPooledReview() throws Exception {
    final RequestContext requestContext = initApiClientWithTestUser();
    final ProcessInfo processInfo = startReviewPooledProcess(requestContext);
    assertNotNull(processInfo);
    assertNotNull(processInfo.getId());
    cleanupProcessInstance(processInfo.getId());
}
Also used : RequestContext(org.alfresco.rest.api.tests.client.RequestContext) ProcessInfo(org.alfresco.rest.workflow.api.model.ProcessInfo) Test(org.junit.Test)

Example 43 with RequestContext

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

the class ProcessWorkflowApiTest method testUpdateProcessVariableWithWrongType.

@SuppressWarnings("unchecked")
@Test
public void testUpdateProcessVariableWithWrongType() throws Exception {
    final RequestContext requestContext = initApiClientWithTestUser();
    ProcessInfo processRest = startParallelReviewProcess(requestContext);
    try {
        assertNotNull(processRest);
        String processId = processRest.getId();
        // Update an existing variable with wrong type
        JSONObject variableJson = new JSONObject();
        variableJson.put("name", "wf_requiredApprovePercent");
        variableJson.put("value", 55.99);
        variableJson.put("type", "d:double");
        try {
            publicApiClient.processesClient().updateVariable(processId, "wf_requiredApprovePercent", variableJson);
            fail("Exception expected");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.BAD_REQUEST.value(), e.getHttpResponse().getStatusCode());
        }
        variableJson = new JSONObject();
        variableJson.put("name", "wf_requiredApprovePercent");
        variableJson.put("value", 55.99);
        variableJson.put("type", "d:int");
        JSONObject resultEntry = publicApiClient.processesClient().updateVariable(processId, "wf_requiredApprovePercent", variableJson);
        assertNotNull(resultEntry);
        JSONObject result = (JSONObject) resultEntry.get("entry");
        assertEquals("wf_requiredApprovePercent", result.get("name"));
        assertEquals(55l, result.get("value"));
        assertEquals("d:int", result.get("type"));
        assertEquals(55, activitiProcessEngine.getRuntimeService().getVariable(processId, "wf_requiredApprovePercent"));
        JSONObject processvariables = publicApiClient.processesClient().getProcessvariables(processId);
        assertNotNull(processvariables);
        // Add process variables to map for easy lookup
        Map<String, JSONObject> variablesByName = new HashMap<String, JSONObject>();
        JSONObject entry = null;
        JSONArray entries = (JSONArray) processvariables.get("entries");
        assertNotNull(entries);
        for (int i = 0; i < entries.size(); i++) {
            entry = (JSONObject) entries.get(i);
            assertNotNull(entry);
            entry = (JSONObject) entry.get("entry");
            assertNotNull(entry);
            variablesByName.put((String) entry.get("name"), entry);
        }
        JSONObject approvePercentObject = variablesByName.get("wf_requiredApprovePercent");
        assertNotNull(approvePercentObject);
        assertEquals(55l, approvePercentObject.get("value"));
        assertEquals("d:int", approvePercentObject.get("type"));
        // set a new variable
        variableJson = new JSONObject();
        variableJson.put("name", "testVariable");
        variableJson.put("value", "text");
        variableJson.put("type", "d:text");
        resultEntry = publicApiClient.processesClient().updateVariable(processId, "testVariable", variableJson);
        assertNotNull(resultEntry);
        result = (JSONObject) resultEntry.get("entry");
        assertEquals("testVariable", result.get("name"));
        assertEquals("text", result.get("value"));
        assertEquals("d:text", result.get("type"));
        assertEquals("text", activitiProcessEngine.getRuntimeService().getVariable(processId, "testVariable"));
        // change the variable value and type (should be working because no content model type)
        variableJson = new JSONObject();
        variableJson.put("name", "testVariable");
        variableJson.put("value", 123);
        variableJson.put("type", "d:int");
        resultEntry = publicApiClient.processesClient().updateVariable(processId, "testVariable", variableJson);
        assertNotNull(resultEntry);
        result = (JSONObject) resultEntry.get("entry");
        assertEquals("testVariable", result.get("name"));
        assertEquals(123l, result.get("value"));
        assertEquals("d:int", result.get("type"));
        assertEquals(123, activitiProcessEngine.getRuntimeService().getVariable(processId, "testVariable"));
        // change the variable value for a list of noderefs (bpm_assignees)
        final JSONObject updateAssigneesJson = new JSONObject();
        updateAssigneesJson.put("name", "bpm_assignees");
        updateAssigneesJson.put("type", "d:noderef");
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                JSONArray assigneeArray = new JSONArray();
                assigneeArray.add(requestContext.getRunAsUser());
                updateAssigneesJson.put("value", assigneeArray);
                return null;
            }
        }, requestContext.getRunAsUser(), requestContext.getNetworkId());
        resultEntry = publicApiClient.processesClient().updateVariable(processId, "bpm_assignees", updateAssigneesJson);
        assertNotNull(resultEntry);
        final JSONObject updateAssigneeResult = (JSONObject) resultEntry.get("entry");
        assertEquals("bpm_assignees", updateAssigneeResult.get("name"));
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                JSONArray assigneeArray = (JSONArray) updateAssigneeResult.get("value");
                assertNotNull(assigneeArray);
                assertEquals(1, assigneeArray.size());
                return null;
            }
        }, requestContext.getRunAsUser(), requestContext.getNetworkId());
        assertEquals("d:noderef", updateAssigneeResult.get("type"));
        // update the bpm_assignees with a single entry, should result in an error
        final JSONObject updateAssigneeJson = new JSONObject();
        updateAssigneeJson.put("name", "bpm_assignees");
        updateAssigneeJson.put("type", "d:noderef");
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                updateAssigneeJson.put("value", requestContext.getRunAsUser());
                return null;
            }
        }, requestContext.getRunAsUser(), requestContext.getNetworkId());
        try {
            publicApiClient.processesClient().updateVariable(processId, "bpm_assignees", updateAssigneeJson);
            fail("Exception expected");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.BAD_REQUEST.value(), e.getHttpResponse().getStatusCode());
        }
        // change the variable value with a non-existing person
        variableJson = new JSONObject();
        variableJson.put("name", "bpm_assignees");
        JSONArray assigneeArray = new JSONArray();
        assigneeArray.add("nonExistingPerson");
        variableJson.put("value", assigneeArray);
        variableJson.put("type", "d:noderef");
        try {
            publicApiClient.processesClient().updateVariable(processId, "bpm_assignees", variableJson);
            fail("Exception expected");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.BAD_REQUEST.value(), e.getHttpResponse().getStatusCode());
        }
    } finally {
        cleanupProcessInstance(processRest.getId());
    }
}
Also used : HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) ProcessInfo(org.alfresco.rest.workflow.api.model.ProcessInfo) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) JSONObject(org.json.simple.JSONObject) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 44 with RequestContext

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

the class ProcessWorkflowApiTest method testUpdateInitiatorAndGetProcessVariables.

// MNT-17918
@SuppressWarnings("unchecked")
@Test
public void testUpdateInitiatorAndGetProcessVariables() throws Exception {
    RequestContext requestContext = initApiClientWithTestUser();
    String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
    RequestContext adminContext = new RequestContext(requestContext.getNetworkId(), tenantAdmin);
    ProcessInfo processRest = startAdhocProcess(requestContext, null);
    try {
        assertNotNull(processRest);
        String processId = processRest.getId();
        // Update initiator variable to "admin"
        JSONObject variableJson = new JSONObject();
        variableJson.put("name", "initiator");
        variableJson.put("type", "d:noderef");
        NodeRef personRef = TenantUtil.runAsUserTenant(new TenantRunAsWork<NodeRef>() {

            @Override
            public NodeRef doWork() throws Exception {
                String assignee = adminContext.getRunAsUser();
                NodeRef personRef = getPersonNodeRef(assignee).getNodeRef();
                variableJson.put("value", personRef.toString());
                return personRef;
            }
        }, adminContext.getRunAsUser(), adminContext.getNetworkId());
        JSONObject resultEntry = publicApiClient.processesClient().updateVariable(processId, "initiator", variableJson);
        assertNotNull(resultEntry);
        final JSONObject updateInitiatorResult = (JSONObject) resultEntry.get("entry");
        assertEquals("initiator", updateInitiatorResult.get("name"));
        assertEquals("d:noderef", updateInitiatorResult.get("type"));
        assertNotNull("Variable value should be returned", updateInitiatorResult.get("value"));
        assertEquals(personRef.getId(), updateInitiatorResult.get("value"));
        // Get process variables after updating "initiator"
        publicApiClient.setRequestContext(adminContext);
        resultEntry = publicApiClient.processesClient().getProcessvariables(processId);
        assertNotNull(resultEntry);
        validateVariableField(resultEntry, "initiator", adminContext.getRunAsUser());
    } finally {
        cleanupProcessInstance(processRest.getId());
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.simple.JSONObject) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) ProcessInfo(org.alfresco.rest.workflow.api.model.ProcessInfo) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) Test(org.junit.Test)

Example 45 with RequestContext

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

the class ProcessWorkflowApiTest method testGetProcessItem.

@Test
public void testGetProcessItem() throws Exception {
    final RequestContext requestContext = initApiClientWithTestUser();
    NodeRef[] docNodeRefs = createTestDocuments(requestContext);
    final ProcessInfo processRest = startAdhocProcess(requestContext, docNodeRefs);
    assertNotNull(processRest);
    final String newProcessInstanceId = processRest.getId();
    ProcessesClient processesClient = publicApiClient.processesClient();
    JSONObject itemJSON = processesClient.findProcessItem(newProcessInstanceId, docNodeRefs[0].getId());
    assertNotNull(itemJSON);
    assertEquals(docNodeRefs[0].getId(), itemJSON.get("id"));
    assertEquals("Test Doc1", itemJSON.get("name"));
    assertEquals("Test Doc1 Title", itemJSON.get("title"));
    assertEquals("Test Doc1 Description", itemJSON.get("description"));
    assertNotNull(itemJSON.get("createdAt"));
    assertEquals(requestContext.getRunAsUser(), itemJSON.get("createdBy"));
    assertNotNull(itemJSON.get("modifiedAt"));
    assertEquals(requestContext.getRunAsUser(), itemJSON.get("modifiedBy"));
    assertNotNull(itemJSON.get("size"));
    assertNotNull(itemJSON.get("mimeType"));
    cleanupProcessInstance(processRest.getId());
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ProcessesClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.ProcessesClient) JSONObject(org.json.simple.JSONObject) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) ProcessInfo(org.alfresco.rest.workflow.api.model.ProcessInfo) 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