# implementing scala code in the simulation script to generate HMAC and using ELFileBody

**URL:** <https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965>\
**Category:** Gatling (Open-Source)\
**Created:** [February 4, 2015, 1:32pm UTC](https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965 "2015-02-04T13:32:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [February 4, 2015, 1:32pm UTC](https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965/1 "2015-02-04T13:32:37Z")

</div>

Hi,

I tried to do this:

`

```auto
package com.site

import scala.concurrent.duration._

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._

class checkXXXSimulation extends Simulation {

 val httpProtocol = http
  .baseURL("http://stage-xxx.xxx.com")
  .inferHtmlResources()
  .acceptEncodingHeader("gzip,deflate")
  .contentTypeHeader("text/xml;charset=UTF-8")
  .userAgentHeader("Apache-HttpClient/4.1.1 (java 1.5)")

 val headers_0 = Map("SOAPAction" -> """"http://xxx.cardservices.xxx.com/Creditxxx2"""")

  val md = java.security.MessageDigest.getInstance("SHA-1")
  val str = " “7080001237555” + ”1104” + ”7080001237555” + ”2015-02-04 12:23:55” + ”xxx” + ${MemberId) + ”2” +”hJSaxxx5M9” "

```

```auto
  val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))

 val scn = scenario("checkxxxSimulation")

  .feed(csv("data/memberid.csv").random)

  .exec(http("request_0")
   .post("/xxxCardservicesxxxApi/ValueCodeService.asmx")
   .headers(headers_0)
   .body(ELFileBody("checkxxx_request.txt"))
   .check(status.is(200)))

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

```

`

Two problems:

1. I want to ‘feed’ my definition with a variable from a .csv file called ‘member’, however I cannot put the .feed infront of the scala code (val str = " “70…) How Can I feed a scala implementation with a .csv file?

2. When I have a request body (checkxxx\_request.txt) with content (not complete):

```auto
      <!--Optional:-->
            <pos:Footer>
               <!--Optional:-->
               <api:MAC>${ha}</api:MAC>
            </pos:Footer>
         </pos:wrapper>
      </pos:CreditXXX2>
   </soapenv:Body>
</soapenv:Envelope>

```

I get the error:

> No attribute named ‘ha’ is defined

Why?

---

<div class="post-metadata">

**Author:** ![Stefan\_Magnus\_Landro](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@Stefan\_Magnus\_Landro](https://community.gatling.io/u/Stefan_Magnus_Landro)\
**Post date:** [February 4, 2015, 7:20pm UTC](https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965/2 "2015-02-04T19:20:41Z")

</div>

You have to write a session fn and add the variable to the session. See session api.

Stefan

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [February 4, 2015, 7:44pm UTC](https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965/3 "2015-02-04T19:44:32Z")

</div>

session fn?

---

<div class="post-metadata">

**Author:** ![Abhinav\_Gogna](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Abhinav\_Gogna](https://community.gatling.io/u/Abhinav_Gogna)\
**Post date:** [February 4, 2015, 7:46pm UTC](https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965/4 "2015-02-04T19:46:19Z")

</div>

Session function

Eg:  
`exec{session =>`

```auto
val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))
session.set("ha",ha)
}

```

` `

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [February 4, 2015, 8:28pm UTC](https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965/5 "2015-02-04T20:28:15Z")

</div>

Ok, then I have this:

`

```auto
package com.xxx

import io.gatling.core.Predef._
import io.gatling.http.Predef._

class checkxxxSimulation extends Simulation {

 val httpProtocol = http
  .baseURL("http://xxx.xxx.com")
  .inferHtmlResources()
  .acceptEncodingHeader("gzip,deflate")
  .contentTypeHeader("text/xml;charset=UTF-8")
  .userAgentHeader("Apache-HttpClient/4.1.1 (java 1.5)")

 val scn = scenario("checkxxxSimulation")

    .feed(csv("data/members.csv").random)

    .exec{session =>
    val md = java.security.MessageDigest.getInstance("SHA-1")
    val str = " “7080001237xxx” + ”1108” + ”7080001237xxx” + ”2015-02-04 12:23:55” + ”xxx” + ${member) + ”2” +”hJSaxxx” "
    val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))
    http("checkxxx")
      .post("/xxx/xxx.asmx")
      .headers(Map("SOAPAction" -> """"http://xxx.xxx.xxx.com/Checkxxx""""))
      .body(ELFileBody("RecordedSimulation_0000_request.txt"))
    session.set("ha",ha)
      }
 setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}

```

`

Further I have the request body:

`

```auto
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:tns="http://xxx.xxx.xxx.com/" xmlns:s1="http://xxx.xxx.xxx.Com/">
   <soap:Body>
      <tns:CheckEVC2 xmlns="http://xxx.xxx.xxx.com/">
         <tns:wrapper>
            <tns:CheckxxxRequest>
               <tns:ValueCodeID>${member)</tns:ValueCodeID>
               <tns:ValueCodeType>2</tns:ValueCodeType>
            </tns:CheckxxxRequest>
            <tns:Footer xsi:type="s1:Footer">
               <s1:MAC>${ha)</s1:MAC>
            </tns:Footer>
            <tns:Header xsi:type="s1:Header">
               <s1:AcquirerId>company</s1:AcquirerId>
               <s1:AgreementID>7080001237xxx</s1:AgreementID>
               <s1:CashRegisterID/>
               <s1:ClientIP/>
               <s1:MerchantConceptID>1108</s1:MerchantConceptID>
               <s1:MerchantID>708000123xxx</s1:MerchantID>
               <s1:TransmissionTime>2015-02-04 12:23:55</s1:TransmissionTime>
            </tns:Header>
         </tns:wrapper>
      </tns:Checkxxx>
   </soap:Body>
</soap:Envelope>

```

`

and a .csv file looking like this:

```auto
member
32014142
32019332

```

```auto
.

```

```auto
.

```

```auto
.

```

```auto

```

```auto
Running this gives me the error:

```

```auto

```

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [February 4, 2015, 8:48pm UTC](https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965/6 "2015-02-04T20:48:03Z")

</div>

Maybe like this (if the ‘session’ is accessible between my two .exec’s ? Are they?

`

```auto
val scn = scenario("checkxxxSimulation")

   .feed(csv("data/members.csv").random)

   .exec{session =>
   val md = java.security.MessageDigest.getInstance("SHA-1")
   val str = " “7080001237xxx” + ”1108” + ”7080001237xxx” + ”2015-02-04 12:23:55” + ”xxx” + ${medlem) + ”2” +”hJSaGExxx” "
   val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))
   session.set("ha", ha)}

   .exec(
   http("checkxxx")
     .post("/xxx/xxx.asmx")
     .headers(Map("SOAPAction" -> "http://xxx.xxx.xxx.com/Checkxxx"))
     .body(ELFileBody("checkxxx_request.txt")))

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

```

`

---

<div class="post-metadata">

**Author:** ![Abhinav\_Gogna](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Abhinav\_Gogna](https://community.gatling.io/u/Abhinav_Gogna)\
**Post date:** [February 4, 2015, 10:16pm UTC](https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965/7 "2015-02-04T22:16:02Z")

</div>

You can not put your post request inside the session fn. Session fn is usually used for setting up attributes, which are available for the rest of the iteration.

Here is the link that describes it. [http://gatling.io/docs/2.1.4/general/scenario.html#structure-elements](http://gatling.io/docs/2.1.4/general/scenario.html#structure-elements)

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [February 4, 2015, 10:26pm UTC](https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965/8 "2015-02-04T22:26:08Z")

</div>

No… I found out what you are saying:

This works:

`

```auto
val scn = scenario("checkxxxSimulation")

   .feed(csv("data/members.csv").random)

   .exec{session =>
   val md = java.security.MessageDigest.getInstance("SHA-1")
   val str = " “7080001237xxx” + ”1104” + ”7080001237xxx” + ”2015-02-04 12:23:55” + ”xxx” + ${medlem) + ”2” + ”hJSaGxxx” "
   val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))
   val mac = ha.substring(0, ha.length() / 2 )
   session.set("mac", mac)
   }

   .exec(session => {
   println((session("mac").as[String]))
   session})

   .exec(
   http("checkxxx")
     .post("/xxx/xxx.asmx")
     .headers(Map("SOAPAction" -> "http://xxx.xxx.xxx.com/xxx"))
     .body(ELFileBody("checkxx_request.txt"))
     .check(status.is(200)))

```

`

The problem now is that the actual _decoding_ does not work (or the string concatenate?).

`

```auto
val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))

```

`

Any tips on what might be wrong with the decoding?

Cheers!

---

<div class="post-metadata">

**Author:** ![Stefan\_Magnus\_Landro](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@Stefan\_Magnus\_Landro](https://community.gatling.io/u/Stefan_Magnus_Landro)\
**Post date:** [February 4, 2015, 10:34pm UTC](https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965/9 "2015-02-04T22:34:42Z")

</div>

you shouldn’t use sun packages: [http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html](http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html)  
Use this: [http://docs.oracle.com/javase/8/docs/api/java/util/Base64.Encoder.html](http://docs.oracle.com/javase/8/docs/api/java/util/Base64.Encoder.html)  
But that is probably not the issue. It’s probably all the quotes. Read this: [http://www.tutorialspoint.com/scala/scala\_strings.htm](http://www.tutorialspoint.com/scala/scala_strings.htm)

HTH

Stefan

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [February 4, 2015, 11:43pm UTC](https://community.gatling.io/t/implementing-scala-code-in-the-simulation-script-to-generate-hmac-and-using-elfilebody/1965/10 "2015-02-04T23:43:13Z")

</div>

Ok this works:

```auto
val str = "7080001237543" + "1105" + "7080001237543" + "2015-02-04 12:23:55" + "site" + "0032014143" + "2" + "hJSbGEx5M7"

```

Can I replace the number “0032014143” with a variable ${member}inside the concatenating? I need to parameterize it for each request.
