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)
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.
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)
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.
@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
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: