gatling Custom validations

59 views
Skip to first unread message

Sreelakshmi Ck

unread,
Oct 18, 2017, 10:31:08 AM10/18/17
to Gatling User Group
Hi,

I am new to gatling and scala. 

I want to fail a request, if the json response didn't match the json schema.
This is my schema validator function. 


 def validateJsonAgainstSchema(jsonResponseExpr:Expression[String], schemaFile:String)(session: Session): Validation[Session] = {

    val jsonResponse=jsonResponseExpr(session)
    val jsonSchema = testUtilsObj.readFile(schemaFile)
    val schema: JsonNode = asJsonNode(parse(jsonSchema))
    val instance: JsonNode = asJsonNode(parse(jsonResponse.get))
    val validator = JsonSchemaFactory.byDefault().getValidator
    val processingReport = validator.validate(schema, instance)

    if (processingReport.isSuccess) {
           Success(session)
         } else {
        println("****validation Failed*******")
        println(processingReport.map(_.asJson()).mkString("\n"))
       Failure(processingReport.map(_.asJson()).mkString("\n"))
    }
  }


  I call this function in exec block as below, and the gatling test passes even if the function returns failure.
   .exec(
        valObj.validateJsonAgainstSchema("${jsonResponse}","Schemas/searchQueryResponse.json") _
      )

  How to make the call to 'validateJsonAgainstSchema' in the request check block. Can some one give me example of implementing custom validations?

  Thanks,
  Sreelakshmi
Reply all
Reply to author
Forward
0 new messages