Harrison
Harrison

Reputation: 161

Using groovy script as a value for property in SoapUI

A am not familiar with soapUI and groovy so maybe somebody can help me. I tried to use this property definition to generate unique id

${=((0..<20).collect{['a'..'z','0'..'9'].flatten()[new Random().nextInt(['a'..'z','0'..'9'].flatten().size())]}.join())}

But I get an error because this expression contains nested curly braces and I can't find the way to escape them. Maybe somebody know the way?

Thanks in advance.

Updated. I changed the script and now it works fine in script window in SoapUI.

new GroovyShell().evaluate("(0..<20).collect {[\u0027a\u0027..\u0027z\u0027,\u00270\u0027..\u00279\u0027].flatten()[new Random().nextInt ([\u0027a\u0027..\u0027z\u0027,\u00270\u0027..\u00279\u0027].flatten().size())]}.join()"

I replaced single quotes with their unicode code because of xml validation fails.

But when I try to use it as value for property, it throws and error.

${=new GroovyShell().evaluate("(0..<20).collect {[\u0027a\u0027..\u0027z\u0027,\u00270\u0027..\u00279\u0027].flatten()[new Random().nextInt ([\u0027a\u0027..\u0027z\u0027,\u00270\u0027..\u00279\u0027].flatten().size())]}.join()"}

startup failed: Script183.groovy: 1: unexpected char: 0xFFFF @ line 1, column 136.    .flatten()[new Random().nextInt([\u0027a

I would think that I have a syntax error in my script, but it throws an error only in when I use it like a property. Does anybody know how to fix this? If I could use it as property, I could make happy a lot people ;) as they just need to use the name of property and not to call any scripts.

Updated. I deleted (0..<20).collect part and now it is ok, but it generates only one symbol. So the problem somewhere here.

Upvotes: 1

Views: 1674

Answers (2)

Martin Spamer
Martin Spamer

Reputation: 5585

I use the following to generate correlation IDs.

${=java.util.UUID.randomUUID()}

Upvotes: 2

Harrison
Harrison

Reputation: 161

I have solved the problem. For some reasons it doesn't want to handle nested curly braces as they are. I replaced them with their unicode codes. The the final solution for storing this expression as a value for property is below

${=new GroovyShell().evaluate("(0..20).collect\u007B[\u0027a\u0027..\u0027z\u0027,\u00270\u0027..\u00279\u0027].flatten()[new Random().nextInt([\u0027a\u0027..\u0027z\u0027,\u00270\u0027..\u00279\u0027].flatten().size())]\u007D.join()")}

Upvotes: 1

Related Questions