Response body: JSON value is equal or greater than

@Ibus, thank you very much!

So thanks to all and with help of this post from @Ibus, the final steps and code for validating JSON response including if keys’ values are equal or greater than 0 are:

At first let’s imagine you want to verify that response:

{
  "total":6.98,
  "available":0.0,
  "pending":0.0,
  "paid":6.98
}

You can use for that JSON Schema specification.

  1. Add to your project these jars:

or use @Ibus’s method from this post.

  1. Create custom keyword:
package verify

import org.everit.json.schema.Schema
import org.everit.json.schema.ValidationException
import org.everit.json.schema.loader.SchemaLoader
import org.json.JSONObject
import org.json.JSONTokener
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.util.KeywordUtil

@Keyword
def verifyJsonSchema(String jsonString, String schemaString) {
	JSONObject rawSchema = new JSONObject(new JSONTokener(schemaString))
	Schema schema = SchemaLoader.load(rawSchema)
	try {
		schema.validate(new JSONObject(jsonString))
		KeywordUtil.markPassed("Valid schema")
	} catch (Exception e) {
		StringBuffer outmessage = new StringBuffer()
		outmessage << e.getMessage() << "\n"
		e.getAllMessages().each { msg -> outmessage << "$msg \n"}
		KeywordUtil.markFailed(outmessage as String)
	}
}
  1. Use this verification code:
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webservice.verification.WSResponseManager

import internal.GlobalVariable as GlobalVariable

RequestObject request = WSResponseManager.getInstance().getCurrentRequest()

ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()

jsonString = response.getResponseText()

schemaString = '''
{
	"type": "object",
	"required": ["total", "available", "pending", "paid"],
	"properties": {
		"total": {
			"type": "number",
			"minimum": 0.0
		},
		"available": {
			"type": "number",
			"minimum": 0.0
		},
		"pending": {
			"type": "number",
			"minimum": 0.0
		},
		"paid": {
			"type": "number",
			"minimum": 0.0
		}
	}
}
'''

CustomKeywords.'verify.Verify.verifyJsonSchema'(jsonString, schemaString)

Also you can use schema from the file system, check how in this post.

1 Like