Response body: JSON value is equal or greater than

Hello! I’m new to Katalon. Tell me please is there a simple method to verify value of JSON response by “Is equal or greater than”?

I see there is method which verify JSON value:

WS.verifyElementPropertyValue(response, ‘total’, 0)

But is it possible to use it to verify value by “Is equal or greater than”?

Hello,

have a look here - it helps you to get a value from JSON response and the comparison is very easy then.

https://docs.katalon.com/katalon-studio/tutorials/parse_json_responses.html

1 Like

Hello @Marek_Melocik,

According that tutorial if i get such response:

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

This code gives success verification:

import static org.assertj.core.api.Assertions.*

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 groovy.json.JsonSlurper
import internal.GlobalVariable as GlobalVariable

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

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

'Verify response code'

WS.verifyResponseStatusCode(response, 200)

assertThat(response.getStatusCode()).isEqualTo(200)

'Set variables'

String jsonString = response.getResponseText()

JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(jsonString)

def totalValue = parsedJson.total
def availableValue = parsedJson.available
def pendingValue = parsedJson.pending
def paidValue = parsedJson.paid

'Verify if all keys are present'

import com.kms.katalon.core.util.KeywordUtil

if(totalValue == null) {
	KeywordUtil.markFailed("Key is not present")
}
if(availableValue == null) {
	KeywordUtil.markFailed("Key is not present")
}
if(pendingValue == null) {
	KeywordUtil.markFailed("Key is not present")
}
if(paidValue == null) {
	KeywordUtil.markFailed("Key is not present")
}

'Verify if all keys\'s values are correct'

if(totalValue == "6.98") {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(availableValue == "0.0") {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(pendingValue == "0.0") {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(paidValue == "6.98") {
	KeywordUtil.markFailed("Key has incorrect value")
}

But if I want check the key values as float numbers verification fails:

import static org.assertj.core.api.Assertions.*

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 groovy.json.JsonSlurper
import internal.GlobalVariable as GlobalVariable

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

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

'Verify response code'

WS.verifyResponseStatusCode(response, 200)

assertThat(response.getStatusCode()).isEqualTo(200)

'Set variables'

String jsonString = response.getResponseText()

JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(jsonString)

float totalValue = parsedJson.total
float availableValue = parsedJson.available
float pendingValue = parsedJson.pending
float paidValue = parsedJson.paid

'Verify if all keys are present'

import com.kms.katalon.core.util.KeywordUtil

if(totalValue == null) {
	KeywordUtil.markFailed("Key is not present")
}
if(availableValue == null) {
	KeywordUtil.markFailed("Key is not present")
}
if(pendingValue == null) {
	KeywordUtil.markFailed("Key is not present")
}
if(paidValue == null) {
	KeywordUtil.markFailed("Key is not present")
}

'Verify if all keys\'s values are correct'

if(totalValue >= 0) {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(availableValue >= 0) {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(pendingValue >= 0) {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(paidValue >= 0) {
	KeywordUtil.markFailed("Key has incorrect value")
}

12-29-2018 11:04:40 AM Test Cases/Content of OK responses

Log:

Elapsed time: 2,710s

Test Cases/Content of OK responses FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: Key has incorrect value
	at com.kms.katalon.core.util.KeywordUtil.markFailed(KeywordUtil.java:18)
	at com.kms.katalon.core.util.KeywordUtil$markFailed.call(Unknown Source)
	at WSVerification1546070682070.run(WSVerification1546070682070:53)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.WSVerificationExecutor.runScript(WSVerificationExecutor.java:146)
	at com.kms.katalon.core.main.WSVerificationExecutor.doExecute(WSVerificationExecutor.java:140)
	at com.kms.katalon.core.main.WSVerificationExecutor.processExecutionPhase(WSVerificationExecutor.java:123)
	at com.kms.katalon.core.main.WSVerificationExecutor.accessMainPhase(WSVerificationExecutor.java:115)
	at com.kms.katalon.core.main.WSVerificationExecutor.execute(WSVerificationExecutor.java:103)
	at com.kms.katalon.core.main.TestCaseMain.runWSVerificationScript(TestCaseMain.java:119)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestAndVerifyKeyword$_sendRequestAndVerify_closure1.doCall(SendRequestAndVerifyKeyword.groovy:51)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestAndVerifyKeyword$_sendRequestAndVerify_closure1.call(SendRequestAndVerifyKeyword.groovy)
	at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:54)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestAndVerifyKeyword.sendRequestAndVerify(SendRequestAndVerifyKeyword.groovy:42)
	at com.kms.katalon.core.webservice.keyword.builtin.SendRequestAndVerifyKeyword.execute(SendRequestAndVerifyKeyword.groovy:37)
	at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:53)
	at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.sendRequestAndVerify(WSBuiltInKeywords.groovy:65)
	at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords$sendRequestAndVerify.call(Unknown Source)
	at Content of OK responses.run(Content of OK responses:16)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1546070677911.run(TempTestCase1546070677911.groovy:22)

Tell me please what am I doing wrong?

Good morning,

you should compare float differently than int. Read this article:

@Marek_Melocik but does it matter in the case of comparison if value equal or greater than? And how using that info to do that?

I’ve even tried this (maybe I did it wrong) but verification still failed and I got Key has incorrect value:

import static org.assertj.core.api.Assertions.*

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 groovy.json.JsonSlurper
import internal.GlobalVariable as GlobalVariable

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

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

'Verify response code'

WS.verifyResponseStatusCode(response, 200)

assertThat(response.getStatusCode()).isEqualTo(200)

'Set variables'

String jsonString = response.getResponseText()

JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(jsonString)

BigDecimal totalValue = parsedJson.total
BigDecimal availableValue = parsedJson.available
BigDecimal pendingValue = parsedJson.pending
BigDecimal paidValue = parsedJson.paid

BigDecimal cbase = 0.00

'Verify if all keys are present'

import com.kms.katalon.core.util.KeywordUtil

if(totalValue == null) {
	KeywordUtil.markFailed("Key is not present")
}
if(availableValue == null) {
	KeywordUtil.markFailed("Key is not present")
}
if(pendingValue == null) {
	KeywordUtil.markFailed("Key is not present")
}
if(paidValue == null) {
	KeywordUtil.markFailed("Key is not present")
}

'Verify if all keys\'s values are correct'

if(totalValue.compareTo(cbase) >= 0) {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(availableValue.compareTo(cbase) >= 0) {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(pendingValue.compareTo(cbase) >= 0) {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(paidValue.compareTo(cbase) >= 0) {
	KeywordUtil.markFailed("Key has incorrect value")
}

Ok I think I see an issue. Given that all values must be greater than or equal to zero (point zero zero zero etc.), update your code this way:

if(totalValue.compareTo(cbase) == -1) {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(availableValue.compareTo(cbase) == -1) {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(pendingValue.compareTo(cbase) == -1) {
	KeywordUtil.markFailed("Key has incorrect value")
}
if(paidValue.compareTo(cbase) == -1) {
	KeywordUtil.markFailed("Key has incorrect value")
}

Why? The answer is in compareTo() method description.

* Returns:

-1, 0, or 1 as this `BigDecimal` is numerically less than, equal to, or greater than `val` .

It means, if totalValue (6.98) is less than cbase (0.00), compareTo() returns -1. So it is enough to check this if you don’t care if the value is 0.00 or greater.

1 Like

@Marek_Melocik thank you for your time and help! So that’s the only way to verify if JSON key value is equal or greater than (for example 0)?

There are plenty of ways how to compare different objects, it depends what you need.

I only need to verify that JSON key value is present and is equal or greater than 0 but it seems to me that my code is redundant…

Well, you can do both verification in a single step.

if(totalValue == null || totalValue.compareTo(cbase) == -1) {
	KeywordUtil.markFailed("Key is not present or has incorrect value")
}
if(availableValue == null || availableValue.compareTo(cbase) == -1) {
	KeywordUtil.markFailed("Key is not present or has incorrect value")
}
if(pendingValue == null || pendingValue.compareTo(cbase) == -1) {
	KeywordUtil.markFailed("Key is not present or has incorrect value")
}
if(paidValue == null || paidValue.compareTo(cbase) == -1) {
	KeywordUtil.markFailed("Key is not present or has incorrect value")
}

Or - extract the verification step into a separate method.

void verifyValues(BigDecimal jsonValue) {
	BigDecimal cbase = 0.0
	if(jsonValue == null || jsonValue.compareTo(cbase) == -1) {
		KeywordUtil.markFailed("Key is not present or has incorrect value")
	}
}
verifyValues(totalValue)
verifyValues(availableValue)
verifyValues(pendingValue)
verifyValues(paidValue)
1 Like

Thanks for the method info! I know I can use OR statement. I meant isn’t there any other more simple way than using BigDecimal and compareTo() method to compare floating numbers which were gotten from JSON response?

There is always a better way how to do anything, but I am not aware of any. You’d still need to convert your JSON value to some object and compare it. Ultimately, I don’t think this is a difficult approach, I’d say it is quite easy.

1 Like

Thank you very much!

@alexfeel a different approach will be to use Json schema validation.
The field presence and value type verification is easier … and for values you can use exclusiveMinimum

https://json-schema.org/latest/json-schema-validation.html#rfc.section.6.2.5

1 Like

@Ibus thank you! I see there is more suitable minimum https://json-schema.org/latest/json-schema-validation.html#rfc.section.6.2.4 for my purposes. But how to use it in that code? Do I need to use that http://json-schema.org/implementations.html#validator-java?

I made a post here, how i did it with everit-validator:

Unfortunately the sample code was malformed when the forum has been migrated to the new platform, but the implementation is based on the doc provided on git:

java-json-tools + jackson will work too.

1 Like

Thanks! I give it a try later. I see schema looks like swagger API definition, maybe it’s possible to use that definition as validation schema?

I am not familiar with swagger definitions, but basicaly you need as follows:

  • type - to check first if the response is an object and later for each field
  • required - to check the field presence
  • properties - where you describe additional validation’s for each field.

So, putting all this together, for your case, the schema should look like:

{
	"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
		}
	}
}
1 Like

@Ibus thank you very much, it’s really good way to verify JSON responses!

I’ve used custom keywords:

package verify
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testcase.TestCaseFactory
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testdata.TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords

import internal.GlobalVariable

import MobileBuiltInKeywords as Mobile
import WSBuiltInKeywords as WS
import WebUiBuiltInKeywords as WebUI

import org.openqa.selenium.WebElement
import org.openqa.selenium.WebDriver
import org.openqa.selenium.By

import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory
import com.kms.katalon.core.webui.driver.DriverFactory

import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObjectProperty

import com.kms.katalon.core.mobile.helper.MobileElementCommonHelper
import com.kms.katalon.core.util.KeywordUtil

import com.kms.katalon.core.webui.exception.WebElementNotFoundException


class VerifyValues {
	@Keyword
	def valueIsPresent(BigDecimal jsonValue) {
		if(jsonValue == null) {
			KeywordUtil.markFailed("Key is not present")
		}
	}
	@Keyword
	def valueIsEqualOrGreaterThan(BigDecimal jsonValue) {
		BigDecimal cbase = 0.0
		if(jsonValue.compareTo(cbase) == -1) {
			KeywordUtil.markFailed("Key has incorrect value")
		}
	}
}

And my code is:

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 groovy.json.JsonSlurper
import internal.GlobalVariable as GlobalVariable

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

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

'Verify response code'

WS.verifyResponseStatusCode(response, 200)

'Set variables'

String jsonString = response.getResponseText()

JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(jsonString)

def totalValue = parsedJson.total
def availableValue = parsedJson.available
def pendingValue = parsedJson.pending
def paidValue = parsedJson.paid

'Verify if all keys are present'

CustomKeywords.'verify.VerifyValues.valueIsPresent'(totalValue)
CustomKeywords.'verify.VerifyValues.valueIsPresent'(availableValue)
CustomKeywords.'verify.VerifyValues.valueIsPresent'(pendingValue)
CustomKeywords.'verify.VerifyValues.valueIsPresent'(paidValue)

'Verify if all keys\'s values are correct'

CustomKeywords.'verify.VerifyValues.valueIsEqualOrGreaterThan'(totalValue)
CustomKeywords.'verify.VerifyValues.valueIsEqualOrGreaterThan'(availableValue)
CustomKeywords.'verify.VerifyValues.valueIsEqualOrGreaterThan'(pendingValue)
CustomKeywords.'verify.VerifyValues.valueIsEqualOrGreaterThan'(paidValue)