Groovy Script for reading json file and post to REST API Request in SOAPUI

//Import groovy JSON
import groovy.json.*

//Read the JOSN File and parse the json arrays
def fileContent = new File('C:\\Office\\records_JSON.json').text
def json = new JsonSlurper().parseText(fileContent)

//opening new file for storing the REST API Response.
File file = new File("C:\\Office\\Response.txt")

//Count the number of records
def lt = json.size()

//Preparing the list of arrays
//def forgivnessMap = [:]
def forgivnessDataMap = [:]

//Loop to POST Forgivness request for number of records
for (i = 0; i <lt; i++)

//Mapping the array values to the vaiables
def amt = json[i].amount
def div = json[i].division

//Mapping varaibles to the LIST MAP

//Eliminate the mapping of divisionnumber if its is NULL
if(div != null)

//Preapring the JSON Payload for forgivness
def ForgivnessPayload = new JsonBuilder(forgivnessDataMap).toPrettyString()

log.info("forgivnessBody ="+ForgivnessPayload)

//Calling the POST Forgivness API for each record in the JSON File.
//def POSTForgivness = testRunner.testCase.getTestStepByName("postforgivness").getTestRequest()

//Get the next step
def nextStep = context.testCase.getTestStepByName("postforgivness")
//Set the new request
nextStep.httpRequest.requestContent = ForgivnessPayload
//run next step
nextStep.run(testRunner, context)

//Reading the response after the POST Forgivness request
def ForgivnessResponse = testRunner.testCase.testSteps["postforgivness"].testRequest.response.contentAsString
def ForgivnessResponseJson = new JsonSlurper().parseText(ForgivnessResponse)

//saving the ForgivnessResponse into textfile.


No comments:

Post a Comment