Gatling print to file if KO - scala

I have an .exec which for some values in my parameter list results in KO (value does not exists in the SUT).
I further have the need to print these values to a file so I later can remove them from the parameter list in order to not get KO`s.
I have a writer defined
val writer = {
val fos = new java.io.FileOutputStream("testresultater.txt")
new java.io.PrintWriter(fos,true)
}
and wonder how I could do this just for KOs inside the .exec resulting in KOs for some values like this:
.exec(http("request_lagDokument")
.post("/proxy/dokumenter/api/v1/SaveDokumentFil?IsDebug=true")
.headers(headers_3)
.body(ElFileBody("magnus/LagDokument.json"))
.check(status.is(expected = 200))
.check(jsonPath("$.DokumentGuid").saveAs("DokumentGuid")))
//if KO then:
.exec((s: Session) => {
writer.println(s.attributes("pnr"))
s
})
Is this possible?

You can do this by having a session function that is always executed with the conditional logic inside
.exec(session = {
if (session.isFailed) {
writer.println(s.attributes("pnr"))
}
session
})
or you can use the dsl's doIf
.doIf(session => session.isFailed) {
exec(session => {
writer.println(s.attributes("pnr"))
session
}
}

Related

Use stored values in another scenerio : Gatling

I want to store returned values from scenerio1 to list and use the same stored values in another scenerio .
I tried to do like below . but in get scenerio this gives error id not found . I also noticed that feeder is initialized before scenerio execution itself.
Could some one please help on this .
var l: List[String] = List()
var ids = l.map(id => Map(“id” -> id)).iterator
val install = scenario(" Install")
.during(Configuration.duration) {
group("installTest") {
exec(
testChain.installApplication()
).exec(session=>{
l = session(“id”).as[String].trim :: l
print(l)
session
})
}
}
val get = scenario(“get”)
.during(Configuration.duration) {
group(“getTest”) {
feed(ids)
exec(
session=>{
print(session(“id”).as[String])
session
}
)
}
}
setUp(
warmupScenario.inject(constantConcurrentUsers(1) during(1))
.andThen(
install.inject(nothingFor(10 seconds), (rampUsers(Configuration.users) during (Configuration.ramp seconds))))
.andThen(
get.inject(nothingFor(Configuration.duration seconds), (rampUsers(Configuration.users) during (Configuration.ramp seconds))))
)
.assertions(details("installTest").successfulRequests.percent.gte(Configuration.passPercentage))
.protocols(HTTP_PROTOCOL)

How to read the JSON file from HDFS path and use instead of MAP in Scala code

val airportCodeToTimezoneMap = Map("MAA" -> "Asia/Kolkata" , "SHJ" -> "Asia/Dubai")
val boardPoint = "MAA"
if( airportCodeToTimezoneMap.contains( boardPoint )) {
println("Code exists with value :" + airportCodeToTimezoneMap(boardPoint))
} else {
println("Code Not exist")
}
val tempValue = airportCodeToTimezoneMap(boardPoint)
println(tempValue)
Here instead of
MAP i want to configure in JSON File in HDFS path and use it .
Say For E.G
{
"airportCode": "AAL",
"zoneId": "Europe/Copenhagen"
},
{
"airportCode": "AAM",
"zoneId": "Africa/Johannesburg"
}
Please tell me how to write it .

How to capture by reference in rust macro

I have a macro to generate match arms:
macro_rules! sort_by {
( $query:ident, $sort_by:expr, { $( $name:pat => $column:path,)+ } ) => {
match $sort_by.column {
$(
$name => if $sort_by.descending {
$query = $query.order_by($column.desc());
} else {
$query = $query.order_by($column.asc());
},
)+
}
}
}
and I want to call it like this:
sort_by!(query, sort_by.unwrap_or(Sort::desc("id")), {
"id" => table::id,
"customerName" => table::customer_name,
});
But I'm getting an error:
sort_by!(query, &sort_by.unwrap_or(Sort::desc("id")), {
^^^^^^^ value moved here in previous iteration of loop
So I have to call it like this:
let sort = sort_by.unwrap_or(Sort::desc("id"));
sort_by!(query, &sort, {
"id" => table::id,
"customerName" => table::customer_name,
});
What should I change to be able to use the expression directly in the macro invocation?
Using a macro is equivalent to substituting the code it expands to into its call site. This means if the macro expansion contains $sort_by multiple times, the code will evaluate the expression you pass in as $sort_by multiple times. If the expression consumes some variable, this will be invalid.
This is in contrast to how function calls work. If you pass an expression to a function, it will be evaluated before calling the function, and only the result is passed to the function.
If this is the source of your problem, you can fix it by assigning $sort_by to a local variable inside your macro expansion, and only access the local variable subsequently:
macro_rules! sort_by {
($query:ident, $sort_by:expr, { $($name:pat => $column:path,)+ }) => {
let sort_by = $sort_by;
match sort_by.column {
$(
$name => if sort_by.descending {
$query = $query.order_by($column.desc());
} else {
$query = $query.order_by($column.asc());
},
)+
}
}
}
(Note that I could not test this, since your example is incomplete.)

scala.js form processing in client / access to form on scala.js client

I want submit a form and want to show the user the process with spinner and reload the new information.
#JSExport
def addToCart(form: html.Form): Unit = {
form.onsubmit = (e: dom.Event) => {
e.preventDefault()
}
val waitSpan = span(
`class` := Waiting.glyphIconWaitClass
)
val waiting = form.getElementsByTagName("button").head.appendChild(waitSpan.render)
dom.window.alert(JSON.stringify( form.elements.namedItem("quantity") ))
Ajax.InputData
Ajax.post(form.action,withCredentials = true).map{q =>
//
}
}
I have no access to form data. Also I cannot execute an ajax call to proof the form and execute it. I have found no way. Someone has an idea?
jQuery helps. I used them now to serialize the form. But now I have no longer the ability of play forms with bindOfRequest()
val jForm = $("#"+form.id)
val serialized = jForm.serialize()
Ajax.post(s"/js/api/form/${UUID.randomUUID().toString}",withCredentials = false,timeout = 12000,data = serialized,headers = Map("X-CSRFToken"->"nocheck","Csrf-Token"->"nocheck"))
I get always:
occurrence%5B%5D=400g&quantity=1&csrfToken=c1606da9a261a7f3284518d4f1fd63eaa8bbb59e-1483472204854-1c5af366c62520883474c160
But now I don´t know what I have to do. Sorry.
def executeAddToCartForm(articleId: UUID) = silhouette.UserAwareAction.async{implicit req =>
val form = complexCartForm.bindFromRequest()
Try(form.get) match {
case Success((i,seq)) => println("article: " + i)
case _ => println(form.errors.mkString + " " + req.body.asText + " " + URLDecoder.decode(req.body.asText.get,"UTF-8"))
}
Future.successful(Ok("danke"))
}
Always get failure :( I will have a look at react.
ADDED
Sometimes I need more sleep!
Ajax.post(
url = form.action,
withCredentials = true,
timeout = 12000,
data = serialized,
headers = Map("Content-Type" -> "application/x-www-form-urlencoded")
)
with this: headers = Map("Content-Type" -> "application/x-www-form-urlencoded") I can use the bindFromRequest() as usually :)
Coffee I need more

Adding a key value pair to an existing json object in perl

I want to add a key value pair to a JSON object. Following is the structure of Param{Data} variable for the below code.
$VAR1 = {
'ArticleID' => '86',
'OldTicketData' => {
...
},
'TicketID' => '67'
};
Following is the function in which I want to perform the mentioned operation:
sub PrepareRequest {
my ( $Self, %Param ) = #_;
my %TicketInfo = $Self->{TicketObject}->ArticleGet(
ArticleID => $Param{Data}->{ArticleID},
userID => $Param{Data}->{CustomerID},
);
my %newParamData = to_json($Param{Data});
%newParamData->{'OldTicketData'}->{'Body'}=$TicketInfo{Body};
return {
Success => 1,
Data => %newParamData,
};
}
Above function returns 'OldTicketData'. I want following key-pair attached to 'OldTicketData' element of the JSON object ->('Body', $TicketInfo{Body}). Consider, $TicketInfo{Body} returns a string 'someString'.
Your code is the wrong way around. You need to add the key to the hash reference first, before you turn it into JSON.
$Param{Data}->{'OldTicketData'}->{'Body'}=$TicketInfo{Body};
my $newParamData = to_json($Param{Data});
In addition, since to_json returns a string, which is scalar, you need to use $newParamData instead of %newParamData.
Of course you need to fix your return as well.
return {
Success => 1,
Data => $newParamData,
};