tomaszarmata
10 years agoContributor
Wierd diffrence in groovy map objects.
Hey,
I have two objects. First (groovy object) holds object structure and second (xml object) holds test data which I want to put into first object.
First I need to check if first object contain all fields of second object. Then fill up values. I need to do that recursive, becouse of fact that I don't know how many depths will be in that objects.
Here is simple example of my task (originaly I have xml and groovy objects from other classes):
Script library class:
package example
class ExampleFeeder {
def static log
def resourceName
def testData
def baseRoot
def fields = [:]
def key
def subresource
def build () {
testData = returnXmlObject ()
baseRoot = returnGroovyObject ()
log.info resourceName
log.info baseRoot
log.info baseRoot.keySet ()
log.info baseRoot.containsKey ( "$resourceName" )
log.info baseRoot."$resourceName"
buildFieldsFromParameters ()
buildFieldsFromResources ()
insertFieldsIntoBaseRoot ()
}
def build ( subrequest, currentRoot ) {
testData = subrequest
baseRoot = currentRoot
log.info resourceName
log.info baseRoot
log.info baseRoot.keySet ()
log.info baseRoot.containsKey ( "$resourceName" )
log.info baseRoot."$resourceName"
buildFieldsFromParameters ()
buildFieldsFromResources ()
insertFieldsIntoBaseRoot ()
}
def buildFieldsFromParameters () {
testData.parameters.parameter.each {
if ( baseRoot."$resourceName"[0].containsKey ( it.@name ) ) {
fields.put ( it.@name, it.value.text() )
}
}
}
def buildFieldsFromResources () {
testData.subresources.subresource.each {
key = it.@name
if ( baseRoot."$resourceName"[0].containsKey ( key ) ) {
subresource = new ExampleFeeder ( resourceName: key )
subresource.build ( it, [ "$key": baseRoot."$resourceName"[0]."$key" ] )
fields.put ( key, [ subresource.fields ] )
}
}
}
def insertFieldsIntoBaseRoot () {
fields.each {
baseRoot."$resourceName"[0].put ( it.key, it.value )
}
}
def returnGroovyObject () {
def groovyObject = [
'presentations' : [ [
'person-count' : null,
'date' : null,
'presentation-has-products' : [ [
'product-id' : null
] ]
] ]
]
return groovyObject
}
def returnXmlObject () {
def xml = '''<request name="presentations">
<parameters>
<parameter name="person-count">
<value>5</value>
</parameter>
<parameter name="date">
<value>2015-11-09 11:37:02</value>
</parameter>
</parameters>
<subresources>
<subresource name="presentation-has-products">
<parameters>
<parameter name="product-id">
<value>6638</value>
</parameter>
</parameters>
</subresource>
</subresources>
</request>'''
return new XmlParser().parseText ( xml )
}
}
Groovy test step:
import example.ExampleFeeder def log = log ExampleFeeder.log = log def example = new ExampleFeeder ( resourceName: 'presentations' ) example.build ()
This code doesn't work. If You take a look at that what log.info prints You will see why. But I don't understand why is that diffrence.
Could someone help me understand my mistakes? Or give me other idea to do that task?
Just a simple change to be made:
from
def groovyObject2 = [ "$name" : 'something' ]
to
def groovyObject2 = [ (name) : 'something' ]