1

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

abishek kachroo
  • 35
  • 1
  • 11
  • Try specifying which schema draft you want to use when creating your `JsonSchemaFactory`. `DRAFTV4` is what I use. – Andrew Nolan Jul 29 '17 at 18:47
  • i have used that and it doesn't work and I think it is used to only check that the json response is of DraftV4 version – abishek kachroo Jul 29 '17 at 20:06
  • Can you post the schema you are validating? – Andrew Nolan Jul 29 '17 at 20:13
  • this is the code that I have created using JsonSchemaFactory JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder(). setValidationConfiguration(ValidationConfiguration.newBuilder().setDefaultVersion(SchemaVersion.DRAFTV4).freeze()).freeze(); given().contentType(ContentType.JSON).body(jsonasmap).when().post("https://***d.execute-api.us-***-*.amazonaws.com/some-api").then().assertThat().body(JsonSchemaValidator.matchesJsonSchema(file).using(jsonSchemaFactory)).log().all(); If you have any suggestions on improving it so the result could be acheived please tell – abishek kachroo Jul 29 '17 at 20:19
  • the question is whatever I am putting in schema whether its correct or incorrect , it doesn't matter it will show pass anyway – abishek kachroo Jul 29 '17 at 21:27
  • Then it is a false positive. I just tried on one of my tests with just `{}` in a test.json file returned a passed test. So it seems if schema is incorrect that you are trying to validate against, you will get a pass when it should be a fail – Andrew Nolan Jul 30 '17 at 19:32

1 Answers1

2

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.

BigGinDaHouse
  • 1,282
  • 9
  • 19
  • Sadly I was writing mine by hand for the last few weeks. Didnt think to check for tools to do a schema for you. I found this one: https://jsonschema.net/#/editor – Andrew Nolan Jul 30 '17 at 19:34
  • there are plenty of tools indeed, I stacked to one I mentioned, cause it is very easy to install, do not require configuration and do the job really good, using it already year or so in lots of tests – BigGinDaHouse Jul 31 '17 at 22:07
  • thank you so much @BigGinDaHouse I was writing myself the schema it was not in the correct order so may that can be the reason I was not getting any validation for that Thanks a tonne – abishek kachroo Aug 01 '17 at 18:16