Stress Test POST with Multiple Body using Apache JMeter - java

How can I stress test POST multiple Body with specified body data?
Here are my Body Data:
{
"car": {
"brand": "toyota",
"model": "avanza",
},
"customer_id": "string"
}
How can I stress test it with multiple body from that ?
example like this one:
{
"car": {
"brand": "toyota",
"model": "avanza",
},
"customer_id": "string"
}
{
"car": {
"brand": "daihatsu",
"model": "xenia",
},
"customer_id": "string"
}
{
"car": {
"brand": "suzuki",
"model": "ertiga",
},
"customer_id": "string"
}
I have tried using User Defined Variables, but I can't use this one because I need specific body data from brand and model. Not like this brand = toyota, model = ertiga

First of all you need to store these brand/model pairs somewhere, i.e. in a file which looks like:
toyota,avanza
suzuki,ertiga
daihatsu,xenia
Second, you need to generate the request body JSON (by the way, your example is not valid, my code snippet assumes the following output:
[
{
"car": {
"brand": "toyota",
"model": "avanza"
},
"customer_id": "string"
},
{
"car": {
"brand": "daihatsu",
"model": "xenia"
},
"customer_id": "string"
},
{
"car": {
"brand": "suzuki",
"model": "ertiga"
},
"customer_id": "string"
}
]
Add JSR223 PreProcessor as a child of the request you want to parameterize and put the following code into "Script" area:
def payload = []
new File('test.csv').readLines().each { line ->
def car = [:]
car.put('brand', line.split(',')[0])
car.put('model', line.split(',')[1])
def car_entry = [:]
car_entry.put('car', car)
car_entry.put('customer_id', 'string')
payload.add(car_entry)
}
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
That's it, you should be able to refer the generated request body as ${payload} JMeter Variable where required
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

You can parametrize band/model and try to read in form csv file
so your body will be as below
"car": {
"brand": "${BRAND}",
"model": "${MODEL}"},
"customer_id": "string"}
Your csv file will be as below
BRAND,MODEL
toyota,avanza
suzuki,ertiga
daihatsu,xenia
to read data from CSV check out CSV_Data_Set_Config

Related

How to remove an attribute from RequestSpecification/FilterableRequestSpecification body?

Dears,
I am working on creating a simple method which will take String argument which will be a path or other kind "pointer" to attribute/s in JSON and this method will remove those attribute/s.
My problem is I can find values of those attribute/s using JsonPath, but I can't find methods in rest assured (or other libraries) which could remove/delete attributes by given path.
JSON is already added earlier so i need to pull him from RequestSpecification or FilterableRequestSpecification object ex.
RequestSpecification rs = *objFromContext*;
FilterableRequestSpecification frs= (FilterableRequestSpecification) rs;
frs.getBody();
I've tried to work with JSONObject class and remove() but it doesn't work on complex JSONs.
given example JSON
{
"created": "string",
"updated": "string",
"items": [
{
"code": "TEST",
"nested": {
"code": "test",
"name": "name",
"other": [
{
"code": "TEST",
"name": "myName",
"quantity": 1
}
]
},
"itemsProperties": [
{
"code": "value1",
"name": "name",
"value": 123
}
]
},
{
"code": "TEST",
"nested": {
"code": "test",
"name": "name",
"other": [
{
"code": "TEST",
"name": "myName",
"quantity": 1
}
]
},
"itemsProperties": [
{
"code": "value2",
"name": "name",
"value": 123
}
]
}
],
"timer": {
"startDate": "2015-01-01",
"endDate": "2021-01-02"
},
"id": "myId"
}
using JsonPath jp = JsonPath.from(httpRequest.getBody().toString());
and then jp.get(items.itemsproperties.code) i can find value1 and value2.
I stuck in this point: How to remove those attributes from sended body?
I know i can convert body into JSONObject and then go field after field conversion between getJSONArray and GetJSONOBject and remove those fields, but i would like to make this metod much more universal.
Is this possible?
If you want to manipulate json in Rest-Assured JsonPath, then the answer is No. You can't do that. JsonPath help you to extract value from json, that's it, no more.
You have to use different libraries to remove key-value pair.
For example: using JsonPath Jayway
DocumentContext parse = JsonPath.parse(body);
parse.delete("$..itemsProperties..code");
System.out.println(parse.jsonString());

Filter nested json data using jsonpath as in example

I am using jsonpath to filter.
Json(Dummy json just to explain) source String, which is basically a list of Operating systems and details of its programs etc. In this example, the OS whose id = 1403 is a windows 10 OS and has 2 features acchritecture and browser. There are more details to the browser feature as shown in json
[
{
"id": 1403,
"os": "window 10",
"features": [
{
"id": 1356,
"name": "architecture",
"value": [
{
"id": 1308,
"feature": [
{
"id": 1262,
"key": "name",
"value": "amd64"
}
]
}
],
"category": "cat1"
},
{
"id": 1357,
"name": "browser",
"value": [
{
"id": 1309,
"feature": [
{
"id": 1263,
"key": "name",
"value": "Firefox"
},
{
"id": 1265,
"key": "version",
"value": "187"
}
]
}
],
"category": "cat2"
}
]
},
{
"id": 2804,
"os": "window 7",
"features": [
{
"id": 2764,
"name": "architecture",
"value": [
{
"id": 2719,
"feature": [
{
"id": 2679,
"key": "name",
"value": "amd64"
}
]
}
],
"category": "cat1"
},
{
"id": 2765,
"name": "browser",
"value": [
{
"id": 2722,
"feature": [
{
"id": 2685,
"key": "name",
"value": "Chrome"
},
{
"id": 2684,
"key": "version",
"value": "87.0.4280.88"
}
]
}
],
"category": "cat2"
}
]
}
]
I want to be able to filter the json such that
features[*].name == 'browser' and features[*].value[*].feature[*].value == 'chrome'
What will be the JsonPath string that can help me achieve above query? The above query uses similar syntax used by JsonPath string but doesn't do the job. Its just to explain.
There is another example here gets Movie Title Given 'Starring' field
And would like to get the full OS json that fulfils this condition. In this case a array of OS which contains only one OS i.e. with id= 2804
[
{
"id": "2804",
...
}
]
I am stuck much before what aim to achieve. Here is my code to get all the OS that have "name=browser". I get the array but it only contains value[] items. I want it get the full json. It returns object with IDs- 1357, 2765.
List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString)
.read("$[*].features[*].[?(#.name == 'browser')]");
To get the outer array you need to use the filter like $[?(...)]
For your current use case, we need to use nested array filters. There is an open issue in JsonPath for filter on children level. (Refer here).
Luckily, there is a workaround suggested to use contains over here.
we can use the below expression to filter:
List<Object> expensive = JsonPath.parse(jsonDataSourceString)
.read("$[?(#.features[?(#.name == 'browser')].value[*].feature[*].value contains 'Chrome')]");
Prints the below output
{id=2804, os=window 7, features=[{"id":2764,"name":"architecture","value":[{"id":2719,"feature":[{"id":2679,"key":"name","value":"amd64"}]}],"category":"cat1"},{"id":2765,"name":"browser","value":[{"id":2722,"feature":[{"id":2685,"key":"name","value":"Chrome"},{"id":2684,"key":"version","value":"87.0.4280.88"}]}],"category":"cat2"}]}

how to create linked hash map object for this json

I am trying to call an API using Retrofit in android. For this API I need the input parameter JSON object in proper sequence.
Only if I have the json in required sequence I get successful response from the API, otherwise it gives an error. To handle this issue I got one solution that is to first create a LinkedHashMap of input parameter then create the JSON of that LinkedHashMap. This way I'm acheaving the response from api.
but right now im confused how to create linkedHash map for below json
{
"RequestXml": {
"Authenticate": {
"InterfaceCode": "1",
"InterfaceAuthKey": "AirticketOnlineWebSite",
"AgentCode": "MOS0000001",
"Password": "KGBW5P"
},
"BookTicketRequest": {
"TrackNo": "0$182967|4|1AO",
"MobileNo": "9099776464",
"AltMobileNo": "9898989898",
"Email": "abc#gmail.com",
"Address": "Test",
"ClientRequestID": "",
"Passengers": {
"Passenger": [
{
"PaxSeqNo": "1",
"Title": "Mr",
"FirstName": "Savan",
"LastName": "Test",
"PassengerType": "A",
"DateOfBirth": "01/12/1992",
"PassportNo": "RTTTTGGBGB56356",
"PassportExpDate": "01/12/2024",
"PassportIssuingCountry": "IND",
"NationalityCountry": "IND"
}
]
},
"Segments": {
"Segment": [
{
"TrackNo":"0$182967|4|1AO",
"SegmentSeqNo": "1",
"AirlineCode": "UK",
"FlightNo": "888",
"FromAirportCode": "BOM",
"ToAirportCode": "DEL",
"DepDate": "30/09/2019",
"DepTime": "14:00",
"ArrDate": "30/09/2019",
"ArrTime": "16:00",
"FlightClass": "E",
"MainClass": "Y"
}
]
},
"AdditionalServices": {
},
"TotalAmount": "4735",
"MerchantCode": "PAY9zJhspxq7m",
"MerchantKey": "eSpbcYMkPoZYFPcE8FnZ",
"SaltKey": "WHJIIcNjVXaZj03TnDme",
"IsTicketing": "Yes"
}
}
}

Java: Properties file as JSON in Android

On the server I have a .properties file with the following structure
foo.0.a = foo0A
foo.0.b = foo0B
foo.1.a = foo1A
foo.1.b = foo1B
...
bar.0.a = bar0a
bar.0.b = bar0b
...
I'm using Volley to fetch the file from the server as JSONObject but the output is being returned in the following form
"properties": [
{
"key": "foo.0.a",
"type": "text/plain",
"value": "foo0A"
},
{
"key": "bar.0.a",
"type": "text/plain",
"value": "bar0a"
},
]
This is not the format I'm looking for, and I think it is not convenient anyway.
The wanted format is something like this:
{
"foo": [
{
"id": 0,
"a": "foo0a",
"b": "foo0b"
}, ...
]
"bar": [
{
"id": 0,
"a": "bar0a",
"b": "bar0b"
}, ...
]
}
My question: Should the properties' structure be changed? Or this can be solved by some code on the client side? If it should be changed, what is the best structure?

Exclude properties from Json data without deserializing it

I have Json data like this:
{
"_id": "123",
"transaction": {
"className": "ExpenseReport",
"id": "789",
"createdBy": {
"firstName": "Donald",
"lastName": "Morgan",
"address": {
"street1": "1362 Woodlawn Lane",
"street2": "Suite #100805",
"place": {
"city": "Darien",
"state": "CA",
"country": "USA",
"number": "OBJ-4823478",
"createdBy": "Brett Wright"
},
"zip": 88884,
"number": "OBJ-5740231",
"createdBy": "Brett Wright"
},
"number": "OBJ-3561551",
"createdBy": "Brett Wright"
},
"score": 12,
"reasonCodes": [
"these",
"are",
"strings"
]
}
}
I want a subset of this data after excluding some properties, say something like this:
{
"_id": "123",
"transactionType": "EXPENSE_REPORT",
"transaction": {
"className": "ExpenseReport",
"id": "789",
"createdBy": {
"firstName": "Donald",
"lastName": "Morgan",
"address": {
"street1": "1362 Woodlawn Lane",
"street2": "Suite #100805",
"place": {
"city": "Darien",
"state": "CA",
"country": "USA"
},
"createdBy": "Brett Wright"
},
"createdBy": "Brett Wright"
},
"score": 12
}
}
Now one way would be to deserialize the original json data into a POJO, use Jackson Views to annotate the required properties, and then serialize the same POJO again to get the Json data without the properties.
But I want to achieve something like this WITHOUT DESERIALIZING the Json data, say by just parsing the json data and removing the key-value pairs if they are not found in a collection. Is anyone aware of any library that does that?
Jackson allows you to do only the parsing step using ObjectMapper.readTree()
JsonNode root = om.readTree(input);
The resulting JsonNodes are mutable, so something like this does the job:
ObjectNode place = (ObjectNode)(root.findPath("transaction")
.findPath("createdBy")
.findPath("address")
.findPath("place")
);
place.remove("number");
This is --unfortunately-- not too nice, but can be easily wrapped into a generic method that takes a property path:
void deleteProperty(JsonNode root, List<String> propPath)
{
JsonNode node = root;
for (String propName: propPath.subList(0, propPath.size() - 1)) {
node = node.findPath(propName);
}
// completely ignore missing properties
if ((! node.isMissingNode()) && (! node.isEmpty())) {
if (node instanceof ObjectNode) {
final ObjectNode parent = (ObjectNode)node;
parent.remove(propPath.get(propPath.size() - 1));
}
}
}
It is then possible to write out the modified node tree using writeTree().
There is also the property filter API. Unfortunately while it is easy to filter out individual properties with it, it is non-trivial to use it for property paths. For example, in your case, the trivial filter can only filter out all createdBy properties.

Categories

Resources