简体   繁体   中英

Gatling: Transforming response and writing it to JSON file

Fairly new to Scala/Gatling and I have searched the SO a lot, but nothing specific to my issue has been answered. So here's my code

    val adminUplFile: ScenarioBuilder = scenario("Admin user uploads file")
    .exec(http("Upload File")
      .post("/file") 
      .header("username", "example") r
      .header("password", "80HL/d1fETqxuCArAbIU6/Sb3F2nSTCqw/eqw1lzJio=") 
      .formUpload("file", "uploadFile") // body params ("key" , "path of the file/name")
      .formParamSeq(Seq(("uploadByID", "1"), ("location", "meetings"), ("personID", "0"), ("uploadTimeStamp", "2019-01-15 10:01:04.426"), ("md5Hash", "n95QI48+Uqxfw0hTnJdqMA=="))) 
      .check(status.is(200)) 
      .check(jsonPath("$.uuid").saveAs("guidList"))) 
    .exec { session =>
    val writer = new PrintWriter(new FileOutputStream(new File("src/gatling/resources/extractedData/GuidList.json"), true))
    writer.write(session("\"guidList\"").as[String].trim)
    writer.write("\n")
    writer.close()
    session  }

setUp(
    adminUplFile.inject(constantUsersPerSec(2) during (1 seconds)).protocols(httpConf)
  )

and here's the response I'm trying to extract :

 {
    "uuid": "3a917e22-3c76-45de-a104-4e2aa4b72a35"
}

So what I get written to the file is :
"3a917e22-3c76-45de-a104-4e2aa4b72a35"
"929c89c0-7a1a-4a18-8ee8-2958c9cd430f"

and what I want is :
[

"3a917e22-3c76-45de-a104-4e2aa4b72a35" ,
"929c89c0-7a1a-4a18-8ee8-2958c9cd430f"

]

just looking at it, it looks a simple case of adding square brackets and a comma to the file but in reality, with every virtual user trying to append the same file I'm struggling on how to make it format in a way that square brackets don't get used again and only once.

So I don't want :
["3a917e22-3c76-45de-a104-4e2aa4b72a35"],
["929c89c0-7a1a-4a18-8ee8-2958c9cd430f"]

Basically used before after hooks of Gatling with some printwriter magic

Here is my code :

 try {
  val br = new BufferedReader(new FileReader(deleteGuidsFilePath))
  var last : String = ""

  var line = br.readLine
  while ({line != null})
  {
    last = line
    line = br.readLine
  }

  br.close()
  afterPw.write(last.replace(",", "]"))
  afterPw.close()
}
catch {
  case e: Throwable => println("Couldn't read the file")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM