Validate JSON Schema using Rest Assured
JSON Schema is a powerful tool for validating the structure of JSON data or your JSON Response. JSON Schema itself is written in JSON. It is data itself, not a computer program. It’s just a declarative format for “describing the structure of other data”. The JSON document being validated or described we call the instance, and the document containing the description is called the schema.
JSON Schema validation is done to check whether the response received is correct or is structured according to the defined schema for your project. Let's look at an example of JSON Schema validation using Rest Assured. Here we are making use of Google Search API that we studied in Testing GET Requests and their Responses using Rest Assured. I would highly recommend reading previous posts before reading this one.
Lets us create a JSON File where we will define the JSON Schema for our response ( JSON Schema of a Response that we will get when we search for Churchgate Station ).
{ "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "html_attributions": { "type": "array", "items": {} }, "results": { "type": "array", "items": { "type": "object", "properties": { "formatted_address": { "type": "string" }, "geometry": { "type": "object", "properties": { "location": { "type": "object", "properties": { "lat": { "type": "number" }, "lng": { "type": "number" } }, "required": [ "lat", "lng" ] }, "viewport": { "type": "object", "properties": { "northeast": { "type": "object", "properties": { "lat": { "type": "number" }, "lng": { "type": "number" } }, "required": [ "lat", "lng" ] }, "southwest": { "type": "object", "properties": { "lat": { "type": "number" }, "lng": { "type": "number" } }, "required": [ "lat", "lng" ] } }, "required": [ "northeast", "southwest" ] } }, "required": [ "location", "viewport" ] }, "icon": { "type": "string" }, "id": { "type": "string" }, "name": { "type": "string" }, "photos": { "type": "array", "items": { "type": "object", "properties": { "height": { "type": "integer" }, "html_attributions": { "type": "array", "items": { "type": "string" } }, "photo_reference": { "type": "string" }, "width": { "type": "integer" } }, "required": [ "height", "html_attributions", "photo_reference", "width" ] } }, "place_id": { "type": "string" }, "rating": { "type": "number" }, "reference": { "type": "string" }, "types": { "type": "array", "items": { "type": "string" } } }, "required": [ "formatted_address", "geometry", "icon", "id", "name", "photos", "place_id", "rating", "reference", "types" ] } }, "status": { "type": "string" } }, "required": [ "html_attributions", "results", "status" ] }
We can also generate JSON Schema for the available response on the following site :
You need to copy the response in JSON Field and then click on Generate Schema button and the schema will be automatically generated. Copy that schema and paste it in text file and name it as json-schema.json. In your Eclipse project, paste that file under src/test/java as shown below:
Now lets have a look at an example that will help us to validate JSON schema:
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
public class JsonSchemaValidationTest {
@BeforeClass
public void setBaseUri () {
RestAssured.baseURI = "https://maps.googleapis.com";
}
@Test
public void validateJSON () {
given().param ("query", "Churchgate Station")
.param ("key", "XYZ")
.when ()
.get ("/maps/api/place/textsearch/json")
.then ()
.body (matchesJsonSchemaInClasspath("json-schema.json"));
}
}
We will need to import the method matchesJsonScheamInClasspath, so we will add the following import:
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath.
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath.
In the setBaseUri method, We are simply setting the default URI to use in all of our tests. Now when we write out a test that calls an API (as we will do in the next step), we don’t have to type in the full address of the API. In this case, the baseUri is https://maps.googleapis.com.
In validateJSON method, Under Given, we will mention the parameters (query and key )while making the API call.When is used for making the API call. Under Then we call Body method which is an option to validate response in which we pass a call to method matchesJsonSchemaInClasspath which will check whether the repsonse receive matches with the schema declared in json-schema.json file
That's all about validating JSON Schema using Using Rest Assured.
That's all about validating JSON Schema using Using Rest Assured.
Comments
Post a Comment