Search in sources :

Example 71 with PublicApiException

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

the class ProcessWorkflowApiTest method testGetProcessItems.

@Test
public void testGetProcessItems() 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 itemsJSON = processesClient.findProcessItems(newProcessInstanceId);
    assertNotNull(itemsJSON);
    JSONArray entriesJSON = (JSONArray) itemsJSON.get("entries");
    assertNotNull(entriesJSON);
    assertTrue(entriesJSON.size() == 2);
    boolean doc1Found = false;
    boolean doc2Found = false;
    for (Object entryObject : entriesJSON) {
        JSONObject entryObjectJSON = (JSONObject) entryObject;
        JSONObject entryJSON = (JSONObject) entryObjectJSON.get("entry");
        if (entryJSON.get("name").equals("Test Doc1")) {
            doc1Found = true;
            assertEquals(docNodeRefs[0].getId(), entryJSON.get("id"));
            assertEquals("Test Doc1", entryJSON.get("name"));
            assertEquals("Test Doc1 Title", entryJSON.get("title"));
            assertEquals("Test Doc1 Description", entryJSON.get("description"));
            assertNotNull(entryJSON.get("createdAt"));
            assertEquals(requestContext.getRunAsUser(), entryJSON.get("createdBy"));
            assertNotNull(entryJSON.get("modifiedAt"));
            assertEquals(requestContext.getRunAsUser(), entryJSON.get("modifiedBy"));
            assertNotNull(entryJSON.get("size"));
            assertNotNull(entryJSON.get("mimeType"));
        } else {
            doc2Found = true;
            assertEquals(docNodeRefs[1].getId(), entryJSON.get("id"));
            assertEquals("Test Doc2", entryJSON.get("name"));
            assertEquals("Test Doc2 Title", entryJSON.get("title"));
            assertEquals("Test Doc2 Description", entryJSON.get("description"));
            assertNotNull(entryJSON.get("createdAt"));
            assertEquals(requestContext.getRunAsUser(), entryJSON.get("createdBy"));
            assertNotNull(entryJSON.get("modifiedAt"));
            assertEquals(requestContext.getRunAsUser(), entryJSON.get("modifiedBy"));
            assertNotNull(entryJSON.get("size"));
            assertNotNull(entryJSON.get("mimeType"));
        }
    }
    assertTrue(doc1Found);
    assertTrue(doc2Found);
    cleanupProcessInstance(processRest.getId());
    try {
        processesClient.findProcessItems("fakeid");
        fail("Exception expected");
    } catch (PublicApiException e) {
        assertEquals(404, e.getHttpResponse().getStatusCode());
    }
}
Also used : PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ProcessesClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.ProcessesClient) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) 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)

Example 72 with PublicApiException

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

the class ProcessWorkflowApiTest method testCreateProcessInstanceWithId.

@Test
@SuppressWarnings("unchecked")
public void testCreateProcessInstanceWithId() throws Exception {
    final RequestContext requestContext = initApiClientWithTestUser();
    org.activiti.engine.repository.ProcessDefinition processDefinition = activitiProcessEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionKey("@" + requestContext.getNetworkId() + "@activitiAdhoc").singleResult();
    ProcessesClient processesClient = publicApiClient.processesClient();
    JSONObject createProcessObject = new JSONObject();
    createProcessObject.put("processDefinitionId", processDefinition.getId());
    final JSONObject variablesObject = new JSONObject();
    variablesObject.put("bpm_dueDate", ISO8601DateFormat.format(new Date()));
    variablesObject.put("bpm_priority", 1);
    variablesObject.put("bpm_description", "test description");
    TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            variablesObject.put("bpm_assignee", requestContext.getRunAsUser());
            return null;
        }
    }, requestContext.getRunAsUser(), requestContext.getNetworkId());
    createProcessObject.put("variables", variablesObject);
    ProcessInfo processRest = processesClient.createProcess(createProcessObject.toJSONString());
    assertNotNull(processRest);
    assertNotNull(processRest.getId());
    HistoricProcessInstance processInstance = activitiProcessEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(processRest.getId()).singleResult();
    assertEquals(processInstance.getId(), processRest.getId());
    assertEquals(processInstance.getStartActivityId(), processRest.getStartActivityId());
    assertEquals(processInstance.getStartUserId(), processRest.getStartUserId());
    assertEquals(processInstance.getStartTime(), processRest.getStartedAt());
    assertEquals(processInstance.getProcessDefinitionId(), processRest.getProcessDefinitionId());
    assertEquals("activitiAdhoc", processRest.getProcessDefinitionKey());
    assertNull(processRest.getBusinessKey());
    assertNull(processRest.getDeleteReason());
    assertNull(processRest.getDurationInMs());
    assertNull(processRest.getEndActivityId());
    assertNull(processRest.getEndedAt());
    assertNull(processRest.getSuperProcessInstanceId());
    Map<String, Object> variables = activitiProcessEngine.getRuntimeService().getVariables(processRest.getId());
    assertEquals("test description", variables.get("bpm_description"));
    assertEquals(1, variables.get("bpm_priority"));
    cleanupProcessInstance(processRest.getId());
    // Test same create method with an admin user
    String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
    publicApiClient.setRequestContext(new RequestContext(requestContext.getNetworkId(), tenantAdmin));
    processRest = processesClient.createProcess(createProcessObject.toJSONString());
    assertNotNull(processRest);
    variables = activitiProcessEngine.getRuntimeService().getVariables(processRest.getId());
    assertEquals("test description", variables.get("bpm_description"));
    assertEquals(1, variables.get("bpm_priority"));
    cleanupProcessInstance(processRest.getId());
    // Try with unexisting process definition ID
    publicApiClient.setRequestContext(requestContext);
    createProcessObject = new JSONObject();
    createProcessObject.put("processDefinitionId", "unexisting");
    try {
        processesClient.createProcess(createProcessObject.toJSONString());
        fail();
    } catch (PublicApiException e) {
        // Exception expected because of wrong process definition id
        assertEquals(HttpStatus.BAD_REQUEST.value(), e.getHttpResponse().getStatusCode());
        assertErrorSummary("No workflow definition could be found with id 'unexisting'.", e.getHttpResponse());
    }
}
Also used : ProcessesClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.ProcessesClient) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) ProcessInfo(org.alfresco.rest.workflow.api.model.ProcessInfo) Date(java.util.Date) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 73 with PublicApiException

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

the class ProcessWorkflowApiTest method testMNT12382.

@Test
public void testMNT12382() throws Exception {
    currentNetwork = getTestFixture().getRandomNetwork();
    TestPerson initiator = currentNetwork.getPeople().get(0);
    RequestContext requestContext = new RequestContext(currentNetwork.getId(), initiator.getId());
    publicApiClient.setRequestContext(requestContext);
    ProcessInfo processInfo = startReviewPooledProcess(requestContext);
    final List<TestPerson> persons = transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<List<TestPerson>>() {

        @SuppressWarnings("synthetic-access")
        public List<TestPerson> execute() throws Throwable {
            ArrayList<TestPerson> persons = new ArrayList<TestPerson>();
            persons.add(currentNetwork.createUser(new PersonInfo("Maxim0", "Bobyleu0", "maxim0.bobyleu0", "password", null, "skype", "location", "telephone", "mob", "instant", "google")));
            persons.add(currentNetwork.createUser(new PersonInfo("Maxim1", "Bobyleu1", "maxim1.bobyleu1", "password", null, "skype", "location", "telephone", "mob", "instant", "google")));
            persons.add(currentNetwork.createUser(new PersonInfo("Maxim2", "Bobyleu2", "maxim2.bobyleu2", "password", null, "skype", "location", "telephone", "mob", "instant", "google")));
            return persons;
        }
    }, false, true);
    final MemberOfSite memberOfSite = currentNetwork.getSiteMemberships(initiator.getId()).get(0);
    // startReviewPooledProcess() uses initiator's site id and role name for construct bpm_groupAssignee, thus we need appropriate things for created users
    transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

                @Override
                public Void doWork() throws Exception {
                    TestSite initiatorSite = (TestSite) memberOfSite.getSite();
                    initiatorSite.inviteToSite(persons.get(0).getId(), memberOfSite.getRole());
                    initiatorSite.inviteToSite(persons.get(1).getId(), memberOfSite.getRole());
                    // this user wouldn't be in group
                    initiatorSite.inviteToSite(persons.get(2).getId(), SiteRole.SiteConsumer == memberOfSite.getRole() ? SiteRole.SiteCollaborator : SiteRole.SiteConsumer);
                    return null;
                }
            }, AuthenticationUtil.getAdminUserName(), currentNetwork.getId());
            return null;
        }
    }, false, true);
    String processId = processInfo.getId();
    // getting process items by workflow initiator
    ProcessesClient processesClient = publicApiClient.processesClient();
    JSONObject initiatorItems = processesClient.findProcessItems(processId);
    // getting unclaimed process items by user in group
    requestContext = new RequestContext(currentNetwork.getId(), persons.get(0).getId());
    publicApiClient.setRequestContext(requestContext);
    JSONObject items1 = processesClient.findProcessItems(processId);
    assertEquals(initiatorItems.toJSONString(), items1.toJSONString());
    // getting unclaimed process items by user not in group
    requestContext = new RequestContext(currentNetwork.getId(), persons.get(2).getId());
    publicApiClient.setRequestContext(requestContext);
    try {
        JSONObject items2 = processesClient.findProcessItems(processId);
        fail("User not from group should not see items.");
    } catch (PublicApiException e) {
        // expected
        assertEquals(403, e.getHttpResponse().getStatusCode());
    }
    // claim task
    TaskService taskService = activitiProcessEngine.getTaskService();
    Task task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
    TestPerson assignee = persons.get(1);
    taskService.setAssignee(task.getId(), assignee.getId());
    // getting claimed process items by assignee
    requestContext = new RequestContext(currentNetwork.getId(), assignee.getId());
    publicApiClient.setRequestContext(requestContext);
    JSONObject items3 = processesClient.findProcessItems(processId);
    assertEquals(initiatorItems.toJSONString(), items3.toJSONString());
    // getting claimed process items by user in group
    requestContext = new RequestContext(currentNetwork.getId(), persons.get(0).getId());
    publicApiClient.setRequestContext(requestContext);
    try {
        JSONObject items4 = processesClient.findProcessItems(processId);
        fail("User from group should not see items for claimed task by another user.");
    } catch (PublicApiException e) {
        // expected
        assertEquals(403, e.getHttpResponse().getStatusCode());
    } finally {
        cleanupProcessInstance(processId);
    }
}
Also used : ProcessesClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.ProcessesClient) Task(org.activiti.engine.task.Task) PersonInfo(org.alfresco.rest.api.tests.PersonInfo) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) TaskService(org.activiti.engine.TaskService) ArrayList(java.util.ArrayList) 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) JSONObject(org.json.simple.JSONObject) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) Test(org.junit.Test)

Example 74 with PublicApiException

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

the class ProcessWorkflowApiTest method testGetProcessInstancesWithSorting.

@Test
public void testGetProcessInstancesWithSorting() throws Exception {
    final RequestContext requestContext = initApiClientWithTestUser();
    final ProcessInfo process1 = startAdhocProcess(requestContext, null, "akey");
    final ProcessInfo process2 = startAdhocProcess(requestContext, null, "bkey");
    final ProcessInfo process3 = startAdhocProcess(requestContext, null, "aakey");
    try {
        // sort on business key ascending
        ProcessesClient processesClient = publicApiClient.processesClient();
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("orderBy", "businessKey ASC");
        ListResponse<ProcessInfo> processList = processesClient.getProcesses(paramMap);
        assertNotNull(processList);
        assertEquals(3, processList.getList().size());
        assertEquals(process3.getId(), processList.getList().get(0).getId());
        assertEquals(process1.getId(), processList.getList().get(1).getId());
        assertEquals(process2.getId(), processList.getList().get(2).getId());
        // sort on business key descending
        paramMap.put("orderBy", "businessKey DESC");
        processList = processesClient.getProcesses(paramMap);
        assertNotNull(processList);
        assertEquals(3, processList.getList().size());
        assertEquals(process2.getId(), processList.getList().get(0).getId());
        assertEquals(process1.getId(), processList.getList().get(1).getId());
        assertEquals(process3.getId(), processList.getList().get(2).getId());
        paramMap.put("orderBy", "startedAt ASC");
        processList = processesClient.getProcesses(paramMap);
        assertNotNull(processList);
        paramMap.put("orderBy", "endedAt ASC");
        processList = processesClient.getProcesses(paramMap);
        assertNotNull(processList);
        paramMap.put("orderBy", "durationInMillis ASC");
        processList = processesClient.getProcesses(paramMap);
        assertNotNull(processList);
        // sort on more than 1.
        paramMap.put("orderBy", "startedAt, endedAt");
        try {
            processList = processesClient.getProcesses(paramMap);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.BAD_REQUEST.value(), e.getHttpResponse().getStatusCode());
        }
        // sort on non existing key
        paramMap.put("orderBy", "businessKey2 ASC");
        try {
            processList = processesClient.getProcesses(paramMap);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.BAD_REQUEST.value(), e.getHttpResponse().getStatusCode());
        }
        // sort on non existing sort order
        try {
            paramMap.put("orderBy", "businessKey ASC2");
            processList = processesClient.getProcesses(paramMap);
            fail("forbidden expected");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.BAD_REQUEST.value(), e.getHttpResponse().getStatusCode());
        }
    } finally {
        cleanupProcessInstance(process1.getId(), process2.getId(), process3.getId());
    }
}
Also used : PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) ProcessesClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.ProcessesClient) HashMap(java.util.HashMap) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) ProcessInfo(org.alfresco.rest.workflow.api.model.ProcessInfo) Test(org.junit.Test)

Example 75 with PublicApiException

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

the class ProcessWorkflowApiTest method testCreateProcessInstanceFromOtherNetwork.

@Test
@SuppressWarnings("unchecked")
public void testCreateProcessInstanceFromOtherNetwork() throws Exception {
    final RequestContext requestContext = initApiClientWithTestUser();
    org.activiti.engine.repository.ProcessDefinition processDefinition = activitiProcessEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionKey("@" + requestContext.getNetworkId() + "@activitiAdhoc").singleResult();
    TestNetwork anotherNetwork = getOtherNetwork(requestContext.getNetworkId());
    String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + anotherNetwork.getId();
    RequestContext otherContext = new RequestContext(anotherNetwork.getId(), tenantAdmin);
    publicApiClient.setRequestContext(otherContext);
    ProcessesClient processesClient = publicApiClient.processesClient();
    JSONObject createProcessObject = new JSONObject();
    createProcessObject.put("processDefinitionId", processDefinition.getId());
    final JSONObject variablesObject = new JSONObject();
    variablesObject.put("bpm_dueDate", ISO8601DateFormat.format(new Date()));
    variablesObject.put("bpm_priority", 1);
    variablesObject.put("bpm_description", "test description");
    TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            variablesObject.put("bpm_assignee", requestContext.getRunAsUser());
            return null;
        }
    }, requestContext.getRunAsUser(), requestContext.getNetworkId());
    createProcessObject.put("variables", variablesObject);
    try {
        processesClient.createProcess(createProcessObject.toJSONString());
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.BAD_REQUEST.value(), e.getHttpResponse().getStatusCode());
    }
}
Also used : ProcessesClient(org.alfresco.rest.workflow.api.tests.WorkflowApiClient.ProcessesClient) Date(java.util.Date) 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)

Aggregations

PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)78 Test (org.junit.Test)73 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 NodeRef (org.alfresco.service.cmr.repository.NodeRef)15 HashMap (java.util.HashMap)13 List (java.util.List)13 ListResponse (org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse)13 JSONArray (org.json.simple.JSONArray)13 TestSite (org.alfresco.rest.api.tests.RepoService.TestSite)12 TenantRunAsWork (org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork)11 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)9