How to validate a json schema in rest assured

Steps:

1. Check the response of your api.

2. Use a json schema generator online, to generate a schema for the json you are using

3. In src/test/resources create a .json file and paste that schema

4. Use validatableResponse interface to validate the schema

5. import hamcreshmatchers, 

6.Import jsonschemavalidator to validate the schema

import io.restassured.module.jsv.JsonSchemaValidator;

import io.restassured.path.json.JsonPath;

import org.hamcrest.Matchers;

Response r1=RestAssured.given()

.baseUri(“http://xxx.x.x.1:8090/api/collections/books/records“)

.when()

.get();

ValidatableResponse r2=r1

.then()

.assertThat()

.statusCode(200)

.body(JsonSchemaValidator.matchesJsonSchemaInClasspath(“firstjsonschema.json”));

System.out.println(r2);

r2.body(“items[0].bookname”, Matchers.equalTo(“The Bhagavat Gita”));

How to create a JWT token and use it

  • Library : Rest Assured
  • Framwork : BDD, Cucumber, Page Object Model
  • Language : JAVA
  • IDE: Eclipse
  • API : Pocketbase

Steps :

  1. Create a collection in Pocketbase – Books

Create some fields like these

{

   “id”:”test123″,

    “bookname”: “Algorithms”,

    “price”:100,

    “sellername”:”local”

    “sold”: false

}

2. Now go to edit collection>API rules>>>Here you can edit the behaviour of the api.

Now, I want this api to create a record, only when they are authorised user. 

So I set this in the create rule>> @request.auth.id != “”

Save changes.

3. Steps for Pocketbase:

So in pocketbase I have setup a users collection. Where I’ll see Auth with password.

Steps for rest assured

I need to use the JWT token to create any record in the books collection.

  1. Make a post call to(api/collections/users/auth-with-password) and get the token
  2. Use> jsonPath().getString(“token”) > to get the value of the token field
  3. Now make a post call to > api/collections/books/records
  4. In headers give the token value >> header(“Authorization”,”Bearer “+ jwttokenValue)

Now go to edit collection>API rules>>>Here you can edit the behaviour of the api.

Now, I want this api to create a record, only when they are authorised user. 

So I set this in the create rule>> @request.auth.id != “”

Save changes.

Steps for pocketable:

So in pocketbase I have setup a users collection. Where I’ll see Auth with password.

Steps for rest assured

I need to use the JWT token to create any record in the books collection.

  1. Make a post call to(api/collections/users/auth-with-password) and get the token
  2. Use> jsonPath().getString(“token”) > to get the value of the token field
  3. Now make a post call to > api/collections/books/records

• 4. In headers give the token value >> header(“Authorization”,”Bearer “+ jwttokenValue)