0

How do I write a custom Gatling feeder based off a JSON file that has certain values that are stubbed and need to be replaced before being sent? For example

{"payloads":[
  {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting1"},
  {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting2"},
  {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting3"}
]}

jsonFile("/opt/gatling/user-files/simulation/cannedPayloads.json")

won't work I think because it's not actually valid JSON in the file. I've tried:

val jsonFileContents = Source.fromFile("/opt/gatling/user-files/simulation/cannedPayloads.json").getLines.mkString
.replaceAll("<GUID>", java.util.UUID.randomUUID().toString())
.replaceAll("<TIME>", Instant.now().toEpochMilli().toString())

val feeder = JsonPath.query("$.payloads[*]", jsonFileContents).right.get.toArray.circular

val scn1 = scenario("CannedTestSimulation").exec(feed(feeder).exec(
  http("to ingestion").post(url).body(StringBody("$")).asJSON
)
PnzrDrgoon
  • 43
  • 1
  • 7

1 Answers1

0

I circumnavigated it by reading in the file, performing my replaces, writing that to a temp file, and using the build in jsonFile from Gatling. The JSON base became

[
  {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting1"},
  {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting2"},
  {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting3"}
]

The scenario becomes

val scn1 = scenario("CannedTestSimulation").feed(jsonFile(CannedRequests.createTempJsonFile())).exec(
  http("send data").post(url).body(StringBody("$")).asJSON
)

and CannedRequests became

object CannedRequests {
val jsonFile = "/opt/gatling/user-files/simulations/stubbed_data.json"


def jsonFileContents(testSessionId: String): String = 
Source.fromFile(jsonFile).getLines.mkString
.replaceAll("<GUID>", "gatling_"+testSessionId)
.replaceAll("<TIME>", Instant.now().toEpochMilli().toString())

def createTempFile(contents: String, testSessionId: String): File = {
 val tempFile = File.createTempFile(testSessionId, ".json")
 val bw = new BufferedWriter(new FileWriter(tempFile))
 bw.write(contents)
 bw.close

 tempFile
}

def createTempJsonFile():String = {
val tempSessionId = java.util.UUID.randomUUID().toString()
val tempContents = jsonFileContents(tempSessionId)
val tempFile = createTempFile(tempContents, tempSessionId)

tempFile.getAbsolutePath
}
}
PnzrDrgoon
  • 43
  • 1
  • 7