Hello,
I have a case where I my JSON response body consists of the following:
`
{
“resultCode”: “SUCCESS”,
“errorCode”: null,
“errorMessage”: null,
“membershipCoupons”: [
{
“membershipId”: “136134567”,
“coupons”: [
{
“offerId”: “000C2977456”,
“description”: "PRODUCT_1 ",
“graphics”: [
{
“width”: 400,
“height”: 200,
“quality”: “Low”,
“url”: “\n http://www.mearsk.dk:8080/02_201442_005/000C2977456Low.jpg”
},
{
“width”: 900,
“height”: 600,
“quality”: “Hight”,
“url”: “\n http://www.mearsk.dk:8080/02_201442_005/000C2977456High.jpg”
}
],
“endTime”: 1417305600000,
“activationDate”: 1416244318947,
“redemption”: null
}
]
},
{
“membershipId”: “810456459”,
“coupons”: [
{
“offerId”: “000C297765971E23432”,
“description”: "PRODUCT_2 ",
“graphics”: [
{
“width”: 400,
“height”: 200,
“quality”: “Low”,
“url”: “\n http://www.maersk.dk:8080/02_201442_005/000C297765971E23432Low.jpg”
},
{
“width”: 900,
“height”: 600,
“quality”: “Hight”,
“url”: “\n http://www.maersk.dk:8080/02_201442_005/000C297765971E23432High.jpg”
}
],
“endTime”: 1417305600000,
“activationDate”: null,
“redemption”: null
},
{
“offerId”: “000C29776FR5456065F7”,
“description”: "PRODUCT_3 ",
“graphics”: [
{
“width”: 400,
“height”: 200,
“quality”: “Low”,
“url”: “\n http://www.mearsk.dk:8080/02_201442_005/000C29776FR5456065F7Low.jpg”
},
{
“width”: 900,
“height”: 600,
“quality”: “Hight”,
“url”: “\n http://www.mearsk.dk:8080/02_201442_005/000C29776FR5456065F7High.jpg”
}
],
“endTime”: 1417305600000,
“activationDate”: null,
“redemption”: null
}
]
}
]
}
`
I want to collect all ‘offerId’s’ from the getCouponList and use them in the next request (activateCoupon). In this concrete example that is two offerId’s.
This code is now working for one (the first ‘activationDate=null’ it finds), but I want to expand this functionality to save all offerId’s where activationDate=null at once.
I guess in pseudo-code this will be:
- getCouponlist() and while there are coupons left in the json response with activationDate=null - save them to be reused
- Use the saved list form 1) and repeat the activateCoupon() until the list is empty
Maybe something like:
.check(jsonPath("$.membershipCoupons[*].coupons[?(@.activationDate==null)].offerId").saveAs(“offerId”))
while (offerId != null) repeat the saving of offerIds.
Here is the simulation code:
`
package dk.maersk
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class AppSimulation extends Simulation {
val httpConf = http
.baseURL(“https://site.maersk.dk”)
.acceptEncodingHeader(“gzip,deflate”)
.headers(Map(“Content-Type” → “application/json”, “charset” → “UTF-8”, “User-Agent” → “Android(4.4)/Maersk(0.4)/0.1”))
val scn = scenario(“Scenario”)
.feed(csv(“memberInfo.csv”).random)
.exec(
http(“getProfile”)
.get("/user/profile")
.header( “X-Token”, “${memberID}” )
.check(status.is(200))
.check(jsonPath("$.resultCode").is(“SUCCESS”))
.check(jsonPath("$.profile.memberships[0].number").is("${memberID}"))
)
.exec(
http(“getCouponList”)
.get("/coupon/all")
.header(“X-Token”, “${memberID}”)
.check(status.is(200))
.check(jsonPath("$.resultCode").is(“SUCCESS”))
.check(jsonPath("$.membershipCoupons[*].coupons[?(@.activationDate==null)].offerId").saveAs(“offerId”))
)
.exec(
http(“activateCoupon”)
.post("/coupon/activate")
.header(“X-Token”, “${memberID}”)
.body(ELFileBody(“activateCoupons.json”)).asJSON
.check(status.is(200))
.check(jsonPath("$.resultCode").is(“SUCCESS”))
)
setUp(scn.inject(rampUsersPerSec(1) to(100) during (5 minutes))).maxDuration(7 minutes).protocols(httpConf)
}
`
My request body looks like this:
`
{“offerId”: “${offerId}”, “membershipId”: “${memberID}”, “osName”: “Android”, “osVersion”: “4.4”}
`
and I have a .csv file with memberId’s.
So how would i traverse the json response and saving offerIds for reuse in the subsequent request as long as there is elements in the json body with activtatioDate = null ?