SoapUI: add custom query param to all service requests

I had a busy day today with SoapUI 🙂

Another script for you guys, will add/remove query parameter to all service resources.

import com.eviware.soapui.impl.rest.RestResource

def project = testRunner.testCase.testSuite.getProject()
List<RestResource> ops = project.getInterfaces()["your_service_name"].getOperationList()

String paramName = "debug"
String paramValue = "true"
int addCnt = 0;
int remCnt = 0;
int errCnt = 0;
int noOfOper = ops.size()

for (RestResource res : ops)
{
	res.each{
		oper -> 
			Boolean result = toggleDebug(oper, paramName, paramValue)
			if ( result == null) 
				{ 
					errCnt++; 
				}
			else if (result) 
				{ 
					remCnt++; 
				}
			else if ( !result )
				{
					addCnt++;
				}
	}
}

log.info "DEBUG parameter was ADDED: " + addCnt + " times, REMOVED: " + remCnt + " times. Errors: " + errCnt + ". Number of all srvs operations: " + noOfOper

public Boolean toggleDebug(RestResource oper, String paramName, String paramValue)
{
		if ( oper.getProperties().containsKey(paramName) )
		{
			oper.removeProperty(paramName)
			log.info "DEBUG mode query param was REMOWED from operation: '"  + oper.getName() + "'"
			return true;
		}
		else
		{
			oper.addProperty(paramName)
			oper.getProperties()[paramName].setDefaultValue(paramValue)
			log.info "DEBUG mode query param was ADDED to operation: '"  + oper.getName() + "' with value: " + paramValue
			return false;
		}
		return null;
}

SoapUI: switching between different project configurations

Woohoo!

Today I’ve got another SoapUI script, that can be used to quickly change between project configurations for different environments, i.e.: DEV, STAGING, PROD etc.

Before you start executing the script below, create two text files in the same dir as your SoapUI project file, named:
DEV.properties
STAGE.properties

Once you’re done, execute the script.
In result it will create/update project property called “MainEdnpoint”.

Later on, you can take advantage of such property by replacing all the service endopoints by only one value:
${#Project#MainEdnpoint}
This way, all your request will point at the host you want to currently run test against. This is very simple & flexible way to manage you scripts for all different environments.

Same way you can add another property, like: “serviceContext” which will point at current context path independent from any changes made by devs/ops
To use this property, just change you service request path:
from i.e: /service-v0.1/listPosts
to: /${#Project#serviceContext}/listPosts

String env = "DEV"
def filename = context.expand( '${projectDir}'+File.separator+File.separator+env+'.properties' )
def file = new java.io.File( filename )

if( file.exists() )
{
	def props = new java.util.Properties()
	props.load( new java.io.FileReader( file ))

	def propsMap = [:]

	propsMap.put("MainEdnpoint", props.get( "MainEdnpoint" ) )
	// add your subsequent property names here

	propsMap.each() {
		key, value ->
		checkAndSetProperty(key, value)
	}
}
else log.info( "Missing [" + filename + "]" )


public void checkAndSetProperty(def propName, def prop)
{
	// get test project object
	def project = testRunner.testCase.testSuite.getProject()

	if( prop != null && prop.trim().length() > 0 ) 
	{
		project.setPropertyValue( propName , prop )
	}
	else
	{
		log.info( "Property " + propName + " in configuration file has an empty value. Project property was cleared!!!!" )
		project.setPropertyValue( propName , "" )
	}
}

SoapUI: change paths for all REST service resources

Phew…
Finally I’ve managed to find a way to change all the resource paths in SoapUI’s REST service resources.

This solution can be helpful if you need to quickly add/change path parameter(s) to/in all service requests.

Quick explanation: Script below adds/removes a “?debug” or “&debug” param to all resource defined in your REST service.

import com.eviware.soapui.impl.rest.RestResource

def project = testRunner.testCase.testSuite.getProject()
String restServiceName = "your_rest_service_name"
List<RestResource> ops = project.getInterfaces()[restServiceName].getOperationList()

for (RestResource res : ops)
{
	res.each{
		op -> toggleDebug(op)
	}
}

public void toggleDebug(RestResource op)
{
	String path = op.getFullPath();

	if ( path.contains("&debug") || path.contains("?debug") )
	{
		if (path.contains("&debug"))
			{
				path = path.replaceAll("&debug","")
			}
		if (path.contains("/?debug"))
			{
				path = path.replaceAll("\\?debug","")
			}
		op.setPath(path)
		log.info "DEBUG mode DISABLED for operation: '" + op.getName() + "'. New path: "  + path
	}
	else
	{
		if (path.contains("?"))
			{
				path += "&debug"
			}
		else
			{
				path += "?debug"
			}
		op.setPath(path)
		log.info "DEBUG mode ENABLED for operation: '" + op.getName() + "'. New path: "  + path
	}
}