urdshals
urdshals

Reputation: 1

How to edit a value (list of entries) from an api response to use in a request body in Gatling/Scala

I have an issue that I'm hoping someone can help me with. I'm pretty new to coding and Gatling, so I'm not sure how to proceed.

I'm using Gatling (with Scala) to create a performance test scenario that contains two API-calls.

  1. GetInformation
  2. SendInformation

I'm storing some of the values from the GetInformation response so I can use it in the body for the SendInformation request. The problem is that some information from the GetInformation response needs to be edited/removed before it is included in the body for SendInformation.

Extract of the GetInformation response:

{
  "parameter": [
    {
      "name": "ResponseFromGetInfo",
      "type": "document",
      "total": 3,
      "entry": [
        {
          "fullUrl": "urn:uuid:4ea859d0-daa4-4d2a-8fbc-1571cd7dfdb0",
          "resource": {
            "resourceType": "Composition"
          }
        },
        {
          "fullUrl": "urn:uuid:1b10ed79-333b-4838-93a5-a40d22508f0a",
          "resource": {
            "resourceType": "Practitioner"
          }
        },
        {
          "fullUrl": "urn:uuid:650b8e7a-2cfc-4b0b-a23b-a85d1bf782de",
          "resource": {
            "resourceType": "Dispense"
          }
        }
      ]
    }
  ]
}

What I want is to store the list in "entry" and remove the entries with resourceType = "Dispense" so I can use it in the body for SendInformation.

It would have been ok if the entry list always had the same number of entries and order, but that is not the case. The number of entries can be several hundred and the order of entries varies. The number of entries are equal to the "total" value that is included in the GetInformation response.

I've thought about a few ways to solve it, but now I'm stuck. Some alternatives:

  1. Extract the entire "entry" list using .check(jsonPath("$.parameter[0].entry").saveAs("entryList")) and then iterate through the list to remove the entries with resourceTypes = "Dispense". But I don't know how to iterate over a value of type io.gatling.core.session.SessionAttribute, or if this is possible. It would have been nice if I could iterate over the entry list and check if parameter[0].entry[0].resourceType = "Dispense", and remove the entry if the statement is true.
  2. I'm also considering If I can use StringBuilder in some way. Maybe if I check one entry at the time using .check(parameter[0].entry[X].resourceType != dispense, and if true then append it to a stringBuilder.

Does someone know how I can do this? Either by one of the alternatives that I listed, or in a different way? All help is appreciated :)

So maybe in the end it will look something like this:

  val scn = scenario("getAndSendInformation")

    .exec(http("getInformation")
      .post("/Information/$getInformation")
      .body(ElFileBody("bodies/getInformtion.json"))
      
      // I can save total, så I know the total number of entries in the entry list
      .check(jsonPath("$.parameter[0].total").saveAs("total"))
      
      //Store entire entry list
      .check(jsonPath("$.parameter[0].entry").saveAs("entryList"))
      
      //Or store all entries separatly and check afterwards who have resourceType = "dispense"? Not sure how to do this..
      .check(jsonPath("$.parameter[0].entry[0]").saveAs("entry_0"))
      .check(jsonPath("$.parameter[0].entry[1]").saveAs("entry_1"))
      //...
      .check(jsonPath("$.parameter[0].entry[X]").saveAs("entry_X"))
    )

    //Alternativ 1
    .repeat("${total}", "counter") {
      exec(session => {
        //Do some magic here 
        //Check if session("parameter[0]_entry[counter].resourceType") = "Dispense" {
        // if yes, remove entry from entry list}  
        session})}
  
    //Alternativ 2
      val entryString = new StringBuilder("")
      .repeat("${total}", "counter") {
        exec(session => {
          //Do some magic here 
          //Check if session("parameter[0]_entry[counter].resourceType") != "Dispense" {
          // if yes, add to StringBuilder}  
          // entryString.append(session("parameter[0]_entry[counter]").as[String] + ", ")
          session})}
        
       .exec(http("sendInformation")
         .post("/Information/$sendInformation")
         .body(ElFileBody("bodies/sendInformationRequest.json"))) 

Upvotes: 0

Views: 312

Answers (1)

Stéphane LANDELLE
Stéphane LANDELLE

Reputation: 6608

I'm pretty new to coding I'm using Gatling (with Scala)

Gatling with Java would probably be an easier solution for you.

check(jsonPath("$.parameter[0].entry").saveAs("entryList"))

This is going to capture a String, not a list. In order to be able to iterate, you have to use ofXXX/ofType[], see https://docs.gatling.io/reference/script/core/checks/#jsonpath

Then, in order to generate the next request's body, you could consider a templating engine such as PebbleBody (https://docs.gatling.io/reference/script/protocols/http/request/#pebblestringbody) or indeed use StringBody with a function with a StringBuilder.

Upvotes: 0

Related Questions