Thursday 27 October 2016

Automating REST APIs Series.
PART 1: Using Jersey Client

        In this series we are going to learn about automating REST APIs. This post is dedicated for Jersey Client. 

Below link has the basic information about REST APIs
http://www.qaautomationsimplified.com/api/an-introduction-to-rest/ 

Idea behind using Jersey Client+Java:
1. We will be creating instances of Jersey client for various methods like GET,PUT,POST etc. by mentioning the request URL

 In our example We will be sending GET Request to http://restcountries.eu/rest/v1/name/norway

  So we will be creating instance of JerseyClient and setting the URL as :  http://restcountries.eu/rest/v1/name/norway

2. We will be composing the request body as JSON

  As the above example is a GET request , so it can not have a request body. In case of    POST, PUT etc if we need to set the request body then that needs to be represented in  JSON format according to the design if the API. So we will be  extensively using org.json  methods/APIs to build JSON objects and set it as Jersey request body.

3. Execute the desired Jersey method when headers etc are all set

 As the request body contains request information in JSON format , so we need to set a    header – Content-Type as ‘application/json’ , which gives instruction to server that  request body is represented as JSON and should be parsed accordingly to process the  request.

4. Capture the response and convert it to a JSON object.

 Response object is captured and parsed to a JSON object.

5. Perform the assertions based on the requirements.

Steps to automate REST APIs using Jersey Client:
#1. Create a Maven Project
       
       To know about creating a simple maven project refer the below link

#2. Add the below dependencies to pom.xml
            <dependency>
          <groupId>com.sun.jersey</groupId>
          <artifactId>jersey-client</artifactId>
          <version>1.18.1</version>
      </dependency>
      <dependency>
          <groupId>com.sun.jersey</groupId>
          <artifactId>jersey-json</artifactId>
          <version>1.18.1</version>
               <exclusions>
                  <exclusion>
                     <groupId>com.sun.xml.bind</groupId>
                     <artifactId>jaxb-impl</artifactId>
                   </exclusion>
               </exclusions>
      </dependency>
      <dependency>
           <groupId>com.sun.jersey</groupId>
           <artifactId>jersey-bundle</artifactId>
           <version>1.10</version>
      </dependency>
      <dependency>
           <groupId>org.json</groupId>
           <artifactId>json</artifactId>
           <version>20140107</version>
       </dependency>


#3. After adding the dependency, create a class and below is the sample code for    checking.

     In this example I have used a public webservice which has the details about European countries and their currencies etc. 
      
     Here I am going to validate the capital of norway.
     
 Client _client = Client.create();
WebResource wr = _client.resource("http://restcountries.eu/rest/v1/name/norway");
 WebResource.Builder builder = wr.getRequestBuilder();
 //Adding headers to the request
 builder = builder.header("Accept", "application/json");
 //Fetching Response from API
 ClientResponse cr = builder.method("GET", ClientResponse.class,null);
 String responseBodyString = cr.getEntity(String.class).trim();
 System.out.println("This is the response: "+responseBodyString);
 System.out.println(cr.getStatus());
 //Fetching response in JSON
JSONArray jsonResponse = new JSONArray(responseBodyString);
System.out.println(jsonResponse);
//Fetching value of capital parameter
String capital = jsonResponse.getJSONObject(0).getString("capital");
//Asserting that capital of Norway is Oslo
Assert.assertEquals(capital, "Oslo");

In the long scale projects, we may need to handle request and response more in terms of json. We can use below dependency for handling json response/request

            <dependency>
          <groupId>com.google.code.gson</groupId>
          <artifactId>gson</artifactId>
          <version>2.2.4</version>
      </dependency>


In our next post we will discuss about Apache httpclient.