Forum Discussion

Satish_1's avatar
Satish_1
New Contributor
8 years ago

How to read and update json messagein Groovy script via SoapUI NG pro

Hi I am new to SoapUI pro NG tool and Groovy scripting.  

I have to perform below steps in my project suite. I have use POST or PUT method so that json message will be sent to server and get the response. The excel sheet will be my data sink here.

1. Read sample Json message 

2. update the json message attribute values

3. post the message to server

4. Capture the response of json message and validate for any error.

If someone can provide the basic steps for writing the code in Groovy scripting I don't have idea what all package i need to associate while running groovy step.

Sample Json message

{
  "type":"HAM",
  "hotelCode": "HHHHHH",
  "lastUpdatedUser": "TOGAF003",
  "hotelAttributeItems": [
    {
    "type": "HAM",
    "hotelCode": "HHHHHH",
    "PMSAttributeCode": "KN",
    "PMSAttributeName": "KingBed",
    "DSPCategoryCode": 2,
    "positionInCategory": 2,
    "DSPItemCode": "MTED0001",
    "attributeName": "1KingBed",
    "isDSPSellable": "Y",
    "allowRequestOnly": "Y",
    "allowInGRT": "Y",
    "zeroCharge": "N",
    "isPremium": "N",
    "isGuestViewable": "N",
    "isActive": "Y",
    "remarks": "testing one."
}
  ]
}

Thank you in advance.

Satish

3 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    You provided some json. But not sure what is the expectation out of that?
    • Satish_1's avatar
      Satish_1
      New Contributor

      Hello Rao,

      Thanks for your reply.

      I gave sample json message that I would like to read and update via groovy scripting in SoapUI pro. I am using SoapUI NG pro licensed tool. 

      Can you please provide me some links where mentioned to read json message, what package to be included while running etc.

       

      Thanks,

      SL

      • New2API's avatar
        New2API
        Frequent Contributor

        Satish, below script should provide you some direction. In this example, previous step is a REST Call and it uses it's response to use as a request for another REST. 

         

        import com.eviware.soapui.support.XmlHolder
        import com.eviware.soapui.support.types.StringToObjectMap
        import net.sf.*
        import net.sf.json.*
        import net.sf.json.groovy.*
        import groovy.json.JsonSlurper
        import com.eviware.soapui.support.GroovyUtils
        import groovy.json.JsonBuilder


        //## Get previous test step name //
        def currentStepInd = context.currentStepIndex
        def TestStepName = testRunner.testCase.getTestStepAt(currentStepInd - 1).name  //Assuming REST request is your prior step


        //## Parse json response ##
        def response = context.expand( '${'+TestStepName+'#Response}' ).toString()
        def json = new JsonSlurper().parseText response

        //##Update JSON Nodes ##//
        def jsonbuilder = new JsonBuilder(json)
        jsonbuilder.content.status = 'Active' // Assuming 'Status' is one of json node


        //## Build json body and write to a file as string ##//
        def req = jsonbuilder.toString().replaceAll(~/\\n/,"\\n").replaceAll(~/\\r/,"\\r")  //escape char handler if needed

        //##Update TestCase Property to contain parsed json ##//
        testRunner.testCase.testSuite.setPropertyValue("JSONBODY", "${req}")

         

        in the next REST step call this JSON  using  - ${#TestSuite#JSONBODY}

         

        for Excel reading you could refer to below link-

        http://www.techiethoughts.com/technical-posts/soap-ui/how-to-read-data-from-excel-sheet-and-store-it-in-an-array-list-to-process-them-using-groovy-script..html

         

        if you store your JSON in a text file then you also use above like this

         

        //## READ canned request from a file to modify ##//
        def JSON = new File("C:\Requests\JsonBody.txt").text
        def json= new JsonSlurper().parseText JSON 

         

        Hope this helps!