Forum Discussion

roja1's avatar
roja1
Contributor
5 years ago

How to handle Unparsable JSON string in groovy

Hi,

 

I have an input excel where I'm passing the expected output to all my test steps.I have used JSONAssert.assertEquals to compare the response from soapUI with the expected output. Some of my test steps doesn't give any json body. In that case I'm getting Unparsable JSON string error.Below is the code which I have used. Please suggest me on how to handle this

 

import static java.nio.charset.StandardCharsets.*;
import java.lang.*;
import com.eviware.soapui.model.testsuite.Assertable.AssertionStatus
import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep
import com.eviware.soapui.support.XmlHolder
import jxl.*
import jxl.write.*
import org.skyscreamer.jsonassert.JSONAssert
import org.skyscreamer.jsonassert.JSONCompareMode
import groovy.json.JsonSlurper
def count=0,i=1,j=1,k=1
def inputFileName,outputFileName
def inputFile = testRunner.testCase.testSuite.getPropertyValue("inputFileName")
def outputFile = testRunner.testCase.testSuite.getPropertyValue("outputFileName")
def testStep = testRunner.testCase.testSteps["Properties"]

Workbook inputWB = Workbook.getWorkbook(new File(inputFile))
Sheet inputSheet = inputWB.getSheet(0)
no_of_rows= inputSheet.getRows().toInteger()

Workbook existingInputWB=Workbook.getWorkbook(new File(inputFile))
WritableWorkbook outputWB=Workbook.createWorkbook(new File(outputFile),existingInputWB)

testCaseName=testRunner.testCase.name
outputWB.createSheet(testCaseName,2)
WritableSheet sheet_writable = outputWB.getSheet(testCaseName)
jxl.write.Label testStepCell = new jxl.write.Label(0,0 ,"Test Step")
jxl.write.Label reqUrlCell = new jxl.write.Label(2,0 ,"Request URL")
jxl.write.Label reqPayloadCell = new jxl.write.Label(3,0 ,"Request Payload")
jxl.write.Label responseCell = new jxl.write.Label(4,0 ,"Response")
jxl.write.Label statusCell = new jxl.write.Label(1,0 ,"Status")
jxl.write.Label reasonCell = new jxl.write.Label(5,0 ,"Reason")
sheet_writable.addCell(testStepCell)
sheet_writable.addCell(reqUrlCell)
sheet_writable.addCell(reqPayloadCell)
sheet_writable.addCell(responseCell)
sheet_writable.addCell(statusCell)
sheet_writable.addCell(reasonCell)
(0..context.testCase.testStepCount-1).each{
def step = context.testCase.testStepList[it]
if ( step instanceof RestTestRequestStep) {
jxl.write.Label stepName = new jxl.write.Label(0,i ,step.name)
sheet_writable.addCell(stepName)
def tr=testRunner.testCase.getTestStepByName(step.name)
def String endPointUrl= tr.getHttpRequest().getResponse().getURL()
jxl.write.Label reqUrl = new jxl.write.Label(2,i ,endPointUrl)
sheet_writable.addCell(reqUrl)
def payload = context.expand(step.getPropertyValue('Request'))
jxl.write.Label reqPayload = new jxl.write.Label(3,i ,payload)
sheet_writable.addCell(reqPayload)
def response = context.expand(step.getPropertyValue('Response'))
jxl.write.Label reqResponse = new jxl.write.Label(4,i ,response)
sheet_writable.addCell(reqResponse)
count=count+1
i=i+1
}
}

(0..context.testCase.testStepCount-1).each{
def step1 = context.testCase.testStepList[it]
if ( step1 instanceof RestTestRequestStep) {
for(j=1;j<no_of_rows;j++){
Cell f=inputSheet.getCell(0,j)
step_name=f.getContents()
Cell f1=inputSheet.getCell(3,j)
def String step_response=f1.getContents()
step_response=step_response.replaceAll(" ","")
def String response = context.expand(step1.getPropertyValue('Response'))
response=response.replaceAll(" ","")
if(step1.name==step_name&&step_response!=null){
try{
JSONAssert.assertEquals(step_response,response,false)
jxl.write.Label status = new jxl.write.Label(1,k ,"Passed")
sheet_writable.addCell(status)
}catch(AssertionError e){
log.info "${e}"
String exception = e.toString()
exception=exception.replace("java.lang.AssertionError:","")
jxl.write.Label status = new jxl.write.Label(1,k ,"Failed")
sheet_writable.addCell(status)
jxl.write.Label reason = new jxl.write.Label(5,k ,exception)
sheet_writable.addCell(reason)
}
k=k+1
}

}
}
}
outputWB.write()
outputWB.close()
existingInputWB.close()

1 Reply

  • pb1's avatar
    pb1
    Contributor

    I'd use the following function to validate json

     

    import groovy.json.JsonSlurper
    
    Boolean isValidJson(jsonString){
    	result = true
    	try{
    		new JsonSlurper().parseText(jsonString)
    	}
    	catch(Exception e){
    		result = false
    	}
    	return result
    }

    And then call the validation before passing the values to assertEquals:

    assert isValidJson(response) == true
    assert isValidJson(step_response) == true
    JSONAssert.assertEquals(step_response,response,false)

    Instead of asserting it to true, you can also just use an if() to handle the malformatted json as you see fit.