Forum Discussion
If I was just sending the request there would be no issue, I am making reference to two specific elements in the request which need changed which is where the issue arises. I am setting a property within the test case called NextRequestJson which does not like when I change these elements.
My question is how to change these elements- postal address and email address- and not get a bad request exception.
Instead of setting the new JSON into a request property and reading from it, you could try setting the JSON directly as the request to the next test step as in the snippet below -
def nextTestStep = testRunner.testCase.testSuite.project.testSuites["Avios"].testCases["SuccessJoin"].testSteps["Properties"]
...
// def object = (build the new JSON with the updated email and postal address)
...
def jsonReqAsString = JsonOutput.toJson(object)
log.info(jsonReqAsString)
nextTestStep .setPropertyValue('Request', jsonReqAsString);
HTH!
- JackMcC109 years agoNew Contributor
Ok and what would the syntax be for building the object?
- 01streams9 years agoNew Contributor
Your script to update the JSON string is perfectly right. As I said earlier when I ran your script, it generated the updated JSON (attached below) with the new email and postal address (highlighted in bold).
{
"member": {
"person": {
"dateOfBirth": "1990-12-15",
"locale": {
"languageCode": "en"
},
"countryOfResidence": "US",
"name": {
"title": "Mr",
"familyName": "SMITH",
"middleInitial": ["L"],
"type": "MARRIED_NAME",
"firstName": "Jack"
},
"gender": "MALE",
"postalAddresses": {
"preferredPostalAddress": {
"addressLine": "339 Swords manor",
"type": "PERSONAL",
"placeType": "NOT_KNOWN",
"country": "US"
}
},
"telecomAddresses": {
"preferredTelecomAddress": {
"areaCode": "087",
"deviceType": "MOBILE",
"number": "1234560",
"type": "PERSONAL"
}
},
"emailAddresses": {
"preferredEmailAddress": {
"email": "1468941739735@aerlingus.com",
"mailboxType": "INDIVIDUAL",
"type": "BUSINESS"
}
}
}
}
}- 01streams9 years agoNew Contributor
This would be your Groovy code:
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import groovy.json.JsonBuilderdef nextTestStep = testRunner.testCase.testSuite.project.testSuites["Avios"].testCases["SuccessJoin"].testSteps["Properties"]
def username = System.currentTimeMillis()
def email = username + "@aerlingus.com"
def random = Math.abs(new Random().nextInt() % 600) + 1
def address = random + " Swords manor"
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText'''{"member":{
"person":{
"name":{
"title":"Mr",
"firstName":"Jack",
"middleInitial":[
"L"
],
"familyName":"SMITH",
"type":"MARRIED_NAME"
},
"dateOfBirth":"1990-12-15",
"gender":"MALE",
"countryOfResidence":"US",
"locale":{
"languageCode":"en"
},
"postalAddresses":{
"preferredPostalAddress":{
"type":"PERSONAL",
"placeType":"NOT_KNOWN",
"addressLine":[
"13120 River valley drive",
"Swords"
],
"country":"US"
}
},
"emailAddresses":{
"preferredEmailAddress":{
"email":"jack176405238@aol.com",
"type":"BUSINESS",
"mailboxType":"INDIVIDUAL"
}
},
"telecomAddresses":{
"preferredTelecomAddress":{
"areaCode":"087",
"number":"1234560",
"type":"PERSONAL",
"deviceType":"MOBILE"
}
}
}
}
}'''object.member.person.emailAddresses.preferredEmailAddress.email = email
object.member.person.postalAddresses.preferredPostalAddress.addressLine = addressdef jsonReqAsString = JsonOutput.toJson(object)
log.info(jsonReqAsString)
nextTestStep.setPropertyValue('Request', jsonReqAsString);
Related Content
- 5 years ago
- 3 years ago
Recent Discussions
- 7 days ago
- 12 days ago