Search in sources :

Example 26 with HttpPut

use of org.apache.http.client.methods.HttpPut in project Activiti by Activiti.

the class TaskVariableResourceTest method testUpdateBinaryTaskVariable.

/**
   * Test updating a single task variable using a binary stream.
   * PUT runtime/tasks/{taskId}/variables/{variableName}
   */
public void testUpdateBinaryTaskVariable() throws Exception {
    try {
        Task task = taskService.newTask();
        taskService.saveTask(task);
        taskService.setVariable(task.getId(), "binaryVariable", "Original value".getBytes());
        InputStream binaryContent = new ByteArrayInputStream("This is binary content".getBytes());
        // Add name, type and scope
        Map<String, String> additionalFields = new HashMap<String, String>();
        additionalFields.put("name", "binaryVariable");
        additionalFields.put("type", "binary");
        additionalFields.put("scope", "local");
        // Upload a valid BPMN-file using multipart-data
        HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLE, task.getId(), "binaryVariable"));
        httpPut.setEntity(HttpMultipartHelper.getMultiPartEntity("value", "application/octet-stream", binaryContent, additionalFields));
        CloseableHttpResponse response = executeBinaryRequest(httpPut, HttpStatus.SC_OK);
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertEquals("binaryVariable", responseNode.get("name").asText());
        assertTrue(responseNode.get("value").isNull());
        assertEquals("local", responseNode.get("scope").asText());
        assertEquals("binary", responseNode.get("type").asText());
        assertNotNull(responseNode.get("valueUrl").isNull());
        assertTrue(responseNode.get("valueUrl").asText().endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLE_DATA, task.getId(), "binaryVariable")));
        // Check actual value of variable in engine
        Object variableValue = taskService.getVariableLocal(task.getId(), "binaryVariable");
        assertNotNull(variableValue);
        assertTrue(variableValue instanceof byte[]);
        assertEquals("This is binary content", new String((byte[]) variableValue));
    } 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 : Task(org.activiti.engine.task.Task) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpPut(org.apache.http.client.methods.HttpPut)

Example 27 with HttpPut

use of org.apache.http.client.methods.HttpPut in project Activiti by Activiti.

the class TaskVariablesCollectionResourceTest method testCreateSingleTaskVariableEdgeCases.

/**
   * Test creating a single task variable, testing edge case exceptions. 
   * POST runtime/tasks/{taskId}/variables
   */
public void testCreateSingleTaskVariableEdgeCases() throws Exception {
    try {
        // Test adding variable to unexisting task
        ArrayNode requestNode = objectMapper.createArrayNode();
        ObjectNode variableNode = requestNode.addObject();
        variableNode.put("name", "existingVariable");
        variableNode.put("value", "simple string value");
        variableNode.put("scope", "local");
        variableNode.put("type", "string");
        HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, "unexisting"));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeBinaryRequest(httpPost, HttpStatus.SC_NOT_FOUND));
        // Test trying to create already existing variable
        Task task = taskService.newTask();
        taskService.saveTask(task);
        taskService.setVariable(task.getId(), "existingVariable", "Value 1");
        httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeBinaryRequest(httpPost, HttpStatus.SC_CONFLICT));
        // Test same thing but using PUT (create or update)
        HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()));
        httpPut.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeBinaryRequest(httpPut, HttpStatus.SC_CREATED));
        // Test setting global variable on standalone task
        variableNode.put("name", "myVariable");
        variableNode.put("value", "simple string value");
        variableNode.put("scope", "global");
        variableNode.put("type", "string");
        httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeBinaryRequest(httpPost, HttpStatus.SC_BAD_REQUEST));
        // Test creating nameless variable
        variableNode.removeAll();
        variableNode.put("value", "simple string value");
        httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeBinaryRequest(httpPost, HttpStatus.SC_BAD_REQUEST));
        // Test passing in empty array
        requestNode.removeAll();
        httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        closeResponse(executeBinaryRequest(httpPost, HttpStatus.SC_BAD_REQUEST));
        // Test passing in object instead of array
        httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()));
        httpPost.setEntity(new StringEntity(objectMapper.createObjectNode().toString()));
        closeResponse(executeBinaryRequest(httpPost, HttpStatus.SC_BAD_REQUEST));
    } 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) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HttpPut(org.apache.http.client.methods.HttpPut)

Example 28 with HttpPut

use of org.apache.http.client.methods.HttpPut in project Activiti by Activiti.

the class TaskVariablesCollectionResourceTest method testCreateMultipleTaskVariables.

/**
   * Test creating a multipe task variable in a single call.
   * POST runtime/tasks/{taskId}/variables
   */
public void testCreateMultipleTaskVariables() throws Exception {
    try {
        Task task = taskService.newTask();
        taskService.saveTask(task);
        ArrayNode requestNode = objectMapper.createArrayNode();
        // String variable
        ObjectNode stringVarNode = requestNode.addObject();
        stringVarNode.put("name", "stringVariable");
        stringVarNode.put("value", "simple string value");
        stringVarNode.put("scope", "local");
        stringVarNode.put("type", "string");
        // Integer
        ObjectNode integerVarNode = requestNode.addObject();
        integerVarNode.put("name", "integerVariable");
        integerVarNode.put("value", 1234);
        integerVarNode.put("scope", "local");
        integerVarNode.put("type", "integer");
        // Short
        ObjectNode shortVarNode = requestNode.addObject();
        shortVarNode.put("name", "shortVariable");
        shortVarNode.put("value", 123);
        shortVarNode.put("scope", "local");
        shortVarNode.put("type", "short");
        // Long
        ObjectNode longVarNode = requestNode.addObject();
        longVarNode.put("name", "longVariable");
        longVarNode.put("value", 4567890L);
        longVarNode.put("scope", "local");
        longVarNode.put("type", "long");
        // Double
        ObjectNode doubleVarNode = requestNode.addObject();
        doubleVarNode.put("name", "doubleVariable");
        doubleVarNode.put("value", 123.456);
        doubleVarNode.put("scope", "local");
        doubleVarNode.put("type", "double");
        // Boolean
        ObjectNode booleanVarNode = requestNode.addObject();
        booleanVarNode.put("name", "booleanVariable");
        booleanVarNode.put("value", Boolean.TRUE);
        booleanVarNode.put("scope", "local");
        booleanVarNode.put("type", "boolean");
        // Date
        Calendar varCal = Calendar.getInstance();
        String isoString = getISODateString(varCal.getTime());
        ObjectNode dateVarNode = requestNode.addObject();
        dateVarNode.put("name", "dateVariable");
        dateVarNode.put("value", isoString);
        dateVarNode.put("scope", "local");
        dateVarNode.put("type", "date");
        // Create local variables with a single request
        HttpPost httpPost = new HttpPost(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()));
        httpPost.setEntity(new StringEntity(requestNode.toString()));
        CloseableHttpResponse response = executeBinaryRequest(httpPost, HttpStatus.SC_CREATED);
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertTrue(responseNode.isArray());
        assertEquals(7, responseNode.size());
        // Check if engine has correct variables set
        Map<String, Object> taskVariables = taskService.getVariablesLocal(task.getId());
        assertEquals(7, taskVariables.size());
        assertEquals("simple string value", taskVariables.get("stringVariable"));
        assertEquals(1234, taskVariables.get("integerVariable"));
        assertEquals((short) 123, taskVariables.get("shortVariable"));
        assertEquals(4567890L, taskVariables.get("longVariable"));
        assertEquals(123.456, taskVariables.get("doubleVariable"));
        assertEquals(Boolean.TRUE, taskVariables.get("booleanVariable"));
        assertEquals(dateFormat.parse(isoString), taskVariables.get("dateVariable"));
        // repeat the process with additional variables, testing PUT of a mixed set of variables
        // where some exist and others do not
        requestNode = objectMapper.createArrayNode();
        // new String variable
        ObjectNode stringVarNode2 = requestNode.addObject();
        stringVarNode2.put("name", "new stringVariable");
        stringVarNode2.put("value", "simple string value 2");
        stringVarNode2.put("scope", "local");
        stringVarNode2.put("type", "string");
        // changed Integer variable
        ObjectNode integerVarNode2 = requestNode.addObject();
        integerVarNode2.put("name", "integerVariable");
        integerVarNode2.put("value", 4321);
        integerVarNode2.put("scope", "local");
        integerVarNode2.put("type", "integer");
        // Create or update local variables with a single request
        HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TASK_VARIABLES_COLLECTION, task.getId()));
        httpPut.setEntity(new StringEntity(requestNode.toString()));
        response = executeBinaryRequest(httpPut, HttpStatus.SC_CREATED);
        responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertTrue(responseNode.isArray());
        assertEquals(2, responseNode.size());
        // Check if engine has correct variables set
        taskVariables = taskService.getVariablesLocal(task.getId());
        assertEquals(8, taskVariables.size());
        assertEquals("simple string value", taskVariables.get("stringVariable"));
        assertEquals("simple string value 2", taskVariables.get("new stringVariable"));
        assertEquals(4321, taskVariables.get("integerVariable"));
        assertEquals((short) 123, taskVariables.get("shortVariable"));
        assertEquals(4567890L, taskVariables.get("longVariable"));
        assertEquals(123.456, taskVariables.get("doubleVariable"));
        assertEquals(Boolean.TRUE, taskVariables.get("booleanVariable"));
        assertEquals(dateFormat.parse(isoString), taskVariables.get("dateVariable"));
    } 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) Task(org.activiti.engine.task.Task) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Calendar(java.util.Calendar) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 29 with HttpPut

use of org.apache.http.client.methods.HttpPut in project Activiti by Activiti.

the class UserResourceTest method testUpdateUserNullFields.

/**
   * Test updating a single user passing in no fields in the json, user should remain unchanged.
   */
public void testUpdateUserNullFields() throws Exception {
    User savedUser = null;
    try {
        User newUser = identityService.newUser("testuser");
        newUser.setFirstName("Fred");
        newUser.setLastName("McDonald");
        newUser.setEmail("no-reply@activiti.org");
        identityService.saveUser(newUser);
        savedUser = newUser;
        ObjectNode taskUpdateRequest = objectMapper.createObjectNode();
        taskUpdateRequest.putNull("firstName");
        taskUpdateRequest.putNull("lastName");
        taskUpdateRequest.putNull("email");
        taskUpdateRequest.putNull("password");
        HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId()));
        httpPut.setEntity(new StringEntity(taskUpdateRequest.toString()));
        CloseableHttpResponse response = executeRequest(httpPut, HttpStatus.SC_OK);
        JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseNode);
        assertEquals("testuser", responseNode.get("id").textValue());
        assertTrue(responseNode.get("firstName").isNull());
        assertTrue(responseNode.get("lastName").isNull());
        assertTrue(responseNode.get("email").isNull());
        assertTrue(responseNode.get("url").textValue().endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, newUser.getId())));
        // Check user is updated in activiti
        newUser = identityService.createUserQuery().userId(newUser.getId()).singleResult();
        assertNull(newUser.getLastName());
        assertNull(newUser.getFirstName());
        assertNull(newUser.getEmail());
    } finally {
        // Delete user after test fails
        if (savedUser != null) {
            identityService.deleteUser(savedUser.getId());
        }
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) User(org.activiti.engine.identity.User) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpPut(org.apache.http.client.methods.HttpPut)

Example 30 with HttpPut

use of org.apache.http.client.methods.HttpPut in project Activiti by Activiti.

the class UserResourceTest method testUpdateUnexistingUser.

/**
   * Test updating an unexisting user.
   */
public void testUpdateUnexistingUser() throws Exception {
    HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_USER, "unexisting"));
    httpPut.setEntity(new StringEntity(objectMapper.createObjectNode().toString()));
    closeResponse(executeRequest(httpPut, HttpStatus.SC_NOT_FOUND));
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpPut(org.apache.http.client.methods.HttpPut)

Aggregations

HttpPut (org.apache.http.client.methods.HttpPut)153 StringEntity (org.apache.http.entity.StringEntity)89 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)50 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)40 HttpResponse (org.apache.http.HttpResponse)29 Test (org.junit.Test)29 JsonNode (com.fasterxml.jackson.databind.JsonNode)27 Deployment (org.activiti.engine.test.Deployment)27 HttpPost (org.apache.http.client.methods.HttpPost)19 HttpGet (org.apache.http.client.methods.HttpGet)17 IOException (java.io.IOException)16 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)16 HttpDelete (org.apache.http.client.methods.HttpDelete)14 HttpEntity (org.apache.http.HttpEntity)13 URI (java.net.URI)12 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)12 HttpHead (org.apache.http.client.methods.HttpHead)11 Execution (org.activiti.engine.runtime.Execution)10 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)9 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)9