REST API Testing Framework

REST API Testing Framework

We will be creating a simple Rest Testing Framework in Java and JUnit that could be used for any testing scenarios.
Rest Testing Framework Overview
The framework should be able to execute the basic REST operations (GET, POST, PUT, PATCH, DELETE) and perform the validations on the code, message, headers and body of the response.
The completed code can be accessed from my GitHub account from where you can collect and make modifications based on your requirements.

Design

Rest Testing Framework
2318.WithAPIArchitecture
We will be having three classes for the framework (in packagecom.axatrikx.controller)
RestExecutor : Performs the HTTP operations using Apache Http Client
RestResponse : A javabean class to hold our response values (code, message, headers, body)
RestValidator : Validates the response with the expected values
The package com.axatrikx.test holds the test scripts.
Note: I will be using ‘json-server‘ to fake the REST API. Its a real handy tool to rapidly setup fake REST API for testing your framework.
We are planning to have the framework implementation in the way shown below.

RestResponse

Its a JavaBean class holding the values from the response for each request. Four variables are provided each having its own getters and setters. See the class here.

RestValidator

This class performs the validation of the response with the expected values. It takes a RestResponse object in its constructor.
It has a method for each type of validations performed and all the methods returns the same object reference which enables chaining possible. The assertions are done using Junit assertions. See the class here.
The code is self explanatory and I’ve added a printBody() method to print the response body during script creation or debugging. This method also returns the Validator object and therefore can be used in the chain at any point.

RestExecutor

This class performs the HTTP operations using Apache HttpClient library and would be having a method for each of the operations. We will look into two methods in detail an you can check the code for the rest.
Constructor
The constructor is a simple one which takes in the url and initilizes the HttpClient object.
The GET method would have the path as one parameter and a HashMap as the second parameter for the headers provided as key value pairs. The method would be returning a RestValidator object containing the values corresponding to the response values of the GET request.
The HttpGet method is initialized with the combined URL, headers are set and request is executed to obtain the response. The response is then processed into our RestResponse object taking in our required values. This response object is used to initialize a RestValidator object corresponding to this GET request which is returned to the user to perform validations.
Note that I have added another GET method without the header parameter. This method just calls the above method with null value for header
POST Method
Post Method is similar to the GET method above, the only difference being the presence of xml body.
A simpler POST method without headers is also provided.
For the remaining methods, check out the complete class.
Now that the core of our framework is done, lets see how to write the test scripts.

TestScript

Since we are using jUnit as the unit testing framework, we can initialize the RestExecutor object in the @BeforeClass method and use the object in the @Test methods.
The executor.get(“/posts”) returns the RestValidator object having the RestResponse corresponding to the request. We can use the various validation methods on the object to perform the validation. As you might have noticed in RestValidator class, the assertions are using junit with the error messages preset. The remaining validations will be skipped if any one of the validations fail. Again, this can be modified according to your needs.
The same method is followed in the post request for the second @Test method. I haven’t added a proper reporting mechanism for this framework, but you can use any reporting libraries for JUnit or create one on your own.
See the completed project in github
I’d suggest to make this framework as Generic so that this can be used project independent. You can refer my article Most commonly used methods
———————————————————————————————————————–



What is API :
An application-programming interface (API) is a set of programming instructions and standards for accessing a Web-based software application or Web tool.

Example :
An API is a software-to-software interface, not a user interface. With APIs, applications talk to each other without any user knowledge or intervention. When you buy movie tickets online and enter your credit card information, the movie ticket Web site uses an API to send your credit card information to a remote application that verifies whether your information is correct. Once payment is confirmed, the remote application sends a response back to the movie ticket Web site saying it’s OK to issue the tickets.
What is JSON :
JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write. JSON can represent two structured types: objects and arrays. An object is an unordered collection of zero or more name/value pairs. An array is an ordered sequence of zero or more values. The values can be strings, numbers, booleans, null, and these two structured types.
JSON is an easier-to-use alternative to XML.
Note : Here i am just conferring how to parse and How to convert JsonObject to JavaObject.So that you could use it your live projects using selenium.
 Application contentType : Json
Objective : To validate Address : Chicago, IL, USA.
GoogleMaps API : http://maps.googleapis.com/maps/api/geocode/json?address=chicago&sensor=false
We can easily parse Json using “json-lib-2.4-jdk15.jar”  Download from here…!
Please consider the below code as a reference.
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.Reporter;
import org.testng.annotations.Test;
public class ReadJsonObject{
@Test
public void aptTesting() throws Exception {
try {
URL url = new URL(
http://maps.googleapis.com/maps/api/geocode/json?address=chicago&sensor=false”);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(“GET”);
conn.setRequestProperty(“Accept”, “application/json”);
if (conn.getResponseCode() != 200) {
throw new RuntimeException(” HTTP error code : ”
+ conn.getResponseCode());
}
Scanner scan = new Scanner(url.openStream());
String entireResponse = new String();
while (scan.hasNext())
entireResponse += scan.nextLine();
System.out.println(“Response : “+entireResponse);
scan.close();
JSONObject obj = new JSONObject(entireResponse );
String responseCode = obj.getString(“status”);
System.out.println(“status : ” + responseCode);
JSONArray arr = obj.getJSONArray(“results”);
for (int i = 0; i < arr.length(); i++) {
String placeid = arr.getJSONObject(i).getString(“place_id”);
System.out.println(“Place id : ” + placeid);
String formatAddress = arr.getJSONObject(i).getString(
“formatted_address”);
System.out.println(“Address : ” + formatAddress);
//validating Address as per the requirement
if(formatAddress.equalsIgnoreCase(“Chicago, IL, USA”))
{
System.out.println(“Address is as Expected”);
}
else
{
System.out.println(“Address is not as Expected”);
}
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
That’s it, Now you know to convert JsonObject to Java Object and use it in your Selenium snippet.

Google Example :

package automationFramework;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.Reporter;
import org.testng.annotations.Test;
@SuppressWarnings(“unused”)
public class RestAPI{
@Test
public void aptTesting() throws Exception {
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(“GET”);
conn.setRequestProperty(“Accept”, “application/json”, null);
if (conn.getResponseCode() != 200) {
throw new RuntimeException(“HTTP error code:” + conn.getResponseCode());
}
Scanner scan = new Scanner(url.openStream());
String entireResponse = new String();
while (scan.hasNext())
entireResponse += scan.nextLine();
System.out.println(“Response :” + entireResponse);
scan.close();
JSONObject obj = new JSONObject(entireResponse );
String responseCode = obj.getString(“status”);
System.out.println(“status :” + responseCode);
JSONArray arr = obj.getJSONArray(“results”);
for (int i = 0; i < arr.length(); i++) {
String placeid = arr.getJSONObject(i).getString(“place_id”);
System.out.println(“Place id :”  + placeid);
String formatAddress = arr.getJSONObject(i).getString(“formatted_address”);
System.out.println(“Address :” + formatAddress);
//validating Address as per the requirement
if(formatAddress.equalsIgnoreCase(“Chicago, IL, USA”))
{
System.out.println(“Address is as Expected”);
}
else
{
System.out.println(“Address is not as Expected”);
}
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s