abishek kachroo
abishek kachroo

Reputation: 45

Matching the Json schema with the response getting from the API in rest-assured

I have a post API , i want methods of JsonSchemaValidator to be used as I want the whole reponse to be validated rather than selected reponse by performing assertion

I have tried to use

  1. matchesJsonSchemaInClasspath("my file name") and
  2. matchesJsonSchema(my file object)

my reposne is coming to be true, method is getting passed but there is no checking or validation with my schema file

public void directLoginWihSchemaValiadtor(){
    File file = new File("C:/Users/abeey/git/SlingAppWebService/Configurations/JsonSchemaValidator_DirectLogin_AWS.json");
    jsonasmap.put("contactNo", "some number");
    jsonasmap.put("loginType","0");
    jsonasmap.put("appid","2");
    jsonasmap.put("co*****ode","IN");
    JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder().
    setValidationConfiguration(ValidationConfiguration.newBuilder().freeze()).freeze();

    given().contentType(ContentType.JSON).body(jsonasmap).when().
    post("https://60i*****.execute-api.us-west-2.amazonaws.com/some-api").
    then().assertThat().
    body(JsonSchemaValidator.matchesJsonSchema(file))).


    log().all();
    jsonasmap.clear();
}

//body(JsonSchemaValidator.matchesJsonSchemaInClasspath("JsonSchemaValidator_DirectLogin_AWS.json").using(jsonSchemaFactory))

I tried to use jsonSchemaFactory to do this but i didnt get that either on what to set as the draftversion or from where to get it

I am new to this , please bear with me if you found this question too simple to be asked

Upvotes: 1

Views: 3671

Answers (1)

BigGinDaHouse
BigGinDaHouse

Reputation: 1346

For such case usually I do following:

  1. use schema generator and create the schema for json body (I use this tool json-schema-generator)

  2. put generated json schemas in the classpath (for example test/resources)

  3. use this code as part of REST Assured test:

    .body(matchesJsonSchemaInClasspath("your_schema_name.json"))

If you want to make sure that schema validation is working, you can edit schema file and change the type of any required field to something else and see that your test will fail.

You can refer to this post of mine to see some code samples.

Upvotes: 2

Related Questions