0

I have 2 http calls in 2 different function def and saving json keys from response body in gatling session. How can I match them?

def getAppData():HttpRequestBuilder = {
      http("get application resource")
        .get("host/app")
        .header("Authorization", "Bearer "+ token)
        .check(status.is(200))
        .check(jsonPath("$..${app_info}").saveAs("app_Response"))

}
def getUserData():HttpRequestBuilder = {
      http("get user data ")
        .get("host/user/data")
        .header("Authorization", "Bearer "+ token)
        .check(status.is(200))
        .check(jsonPath("$..${user_info}").saveAs("userdata_Response"))

}

How do I compare or verify that the json values of app_info and user_info matches ie;

app_Response and userdata_Response

The values of both of these are arrays . For instance, in this format:

"app_info":
    [
         "name",
         "address"
    ]

same for user_info. I gave a try to use in-built methods of jsonPath().equals() but I believe that's not appropriate way for comparing. If not a way using gatling specific methods then perhaps will find how to perform using scala?

Kindly help.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
anonymous-explorer
  • 365
  • 1
  • 12
  • 26

1 Answers1

1

Basically of you use json-spray you should be able to compare both using == operator like described in this other answer here.

Compare json equality in Scala

[Edit] Doing something like this I could compare 2 Json's using spray:

package example

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import spray.json._
import DefaultJsonProtocol._

class MainSimulation extends Simulation {
  val baseUrl = "http://localhost:8080"
  val httpProtocol = http
    .baseUrl(baseUrl)
    .userAgentHeader("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)")
  val header = Map("Content-Type" -> "application/json”,”Accept-Charset" -> "utf-8")

  val scn = scenario("Scenario")
    .exec(http("Get Hello Json")
      .get("/hello/Alessandro")
      .check(status.is(200))
      .check(jsonPath("$").saveAs("activities-1")))
    .exec(http("Get Hello Json")
      .get("/hello/Ronaldo")
      .check(status.is(200))
      .check(jsonPath("$").saveAs("activities-2")))
    .exec(session => {
      println("=======================================================")
      val activities_1 = session("activities-1").as[String]
      val activities_2 = session("activities-2").as[String]
      println(s"Activities 1: ${activities_1.parseJson}")
      println(s"Activities 2: ${activities_2.parseJson}")
      println(s"Are they equal?: ${activities_1.parseJson == activities_2.parseJson}")
      println("=======================================================")
      session
    })

  setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}

and I can see this in the output:

=======================================================
Activities 1: {"activities":["swimming","soccer"],"name":"Alessandro"}
Activities 2: {"activities":["swimming","soccer"],"name":"Alessandro"}
Are they equal?: true
=======================================================
javendo
  • 13
  • 1
  • 5
  • thanks @javendo , I included the maven dependency for it to use spray-json. But when comparing with == using if () or doIfEquals("${userdata_Response}", "${app_Response}") , it complains for an error : **Expression of type unit doesn't conform to expected type HttpRequestBuilder** . Even though changing def function to ChainBuilder() , it throws same error. Any suggestions? – anonymous-explorer Jul 07 '19 at 04:46
  • Actually, I realized I don't need to use this lib for spray-json in order to compare. I am able to do it with .check() itself. Adding this in getUserData() worked for me: `.check(jsonPath("$.${user_info}").is("${app_Response}"))` – anonymous-explorer Jul 08 '19 at 15:25
  • I think you should answer your own question to clarify for others with the same difficulty. Can you do that? If so I can vote on your answer. – javendo Jul 08 '19 at 20:31