Contributions
Re: JSON (REST) to JDBC resultset comparison
Thank you very much, I am not able to get this to work in groovy due to some compile errors but I was able to get this to work in eclipse. I willcontinue to work on finding outwhat's wrong with Groovy but thank you for this.6 years agoPlace ReadyAPI QuestionsReadyAPI Questions6KViews0likes1CommentRe: Compare 2 JSON responses in soapUIPRO/Groovy
I used the script that was provided to me as an asnwer to my older queries and I used get data to add teh JDBC resultset. The script hat was provided allowed to match to JSON responses and I was trying to match a JSON response with a JDBC resulset. This is what I have - //def String mongoResponse def String mongoResponse = context.expand( '${GetBenefitDetail_by Ben Seq#Response}' ) def String response = context.expand( '${JDBC Request#ResponseAsXml}' ) //def String response = context.expand( '${JDBC Request#policy}' ) mapper = new ObjectMapper() JsonNode jsonResponse = mapper.readTree(response); JsonNode mongoDBResponse = mapper.readTree(mongoResponse); Set<JsonPathValue> pathValues = listLeafValues(jsonResponse, '$'); Configuration configuration = Configuration.builder() .jsonProvider(new JacksonJsonNodeJsonProvider()) .mappingProvider(new JacksonMappingProvider(mapper)) .build(); pathValues.each { try { JsonPath jsonPath = JsonPath.compile(it.path); JsonNode result = jsonPath.read(mongoDBResponse, configuration); if (result != it.value) { log.info('Node with path ' + it.path + ' and value ' + it.value + ' cannot be found') } } catch (Exception e) { log.info('Node with path ' + it.path + ' and value ' + it.value + ' cannot be found') } } Set<JsonPathValue> listLeafValues(JsonNode jsonObject, String path) { if (jsonObject.isObject()) { return getValuesFrom((ObjectNode) jsonObject, path); } else if (jsonObject.isArray()) { return getValuesFrom((ArrayNode) jsonObject, path); } else { } } Set<JsonPathValue> getValuesFrom(ObjectNode jsonObject, String path) { Set<JsonPathValue> values = new LinkedHashSet<>(); Iterator<Map.Entry<String, JsonNode>> fields = jsonObject.fields(); while (fields.hasNext()) { Map.Entry<String, JsonNode> field = fields.next(); JsonNode value = field.getValue(); String nodePath = path + "." + field.getKey(); addValue(values, value, nodePath); } return values; } Set<JsonPathValue> getValuesFrom(ArrayNode array, String nodePath) { Set<JsonPathValue> values = new LinkedHashSet<>(); int index = 0; for (JsonNode value : array) { addValue(values, value, nodePath + "[" + index + "]"); index++; } return values; } def addValue(Set<JsonPathValue> values, JsonNode value, String nodePath) { if (value.isObject()) { values.addAll(getValuesFrom((ObjectNode) value, nodePath)); } else if (value.isArray()) { values.addAll(getValuesFrom((ArrayNode) value, nodePath)); } else if (value.isValueNode()) { values.add(new JsonPathValue(nodePath, value)); } }6 years agoPlace ReadyAPI QuestionsReadyAPI Questions6.7KViews0likes0CommentsRe: Convert JSON response into an array - error
What I see is that adding the JDBC resultset into an array is working right and the array is populated with values. . The JSON response after parsing (def jsluper=new JsonSlurper().parseText(jsonresponse)) is also returning values. But after I try to add the JSON values into an array and then I print the array to see what it contains - I get null, null for the JSON array. I have attached my JDBC resultset and JSON response. NOthing has changed in my script6 years agoPlace ReadyAPI QuestionsReadyAPI Questions2.1KViews0likes2CommentsConvert JSON response into an array - error
When I execute this code my json array returns [null, null]. What am I doign wrong? //DB Response def dbresponse = context.expand( '${JDBC Request#ResponseAsXml}' ) def dbsluper=new XmlSlurper().parseText(dbresponse) //Json Response def jsonresponse = context.expand( '${GetBenefitDetail_by Ben Seq#Response}' ) def jsluper=new JsonSlurper().parseText(jsonresponse) //Writing the json specfic data into an array def jsonvalues=[] jsluper.GetBenefitDetailsResult.BenefitDetails.each{ jsonvalues.add(it.key) jsonvalues.add(it.value) } jsonvalues.sort() log.info jsonvalues //Writing the DBData into array def dbvalues= [] dbsluper.ResultSet.each{ dbvalues.add(it.text()) } log.info dbvalues6 years agoPlace ReadyAPI QuestionsReadyAPI Questions2.1KViews0likes4CommentsJSON (REST) to JDBC resultset comparison
I want to compare all the elements of a JSON REST response to corresponding elements in the JDBC resultset. I have a working solution when I need to compare specific tag values but now I have a large nested REST response which I want to compare with corresnponding JDBC resultset. My JDBC response has all the values corresponding to the tags in teh REST response. I dont want to pick and choose the values that I want to compare because I want to look at all the tags returned by the JSOn response. Sample JDBC and JSON response attachedSolved6 years agoPlace ReadyAPI QuestionsReadyAPI Questions6KViews0likes8CommentsRe: Decimal values in JDBC and JSON
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 == jsonDataObjects6 years agoPlace ReadyAPI QuestionsReadyAPI Questions2.5KViews0likes0CommentsRe: Decimal values in JDBC and JSON
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 oneinstanceof(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 tagmode_premium exists several times in the JDBC andwithin thenested 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.6 years agoPlace ReadyAPI QuestionsReadyAPI Questions2.5KViews0likes2CommentsRe: Compare 2 JSON responses in soapUIPRO/Groovy
Once again thank you for the solution. This works perfectly on JSON-JSON compare but when I set the String response = JDBC responseasXML then I get a jackson parser error. I am unable to find out where exactly is the issue. What are some of the things I should be looking at to resolve this issue? Error Message - com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'CSP001A': was expecting ('true', 'false' or 'null') at [Source: (String)"CSP001A"; line: 1, column: 15] error at line: 196 years agoPlace ReadyAPI QuestionsReadyAPI Questions6.7KViews0likes2CommentsRe: Decimal values in JDBC and JSON
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 == jsonDataObjects6 years agoPlace ReadyAPI QuestionsReadyAPI Questions2.5KViews0likes4CommentsDecimal 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?Solved6 years agoPlace ReadyAPI QuestionsReadyAPI Questions2.6KViews0likes6Comments