4.10.17

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++)
{

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


//Mapping varaibles to the LIST MAP
forgivnessDataMap.put("amount",amt.toInteger())



//Eliminate the mapping of divisionnumber if its is NULL
if(div != null)
{
forgivnessDataMap.put("div",downPaymentDocuNo)
}

//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()
//POSTForgivness.setRequestContent(ForgivnessPayload)

//Get the next step
def nextStep = context.testCase.getTestStepByName("postforgivness")
//Set the new request
//nextStep.getT‌​estRequest().setRequestContent(ForgivnessPayload)
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.
file.append(ForgivnessResponseJson)
log.info(ForgivnessResponseJson)

}

No comments:

Post a Comment

How to create a new project code repo in GitLab

Pre-requisites:- 1. Account in GitLab 2. Sourcetree setup on your system 3. Project Code to push into repo Step for how to create a n...