SoapUI Groovy: Splitting out your scripts

Please see updated post here for a simpler solution to passing the data in. I still use the script library for holding all my scripts.

In previous examples I used if/else statements to separate my test case property data. I also used the same scripts over and over to parse arrays, etc. As JSON calls become more complex you need a new way to create the JSON on the fly while keeping everything legible. To accomplish this I combined the method described in my post about creating objects (here) with a switch statement and some reworking of the overall code.

Lets say we have a JSON call that needs to look like this:

{
    "Object1" : 
    {
        "Object2" : [
            {    
                "Something" : 1,                
                "Something2" : "string",                
                "Something3" : ["strings"]
            }],        
        "Object3" : [
            {
                "Something" : "string",                
                "Something2" : 1,                
                "Something3" : true
            }],        
        "Something" : "string",        
        "Something2" : 1,        
        "Something3" : true      
    }
}

As passed through Excel spreadsheets with 3 columns (Object1, Object2, and Object3), my test case properties look like this:

objectProperties

My ‘main’ groovy script which is called from SoapUI to create the JSON:

package qa

public class CreateObject {

	def createJSON(testRunner, testStepLocation) {

		//properties
		def request = testRunner.testCase.getTestStepAt(testStepLocation).getTestRequest()
		def tcProps = testRunner.testCase.getProperties()
		def object1 
		def object1Map = [:]
		def object2
		def object2Map = [:]
		def object3 
		def object3Map = [:]
				
		//grab objects
		for (p in tcProps) {		
			if (p.value.getValue() != "") {		
				switch(p.key) {
			 
				case 'Object1' :
					object1 = p.value.getValue()
					object1Map = new Tools().fixObjectToMap(object1)	
					break

				case 'Object2' :
					object2 = p.value.getValue()
					object2Map = new Tools().fixObjectToMap(object2)	
					break
			
				case 'Object3' :
					object3 = p.value.getValue()
					object3Map =  new Tools().fixObjectToMap(object3)	
					break
				
				default: 'Default'			
				}
			}
		}	
		//set request json
		def c = new Object().Create(object1Map,object2Map,object3Map)		
		request.setRequestContent(c)		
	}			
}

Some of the ‘Object’ script:

package qa

public class Object {

	//create 
	def Create(object1Map,object2Map,object3Map) {

		def builder = new groovy.json.JsonBuilder()				
		def r = builder.Object1 {		
			if (object2Map.size() != 0) {
				Something3([{
					object2Map.each() {
								key,
								value ->
									if (key.equals('Something3')) {
										value = new Tools().simplisticParse(value, String)
										"${key}" value
									} else {
										"${key}" "${value}"
									}
								}	
				}])
			}
			if (object3Map.size() != 0 ) {
					Object3([object3Map]) 
			}
			if (object1Map.size() != 0) {
					Object1([object1Map])
			}				
		}		
		def json = builder.toPrettyString()
		json = groovy.json.StringEscapeUtils.unescapeJava(json)
		return json
	}	

The ‘Tools’ script referenced above:

package qa

public class Tools {

	//parser for converting strings to arrays
	def simplisticParse(String input, Class requiredType) {
		input.dropWhile {
			it != '['
		}
		.drop(1)
		.takeWhile {
			it != ']'
		}
		.split(',') *.asType(requiredType)
	}
	
	//take what was given and take out the tabs and then make a map from it
	def fixObjectToMap(original) {			
		original = original.replaceAll("\\r?\\n","")			
		def result = original.split('&').inject([:]) { map, token ->
		token.split('=').with { map[it[0].trim()] = it[1] }
		map
		}
		return result
	}	
}

Important things to note:
– All of these scripts reside in the same directory so can be referenced in the form of ‘X_file().Y_method’
– The ‘Object’ script actually contains all the different methods for creating JSON so that’s just a small section of what your overall script could look like

Advertisements