SoapUI Groovy: Creating valid JSON from Test Case Properties

Examples of creating valid JSON through SoapUI test case properties


Here’s some examples of how you can take values passed in from datasources (like Excel) and make them into valid JSON on the fly. The variable ‘request’ in each script is the test step that you are writing the JSON to.

Most basic form, create a map, convert the map to JSON
import groovy.json.*

//properties
def request = testRunner.testCase.getTestStepAt(4).getTestRequest()
def tcProps = testRunner.testCase.getProperties()

def map  = [:]

for(p in tcProps){		
 	if (p.value.getValue() != "") {
 		map.put(p.key, p.value.getValue())  					
	}
}


//create valid JSON
def builder =  new JsonBuilder(map)
def json = builder.toPrettyString()
request.setRequestContent(json)
Create an object from your properties
import groovy.json.*

//properties
def request = testRunner.testCase.getTestStepAt(4).getTestRequest()
def tcProps = testRunner.testCase.getProperties()

def map = [:]

for(p in tcProps){		
 	if (p.value.getValue() != "") {
 		map.put(p.key, p.value.getValue())  					
	}	
}


//create valid JSON
def builder =  new JsonBuilder()

def root = builder.SomeObject {	
	map.each() {key,value -> "${key}" "${value}"}
}

def json = builder.toPrettyString()
request.setRequestContent(json)
Take an object which is already in the properties (so you’ve already written the JSON out) and just put it to the request
import groovy.json.*

//properties
def request = testRunner.testCase.getTestStepAt(4).getTestRequest()
def tcProps = testRunner.testCase.getProperties()

def map  = [:]

for(p in tcProps){		
 	if (p.value.getValue() != "") {
 		map.put(p.key, p.value.getValue())		
	}	
}

//create valid JSON
def builder =  new JsonBuilder()

def root = builder {
	MyObject {
		map.each() {key,value -> "${key}" "${value}"}
}

def json = builder.toPrettyString()
//need to do this if using latest Groovy or else it'll escape all your quotes, etc
json = StringEscapeUtils.unescapeJava(json)
request.setRequestContent(json)
Create int arrays as valid JSON

This uses the method dropWhile and takeWhile which were added in 1.8.7 of Groovy (note: this is also the version that fixes the bug that then breaks the passing of objects so you need to unescape the json) and the function called simplisticParse, which I didn’t write, that credit goes to Tim Yates and saved me so much time.

import groovy.json.*
//properties
def request = testRunner.testCase.getTestStepAt(4).getTestRequest()
def tcProps = testRunner.testCase.getProperties()
def map  = [:]

//grab all the properties from the test case and put them in a map as long as they aren't empty or contain CD
for(p in tcProps){		
 			if (p.value.getValue() != "") { 
 			map.put(p.key, p.value.getValue()) 				
	}	
}

//create valid JSON
def builder =  new JsonBuilder()
def root = builder {
	map.each() {key,value -> 
	if (key.equals('IntArrayKey1') || key.equals('IntArrayKey2') || key.equals('IntArrayKey3')) {
		value = simplisticParse(value, Integer)
	"${key}" value
	}else {
		"${key}" "${value}"
	} 				
	}
}

def json = builder.toPrettyString()
request.setRequestContent(json) 

//parser for fixing JSON
def simplisticParse( String input, Class requiredType ) {
  input.dropWhile { it != '[' }
       .drop( 1 )
       .takeWhile { it != ']' }
       .split( ',' )*.asType( requiredType )
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s