@Ibus,
I have this keyword:
class VerifyValues {
@Keyword
def valueIsPresentAndIsEqualOrGreaterThan(BigDecimal jsonValue) {
if(jsonValue == -1.0) {
KeywordUtil.markFailedAndStop("Key is not present")
}
BigDecimal cBase = 0.0
if(jsonValue.compareTo(cBase) == -1) {
KeywordUtil.markFailedAndStop("Key has incorrect value")
}
}
}
And this verification code:
JsonSlurper slurper = new JsonSlurper()
parsedJson = slurper.parseText(jsonString)
keysToCheck = ['total', 'available', 'pending', 'paid']
keysToCheck.each { it ->
valToCheck = (parsedJson.get(it) != null) ? parsedJson.get(it) : -1.0
println("${it} : ${valToCheck}")
CustomKeywords.'verify.VerifyValues.valueIsPresentAndIsEqualOrGreaterThan'(valToCheck)
}
If my input is one of those variation all works fine:
String jsonString = '''{
"total":6.98,
"available":0.0,
"pending":-20.0,
"paid":6.98
}'''
or
String jsonString = '''{
"total":6.98,
"available":0.0,
"pendingssssssssss":0.0,
"paid":6.98
}'''
But if I use:
String jsonString = '''{
"total":6.98,
"available":0.0,
"pending":-1.0,
"paid":6.98
}'''
I get Key is not present
because it matches -1.0 in valToCheck = (parsedJson.get(it) != null) ? parsedJson.get(it) : -1.0
Well I can use some other number and hope that there will never be matches.
And if I use:
String jsonString = '''{
"total":6.98,
"available":0.0,
"pending":,
"paid":6.98
}'''
I get:
Verification FAILED Reason: groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object
In this way parsedJson.get(it) != null
is not processed correctly. I think I don’t understand something