Introduction to REST-API and REST-Assured

This entry is part 2 of 14 in the series REST Assured Tutorial

What is RESTful API :

As per the definition REST is an architectural style of writing applications. So we can say REST is not a new independent technology, it is just a way or a style of writing applications, it has some standards defined to write the code. And when we apply these standards and rules it induces desirable properties like loose coupling and horizontal scalability. The set of these architectural principles is called REpresentational State Transfer(REST) and those principles are as follows :

  • Addressable resources :

    Lets say we have a profile photo on facebook and we call it a resource. Every content is called a resource in REST. Now the principle is every resource must be accessible with a address. In REST when we talk about addresses, we mean URI. So for example your profile photo should be accessible through below URI.
    facebook.com/atulchavan/profilephoto/1
    Here facebook.com is a domain name, “/atulchavan/” is username and “/profilephoto/” is a directory where all my profile photos are stored, “/1/” is the unique id of a specific profile photo. This means every profile photo is accessible through a web URI called as resource URI.

  • A Uniform, constrained interface :

    Now consider your resource is stored on a database and you want to perform CRUD operations on your database entries with the help of REST API. In REST, CRUD operations are directly mapped with HTTP methods. For example if you want to access a database entry you will perform SELECT operation, to map that operation with REST, we can use HTTP GET method, which is commonly used to request data over the internet. This means whenever we send a GET request, a SELECT query should be made to the database, for POST method INSERT query, for PUT method REPLACE query, for DELETE method DELETE query, for PATCH method UPDATE query. There are more HTTP methods supported by REST which we will discuss later in great detail, but these are the most common methods used.

  • Representation-oriented :

    REST services can receive request and send response in different formats(different representation). You interact with services using representations of that service. A resource referenced by one URI can have different formats. Different platforms need different formats. For example, browsers need HTML, JavaScript needs JSON(JavaScript Object Notation) and a Java Application may need XML.

  • Communicate Statelessly :

    RESTful services are built on top of HTTP protocol, because we know common operations are directly mapped with HTTP methods. As HTTP is a stateless protocol, RESTful API cannot communicate by maintaining the state, it should communicate statelessly.

  • Hypermedia As The Engine Of Application State (HATEOAS) :

    As the REST is Stateless, somehow you will need to maintain the state of the application. This principle let your data formats(XML, JSON or anything) drive the state transitions in your applications. The Hypermedia for an instance call it Hyperlink(URI) will be passed with the request data and the response data, for example see the below JSON data:

    Now we can see we have the information about the user, but the data contains nothing about its relevant links, means what if we need address to this resource. So the principle says, you should pass the Hypermedia link for that resource within the request or response data itself. We can solve this by adding data like below :

    In some scenarios we may need to visit to another resource by using current resource, so the address of that new resource will be provided in Hypermedia link.

What is REST-Assured :

It is a java library which will help us in automating the testing of RESTful API. We can use any Java IDE for writing code to test our API. REST assured is helpful in writing small Unit tests as well as large automation frameworks, where we can create test cases for large APIs. It has so many useful functions which we can use to send all types of HTTP methods, accessing public as well as private API by authenticating yourself. Storing the response and later use that response data to validate against the expected data. We can also use TestNG for better execution and generating better validation reports and assertions of your test cases.

Series Navigation<< What is a SOA – Service Oriented ArchitectureREST Assured -Setting up environment using Maven >>