Time for action – working with Repository APIs as REST

As in Chapter 6, The Activiti ProcessEngine API, we have gone through the deployment of a business process and used the Repository API for deployment purposes. In this section, we will perform the following steps to install and deploy the business process using the Repository REST API:

  1. We will start by fetching all the deployed processes from the repository. For this operation, we will enter the URL url/repository/deployments in the browser with the GET method; the response will fetch all the deployed processes from the repository.
    Time for action – working with Repository APIs as REST
  2. All the processes deployed in the process engine will be displayed when we navigate to the preceding URL, as shown in the following screenshot:
    Time for action – working with Repository APIs as REST
  3. To fetch a specific process or resource from a deployment, we need to specify the process ID with the URL, for example, /repository /deployments/{deploymentId}, with the GET method. In the following screenshot, we have provided the deployment ID 23:
    Time for action – working with Repository APIs as REST
  4. On navigating to the preceding URL, only the process that we want to retrieve will be displayed, as shown in the following screenshot:
    Time for action – working with Repository APIs as REST
  5. Apart from fetching the resources for and displaying the process, we can also delete it. To delete the process, we need to set the Method field to DELETE with the URL field set to url /repository/deployment/{deploymentId}, as shown in the following screenshot:
    Time for action – working with Repository APIs as REST
  6. On navigating to the preceding URL, the business process will be deleted from the repository and the response will be as follows:
    Time for action – working with Repository APIs as REST
  7. We can also achieve the preceding functionality by performing class implementation. Create a new project in Eclipse and add some external JAR files, as shown in the following screenshot, that will be required to execute this example as mentioned previously:
    Time for action – working with Repository APIs as REST
  8. We will create a class file Repository_Deployment with the following code:
    private static String REST_URI = "http://localhost:8080/activiti-rest/service";
    
      private static ClientResource getClientResource(String uri) {
        ClientResource resource = new ClientResource(uri);
        resource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "kermit", "kermit");
        return resource;
      }
    
      public static JSONArray getDeployments() throws JSONException, IOException {
        String deploymentURI = REST_URI + "/repository/deployments";
        Representation response = getClientResource(deploymentURI).get(
            MediaType.APPLICATION_JSON);
        JSONObject object = new JSONObject(response.getText());
        if (object != null) {
          JSONArray dataArray = (JSONArray) object.get("data");
          return dataArray;
    
        }
        return null;
      }
  9. In the preceding code, we created a link to the REST URL and authenticated it with the user kermit; the getDeployments() method will fetch all the deployed processes from the engine and return it as a JSONArray object.
  10. Now, to test the REST services, we need to create a Test Rest API class within src/test/java, which will have the following code:
    @Test
      public void deploymentTest() throws JSONException, IOException {
        JSONArray procesArray = Repository_Deployment.getDeployments();
        for (int i = 0; i < processArray.length(); i++) {
          JSONObject jasonObject = (JSONObject) processArray.get(i);
          assertNotNull(jasonObject);
          System.out.println(jasonObject.toString());
        }
      }
  11. To test the given code, you can right-click on the project and navigate to Run As | JUnit Test, as shown in the following screenshot:
    Time for action – working with Repository APIs as REST
  12. On successful execution of the REST service, the output will show the number of processes deployed into the process engine, as shown in the following screenshot:
    Time for action – working with Repository APIs as REST
  13. Now we will provide a URL with a process ID to retrieve a specific process from the repository. We will create a method in the Repository_Deployment class that takes the process ID as an input parameter and provides the result for that process, as shown in the following code:
    public static JSONObject getDeployments(int id) throws JSONException, IOException {
        String deploymentURI = REST_URI + "/repository/deployments/" + id;
        Representation response = getClientResource(deploymentURI).get(
            MediaType.APPLICATION_JSON);
        JSONObject object = new JSONObject(response.getText());
        if (object != null) {
          return object;
        }
        return null;
      }
  14. To test our business process, we will create a method through which we will pass the process ID and retrieve the details of that process, as shown in the following code:
        @Test
      public void deploymentById() throws JSONException, IOException {
        JSONObject processObject = Repository_Deployment.getDeployments(23);
        assertNotNull(processObject);
        System.out.println(processObject.toString());
      }
  15. We have provided an ID to fetch a unique process, so on executing the test file the following output will be displayed:
    Time for action – working with Repository APIs as REST
  16. So far, we have seen how to get the process deployment files; now we will learn how to delete a process from the repository. Create the deleteDeployment method in the Repository_Deployment class; here in the response, we are calling the delete method, as shown in the following code:
    public static void deleteDeployment(int id) {
        String deploymentURI = REST_URI + "/repository/deployments/" + id;
      Representation response = getClientResource(deploymentURI).delete(
            MediaType.ALL);
      }
  17. Now, to access this method in the test file, create a test method from which you will make a call to the deleteDeployment method of Repository_Deployment; on performing the test, the process with the ID 23 will be deleted from the repository, as shown in the following code:
    @Test
      public void deleteDeployment() throws JSONException, IOException {
        Repository_Deployment.deleteDeployment(23);
    	}
  18. To retrieve the resources from a specific deployment, we have to navigate to url /repository/deployments/{deploymentId}/resources.
  19. We will create the getDeploymentResources method in the Repository_Deployments class, where we will pass the deployment ID to access its resources, as shown in the following code:
    public static JSONArray getDeploymentResources(int id) throws IOException,
          JSONException {
        String deploymentURI = REST_URI + "/repository/deployments/" + id + "/resources";
        Representation response = getClientResource(deploymentURI).get( MediaType.APPLICATION_JSON);
        
        String json = "{data :" + response.getText() + "}";
        
        JSONObject object = new JSONObject(json);
        
        if (object != null) {
          JSONArray arr = (JSONArray) object.get("data");
          return arr;
        }
        return null;
      }
  20. To test, we will create the deploymentResource method in the Repository_Deployment class as follows:
    @Test
      public void deploymentResource() throws JSONException, IOException {
        
        JSONArray processArray = Repository_Deployment.getDeploymentResources(23);
        assertNotNull(processArray);
        
        for (int i=0;i<processArray.length();i++){
          JSONObject object = (JSONObject) processArray.get(i);
          assertNotNull(object);
          System.out.println(object.toString());
        }
      }
  21. On running the test file, we should be able to view all the resources of the deployment, as shown in the following screenshot:
    Time for action – working with Repository APIs as REST

What just happened?

We just saw how to use the REST API to deploy and delete the process. We have also fetched the resources from a specified deployment and created some test cases to test our REST API calls.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset