Get first json object from response rest service by Beanshell in jmeter - jmeter

I get this JSON response from my web service:
[
{
"type": "022",
"sendDate": "2020-11-09 12:43:07",
"message": "test1",
"id": 8035,
},
{
"notificationType": "023",
"sendDate": "2020-11-09 11:40:02",
"message": "test2 ",
"id": 8034,
},...
]
Now by Beanshell in JMeter I want to pass first id into new request so in JSR223 PostProcessor i write a simple print like this:
import com.eclipsesource.json.*;
String jsonString = prev.getResponseDataAsString();
log.info("value=" + jsonString);
JsonArray inbox = Json.parse(jsonString).asArray();
//String id_BSH = ((JSONObject) storeArray.get(0)).getAsString("id");
//vars.put("id_BSH", pickup);
but i got this error :
2020-11-10 17:59:55,245 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.eclipsesource.json.*; String jsonString = prev.getResponseDataAsStrin . . . '' : Typed variable declaration : Class: JsonArray not found in namespace : at Line: 4 : in file: inline evaluation of: ``import com.eclipsesource.json.*; String jsonString = prev.getResponseDataAsStrin . . . '' : JsonArray
in inline evaluation of: ``import com.eclipsesource.json.*; String jsonString = prev.getResponseDataAsStrin . . . '' at line number 4
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) ~[?:1.8.0_202]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:224) ~[ApacheJMeter_core.jar:5.3]
at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor.java:45) [ApacheJMeter_components.jar:5.3]
at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:940) [ApacheJMeter_core.jar:5.3]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:572) [ApacheJMeter_core.jar:5.3]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.3]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.3]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_202]

I've heard Groovy "is the new black", moreover you should not be using Beanshell since JMeter 3.1 so you can go for Groovy's JsonSlurper class and store the first ID into a variable with this one-liner:
vars.put("id_BSH", new groovy.json.JsonSlurper().parse(prev.getResponseData()).get(0).id as String)
More information: Apache Groovy - Parsing and Producing JSON

Related

Unable to generate example json value from yaml

I am trying to parse a given yaml to a JSON example like swagger does. I am using the below yaml.
$id: 'https://example.com/person.schema.json'
$schema: 'http://json-schema.org/draft-07/schema#'
title: Person
type: object
properties:
firstName:
type: string
example:MyName
description: The person's first name.
lastName:
type: string
description: The person's last name.
age:
description: Age in years which must be equal to or greater than zero.
type: integer
minimum: 0
I am trying to parse this yaml to its json like the below one. If the example is given in the yaml then it should show the example else it should show a default value.
Person:{
"firstName":"MyName",
"lastName":"string",
"age":0
}
But while parsing I am getting an Exception:
com.fasterxml.jackson.core.JsonParseException: Unrecognized token '$id': was expecting ('true', 'false' or 'null')
at [Source: (String)"$id: 'https://example.com/person.schema.json'
line: 1, column: 4]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804) ~[jackson-core-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:703) ~[jackson-core-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2853) ~[jackson-core-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1899) ~[jackson-core-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:757) ~[jackson-core-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.databind.ObjectMapper._readTreeAndClose(ObjectMapper.java:4042) ~[jackson-databind-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2551) ~[jackson-databind-2.9.8.jar:2.9.8]
at io.swagger.parser.SwaggerCompatConverter.readResourceListing(SwaggerCompatConverter.java:210) [swagger-compat-spec-parser-1.0.44.jar:1.0.44]
at io.swagger.parser.SwaggerCompatConverter.read(SwaggerCompatConverter.java:123) [swagger-compat-spec-parser-1.0.44.jar:1.0.44]
at io.swagger.parser.SwaggerCompatConverter.readWithInfo(SwaggerCompatConverter.java:94) [swagger-compat-spec-parser-1.0.44.jar:1.0.44]
at io.swagger.parser.SwaggerParser.readWithInfo(SwaggerParser.java:42) [swagger-parser-1.0.44.jar:1.0.44]
at io.swagger.v3.parser.converter.SwaggerConverter.readLocation(SwaggerConverter.java:92) [swagger-parser-v2-converter-2.0.12.jar:2.0.12]
at io.swagger.v3.parser.OpenAPIV3Parser.read(OpenAPIV3Parser.java:90) [swagger-parser-v3-2.0.12.jar:2.0.12]
at io.swagger.v3.parser.OpenAPIV3Parser.read(OpenAPIV3Parser.java:78) [swagger-parser-v3-2.0.12.jar:2.0.12]
at com.pega.yaml.yamlToJsonExample.App.main(App.java:19) [classes/:na]
Exception in thread "main" java.lang.NullPointerException
at io.swagger.v3.parser.OpenAPIV3Parser.read(OpenAPIV3Parser.java:91)
at io.swagger.v3.parser.OpenAPIV3Parser.read(OpenAPIV3Parser.java:78)
at com.pega.yaml.yamlToJsonExample.App.main(App.java:19)
I am using swagger inflector 2.0.5 with the following code:
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.oas.inflector.examples.models.Example;
import io.swagger.oas.inflector.examples.ExampleBuilder;
import java.util.Map;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.swagger.oas.inflector.processors.JsonNodeExampleSerializer;
import io.swagger.util.Json;
public class App
{
public static void main( String[] args )
{
OpenAPI swagger = new OpenAPIV3Parser().read("Eclipse Workspace1\\yamlToJsonExample\\src\\main\\java\\com\\pega\\yaml\\yamlToJsonExample\\Test.yaml");
Map < String, Schema > definitions = swagger.getComponents().getSchemas();
Schema model = definitions.get("Person");
Example example = ExampleBuilder.fromSchema(model, definitions);
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
String jsonExample = Json.pretty(example);
System.out.println(jsonExample);
}
}

How to assert failure if string is in JSON response from http request JMeter

I am trying to write a test that ensures that the name property in a JSON response is not MYGROUP. I'm stuck here because I'm trying to use the code from a test that checks that the name property is MYGROUP. I know I need to fix something in the if condition of this test:
# Check AD group deletion
- url: ${ADSubscriptions}/${NetworkID}/clientsyncs/${syncId}/groups
label: 1360_CheckADGroupDeletion
method: GET
headers:
Authorization: Bearer ${access_token}
Content-Type: "application/json"
Accept: "application/json, text/plain, */*"
assert-httpcode:
- 200
- 202
jsr223:
- langauge: groovy
execute: after
script-text: |
import groovy.json.JsonSlurper
def slurperresponse = new JsonSlurper().parseText(prev.getResponseDataAsString())
for (entry in slurperresponse){
if(entry.name == "MYGROUP" || entry.name == "mygroup" ){
{RETURN FALSE HERE}
break;
}
}
- langauge: groovy
execute: after
script-file: jsr223/logger.groovy
parameters: check_adgroup_deletion
The expected json structure for the request used in the test looks like this:
[
{
"id": "id1",
"name": "MYGROUP",
"selected": true
}
]
The above response would equate to a failing test. The below would equate to a passing test.
[
{
"id": "id1",
"name": "MYGROUP",
"selected": false
}
]
The code snippet you're looking for is prev.setSuccessful(false) where prev stands for the parent SampleResult class instance.
See the above JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article to learn more about JMeter API shorthands available for JSR223 blocks.
In particular your case it would be easier to use assert-jsonpath Taurus assertion

Error while uploading a file with gzipped content via HTTP POST

I am trying to upload a file using HTTP POST sampler, at the same time compressing the file content. For this, I have added Content-Encoding:gzip in the Header Manager and in the pre processor of the sampler added following code
import org.apache.commons.io.IOUtils;
import java.util.zip.GZIPOutputStream;
String bodyString = sampler.getArguments().getArgument(0).getValue();
byte [] requestBody = bodyString.getBytes();
ByteArrayOutputStream out = new ByteArrayOutputStream(requestBody.length);
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(requestBody);
gzip.close();
sampler.getArguments().getArgument(0).setValue(out.toString(0));
This was not working and it was throwing following exception
2019-08-07 14:08:21,970 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor
javax.script.ScriptException: Sourced file: inline evaluation of: ``import org.apache.commons.io.IOUtils; import java.util.zip.GZIPOutputStream; S . . . '' : Typed variable declaration : at Line: 5 : in file: inline evaluation of: ``import org.apache.commons.io.IOUtils; import java.util.zip.GZIPOutputStream; S . . . '' : .getValue ( )
Target exception: java.lang.NullPointerException: Attempt to invoke method getValue on null value
in inline evaluation of: ``import org.apache.commons.io.IOUtils; import java.util.zip.GZIPOutputStream; S . . . '' at line number 5
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:87) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_73]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:225) ~[ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:935) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:537) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.1.1 r1855137]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
Now to debug added log.info("sampler-------:"+sampler) before getValue like
log.info("sampler-------:"+sampler);
String bodyString = sampler.getArguments().getArgument(0).getValue();
But in the logs sampler returned empty
Query Data:
So how should I access the file content in the HTTP Request and at the same time gzip it and send?
To gzip the file and its contents use the following code and put it in JSR223 Sampler [this should be before calling POST API] and the assumption is payload data is already copied to a file.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
String source_filepath = "C:/Sample/Input.txt";
String destinaton_zip_filepath = "C:/Sample/Input.gzip";
byte[] buffer = new byte[1024];
try {
FileOutputStream fileOutputStream =new FileOutputStream(destinaton_zip_filepath);
GZIPOutputStream gzipOuputStream = new GZIPOutputStream(fileOutputStream);
FileInputStream fileInput = new FileInputStream(source_filepath);
int bytes_read;
while ((bytes_read = fileInput.read(buffer)) > 0) {
gzipOuputStream.write(buffer, 0, bytes_read);
}
fileInput.close();
gzipOuputStream.finish();
gzipOuputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
vars.put("ZipFileLocation",destinaton_zip_filepath);
Now the gzip file can be used via ${ZipFileLocation} variable.
It is up to how the API is called, following can be the solution:
Directly send this gzip file in the API
Declare a variable and hold all your payload data into it and then call the API -> gzip it
Note : Correct MIME type and Headers need to be used wherever applicable
If you're doing a file upload with JMeter, to wit your file is being passed via "Files Upload" section - you will not be able to get anything using getArguments() function, you will need to use getHTTPFiles() instead, suggested algorithm:
Get the required file path like:
def plainFile = sampler.getHTTPFiles()[0].path
Compress the file according to your server expectations
Replace the original file path with compressed file path
sampler.getHTTPFiles()[0].setPath("/path/to/compressed/file.gz")

JMeter - How to read JSON file?

I used the CSV format for the data files in JMeter. Our VP wants to change the format to JSON. How can I read a JSON file from the disk?
You have at least 2 options:
Use HTTP Request sampler and file protocol like:
JSON files are basically plain-text files so you will be able to use JSON Extractor or JSON Path Extractor to parse JSON data and store the result into JMeter Variables for later reuse
References:
Jayway JsonPath - Getting Started
Advanced Usage of the JSON Path Extractor in JMeter
Use JSR223 Test Elements and Groovy language. Groovy has built-in JSON support via JsonSlurper so you will be able to parse the JSON file programmatically.
References:
The Groovy programming language - Parsing and producing JSON
Groovy - JSON
The best solution for this problem is to use JSR223 Sampler to read JSON file and load into the vars or props and use it wherever it is required.
For this,
Add a JSR223 Sampler in JMeter
Add the below code
import java.io.File
import java.util.Base64
def propertiesFromMap(map, prefix) {
for (item in map) {
id = prefix+item.key;
if (item.value instanceof Map) {
propertiesFromMap(item.value, id+".");
} else {
value = item.value.toString();
log.info("Loading property " + id + ": " + value);
if(value.startsWith("ENC:")) {
props.put(id, new String(Base64.getDecoder().decode(value.substring(4, value.length()))));
} else {
props.put(id, value);
}
}
}
}
def baseDir = org.apache.jmeter.services.FileServer.getFileServer().getBaseDir();
def jsonFilePath= baseDir + "/configs/" + "configuration-dev" + ".json";
log.info("Loading properties from " + jsonFilePath);
def jsonMap = new groovy.json.JsonSlurper().parse(new java.io.File(jsonFilePath));
propertiesFromMap(jsonMap, '');
Create a directory named configs where you placed the .jmx file
Create a file named configuration-dev.json in configs directory
Add JSON content e.g. in the file:
{
"key1": "value1",
"key2": "value2",
"key3": "ENC:base64_encoded_value"
"group1": {
"grp1_key": "value"
}
Access values in Samplers e.g. ${__P(key1)}, it will return value of key1. For group1 variables access e.g. ${__P(group1.grp1_key)}
For accessing the values into JSR223 use props e.g. props.get("key1")
You can pass base64 encoded values into JSON by adding ENC: as value prefix which will be resolved automatically when accessing values from props or __P()

Trying to create connection betwee jmeter and xmpp server

Trying to create connection but displayed error . Below are the Beanshell sampler code .
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
String jabberId = "admin";
String jabberPass = "12345";
String SERVER_ADDRESS = "xxx.xxx.xxx.xxx";
int PORT = 5222; // or any other port
String ServiceName = "Smack";
SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setCompressionEnabled(false)
.setHost(SERVER_ADDRESS)
.setServiceName(ServiceName)
.setPort(DEFAULT_PORT)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setSendPresence(true)
.setDebuggerEnabled(true)
.build();
XMPPTCPConnection con = new XMPPTCPConnection(config);
int REPLY_TIMEOUT = 50000; // 50 seconds, but can be shorter
con.setPacketReplyTimeout(REPLY_TIMEOUT);
//con = getConnection();
con.connect();
//con.login(jabberId,jabberPass);
Below are the error..
Response code: 500 Response message:
org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval
In file: inline evaluation of: ``public XMPPConnection doConnect(String userName,String password) { XMPPConne . . . '' Encountered "( xxx.xxx .xxx" at line 7, column 60.
please tell me what's wrong with it . or give me correct code to connect jmeter to xmpp server .

Resources