Decimal values in JDBC and JSON
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Decimal values in JDBC and JSON
I am trying to compare 2 responses - one JDBC and other json. However, the decimal values in my JDBC show up like this:
<NUMBER_OF_UNITS>0.00000</NUMBER_OF_UNITS>
<MODE_PREMIUM>1200.00</MODE_PREMIUM>
<FACE_AMOUNT>50000.00</FACE_AMOUNT>
and the JSON shows up like this:
"NumberOfUnits" : 0.0,
"ModePremium" : 1200.0,
"FaceAmount" : 50000.0,
How can I compare these fields since the decimal places do not match?
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
(Edit: Sorry for the initial blank reply, my mistake)
Your assertion is matching the string values, thus they are different. If you are only interested in the actual values I would add a Script Assertion to your request with something like this:
// Obtain your values from the respective test steps. def jdbcText = '1200.00' def jsonText = '1200.0' // Convert to a numerical value. def jdbcValue = new BigDecimal(jdbcText) def jsonValue = new BigDecimal(jsonText) assert jdbcValue == jsonValue
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you - this works when I add the values that I want to convert to text as a variable and then do an assert check. But my script takes each row in the JDBC response/JSON response and compares them together. I am unsure where should I convert these values? This is how my script looks today -
I tried few ways but I received the - java.math.BigDecimal(groovy.util.slurpersupport.NodeChildren error.
class Model {
def buildjdbcData(row){
def FIELD7 = row.MODE_PREMIUM
return FIELD7
}
def buildJsonData(tagInfo){
def FIELD7 = tagInfo.ModePremium
return FIELD7
}
}
def jdbcResponse = context.expand( '${JDBC Request#ResponseAsXml}' )
def results = new XmlSlurper().parseText(jdbcResponse)
def jdbcDataObjects = []
results.ResultSet.Row.each { row ->
jdbcDataObjects.add(new Model().buildjdbcData(row))
}
log.info "JDBC "+ jdbcDataObjects
def restResponse = context.expand( '${Get Benefit summary#Response}' )
def arrayOfTagInfo = new JsonSlurper().parseText(restResponse)
def jsonDataObjects = []
arrayOfTagInfo.GetBenefitSummaryResult.getBenefitSummaryRes.each{ tagInfo ->
jsonDataObjects.add(new Model().buildJsonData(tagInfo))
}
log.info "JSON "+jsonDataObjects
if (jdbcDataObjects.size() != jsonDataObjects.size()) {
System.err.println("Jdbc resultset size is : ${jdbcDataObjects.size()} and Json result size is : ${jsonDataObjects.size()}")
}
assert jdbcDataObjects == jsonDataObjects
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm finding it a little difficult to understand exactly what you are trying to achieve, but I think that the following amendment to the initial "Model" class should give you what you want. Note I've added a null check using the ternary operator (taking advantage of Groovy truth) so the behaviour of null values and empty strings will be the same as before (and stopping the BigDecimal constructor throwing an error).
class Model { def buildjdbcData(row){ def FIELD7 = row.MODE_PREMIUM ? new BigDecimal(row.MODE_PREMIUM) : row.MODE_PREMIUM return FIELD7 } def buildJsonData(tagInfo){ def FIELD7 = tagInfo.ModePremium ? new BigDecimal(tagInfo.ModePremium) : tagInfo.ModePremium return FIELD7 } }
Note: The above code always assumes that the "mode premium" values are valid number, should it be possible for it to be not a number then you should add further checking code.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
For some reason I still get the error - groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.math.BigDecimal(groovy.util.slurpersupport.NodeChildren) error at line: 54
I tried this script only by using getData and just comapring one instance of (MODE_PREMIUM) for both JDBC and JSON. It worked and I didn't see any errors at that time. The minute I parse all the values for MODE_PREMIUM from the JBDC to build my array I get the error mentioned above. The tag mode_premium exists several times in the JDBC and within the nested JSON. Each tag has a different value for mode premium. So when I build my JDBCDatabObject set I parse all the values for mode_premium into that set. Similarly, for JSONDataObject set.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Without seeing your full JDC & JSON responses I'm not sure what more advice I can give. I recomend that you read up on the XMLSlurper (to understand how it returns GPaths) and the the JsonSlurper (to understand how returns Maps and/or Lists).
I did notice that the NodeChildren object has a text() method. You could try to see what this gives you, I've also added some log statements to help debugging:
class Model { def buildjdbcData(row){ log.info('Calling buildjdbcData') log.info(row.MODE_PREMIUM.text()) def FIELD7 = row.MODE_PREMIUM ? new BigDecimal(row.MODE_PREMIUM.text()) : row.MODE_PREMIUM return FIELD7 } def buildJsonData(tagInfo){ log.info('Calling buildJsonData') log.info(tagInfo.ModePremium.text()) def FIELD7 = tagInfo.ModePremium ? new BigDecimal(tagInfo.ModePremium.text()) : tagInfo.ModePremium return FIELD7 } }
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you very much. I played around with this and now it is working. This is my final script. Thank you very much.
class Model {
def buildjdbcData(row){
//log.info('Calling buildjdbcData')
//log.info(row.MODE_PREMIUM.text())
def FIELD7 = row.MODE_PREMIUM ? new BigDecimal(row.MODE_PREMIUM.text()) : row.MODE_PREMIUM
def FEILD6 = row.NUMBER_OF_UNITS ? new BigDecimal(row.NUMBER_OF_UNITS.text()) : row.NUMBER_OF_UNITS
return FIELD7
return FILED6
}
/**
* this will accept the json TagInfo
* @Param tagInfo
* @return
*/
def buildJsonData(tagInfo){
//log.info('Calling buildJsonData')
//log.info(tagInfo.ModePremium.text())
def FIELD7 = tagInfo.ModePremium ? new BigDecimal(tagInfo.ModePremium) : tagInfo.ModePremium
def FIELD6 = tagInfo.NumberOfUnits ? new BigDecimal(tagInfo.NumberOfUnits) : tagInfo.NumberOfUnits
return FIELD7
return FIELD6
}
}
def jdbcResponse = context.expand( '${JDBC Request#ResponseAsXml}' )
def results = new XmlSlurper().parseText(jdbcResponse)
//def jdbcObjects = []
def jdbcDataObjects = []
results.ResultSet.Row.each { row ->
jdbcDataObjects.add(new Model().buildjdbcData(row))
}
//log.info "JDBC Response -> "+ results
log.info "JDBC "+ jdbcDataObjects
//def jsonText
def restResponse = context.expand( '${Get Benefit summary#Response}' )
//def json = new JsonSlurper().parseText(jsonText)
//log.info(json)
def arrayOfTagInfo = new JsonSlurper().parseText(restResponse)
def jsonDataObjects = []
//log.info "JSON Response -> "+arrayOfTagInfo
//def json = new JsonSlurper().parseText(jsonText)
arrayOfTagInfo.GetBenefitSummaryResult.getBenefitSummaryRes.each{ tagInfo ->
jsonDataObjects.add(new Model().buildJsonData(tagInfo))
}
log.info "JSON "+jsonDataObjects
//jdbcDataObjects.sort()
//jsonDataObjects.sort()
if (jdbcDataObjects.size() != jsonDataObjects.size()) {
System.err.println("Jdbc resultset size is : ${jdbcDataObjects.size()} and Json result size is : ${jsonDataObjects.size()}")
}
assert jdbcDataObjects == jsonDataObjects
