0

I am trying to create a json using CSV data using Groovy scripting and pass the same in the request body in jmeter but I am not able to create the data:

CSV Created:

0.0.0.0,255.255.255.255
10.0.0.1,255.0.0.0
10.0.0.2,255.0.0.1

Request body needed as :

{"Rule":{"__type":"AndroidSamsungDeviceRelocationRule","RuleId":0,"Name":"Test","DeviceFamily":6,"Targets":{"Groups":[{"Id":"0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000"}],"Devices":[]},"Priority":0,"IsEnabled":true,"StartDate":"/Date(1536856632768)/","EndDate":null,"Mappings":[{"RelocationTarget":{"Id":"0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000","Name":"100MB","Path":"\\\\100MB\\","PathVisible":"\\\\100MB\\"},"IPRange":[{"From":"0.0.0.0","To":"255.255.255.255"}...]}],"EnrollmentCertificateId":null,"EnrollmentCertificateName":""}}

Need the IP address for parameters from and to that are their in the csv in a single request.

code I have tried is :

@Grab('com.xlson.groovycsv:groovycsv:1.3')
import static com.xlson.groovycsv.CsvParser.parseCsv

fh = new File('examples/data/process_csv_file.csv')
def csv_content = fh.getText('utf-8')

def data_iterator = parseCsv(csv_content, separator: ';', readFirstLine: true)

for (line in data_iterator) {
    sum = line[0] as String
    destination = line[1] as String
}

def builder = new groovy.json.JsonBuilder()

builder({
   Rule:{
      __type:"AndroidSamsungDeviceRelocationRule",
      RuleId:0,
      Name:"Test",
      DeviceFamily:6,
      Targets:{
         Groups:[
            {
               Id:"0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000"
            }
         ],
         Devices:[

         ]
      },
      Priority:0,
      IsEnabled:true,
      StartDate:"/Date(1536856632768)/",
      EndDate:null,
      Mappings:[
         {
            RelocationTarget:{
               Id:"0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000",
               Name:"100MB",
               Path:"\\\\100MB\\",
               PathVisible:"\\\\100MB\\"
            },
            IPRange:[
               {
                  "From":"sum",
                  "To":"destination"
               }
            ]
         }
      ],
      EnrollmentCertificateId:null,
      EnrollmentCertificateName:""
   }
})

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

When I am running this through JSR223 Preprocessor I am getting blank request with error HTTP 400.

I need to create a dynamic IPRange loop for number of IPs available in CSV

2 Answers2

0

just check how you are using JsonBuilder

the following two variants produce the same result:

import groovy.json.JsonBuilder

def builder = new JsonBuilder()

builder({
    Rule{
        RuleId(0)
        Devices([1,2,3])
    }
})
println builder.toPrettyString()

another way is to define maps/arrays sequence and then convert to json

import groovy.json.JsonBuilder

def data = [
    Rule: [
        RuleId:0,
        Devices:[1,2,3]
    ]
]
println new JsonBuilder(data).toPrettyString()
daggett
  • 26,404
  • 3
  • 40
  • 56
0

I don't think you can use @Grab annotation in JSR223 Test Elements therefore your test fails at the early beginning.

You can try using below code which should generate the JSON you're looking for without any external dependencies:

def builder = new groovy.json.JsonBuilder()

@groovy.transform.Immutable
class IPRange {
    String From
    String To
}

def ipRanges = new File("examples/data/process_csv_file.csv").readLines().collect { line -> new IPRange(line.split(",")[0], line.split(",")[1]) }
builder.Rule(

        __type: "AndroidSamsungDeviceRelocationRule",
        RuleId: 0,
        Name: "Test",
        DeviceFamily: 6,
        Targets:
                [
                        Groups :
                                [
                                        [
                                                Id: "0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000",
                                        ]
                                ],
                        Devices:
                                [

                                ]
                ],
        Priority: 0,
        IsEnabled: true,
        StartDate: "/Date(1536856632768)/",
        EndDate: null,
        Mappings:
                [
                        [
                                RelocationTarget:
                                        [
                                                Id         : "0000000007.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000.0000000000",
                                                Name       : "100MB",
                                                Path       : "\\\\100MB\\",
                                                PathVisible: "\\\\100MB\\"
                                        ],
                                IPRange         : ipRanges.collect() {
                                    [
                                            From: it.From,
                                            To  : it.To
                                    ]
                                }

                        ]
                ],
        EnrollmentCertificateId: null,
        EnrollmentCertificateName: ""

)

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

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133