1

I am getting a JSON response from an webservice like below . I want to parse all childs of results node using Groovy Json slurper and assert the value is correct.

{
   "status": "Healthy",
   "results":    [
            {
     "name": "Microservice one",
     "status": "Healthy",
     "description": "Url check MSOneURI success : status(OK)"
  },
        {
     "name": "Microservice two",
     "status": "Healthy",
     "description": "Url check MSTwoURI success : status(OK)"
 },
        {
     "name": "Microservice three",
     "status": "Healthy",
     "description": "Url check MSThreeURI success : status(OK)"
  },
        {
     "name": "Microservice four",
     "status": "Healthy",
     "description": "Url check MSFourURI success : status(OK)"
  },
        {
     "name": "Microservice five",
     "status": "Healthy",
     "description": "Url check MSFiveURI success : status(OK)"
  }
   ]
}

This is what I have done - this works .

//imports
import groovy.json.JsonSlurper
import groovy.json.*

//grab the response
def ResponseMessage = messageExchange.response.responseContent
// trim starting and ending double quotes
def TrimResponse     =ResponseMessage.replaceAll('^\"|\"$','').replaceAll('/\\/','')

//define a JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(TrimResponse)
//verify the response to be validated  isn't empty
assert !(jsonSlurper.isEmpty())


//verify the Json response Shows Correct Values 
assert jsonSlurper.status == "Healthy"
def ActualMsNames = jsonSlurper.results*.name.toString()
def ActualMsStatus = jsonSlurper.results*.status.toString()
def ActualMsDescription = jsonSlurper.results*.description.toString()


def ExpectedMsNames = "[Microservice one,Microservice two,Microservice three,Microservice four,Microservice five]"
def ExpectedMsStatus = "[Healthy, Healthy, Healthy, Healthy, Healthy]"
def ExpectedMsDescription = "[Url check MSOneURI success : status(OK),Url check MSTwoURI success : status(OK),Url check MSThreeURI success : status(OK),Url check MSFourURI success : status(OK),Url check MSFiveURI success : status(OK)]"

assert ActualMsNames==ExpectedMsNames
assert ActualMsStatus==ExpectedMsStatus
assert ActualMsDescription==ExpectedMsDescription

But I want to make it better using some kind of for loop which will parse each collection one at a time and assert the value of "name", "status" and "descriptions" at once for each child

Is that possible?

Priyanka Ray
  • 85
  • 2
  • 9
  • Just curious: If you are using SoapUI, why not use one of the built-in assertions? Seems like Content Assertion would be ideal for this. – SiKing May 11 '18 at 22:56
  • Thanks for your pointer @SiKing . We cant use the built in assertion partly because my response is in JSON and - we are using the free version of SOAP UI and it does not come with a JSON Path match content assertion. – Priyanka Ray May 11 '18 at 23:56
  • Neither does the paid version! SoapUI internally converts everything into XML. So you are able to use XPath assertions for any kind of response. – SiKing May 14 '18 at 15:34

1 Answers1

0

Yes, that's certainly possible.

Without knowing more about your actual data it's not possible to give a perfect example, but you could do something like:

jsonSlurper.results?.eachWithIndex { result, i ->
    assert result.name == expectedNames[i]
    assert result.status == expectedStatus[i] // or just "Healthy" if that's the only one
    assert result.description == expectedDescriptions[i]
}

where expectedWhatever is a list of expected result fields. If your expected results really are based on index like in your example, then you could even calculate them within the loop, but I'm guessing that's not the case for real data.

Daniel
  • 3,312
  • 1
  • 14
  • 31
  • Thanks , I have now tried this alternative using .eachWithIndex and it works perfectly. Billion thanks for the pointer – Priyanka Ray May 12 '18 at 01:26