Forum Discussion

AlbertSoapUI's avatar
AlbertSoapUI
Contributor
7 years ago
Solved

Configurable option for generating body content in Post requests

Hi,

 

Our team works on testing a REST API. It is set up by use of a Yaml file (Swagger).

We use certain Post requests (verbs) in a lot of testcases.

Sometimes the definitions of SUT changes, and if the needed structure in a required body content in a Post request changes, we have to change a lot of teststeps in our test.

Say, for instance if for a certain verb its body content

 

{
"key": string
"otherkey": integer
"other stuff": ..
}

should be

{
"key": array of strings
"otherkey": integer
"other stuff": ..
}

then for each teststep using that verb the body content in a request must be updated

 

I want to make the setup more configurable. It should be updated with each new yaml definition.

My idea is to generate the body content for each Post request with a Groovy scripted function (to be written).

The function should be based on the yaml file.

All the key-value pairs to be used in the body content should be read from a DataSource teststep in the testcase. Each key in the DataSource block corresponds with the key in Yaml definition.

I want to write code that reads the Yaml file and generates code to generate the needed body content for any given Post request

In halfbaked pseudo code the generated code should read like this:

 

functionToCreateJsonBodyOfCertainVerb() {
jsonBody = [:]
if (corresponding property in yaml equals string property) {
def someProperty = context.expand('DataSource#someProperty')
}
if (someProperty != '') {
jsonBody['someProperty'] = someProperty
}
...
 
return JsonOutput.toJson(jsonBody)
}

Problem is that in hierarchy of json body you can have double keys

for instance

 

{
something: {
id: 'blah'
},
else: {
id: 'blahblah'
}
}

id is double here and cannot be simply read from a DataSource block with a similar key.

Is there a solution to using keys with the same name?

Or am I making it all way too complicated, and is there already a configurable solution for generating body structures for Post requests?

 

Greetings, Albert