Reputation: 127
I have an API to fetch the list of employee Name's in an organization and it supports order by clause. I invoked an API "get /employeeName?$ordeyby=name desc". I got the results like below,
{
"value":[
{
"name":"Sam"
},
{
"name":"Peter"
},
{
"name":"Harry"
},
{
"name":"Arnold"
}]
}
I have parsed each name and stored into a variable of string type.
How can I verify using JAVA Script/BeanShell/Groovy, that the returned response is in descending order?
Can anyone please help here. Any of the above-mentioned languages is fine and I want this needs to be implemented in JMeter.
Thanks in advance.
Upvotes: 2
Views: 2265
Reputation: 168147
Put the following code into "Script" area:
def expected = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..name').sort().reverse()
new groovy.json.JsonSlurper().parse(prev.getResponseData()).value.eachWithIndex { def entry, int i ->
if (!entry.name.equals(expected.get(i))) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Order mismatch, expected: ' + expected.get(i) + ', got: ' + entry.name)
}
}
More information:
Upvotes: 1
Reputation: 1616
You can sort it in js using this approach
var employers = [
{
"name":"Sam"
},
{
"name":"Peter"
},
{
"name":"Harry"
},
{
"name":"Arnold"
}];
console.log(employers.sort(function(e1,e2){
var alc = e1.name.toLowerCase(), blc = e2.name.toLowerCase();
return alc > blc ? 1 : alc < blc ? -1 : 0;
}));
Upvotes: 1