POMAIIIUK
POMAIIIUK

Reputation: 509

(String interpolation) How to save value to the session using the value from the session as key?

I have the following code that makes a simple POST request. How it should work? Before I have made a request, I generate a testing value "orderName1" and save it to the session. Next, I am using the function "create" to make the request and using the generated value in a function. And it works fine! My request is successful. As result, I have an order with a dynamically generated name "Name_68hKl90G". But I want to save the id, returned from the server into the session. I want to use "Name_68hKl90G" as a key into the session.

How it should work (session):

orderName1 -> Name_68hKl90G

Name_68hKl90G -> 63d80b25cb6c2a7e7e845e40

How it actually work (session):

orderName1 -> Name_68hKl90G

${orderName1} -> 63d80b25cb6c2a7e7e845e40

Code:

val generateStr = (symbols: Integer) => Random.alphanumeric.take(symbols).mkString

val prepareData = exec(
  session => {
    val newSession = session
      .set("orderName1", "Name_" + generateStr(8))
    newSession
  }
)

def create(name: String) = exec(
  http("Create order")
    .post("/shop/order")
    .body(StringBody(s"""{"name":"${name}"}""".stripMargin)).asJson
    .check(status.is(201))
    .check(jsonPath("$.id").saveAs(s"${name}")) 
)

val userScn = scenario("Scenario")
  .exec(
    prepareData,
    create("${orderName1}")
  )

setUp(
  userScn.inject(rampUsers(rampUpUserCount) during (rampUpDuration seconds)),
).protocols(httpProtocol)

Upvotes: 0

Views: 129

Answers (2)

POMAIIIUK
POMAIIIUK

Reputation: 509

It is no need to use session value as a key for other value. You can satisfy all needs by following code:

val generateStr = (symbols: Integer) => Random.alphanumeric.take(symbols).mkString

val prepareData = exec(
  session => {
    val newSession = session
      .set("orderName1", "Name_" + generateStr(8))
    newSession
  }
)

def create(name: String) = exec(
  http("Create order")
    .post("/shop/order")
    .body(StringBody(session => s"""{"name":"${session(name).as[String]}"}""".stripMargin)).asJson
    .check(status.is(201))
    .check(jsonPath("$.id").saveAs(s"${name}_id")) 
)

def rename(name: String, newName: String) = exec(
  http("Update order name")
    .patch(session => s"/shop/order/${session(name + "_id").as[String]}")
    .body(StringBody(session => s"""{"name":"${newName}"}""".stripMargin)).asJson
    .check(status.is(200))
)

val userScn = scenario("Scenario")
  .exec(
    prepareData,
    create("orderName1"),
    rename("orderName1", "New name")
  )

setUp(
  userScn.inject(rampUsers(rampUpUserCount) during (rampUpDuration seconds)),
).protocols(httpProtocol)

Upvotes: 0

Stéphane LANDELLE
Stéphane LANDELLE

Reputation: 6600

def create(name: String) =
exec(
  http("Create order")
    .post("/shop/order")
    .body(StringBody(s"""{"name":"#{$name}"}""")).asJson
    .check(status.is(201))
    .check(jsonPath("$.id").saveAs("id")) 
).exec { session =>
  val nameValue = session(name).as[String]
  val id = session("id").as[String]
  session.remove("id").set(nameValue, id)
}

create("orderName1")

Honestly, this looks overly complicated. I don't get why you don't want to use 2 different attributes, one for the key and one for the value.

Upvotes: 1

Related Questions