Search in sources :

Example 6 with Attachment

use of org.activiti.engine.task.Attachment in project Activiti by Activiti.

the class TaskServiceTest method testTaskAttachmentWithProcessInstanceId.

@Deployment(resources = { "org/activiti/engine/test/api/oneTaskProcess.bpmn20.xml" })
public void testTaskAttachmentWithProcessInstanceId() {
    if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.ACTIVITY)) {
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
        String processInstanceId = processInstance.getId();
        taskService.createAttachment("web page", null, processInstanceId, "weatherforcast", "temperatures and more", "http://weather.com");
        Attachment attachment = taskService.getProcessInstanceAttachments(processInstanceId).get(0);
        assertEquals("weatherforcast", attachment.getName());
        assertEquals("temperatures and more", attachment.getDescription());
        assertEquals("web page", attachment.getType());
        assertEquals(processInstanceId, attachment.getProcessInstanceId());
        assertNull(attachment.getTaskId());
        assertEquals("http://weather.com", attachment.getUrl());
        assertNull(taskService.getAttachmentContent(attachment.getId()));
        // Finally, clean up
        taskService.deleteAttachment(attachment.getId());
        // TODO: Bad API design. Need to fix attachment/comment properly
        ((TaskServiceImpl) taskService).deleteComments(null, processInstanceId);
    }
}
Also used : TaskServiceImpl(org.activiti.engine.impl.TaskServiceImpl) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Attachment(org.activiti.engine.task.Attachment) Deployment(org.activiti.engine.test.Deployment)

Example 7 with Attachment

use of org.activiti.engine.task.Attachment in project Activiti by Activiti.

the class TaskServiceTest method testSaveTaskAttachment.

public void testSaveTaskAttachment() {
    if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.ACTIVITY)) {
        Task task = taskService.newTask();
        task.setOwner("johndoe");
        taskService.saveTask(task);
        String taskId = task.getId();
        identityService.setAuthenticatedUserId("johndoe");
        // Fetch attachment and update its name
        taskService.createAttachment("web page", taskId, null, "weatherforcast", "temperatures and more", "http://weather.com");
        Attachment attachment = taskService.getTaskAttachments(taskId).get(0);
        attachment.setName("UpdatedName");
        taskService.saveAttachment(attachment);
        // Refetch and verify
        attachment = taskService.getTaskAttachments(taskId).get(0);
        assertEquals("UpdatedName", attachment.getName());
        // Finally, clean up
        taskService.deleteTask(taskId);
        assertEquals(0, taskService.getTaskComments(taskId).size());
        assertEquals(1, historyService.createHistoricTaskInstanceQuery().taskId(taskId).list().size());
        taskService.deleteTask(taskId, true);
    }
}
Also used : Task(org.activiti.engine.task.Task) Attachment(org.activiti.engine.task.Attachment)

Example 8 with Attachment

use of org.activiti.engine.task.Attachment in project Activiti by Activiti.

the class AttachmentEventsTest method testAttachmentEntityEventsStandaloneTask.

/**
	 * Test create, update and delete events of users.
	 */
public void testAttachmentEntityEventsStandaloneTask() throws Exception {
    if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.ACTIVITY)) {
        Task task = null;
        try {
            task = taskService.newTask();
            taskService.saveTask(task);
            assertNotNull(task);
            // Create link-attachment
            Attachment attachment = taskService.createAttachment("test", task.getId(), null, "attachment name", "description", "http://activiti.org");
            assertEquals(2, listener.getEventsReceived().size());
            ActivitiEntityEvent event = (ActivitiEntityEvent) listener.getEventsReceived().get(0);
            assertEquals(ActivitiEventType.ENTITY_CREATED, event.getType());
            assertNull(event.getProcessInstanceId());
            assertNull(event.getExecutionId());
            assertNull(event.getProcessDefinitionId());
            Attachment attachmentFromEvent = (Attachment) event.getEntity();
            assertEquals(attachment.getId(), attachmentFromEvent.getId());
            event = (ActivitiEntityEvent) listener.getEventsReceived().get(1);
            assertEquals(ActivitiEventType.ENTITY_INITIALIZED, event.getType());
            listener.clearEventsReceived();
            // Create binary attachment
            attachment = taskService.createAttachment("test", task.getId(), null, "attachment name", "description", new ByteArrayInputStream("test".getBytes()));
            assertEquals(2, listener.getEventsReceived().size());
            event = (ActivitiEntityEvent) listener.getEventsReceived().get(0);
            assertEquals(ActivitiEventType.ENTITY_CREATED, event.getType());
            assertNull(event.getProcessInstanceId());
            assertNull(event.getExecutionId());
            assertNull(event.getProcessDefinitionId());
            attachmentFromEvent = (Attachment) event.getEntity();
            assertEquals(attachment.getId(), attachmentFromEvent.getId());
            event = (ActivitiEntityEvent) listener.getEventsReceived().get(1);
            assertEquals(ActivitiEventType.ENTITY_INITIALIZED, event.getType());
            listener.clearEventsReceived();
            // Update attachment
            attachment = taskService.getAttachment(attachment.getId());
            attachment.setDescription("Description");
            taskService.saveAttachment(attachment);
            assertEquals(1, listener.getEventsReceived().size());
            event = (ActivitiEntityEvent) listener.getEventsReceived().get(0);
            assertEquals(ActivitiEventType.ENTITY_UPDATED, event.getType());
            assertNull(event.getProcessInstanceId());
            assertNull(event.getExecutionId());
            assertNull(event.getProcessDefinitionId());
            attachmentFromEvent = (Attachment) event.getEntity();
            assertEquals(attachment.getId(), attachmentFromEvent.getId());
            assertEquals("Description", attachmentFromEvent.getDescription());
            listener.clearEventsReceived();
            // Finally, delete attachment
            taskService.deleteAttachment(attachment.getId());
            assertEquals(1, listener.getEventsReceived().size());
            event = (ActivitiEntityEvent) listener.getEventsReceived().get(0);
            assertEquals(ActivitiEventType.ENTITY_DELETED, event.getType());
            assertNull(event.getProcessInstanceId());
            assertNull(event.getExecutionId());
            assertNull(event.getProcessDefinitionId());
            attachmentFromEvent = (Attachment) event.getEntity();
            assertEquals(attachment.getId(), attachmentFromEvent.getId());
        } finally {
            if (task != null && task.getId() != null) {
                taskService.deleteTask(task.getId());
                historyService.deleteHistoricTaskInstance(task.getId());
            }
        }
    }
}
Also used : Task(org.activiti.engine.task.Task) ByteArrayInputStream(java.io.ByteArrayInputStream) ActivitiEntityEvent(org.activiti.engine.delegate.event.ActivitiEntityEvent) Attachment(org.activiti.engine.task.Attachment)

Example 9 with Attachment

use of org.activiti.engine.task.Attachment in project Activiti by Activiti.

the class AttachmentEventsTest method testAttachmentEntityEvents.

/**
	 * Test create, update and delete events of attachments on a task/process.
	 */
@Deployment(resources = { "org/activiti/engine/test/api/runtime/oneTaskProcess.bpmn20.xml" })
public void testAttachmentEntityEvents() throws Exception {
    if (processEngineConfiguration.getHistoryLevel().isAtLeast(HistoryLevel.ACTIVITY)) {
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        assertNotNull(task);
        // Create link-attachment
        Attachment attachment = taskService.createAttachment("test", task.getId(), processInstance.getId(), "attachment name", "description", "http://activiti.org");
        assertNull(attachment.getUserId());
        assertEquals(2, listener.getEventsReceived().size());
        ActivitiEntityEvent event = (ActivitiEntityEvent) listener.getEventsReceived().get(0);
        assertEquals(ActivitiEventType.ENTITY_CREATED, event.getType());
        assertEquals(processInstance.getId(), event.getProcessInstanceId());
        assertEquals(processInstance.getId(), event.getExecutionId());
        assertEquals(processInstance.getProcessDefinitionId(), event.getProcessDefinitionId());
        Attachment attachmentFromEvent = (Attachment) event.getEntity();
        assertEquals(attachment.getId(), attachmentFromEvent.getId());
        event = (ActivitiEntityEvent) listener.getEventsReceived().get(1);
        assertEquals(ActivitiEventType.ENTITY_INITIALIZED, event.getType());
        assertEquals(processInstance.getId(), event.getProcessInstanceId());
        assertEquals(processInstance.getId(), event.getExecutionId());
        assertEquals(processInstance.getProcessDefinitionId(), event.getProcessDefinitionId());
        attachmentFromEvent = (Attachment) event.getEntity();
        assertEquals(attachment.getId(), attachmentFromEvent.getId());
        listener.clearEventsReceived();
        // Create binary attachment
        Authentication.setAuthenticatedUserId("testuser");
        attachment = taskService.createAttachment("test", task.getId(), processInstance.getId(), "attachment name", "description", new ByteArrayInputStream("test".getBytes()));
        assertNotNull(attachment.getUserId());
        assertEquals("testuser", attachment.getUserId());
        assertEquals(2, listener.getEventsReceived().size());
        event = (ActivitiEntityEvent) listener.getEventsReceived().get(0);
        assertEquals(ActivitiEventType.ENTITY_CREATED, event.getType());
        assertEquals(processInstance.getId(), event.getProcessInstanceId());
        assertEquals(processInstance.getId(), event.getExecutionId());
        assertEquals(processInstance.getProcessDefinitionId(), event.getProcessDefinitionId());
        attachmentFromEvent = (Attachment) event.getEntity();
        assertEquals(attachment.getId(), attachmentFromEvent.getId());
        event = (ActivitiEntityEvent) listener.getEventsReceived().get(1);
        assertEquals(ActivitiEventType.ENTITY_INITIALIZED, event.getType());
        listener.clearEventsReceived();
        // Update attachment
        attachment = taskService.getAttachment(attachment.getId());
        attachment.setDescription("Description");
        taskService.saveAttachment(attachment);
        assertEquals(1, listener.getEventsReceived().size());
        event = (ActivitiEntityEvent) listener.getEventsReceived().get(0);
        assertEquals(ActivitiEventType.ENTITY_UPDATED, event.getType());
        assertEquals(processInstance.getId(), event.getProcessInstanceId());
        assertEquals(processInstance.getId(), event.getExecutionId());
        assertEquals(processInstance.getProcessDefinitionId(), event.getProcessDefinitionId());
        attachmentFromEvent = (Attachment) event.getEntity();
        assertEquals(attachment.getId(), attachmentFromEvent.getId());
        assertEquals("Description", attachmentFromEvent.getDescription());
        listener.clearEventsReceived();
        // Finally, delete attachment
        taskService.deleteAttachment(attachment.getId());
        assertEquals(1, listener.getEventsReceived().size());
        event = (ActivitiEntityEvent) listener.getEventsReceived().get(0);
        assertEquals(ActivitiEventType.ENTITY_DELETED, event.getType());
        assertEquals(processInstance.getId(), event.getProcessInstanceId());
        assertEquals(processInstance.getId(), event.getExecutionId());
        assertEquals(processInstance.getProcessDefinitionId(), event.getProcessDefinitionId());
        attachmentFromEvent = (Attachment) event.getEntity();
        assertEquals(attachment.getId(), attachmentFromEvent.getId());
    }
}
Also used : Task(org.activiti.engine.task.Task) ByteArrayInputStream(java.io.ByteArrayInputStream) ActivitiEntityEvent(org.activiti.engine.delegate.event.ActivitiEntityEvent) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Attachment(org.activiti.engine.task.Attachment) Deployment(org.activiti.engine.test.Deployment)

Example 10 with Attachment

use of org.activiti.engine.task.Attachment in project Activiti by Activiti.

the class TaskAttachmentResourceTest method testCreateAttachment.

/**
   * Test creating a single attachments for a task
   * POST runtime/tasks/{taskId}/attachments/{attachmentId}
   */
public void testCreateAttachment() throws Exception {
    try {
        Task task = taskService.newTask();
        taskService.saveTask(task);
        ObjectNode requestNode = objectMapper.createObjectNode();
        requestNode.put("name", "Simple attachment");
        requestNode.put("description", "Simple attachment description");
        requestNode.put("type", "simpleType");
        requestNode.put("externalUrl", "http://activiti.org");
        HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_ATTACHMENT_COLLECTION, task.getId()));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        CloseableHttpResponse response = executeRequest(httpPost, HttpStatus.SC_CREATED);
        // Check if attachment is created
        List<Attachment> attachments = taskService.getTaskAttachments(task.getId());
        assertEquals(1, attachments.size());
        Attachment urlAttachment = attachments.get(0);
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertEquals(urlAttachment.getId(), responseNode.get("id").textValue());
        assertEquals("simpleType", responseNode.get("type").textValue());
        assertEquals("Simple attachment", responseNode.get("name").textValue());
        assertEquals("Simple attachment description", responseNode.get("description").textValue());
        assertEquals("http://activiti.org", responseNode.get("externalUrl").textValue());
        assertTrue(responseNode.get("url").textValue().endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_ATTACHMENT, task.getId(), urlAttachment.getId())));
        assertTrue(responseNode.get("taskUrl").textValue().endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK, task.getId())));
        assertTrue(responseNode.get("contentUrl").isNull());
        assertTrue(responseNode.get("processInstanceUrl").isNull());
        assertFalse(responseNode.get("time").isNull());
    } finally {
        // Clean adhoc-tasks even if test fails
        List<Task> tasks = taskService.createTaskQuery().list();
        for (Task task : tasks) {
            taskService.deleteTask(task.getId(), true);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) Task(org.activiti.engine.task.Task) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Attachment(org.activiti.engine.task.Attachment) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

Attachment (org.activiti.engine.task.Attachment)28 Task (org.activiti.engine.task.Task)17 ByteArrayInputStream (java.io.ByteArrayInputStream)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)7 HttpGet (org.apache.http.client.methods.HttpGet)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ActivitiEntityEvent (org.activiti.engine.delegate.event.ActivitiEntityEvent)5 HistoricTaskInstance (org.activiti.engine.history.HistoricTaskInstance)4 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)4 Deployment (org.activiti.engine.test.Deployment)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)3 Item (com.vaadin.data.Item)2 Embedded (com.vaadin.ui.Embedded)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 ActivitiException (org.activiti.engine.ActivitiException)2 AttachmentRenderer (org.activiti.explorer.ui.content.AttachmentRenderer)2 HttpPost (org.apache.http.client.methods.HttpPost)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1