Read YAML components from variable in groovy - groovy

I have a code like below
import groovy.yaml.YamlSlurper
def configYaml = '''\
---
application: "Sample App"
users:
- name: "mrhaki"
likes:
- Groovy
- Clojure
- Java
- name: "Hubert"
likes:
- Apples
- Bananas
connections:
- "WS1"
- "WS2"
'''
// Parse the YAML.
def config = new YamlSlurper().parseText(configYaml)
def data1 = "users"
def date2 = "name"
println config.users.likes // printing correct
println config.$data1.$data2 // getting error
I need to print elements from users.name and I need to print it from variable form.
like "println config.$data1.$data2"

You can do something like below.
def config = new YamlSlurper().parseText(configYaml)
def data1 = "users"
def data2 = "name"
println config.users.likes
println config."$data1"."$data2"

Related

Update a Map in groovy spock framework

I have the below spock specification and want to update the map from data table. Can some body help achieve this
def "groovy map update"() {
setup: "step1"
Map json = [
user :[
name : 'ABC'
]]
when: "step2"
println ('Before modification:')
println (json)
then: "step3"
json.with {
//user.name = value // this one works
(field) = value // this one does not work
}
println ('After modification:')
println (json)
where:
field | value
'user.name' | 'XYZ'
}
The then section is intended for asserts and not for updates etc. So you have to update the map in the when section and then test the result in the then section. For example like this:
def "groovy map update"() {
setup: 'create json'
Map json = [user: [name: 'ABC']]
when: 'update it'
def target = json
for (node in path - path.last()) {
target = target[node]
}
target[path.last()] = value
then: 'check the assignment'
json.user.name == value
where:
path | value
['user', 'name'] | 'XYZ'
}
One way how to update nested Map value can be by using list of path nodes instead of field notation and then iterate over them to obtain the last Map instance and set the value there:
def target = json
for (node in path - path.last()) {
target = target[node]
}
target[path.last()] = value
The accepted solution is correct, I just want to show an alternative doing the same in a slightly different way, assuming you want to stick with the dotted notation for field in your where: block. I just added two more test cases in order to make sure it works as expected.
#Unroll
def "set #field to #value"() {
setup: 'create json'
Map json = [user: [name: 'ABC', address: [street: '21 Main St', zip: '12345', city: 'Hometown']]]
when: 'update it'
def subMap = json
field.split("[.]").each {
if (subMap[it] instanceof Map)
subMap = subMap[it]
else
subMap[it] = value
}
println json
then: 'check the assignment'
json.newField == value ||
json.user.name == value ||
json.user.address.zip == value
where:
field | value
'newField' | 'dummy'
'user.name' | 'XYZ'
'user.address.zip' | '98765'
}
Update: If you want to save a few lines of code you can also use a fold (or reduce or accumulate) operation via inject(..) as described here
#Unroll
def "set #field to #value"() {
setup: 'create json'
Map json = [user: [name: 'ABC', address: [street: '21 Main St', zip: '12345', city: 'Hometown']]]
when: 'update it'
field.split("[.]").inject(json) { subMap, key ->
subMap[key] instanceof Map ? subMap[key] : subMap.put(key, value)
}
println json
then: 'check the assignment'
json.newField == value ||
json.user.name == value ||
json.user.address.zip == value
where:
field | value
'newField' | 'dummy'
'user.name' | 'XYZ'
'user.address.zip' | '98765'
}
Whether you find that readable or not may depend on your familiarity with topics like functional programming in general or map/reduce in particular. The charm here in addition to brevity is that we no longer need a local variable outside of our closure but we just inject (hence the method name) the result of iteration n to iteration n+1.
BTW, as a nice side effect inject(..) as I am using it here returns the previous value of the value you set or overwrite. Just add println in front of field.split("[.]").inject(json) ... in order to see it.
Update 2: Please note that both variants only work if there is no existing field value of type Map in the target field because of the instanceof Map check heuristics in my code. I.e. these two cases would not work:
'user.address' | [street: '23 Test Blvd', zip: '33333', city: 'Somewhere']
'user.address' | '23 Test Blvd, 33333 Somewhere'
This one would work, though, because there is no preexisting value:
'user.alternativeAddress' | [street: '23 Test Blvd', zip: '33333', city: 'Somewhere']

looping a template using groovy GStringTemplateEngine()

My requirement is to create a template engine to support a looping in it.
The final template should look something like this:
#cat output.template
env:
- name : param1
value : 1
- name : param2
value : 2
I have pseudo code to explain my requirement
def f = new File('output.template')
def engine = new groovy.text.GStringTemplateEngine()
def mapping = [
[ name : "param1",
value : "1"],
[ name : "param2",
value : "2" ]
] // This mapping can consists of a multiple key value pairs.
def Template = engine.createTemplate(f).make(mapping)
println "${Template}"
Can someone help me how to achieve this requirement of looping inside the templates and how should I modify my template?
*UPDATE : All the solutions provided by tim_yates or by Eduardo Melzer has resulted in following output with extra blank lines at the end of template. What could be the reason for that?* Are the solution providers not able to see this behavior or the issue is my system only?.
# groovy loop_template.groovy
env:
- name: param1
value : 1
- name: param2
value : 2
root#instance-1:
Change your template file to look like this:
#cat output.template
env:<% mapping.eachWithIndex { v, i -> %>
- name : ${v.name}
value : ${v.value}<% } %>
As you can see, your template file expects an input parameter called mapping, so you need change your main code to something like this:
def f = new File('output.template')
def engine = new groovy.text.GStringTemplateEngine()
def mapping = [
[ name : "param1", value : "1"],
[ name : "param2", value : "2"]
] // This mapping can consists of a multiple key value pairs.
def Template = engine.createTemplate(f).make([mapping: mapping])
println "${Template}"
Output:
#cat output.template
env:
- name : param1
value : 1
- name : param2
value : 2

How to use Groovy script in soapUi to loop multiple time

I am new to SoapUi. I am exploring on how multiple request in soapUi is done using groovy script.
below is the example that im trying to do, based on example that i found through "googling"
import com.eviware.soapui.SoapUI;
import com.eviware.soapui.model.testsuite.*;
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner
import java.util.Random
import com.eviware.soapui.model.testsuite.TestRunner.Status
// Define your testCase pointer
//def testcase = testRunner.testCase.testSuite.project.testSuites["TestSuite - User Management REST API"].getTestCaseByName ("Authenticate User")
def counterUser = testRunner.testCase.testSuite.getPropertyValue( "counter" )
int value = counterUser.toInteger()
String tester = ""
30.times {
value = value + 1
tester = "tester " + value.toString()
testRunner.testCase.testSuite.setPropertyValue( "userName", tester )
testRunner.runTestStepByName("POST - createUser - Create a User")
}
testRunner.testCase.testSuite.setPropertyValue( "counter", value.toString() )
I want to create a 30 users which start from Tester1...tester2.....tester30.
Is it possible to do this way? I keep getting an error such as NullPointerException at this line
int value = counterUser.toInteger()
I got what you say.
That is because, initially there is no value for counter which results to null and you are applying toInteger() over it.
Just change:
From:
int value = counterUser.toInteger()
To:
int value = counterUser?.toInteger() ?: 0

Typecasting in Groovy

I am trying to parse an yaml file in Groovy. However I am facing issue while typecasting the result to Map object.
Here is my logic
import org.yaml.snakeyaml.Yaml
import java.util.Map
Reader reader = null
int tokenCount = 0
def Map map = null
StringTokenizer st = new java.util.StringTokenizer("Country.State.City", ".")
reader = new FileReader("filepath")
String val = null
Yaml yaml = new Yaml()
map = (Map) yaml.load(reader)
tokenCount = st.countTokens()
for (i=1; i < tokenCount; i++) {
String token = st.nextToken()
map = (Map) map.get(token)
}
val = map.get(st.nextToken()).toString()
However I am getting error at line:
map = (Map) map.get(token)
where it says:
"org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'some value' with class 'java.lang.String' to class 'java.util.Map' error at line: 15"..
Where I am going wrong?
your provided yaml file is syntactically incorrect. This is a fixed version:
location: C:\\Users\\amah11\\Desktop\\New folder
type: hi
Header:
Code:
Start: 0
End: 2
value: H00
Owner:
Start: 3
End: 5
value: AIM
User:
Start: 6
End: 8
Value: AIM
number: 1
Note that Code: **Static** in the original messes things up. And all the keys on the final level need a space after the : (e.g. Start:3 is wrong).
The actual error message is:
Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Static Start:0 End:2 value:H00' with class 'java.lang.String' to class 'java.util.Map'
which is rather clear in showing, that there is something wrong with the original file.
You might want to consider using an editor, that detects errors like this right away.
An alternative to the original code, would be the use of inject on the tokenizer:
def map = new Yaml().load(new FileReader("cardconfig.yml"))
println new StringTokenizer('Header.Code.End', '.').inject(map) { r, s -> r.get(s) }
BTW: you don't need to import java.util in groovy.

named parameters with default values in groovy

Is it possible to have named parameters with default values in groovy? My plan is to make a sort of object factory, which can be called with no arguments at all in order to get an object with default values. Also, I'd need the functionality to explicitly set any of the params for the object. I believe this is possible with Python keyword arguments, for example.
The code I'm attempting with right now is something like below
// Factory method
def createFoo( name='John Doe', age=51, address='High Street 11') {
return new Foo( name, age, address )
}
// Calls
Foo foo1 = createFoo() // Create Foo with default values
Foo foo2 = createFoo( age:21 ) // Create Foo where age param differs from defaut
Foo foo3 = createFoo( name:'Jane', address:'Low Street 11' ) // You get the picture
// + any other combination available
The real app that I'm working on will have a lot more of parameters and thus a lot more combinations needed.
Thanks
UPDATE:
The factory method I'm planning is for testing purposes. Cannot really touch the actual Foo class and especially not it's default values.
#dmahapatro and #codelarks answere below had a good point in using a Map as a param that gave me an idea of a possible solution. I could create a map with the wanted defaults and override the needed values, and pass that to the factory method. This'll probably do the job and I'll go with that, unless I get a hint of a better approach.
My current approach below
defaults = [ name:'john', age:61, address:'High Street']
#ToString(includeFields = true, includeNames = true)
class Foo {
// Can't touch this :)
def name = ''
def age = 0
def address = ''
}
def createFoo( Map params ) {
return new Foo( params )
}
println createFoo( defaults )
println createFoo( defaults << [age:21] )
println createFoo( defaults << [ name:'Jane', address:'Low Street'] )
NOTE: leftShift operation ( << ) modifies the the original map, so in the above example age will be 21 in the last method call as well. In my case, this is not a problem as the defaults map can be created freshly each time in setup method.
Groovy does that for you by default (map constructor). You would not need a factory method. Here is an example
import groovy.transform.ToString
#ToString(includeFields = true, includeNames = true)
class Foo{
String name = "Default Name"
int age = 25
String address = "Default Address"
}
println new Foo()
println new Foo(name: "John Doe")
println new Foo(name: "Max Payne", age: 30)
println new Foo(name: "John Miller", age: 40, address: "Omaha Beach")
//Prints
Foo(name:Default Name, age:25, address:Default Address)
Foo(name:John Doe, age:25, address:Default Address)
Foo(name:Max Payne, age:30, address:Default Address)
Foo(name:John Miller, age:40, address:Omaha Beach)
UPDATE
#codelark's astrology :). In case the class is not accessible to set default values, you can do like
#ToString(includeFields = true, includeNames = true)
class Bar{
String name
int age
String address
}
def createBar(Map map = [:]){
def defaultMap = [name:'John Doe',age:51,address:'High Street 11']
new Bar(defaultMap << map)
}
println createBar()
println createBar(name: "Ethan Hunt")
println createBar(name: "Max Payne", age: 30)
println createBar(name: "John Miller", age: 40, address: "Omaha Beach")
//Prints
Bar(name:John Doe, age:51, address:High Street 11)
Bar(name:Ethan Hunt, age:51, address:High Street 11)
Bar(name:Max Payne, age:30, address:High Street 11)
Bar(name:John Miller, age:40, address:Omaha Beach)

Resources