Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

Asserting on contents of a .json response in output log & passing attribute values to propertiesstep

Hi,

 

nmrao put a script together for me to build a GET REST request's URI and query string using groovy (I cant use the OTB functionality due to other project requirements) and the GET request's response is logged in the 'log output' window.

 

I need to assert on contents of this .json response and also perhaps grab certain attribute values for property transfer.

 

I've been investigating how to do this (i was looking at the 'Assertion' test step - but I found the instructions quite confusing!) and was wondering if someone could comment on my groovy script attempt to add assertions into my script?

 

The final bit of Rao's script is as follows:

def response = client.get(path: template.make(binding) as String,
					 accept: ContentType.JSON,
					 query : queryParams
					 )
assert response.statusCode == 200

I tried adding the following assertion

assert response.header.success == "true"

(where json for the success attribute within the header object == $['header']['success'] = "true")

 

But that didnt work.

 

I tried adding into the existing script -  but adding the jsonslurper import dec at the beginning of the script and tried the following (adding the ParsedResponse variable and json path assertions)

 

def client = new RESTClient(serviceHost)
def response = client.get(path: template.make(binding) as String,
					 accept: ContentType.JSON,
					 query : queryParams
					 )
assert response.statusCode == 200
log.info groovy.json.JsonOutput.prettyPrint(response.text)

// Set up JSON parser of the response
def parsedResponse = new JsonSlurper().parseText(response);

//thought this syntax might work for json response - but it didn't
assert (parsedResponse["header"].["Success"] == "true");  
assert (parsedResponse["data"].["Links"].["method"] == "GET");
assert (parsedResponse["data"].[VersionNumber] == 1);

//also tried this syntax for the json response in the logged output - but it didn't
assert parsedResponse.header.Success == "true"
assert parsedResponse.data.Links.method == "GET"
assert parsedResponse.data.VersionNumber == 1

but the script failed before it even got to the assertions - script said "groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText()" <<- so it didnt like my scripting alteration!

 

So - as you can see my groovy is rubbish - can anyone point me in the right direction?  Essentially I need to be able to assert on the contents of the json response (in the log output window) and I also need to be able to grab attribute values and store in a properties step for later use (I need to pass the value onto a following JDBC step).

 

Oh - I've attached an example json response for clarity

 

thanks to all!

 

richie

  • Hi richie,

     

    I've modified your code in the following way:

    import groovy.json.*
    
    def response = context.expand( '${REST Request#Response}' )
    def object = new JsonSlurper().parseText(response)
    
    assert object.header.success == true
    assert object.data[0].Links.method == "GET"
    assert object.data[0].VersionNumber == 1

     

    It works fine for me:

  • Hi richie,

     

    I've modified your code in the following way:

    import groovy.json.*
    
    def response = context.expand( '${REST Request#Response}' )
    def object = new JsonSlurper().parseText(response)
    
    assert object.header.success == true
    assert object.data[0].Links.method == "GET"
    assert object.data[0].VersionNumber == 1

     

    It works fine for me:

  • nmrao's avatar
    nmrao
    Champion Level 3
    It is trivial error.

    You are expecting string "true" instead of boolean value. just remove the double quotes around true.

    You can make out the data type of the json property value. If the value is enclosed between double quotes, then it is String type. Otherwise, it could be boolean, int, double, long etc.,

    In your case


    "success" : true

    So:

    def parsedResponse = new JsonSlurper().parseText(response.text)
    assert true == parsedResponse.header.success
    • richie's avatar
      richie
      Community Hero

      Hey nmraoNBorovykh 

       

      nmrao yep - I already appreciated the datatypes and that boolean isn't a string so doesn't require the quote marks - I was just didn't notice cos I was overwhelmed with the rest of the groovy - trying to pick this stuff up and teaching myself coding from scratch on my weekends is slow going!

       

      the problem in the end wasn't the type - it was the fact that my jsonslurper line 

      def parsedResponse = new JsonSlurper().parseText(response)

      was wrong - I needed to add in the .text

      def parsedResponse = new JsonSlurper().parseText(response.text)

      All of a sudden - the star goes blue and I'm smiling! :)

       

      NBorovykh nmrao  thanks so much for all your help - you two rock! :)

       

      richie

       

       

      as

      • nmrao's avatar
        nmrao
        Champion Level 3
        No problem. You can close the thread.