Rest-Assured Framework from scratch including GIT Repository integration for Continous Integration
Tags: git, restassured, automation, api
Published: 2020-06-24 21:08:11

By the end of this article, you will be able to set up and understand Rest API, Eclipse with TestNG and create Rest Assured Framework from scratch. Also including GIT Repository integration for Continous Integration.

###What is API?

Application Programming Interface (API)
In basic terms, APIs just allow applications to communicate with one another.

When people speak of “an API”, they sometimes generalize and actually mean “a publicly available web-based API that returns data, likely in JSON or XML”. The API is not the database or even the server, it is the code that governs the access point(s) for the server.

###REST APIs:
Web services are purpose-built web servers that support the needs of a site or any other application. Client programs use application programming interfaces (APIs) to communicate with web services. Generally speaking, an API exposes a set of data and functions to facilitate interactions between computer programs and allow them to exchange information. Web API is the face of a web service, directly listening and responding to client requests.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u73zsnfrp6iwgnxq6i1m.jpg)

The REST architectural style is commonly applied to the design of APIs for modern web services. A Web API conforming to the REST architectural style is a REST API. Having a REST API makes a web service “RESTful.” A REST API consists of an assembly of interlinked resources. This set of resources is known as the REST API’s resource model. Well-designed REST APIs can attract client developers to use web services. In today’s open market where rival web services are competing for attention, and aesthetically pleasing REST API design is a must-have feature.

I recommend this youtube video to understand the basics of API and REST API: https://www.youtube.com/watch?v=7YcW25PHnAA

###Setting up Eclipse:

Install the latest version of the eclipse IDE from https://www.eclipse.org/, as the latest versions come with integrated Maven and GIT.

For detailed steps on eclipse installation, go to https://www.eclipse.org/downloads/packages/installer

###Setting up TestNG:

Once Eclipse is installed, 
 Launch Eclipse.
On the menu bar, click Help.
Choose the "Install New Software…" option.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/phz1feghwjf43zgh530z.jpg)

In the Eclipse Install dialog box
Enter "http://dl.bintray.com/testng-team/testng-eclipse-release/" in the Work with box
Click the Add button.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6yssbs9zt4zu3c0affks.png)
In Add repository dialog
Enter "TestNG" in Name Text Field
Click on Add button.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mnem9ninleh8jp8zv16b.png)
It will search for the entered software.
Select the "TestNG" checkbox
Click on the "Next" button.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qknmp1f68exg0nwb31ru.png)
It will give you a review of Items to be installed. Click on Next.
Select the radio button "I accept the terms of the license agreement"
Click on Finish.
It will take time depending on your Internet speed.
If you encounter a Security warning, just click "Install Anyway".
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pleujelnpt21yfo6qblc.png)
Wait for the installation to finish. When Eclipse prompts you for a restart, click "Restart now."
After the restart, verify if TestNG was successfully installed. Click Window > Show View > Other.Then open the Java directory and see if TestNG is included.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pdvf58a7tzesq140ou66.png)
Once we are done with eclipse and TestNG Setup, we can start creating our Rest Assured Framework.
 
##Rest Assured Framework:

Rest-Assured is an open-source Java Domain-Specific Language (DSL).
Using Rest Assured we can eliminate large amounts of code to test complex API response and output.
It supports both XML and JSON format. 
Rest-Assured supports many requests like

* GET
* POST
* PUT
* DELETE
* PATCH
* HEAD

The precondition for Rest Assured Framework:
* Java
* Maven
* TestNG

Now head to eclipse and start creating a project. 
Go to File>New>Other
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1dun6xroe83ll0ypddsk.png)

Select Maven>Maven Project
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jl2sjk3v7kux8cmqmprf.png)

Select “Create a simple project” and click on next
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k4s2rubjuxm19o3yda3j.png)

Now give logical names to “Group Id” and “Artifact Id” and click on Finish
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5lhsi4kpnwoo2ah2ou84.png)

Congrats! You have created a Maven project successfully. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/abegxptkl43naza42k7f.png)

Now let’s add the rest-assured dependency to POM.xml. In your project open POM.xml and add below dependency:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vg2fygi2ot6k9miz66d0.JPG)

Now your POM.xml should look like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tg36so4bc9jtteel1sbo.png)

Once you have added this dependency you will be able to see Maven Dependencies added into your project. It will add all the related JARs to your project, but be sure to check these JARs as they are the most important ones:

**httpclient**: It is the base JAR which Rest-Assured internally uses
**hamcrest-core**: It will be used to see the patterns in the code and put assertions into the code.
**json-path**: If we get the data in JSON format, this JAR helps in formatting the data.
**xml-path**:  If we get the data in XML format, this JAR helps in formatting the data.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tyzzmb7fajd6j8uwsgnu.png)

Once you are done with dependencies, its time to hit the service URL. 

I am going to use Postman as a manual tool which you can get from Chrome web store (go to Chrome Web Store and search for Postman, from there you can download the lite version of Postman) or full-fledged Postman application from their official Website https://www.postman.com/

For Rest Services Endpoint URL, you can go to https://openweathermap.org/api and browse many API service URL’s which is free. There are few paid ones too, you can check out on their website.

Open Postman and hit Send to below GET URL:

http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4yvx0iobfdv6n4xd86e4.png)

Once you hit the Send button, Response Status should be 200OK and you will receive the weather information of London in JSON Format.

Now that you have a perfectly working GET request, let’s automate the service using the Rest-Assured framework in Eclipse.

Go to your project and create a package under src/test/java. Right-click on src/test/java folder and create a package named “serviceRequest”. Under that create a Java class called “getData”.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p1s6wl0lvsjzukjkcs0b.png)

Now write the code in Rest-Assured Project to get the same data we got in Postman and assert the Status:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/afmb1j90ihb2a5wtyiv1.JPG)


Your class should look like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o0023ytk9ntzh1phfaoj.png)

Right-click anywhere on Java class and Run as> TestNG Test
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j2gzho2wrri2l2crdrsh.png)

You should be able to see same response in the console which you received in Postman. Moreover, we have performed an assertion on the Status Code.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nwi32uopc55m2ysn4qb1.png)

Congrats! You have created a Rest Assured framework from scratch and automated a GET service.

Now let’s create a GIT Repository and include our project into that. It is the most commonly used version control system. Git tracks the changes you make to files, so you have a record of what has been done, and you can revert to specific versions should you ever need to. Git also makes collaboration easier, allowing changes by multiple people to all be merged into one source

##GIT:
**Creating a GIT Repository:**
Go to https://github.com/ and create an account
Once the account is created GIT will ask you to create a New Repository (Start a Project).
Create a repository by giving it a Name, Description, and Type of Repository you want (Public or Private) and hit ‘Create Repository’.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tpcdbqazkx0a8bosrj76.png)

Your Repository will be created, copy your Git Repo URL:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tpcdbqazkx0a8bosrj76.png)

**Go to eclipse:**
Click on the Top Left Search button also called “Access commands and other items” (Ctrl+3) and search for Git Repositories.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7dphuijyqaity14oj6ht.png)

Once you have selected GIT Repositories, enter your GIT Repository URL. Other fields will automatically populate, enter your git username and password in the Authentication section, and click on Next.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8bych213uuoe830nux8t.png)

On Branch Selection window click on Next.

On the next window (Local Destination) select your local directory where you want to store your Git Project and click on Finish.

Your Git Repository will be added to the Eclipse-Git Repositories window.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qa14me8aqwjqmnpaupcr.png)

Now, right-click on your project and go to Team> Share Project
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qa14me8aqwjqmnpaupcr.png)

Select your repository from Repository dropdown and click Finish
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u89esd8yg5vyvp3aatzz.png)

Commit and Push the project to the GIT repo:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gvxj4pc1pbqjvl0fslpy.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gvxj4pc1pbqjvl0fslpy.png)

Select all Unstaged Changes and drag it to Staged Changes and give Commit Message
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l8w2v4ihvlr3jjjyeh0h.png)

Push Branch in Remote window will show you details like Source and Destination of your GIT Branch. Click Next 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9daipq7t0e2gbly8u0at.png)


It will ask for Push Confirmation. Click Finish.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sk594ai0nqiq0kdar3hf.png)

You have successfully pushed the local code to GIT. Go to your GIT Repository and refresh it. You should be able to see all local code into your repository.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/atolttcrxkfg7pmgqqsf.png)



I have kept this repository public, you can copy my project from here:

https://github.com/sahilTiwariQA/rest_AssuredFramework




References:
Rest API Design Rulebook - O’Reilly 
https://www.eclipse.org/
https://www.guru99.com/
https://mvnrepository.com/
http://rest-assured.io/
https://openweathermap.org/api