2

I want to write a function to format Vaadin messages. These messages have the format

108|0ff1255e-e2be-4e7b-ac5c-1ff2709ce886[["0_11_12_13_login_username","v","v",["text",["s","Agent00232"]]]]

The first number is the length then there some kind of session id (later called vaadin_security_key) followed by the payload. (In this example I set the value "Agent00232" to the textfield with the connector id "0_11_12_13_login_username") I wrote a function like this:

 def sendMessage(name: String, vaadinCommand: String) = {
    def createMessage(session: Session) = {
      val message = session("vaadin_security_key").as[String] + "\u001d" + vaadinCommand
      val message2 = ELCompiler.compile(message)(classTag[String])(session).toString
      val message3 = message2.substring(8, message2.length - 2)
      val len = message3.length
      val completeMessage = len.toString() + "|" + message3
      completeMessage
    }
    exec(
      ws(name)
        .sendText(createMessage)
        .check(wsAwait
          .within(Vaadin.defaultTimeout)
          .until(1)
          .regex("""(.+)""")
          .saveAs("lastResult")))
      .exec { session =>
        println(session("lastResult").as[String])
        session
      }
  }

I'd like to use this method like this and use EL expressions in the string:

exec(Vaadin.sendMessage("setting Name", "[[\"0_11_12_13_login_username\",\"v\",\"v\",[\"text\",[\"s\",\"${username}\"]]]]"))

Therefore I have to evaluate the String before calculating the length. The method ELCompiler.compile()... returns a Expression[String]. The problem is that I need this string and concat it with the calculated length. When I do message2.toString it returns Success(0ff1255e-e2be-4e7b-ac5c-1ff2709ce886[["0_11_12_13_login_username","v","v",["text",["s","Agent00232"]]]]) and therefor I have to use substring(8, message2.length - 2) to get the evaluated payload (with the security key) to calculate the length of it.

Is there a better (more elegant) way to extract the String out of the expression then the use of substring(...)?

Jens Baitinger
  • 2,230
  • 14
  • 34

1 Answers1

4
  1. Don't explicitly pass a classTag to ELCompiler.compile. If you really were to use this yourself, use ELCompiler.compile[String].
  2. ELCompiler.compile returns an Expression[String], alias a function of Session to Validation[String], so if you apply it, you get a Validation[String] (because the evaluation could fail), hence toString prints the Success, not the value it contains. What you want is to map this Validation container.
  3. You're compiling the expression over and over again, on each function execution. Move it outside the function.
  4. Beware that session("lastResult").as[String] would crash if lastResult could not be saved. Use validate instead.
  5. Use triple quotes so you don't have to escape the inner double quotes.

The first part of your code would then look like:

import io.gatling.core.session._
import io.gatling.core.session.el._

def sendMessage(name: String, vaadinCommand: String) = {

  val message = ("${vaadin_security_key}\u001d" + vaadinCommand).el[String]

  def createMessage = message.map { resolvedMessage =>
    resolvedMessage.length + "|" + resolvedMessage
  }

  exec(
    ws(name)
      .sendText(createMessage)
      .check(wsAwait
        .within(Vaadin.defaultTimeout)
        .until(1)
        .regex("""(.+)""")
        .saveAs("lastResult")))
    .exec { session =>
      println(session("lastResult").validate[String])
      session
    }
}

Then you'd call it with:

exec(Vaadin.sendMessage("setting Name", """[["0_11_12_13_login_username","v","v",["text",["s","${username}"]]]]"""))
ak1ra
  • 383
  • 4
  • 8
Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29
  • You showed me a more elegant way how to write the method createMessage() but you did not answer the question if there is a better way to extract the message out of an Expression[String] – Jens Baitinger Oct 14 '14 at 07:43
  • Because when I answered, you hadn't properly explained why you had to do such a substring. Now that you did, I get it. Editing my answer. – Stephane Landelle Oct 14 '14 at 08:36