Reputation: 45
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
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
Reputation: 1346
For such case usually I do following:
use schema generator and create the schema for json body (I use this tool json-schema-generator)
put generated json schemas in the classpath (for example test/resources)
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