0

I'm new to Scala, and I'm struggling with extracting common parts of my gatling tests.

exec(
  http("Open Page")
    .get("/page")
).exec(
  http("GET from REST")
    .get("/")
    .disableFollowRedirect
    .resources(
      http("x").get("/rest/x/").check(jsonPath("$.x").exists),
      http("y").get("/rest/y/").check(jsonPath("$.y").exists)
    )
)

How can I achieve this:

exec(
  http("Open Page")
    .get("/page")
).exec(
  http("GET from REST")
    .get("/")
    .disableFollowRedirect
    .resources(
      resources
    )
)

val resources = ...???

.resources signature looks like this

def resources(res: HttpRequestBuilder*): 

One more though... Since I'm about to unify some resource values that are passed, there usually is something extra that I have to add, what syntax should be used to make below code correct.

exec(
  http("Open Page")
    .get("/page")
).exec(
  http("GET from REST")
    .get("/")
    .disableFollowRedirect
    .resources(
      common: _*,
      http("z").get("/rest/z/").check(jsonPath("$.z").exists)
    )
)

val common: Seq[HttpRequestBuilder] = Seq(
    http("x").get("/rest/x/").check(jsonPath("$.x").exists),
    http("y").get("/rest/y/").check(jsonPath("$.y").exists)
)

I figured out this

exec(
  http("Open Page")
    .get("/page")
).exec(
  http("GET from REST")
    .get("/")
    .disableFollowRedirect
    .resources(
      common:+
      http("z").get("/rest/z/").check(jsonPath("$.z").exists): _*
    )
)

But maybe there is a "proper" way to do this.

klubi
  • 1,373
  • 1
  • 14
  • 25

1 Answers1

1
val resources: Seq[HttpRequestBuilder] = ??? // Or subtype
...
// Type ascription, but functionally equivalent to other languages' "splat" operators
.resources(resources: _*)

A type ascription is an expression of the form expr: Type. The type of such an expression is Type, and the compiler must somehow make expr conform to that type. The rationale behind using it here for varargs is that the argument of resources is a HttpRequestBuilder* (even though there is no such type), so you can use a type ascription to make the compiler interpret an object of type Seq[HttpRequestBuilder] as a HttpRequestBuilder*, even though such a type doesn't really exist. It uses the wildcard _ so you don't have to type out the entire type name.

EDIT: Yes, if you want to merge the list of resources with something else and then pass it as varargs, you should do

.resources(somethingelse +: resources: _*)

(Use prepend +: because that might be more efficient depending on the implementation of resources.)

HTNW
  • 27,182
  • 1
  • 32
  • 60
  • What you wrote is a complete gibberish for me... Can you write how resource declaration in my case should look? Should two http... lines be enclosed with a curly brackets or something else? – klubi Jun 29 '17 at 19:06
  • What I did: `val res: Seq[HttpRequestBuilder] = Seq( http("x").get("/rest/x/").check(jsonPath("$.x").exists), http("y").get("/rest/y/").check(jsonPath("$.y").exists) )` but this caused compiler to throw "no: _* annotation allowed here" exception on `.resources( res: _* )` – klubi Jun 29 '17 at 19:13
  • What you wrote was correct, but 2 questions. What part of the answer didn't make sense, and what exactly did the compiler say? – HTNW Jun 29 '17 at 19:37
  • As I mentioned, I'm completely new to Scala, literally two days of Gatling POC, so I'm thinking in java, and try to adjust. Answer may have been accurate but my mind wasn't able to process it (maybe it's late:P). Actually it's not an error... just a warning that there is a reference to uninitialized value. ps. now I have extra question.. I'll edit my initial question... – klubi Jun 29 '17 at 20:02
  • Ok.. I had time to actually use it... looks like when I "Make" project in IJ it returns a warning: 'Reference to uninitialized value'. which then results in NPE during execution. – klubi Jul 02 '17 at 13:49
  • Well, then something is uninitialized. You should find what variable that is and fix it. – HTNW Jul 02 '17 at 15:17
  • I'll be verifying that tomorrow... but it looks like the cause was in using `val` when i changed it to `def` warning was gone and tests started to run. – klubi Jul 02 '17 at 19:54