Rest Assured
Rest Assured API is used to test the REST [Representational State Transfer] methods as such GET,POST,DELETE and UPDATE Methods. REST API Documentation can be referred using the link http://www.javadoc.io/doc/com.jayway.restassured/rest-assured/2.9.0.
In this tutorial we will discuss about the usage of GET Method.
Before Getting into this Example Please follow the links mentioned below for better understanding of Rest Services.
Rest API Basics
REST API – FACEBOOK & GOOGLE API EXAMPLES
Pre-requisites
Follow the Steps in the Link to download the sample REST Application and deploy on
a tomcat Server-> REST API Application Live Example
Implementing the Framework
All the below methods taken care as part of Framework
1. Method Response getServiceResponse(String serviceURL) added as part of the RestLibrary.java class which helps to provide the response based on the end point URL passed as a parameter.
1 2 3 4 5 6 7 |
public static Response getServiceResponse(String serviceURL) throws Exception { RequestSpecification request = RestAssured.given(); request.headers(provideHeaders()); Response response = request.when().get(serviceURL); return response; } |
2. Methods used to store the Run time Value in setSysProperty() and fetch the value using getSysProperty().
1 2 3 4 5 6 7 8 9 10 11 12 |
public static String getSysProperty(String propName) { String propValue = System.getProperty(propName); return propValue != null ? propValue : null; } public static void setSysProperty(String propName, String propValue) { if (propValue != null) { System.setProperty(propName, propValue); } else { System.setProperty(propName, (String) null); } } |
3. This framework is implemented with Object Properties where the REST Endpoint url’s are
stored in the configuration.properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@BeforeClass public void setUp() throws IOException { System.out.println("in BeforeClass"); /** * Load the Property File */ // Create FileInputStream object FileInputStream fis=new FileInputStream(new File("configuration.properties")); // Create Properties class object to read properties file pro=new Properties(); // Load file so we can use into our script pro.load(fis); } |
3. Test case to retrieve all the Customers.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Test(description="Retrieve All the Customers") public void verifyGetAllCustomers() { try { responseBody = RestLibrary.getServiceResponse("http://localhost:8080/sqlrest/CUSTOMER/"); System.out.println(responseBody.prettyPrint()); } catch (Exception e) { e.printStackTrace(); Assert.fail("Unable to Retrieve information for Enterprises"); } } |
4. Test case to retrieve a particular customer by updating the Customer ID during run time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@Test(description="Fetch the Details of the Rest Service using GET Method") public void verifyGetMethodRunTimeValues() { try { String url = pro.getProperty("getParticularCustomer"); System.out.println("Url stored in the Property file::" + url); url = url.replace("$CUSTOMERID", "1"); System.out.println("Url updated with Customer ID::: " + url); responseBody = RestLibrary.getServiceResponse(url); System.out.println(responseBody.prettyPrint()); } catch (Exception e) { e.printStackTrace(); Assert.fail("Unable to Retrieve information for Enterprises"); } } |
5. Test case to retrieve a particular customer by updating the Customer ID using setProperty() and getProperty() Methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@Test(description="Using the getProperty and setProperty" ) public void verifyGetServiceUsingProperties() { try { String url = pro.getProperty("getCusomters"); System.out.println("Url stored in the Configuration Properties::" + url); RestLibrary.setSysProperty("CUSTOMERID", "1"); url =url+"/"+RestLibrary.getSysProperty("CUSTOMERID"); System.out.println("Updated url using getSystemProperty Value"+ url); responseBody = RestLibrary.getServiceResponse(url); System.out.println(responseBody.prettyPrint()); } catch (Exception e) { e.printStackTrace(); Assert.fail("Unable to Retrieve information for Enterprises"); } } |
6. This framework is integrated with Allure as well, so the response can be viewed in the report itself.
Logic as follows:
RestAssuredExamples.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package org.totalqa.restassured; import org.testng.Assert; import org.testng.annotations.Test; import com.jayway.restassured.response.Response; public class RestAssuredExamples extends APIBaseClass { private static Response responseBody = null; @Test(description="Fetch the Details of the Rest Service using GET Method",enabled=false) public void verifyGetMethodDynamicUrl() { try { String url = pro.getProperty("getCusomters"); System.out.println("Url stored in the Property file::" + url); responseBody = RestLibrary.getServiceResponse(url); System.out.println(responseBody.prettyPrint()); } catch (Exception e) { e.printStackTrace(); Assert.fail("Unable to Retrieve information for Enterprises"); } } @Test(description="Fetch the Details of the Rest Service using GET Method",enabled=false) public void verifyGetMethodRunTimeValues() { try { String url = pro.getProperty("getParticularCustomer"); System.out.println("Url stored in the Property file::" + url); url = url.replace("$CUSTOMERID", "1"); System.out.println("Url updated with Customer ID::: " + url); responseBody = RestLibrary.getServiceResponse(url); System.out.println(responseBody.prettyPrint()); } catch (Exception e) { e.printStackTrace(); Assert.fail("Unable to Retrieve information for Enterprises"); } } @Test(description="Retrieve All the Customers") public void verifyGetAllCustomers() { try { responseBody = RestLibrary.getServiceResponse("http://localhost:8080/sqlrest/CUSTOMER/"); System.out.println(responseBody.prettyPrint()); } catch (Exception e) { e.printStackTrace(); Assert.fail("Unable to Retrieve information for Enterprises"); } } @Test(description="Using the getProperty and setProperty" ) public void verifyGetServiceUsingProperties() { try { String url = pro.getProperty("getCusomters"); System.out.println("Url stored in the Configuration Properties::" + url); RestLibrary.setSysProperty("CUSTOMERID", "1"); url =url+"/"+RestLibrary.getSysProperty("CUSTOMERID"); System.out.println("Updated url using getSystemProperty Value"+ url); responseBody = RestLibrary.getServiceResponse(url); System.out.println(responseBody.prettyPrint()); } catch (Exception e) { e.printStackTrace(); Assert.fail("Unable to Retrieve information for Enterprises"); } } } |
RestLibrary.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package org.totalqa.restassured; import java.util.LinkedHashMap; import java.util.Map; import com.jayway.restassured.RestAssured; import com.jayway.restassured.response.Response; import com.jayway.restassured.specification.RequestSpecification; public class RestLibrary { private static Map<String,Object> headersMap; public static Response getServiceResponse(String serviceURL) throws Exception { RequestSpecification request = RestAssured.given(); request.headers(provideHeaders()); Response response = request.when().get(serviceURL); return response; } /** * Provide the Header Values * Content-type * tenantID * Authorization * @return * @throws Exception */ public static Map<String, Object> provideHeaders( ) throws Exception{ try{ headersMap = new LinkedHashMap<String, Object>(); // headersMap.put("Content-type","application/json"); // headersMap.put("tenant","tenantID"); // headersMap.put("Authorization","tokenvalue"); return headersMap; }catch(Exception e){ } return headersMap; } public static String getSysProperty(String propName) { String propValue = System.getProperty(propName); return propValue != null ? propValue : null; } public static void setSysProperty(String propName, String propValue) { if (propValue != null) { System.setProperty(propName, propValue); } else { System.setProperty(propName, (String) null); } } } |
APIBaseClass.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package org.totalqa.restassured; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import org.testng.annotations.BeforeClass; public class APIBaseClass { protected static Properties pro; @BeforeClass public void setUp() throws IOException { System.out.println("in BeforeClass"); /** * Load the Property File */ // Create FileInputStream object FileInputStream fis=new FileInputStream(new File("configuration.properties")); // Create Properties class object to read properties file pro=new Properties(); // Load file so we can use into our script pro.load(fis); } } |
configuration.properties
1 2 |
getCusomters=http://localhost:8080/sqlrest/CUSTOMER/ getParticularCustomer=http://localhost:8080/sqlrest/CUSTOMER/$CUSTOMERID |
Attaching the XML Response in Allure Report
1. Refer to complete implementation of Allure Reporting.
2. Add the below code in your Base Class.
1 2 3 4 5 6 |
/*To Attach the XML File to the Allure Report*/ @Attachment(value = "XML Attachment", type = "text/xml") public static byte[] attachFileType_XML(String filePath) throws Exception { return fileToBytes(filePath); } |
Attaching the JSON Response in Allure Report
1. Refer to complete implementation of Allure Reporting.
2. Add the below code in your Base Class.
1 2 3 4 5 6 7 |
/*To Attach the XML File to the Allure Report*/ /*To Attach the JSON File to the Allure Report*/ @Attachment(value="JSON Attachment", type="text/json") public static byte[] attachFileType_JSON(String filePath) throws Exception { return fileToBytes(filePath); } |