Friday, November 20, 2015

Automating webServices using Rest Assured


BASICS

Web services are open standard (XML, SOAP, HTTP etc.) based Web applications that interact with other web applications for the purpose of exchanging data.
Web Services can convert your existing applications into Web-applications
Nowadays most of the web services are either SOAP or REST based. This blog is all about REST based API and how to automate those.
REST : REpresentational State Transfer (REST) is the software architectural style of the World Wide Web. 
To do automation testing of REST based APIs, there are many frameworks but my favorite is REST-assured which offers a bunch of nice features like a DSL(domain-specific language)-like syntax, XPath-Validation, Specification Reuse, easy file uploads. It supports POST, GET, PUT, DELETE, OPTIONS, PATCH and HEAD requests and can be used to validate and verify the response of these requests.
JSON and XML are the two markup languages that can be used in restful web API.


HTTP methods supported by REST are:

  • GET: It requests a resource at the request URL. It should not contain a request body as it will be discarded. May be it can be cached locally or on the server.
  • POST: It submits information to the service for processing; it should typically return the modified or new resource
  • PUT: At the request URL it update the resource
  • DELETE: At the request URL it removes the resource
  • OPTIONS: It indicates which techniques are supported
  • HEAD: About the request URL it returns meta information


 PUT vs POST

“PUT”puts a file or resource at a particular URI and exactly at that URI.  If there is already a file or resource at that URI, PUT changes that file or resource.  If there is no resource or file there, PUT makes one


POST sends data to a particular URI and expects the resource at that URI to deal with the request.  The web server at this point can decide what to do with the data in the context of specified resource

Example : To Test Google Map API

Resource Bundle

URI=https://maps.googleapis.com/maps/api/geocode/json?key1=AIzaSyDM6b6oTEPNknlGAk2tZGas3Hkm-ZVRmJA&address=AdithyaFrondoso

Main Class

 import static com.jayway.restassured.RestAssured.given;  
 import com.jayway.restassured.RestAssured;  
 import com.jayway.restassured.response.Response;  
 public class GoogleMapAPI extends BaseClass{  
  public GoogleMapAPI(String url){  
  RestAssured.baseURI=url;  
 }  
  public Response getAddressResponse(String url)  
  { Response response=given().that().contentType( "application/json").when().get(url);  
  return response; }  
 }  




Test Class
 import static com.jayway.restassured.RestAssured.given;  
 import static org.testng.Assert.*;  
 import java.util.ResourceBundle;  
 import org.testng.annotations.BeforeClass;  
 import org.testng.annotations.Test;  
 import com.google.map.api.util.BaseClass;  
 import com.google.map.api.util.GoogleMapAPI;  
 import com.jayway.restassured.response.Response;  
 public class TestGoogleMapApi extends BaseClass{  
  GoogleMapAPI object;  
  ResourceBundle bundle;  
  String caseId;  
  String app_id;   
  String url;  
  @BeforeClass  
  public void doSetUp()  
  {     
  bundle=ResourceBundle.getBundle("resource");  
   url = bundle.getString("URI");  
  object= new GoogleMapAPI(url) ;   
  }  
  @Test  
  public void testFindresponse()  
  {  
  System.out.println("===============Executing the TCs : Test testFindresponse ======================");  
  System.out.println("===============Executing the Method : testFindresponse ======================");  
  try{  
   Response response=object.getAddressResponse(url+"Patna");   
   System.out.println("The Response is "+response.asString());  
   assertNotNull(response);  
   System.out.println("===============Completed the Method : testFindresponse ======================");  
  }  
  catch(Exception Ex)  
  {  
   Ex.printStackTrace();  
   logger.error("==========================testFindresponse Method Failed==================");  
  }   
  }   
 }  



BASE CLASS
 import org.apache.log4j.Logger;  
 public class BaseClass {  
  protected final Logger logger = Logger.getLogger(this.getClass());  
 }  


Also the pom.xml should contain below dependencies "

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>GoogleMapRestTest</groupId>  
  <artifactId>GoogleMapRestTest</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <name>GoogleMapRestTest</name>  
  <description>GoogleMapRestTest</description>  
  <properties>  
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  </properties>  
  <dependencies>  
  <dependency>  
   <groupId>com.jayway.restassured</groupId>  
   <artifactId>rest-assured</artifactId>  
   <version>2.4.0</version>  
  </dependency>  
  <dependency>  
   <groupId>log4j</groupId>  
   <artifactId>log4j</artifactId>  
   <version>1.2.17</version>  
  </dependency>  
  <dependency>  
   <groupId>org.testng</groupId>  
   <artifactId>testng</artifactId>  
   <version>6.3.1</version>  
   <scope>test</scope>  
  </dependency>  
  <dependency>  
   <groupId>org.seleniumhq.selenium</groupId>  
   <artifactId>selenium-java</artifactId>  
   <version>2.39.0</version>  
   <scope>compile</scope>  
  </dependency>  
  </dependencies>  
  <build>  
  <testSourceDirectory>src/test/java</testSourceDirectory>  
  <testResources>  
   <testResource>  
   <directory>src/test/resources/properties</directory>  
   <includes>  
    <include>**/*</include>  
   </includes>  
   </testResource>  
  </testResources>  
  <plugins>  
   <plugin>  
   <groupId>org.apache.maven.plugins</groupId>  
   <artifactId>maven-compiler-plugin</artifactId>  
   <version>3.0</version>  
   <configuration>  
    <fork>true</fork>  
    <source>1.7</source>  
    <target>1.7</target>  
   </configuration>  
   </plugin>  
   <plugin>  
   <groupId>org.apache.maven.plugins</groupId>  
   <artifactId>maven-surefire-plugin</artifactId>  
   <version>2.19</version>  
   <configuration>  
    <forkMode>always</forkMode>  
    <testFailureIgnore>true</testFailureIgnore>  
    <suiteXmlFiles>  
    <suiteXmlFile>${suiteName}.xml</suiteXmlFile>  
    </suiteXmlFiles>  
   </configuration>  
   </plugin>  
  </plugins>  
  </build>  
  <profiles>  
  <profile>  
   <id>google_testng</id>  
   <properties>  
   <suiteName>google_testng</suiteName>  
   </properties>  
  </profile>  
  </profiles>  
  <repositories>  
  <repository>  
   <id>sonatype</id>  
   <url>https://oss.sonatype.org/content/repositories/snapshots/</url>  
   <snapshots />  
  </repository>  
  <repository>  
   <id>central</id>  
   <name>Central</name>  
   <url>http://repo.maven.apache.org/maven2</url>  
  </repository>  
  </repositories>  
 </project>  

And a TestNG.xml
=============
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite" parallel="none">  
  <test name="Test">  
   <classes>  
    <class name="com.google.map.api.test.TestGoogleMapApi"/>  
   </classes>  
  </test> <!-- Test -->  
 </suite> <!-- Suite -->  
Run As 
======
C:\Users\sjyetki\workspaceMars\GoogleMapRestTest>mvn test -P google_testng

1 comment: