0
votes

I am trying to create a JSON using Groovy Json Builder in Jmeter.

Below is the code I created to get the desired JSON :

import groovy.json.JsonBuilder

def json = new JsonBuilder()

json {
    "Rule":{  
      "__type":"AndroidSamsungDeviceRelocationRule",
      "RuleId":0,
      "Name":"Test",
      "DeviceFamily":6,
      "Targets":{  
         "Groups":[  
            {  
               "Id":"[{LogicalId="${logicalid1}"}]"
            }
         ],
         "Devices":[  

         ]
      },
      "Priority":0,
      "IsEnabled":true,
      "StartDate":"/Date(${__time(,)})/",
      "EndDate":null,
      "Mappings":[  
         {  
            "RelocationTarget":{  
               "Id":"${logicalid2}",
               "Name":"${__P(TargetGroups)}",
               "Path":"\\'${__P(TargetGroups)}'\\",
               "PathVisible":"\\'${__P(TargetGroups)}'\\"
               },
               "IPRange":[  
                 source Double.parseDouble(vars.get("source"))
                 destination Double.parseDouble(vars.get("destination"))
               ]
            }
         ],
         "EnrollmentCertificateId":null,
         "EnrollmentCertificateName":""
      }
   }


sampler.addNonEncodedArgument("",json.toPrettyString(),"")
sampler.setPostBodyRaw(true)

CSV data is as follows:

0.0.0.0,255.255.255.255 10.0.0.1,255.0.0.0

The above CSV can have multiple values from 1 to 100 IPs separated by ',' . The code also needs to be modified as per the number of IPs rows. The ABove CSV data needs to be there under IPRange tag and Result Should be like:

"Rule":{  
      "__type":"AndroidSamsungDeviceRelocationRule",
      "RuleId":0,
      "Name":"Test",
      "DeviceFamily":6,
      "Targets":{  
         "Groups":[  
            {  
               "Id":"[{LogicalId="${logicalid1}"}]"
            }
         ],
         "Devices":[  

         ]
      },
      "Priority":0,
      "IsEnabled":true,
      "StartDate":"/Date(${__time(,)})/",
      "EndDate":null,
      "Mappings":[  
         {  
            "RelocationTarget":{  
               "Id":"${logicalid2}",
               "Name":"${__P(TargetGroups)}",
               "Path":"\\'${__P(TargetGroups)}'\\",
               "PathVisible":"\\'${__P(TargetGroups)}'\\"
               },
               "IPRange":[  
                 {"From":"0.0.0.0","To":"255.255.255.255"}, 
                 {"From":"10.0.0.1","To":"255.0.0.1"},
                  ......
                  .....
               ]
            }
         ],
         "EnrollmentCertificateId":null,
         "EnrollmentCertificateName":""
      }

But the issue I am getting is "Bad Request" in jmeter.

1

1 Answers

0
votes
  1. I fail to see why do you need to build your request body in Groovy, it can be done purely in HTTP Request "Body Data" tab like:

    JMeter HTTP Request Parameterize

    Check out JMeter Parameterization - The Complete Guide for examples on using external data sources for creating data-driven JMeter tests

  2. If you want us to help you with the setup it would be good to see the desired outcome, i.e. JSON payload you would like to generate and the code you've tried as above samples don't make any sense. Get used to check jmeter.log file - normally it contains enough information to get to the bottom of the JMeter test issue. Also I suggest reading Parsing and producing JSON article and JsonBuilder class documentation.