0
votes

I am trying to create a dynamic json using csv data in jmeter with JSR223 PreProcessor

Below is the code for the same I am using CSV for data for Id and Name:

def builder = new groovy.json.JsonBuilder()

@groovy.transform.Immutable
class Items {
    String Id
    String Name
}

def items = new File("Item.txt").readLines().collect { line -> new Items(line.split(",")[0], line.split(",")[1]) }
builder.Rule(

        __type: "DataCollectionRule",
        DeviceFamily: '${__P(DeviceFamily)}',
        RuleId: 0,
        Name: 'test-${__time(yyyy-MM-dd'T'hh:mm:ss)}-${__counter(TRUE,)}',
        Targets:
                [
                        Groups :
                                [
                                        [
                                                Id: '${logicalid1_1}',
                                        ]
                                ],
                        Devices:
                                [

                                ]
                ],
        StartDate: '/Date(${__time(,)})/',        
        IsEnabled: true,
        Priority: 0,
        AlertType: 0,
        DeliverySchedule:
                [
                                                Id         :   1,
                                                Name       :  "Every 30 Minutes",
                                                Period     :  "30M"

                ],
        CollectionSchedule:                      
               [
                                              Id         :   1,
                                                Name       :  "Every 30 Minutes",
                                                Period     :  "30M"
              ],                    
        Items  : items.collect() [
        [

                                                Id         : it.Id,
                                                Name       : it.Name                                                

        ]
                ],
        LocationAccuracy:
                [
                                                UseGPS     :  false,
                                       DistanceInMeters : 100,
                                       ReportToServer  : true,
                                       AccuracyInMeters : 10
             ],
          HasDolphinCounters: false,
          EnrollmentCertificateId: null,
          EnrollmentCertificateName: "",
        DatabaseHighWatermark: 28,
        DatabaseLowWatermark: 14,
        DeviceHighWatermark: 400,
        DeviceLowWatermark:  200

)

sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', builder.toPrettyString(), '')
sampler.setPostBodyRaw(true);

While running the test I am getting HTTP 400 with Bad Request

Log message is as shown below:

2018-09-24 13:49:23,669 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: it for class: Script32 at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-all-2.4.13.jar:2.4.13] at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.13.jar:2.4.13] at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_151] at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:221) ~[ApacheJMeter_core.jar:4.0 r1823414] at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:4.0 r1823414] at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:849) [ApacheJMeter_core.jar:4.0 r1823414] at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:467) [ApacheJMeter_core.jar:4.0 r1823414] at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414] at java.lang.Thread.run(Unknown Source) [?:1.8.0_151] Caused by: groovy.lang.MissingPropertyException: No such property: it for class: Script32 at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53) ~[groovy-all-2.4.13.jar:2.4.13] at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52) ~[groovy-all-2.4.13.jar:2.4.13] at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307) ~[groovy-all-2.4.13.jar:2.4.13] at Script32.run(Script32.groovy:46) ~[?:?] at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317) ~[groovy-all-2.4.13.jar:2.4.13] ... 9 more

CSV is as follows :

   -1,BatteryStatus
    -3,AvailableMemory
    -5,AvailableStorage

Thank you in advance

2

2 Answers

1
votes

You have to use {} for a closure here:

items.collect() { // wrong: [
    // ...
} // wrong: ]

Or just items.collect { ... }

With the [] the compiler will see that as a map literal and you get above errors (it is undefined)

0
votes

I believe you should be copying and pasting the example code more accurately, to wit your "Items" section should look like:

Items: items.collect() {
    [
            Id  : it.Id,
            Name: it.Name
    ]
}

Also be aware that you should not be using JMeter Functions and or Variables in Groovy scripts directly as it conflicts with GString Template feature and makes caching of compiled scripts impossible negatively impacting performance.

So I would also recommend changing:

  • ${__P(DeviceFamily) to props.get('DeviceFamily)`
  • ${__time(yyyy-MM-dd'T'hh:mm:ss)} to new Date().format("yyyy-MM-dd'T'hh:mm:ss")
  • etc.

See The Groovy Templates Cheat Sheet for JMeter article for more information on Groovy scripting in JMeter if needed