Get a random field from a Delayed Response response with multiple items
Migrating from Ruby to Java, I need to parse and get one field randomly from the response of multiple items.
This is my ApiCall method that I am using to get the response:
Response response = given().
headers(this.headers).
params(this.body).
when().
post(this.url).
then().
contentType(ContentType.JSON).
statusCode(200).
extract().
response();
And it gives me this JSON structure:
{
"Contents": {
"Title": "Search results",
"Count": "10",
"Page": "1",
"TotalCount": "1",
"TotalPages": 2,
"Genres": [
"Genre_1",
"Genre_2",
"Genre_3"
],
"Contents": [
{
"title": "content1_title",
"original_text": "original text 1",
"full_text": "Sample full sized text 1",
"short_text": "Sample short text 1",
"children_ids": {
"item": [
1,
2
]
},
"children_uuids": {
"item": [
"item1_uuid",
"item2_uuid"
]
},
"parent_ids": {
"item": [
1
]
},
"parent_uuids": {
"item": [
"item1_uuid"
]
},
"aired_from": "1994-01-01",
"aired_to": "1994-12-31",
"tags": {
"item": [
""
]
},
"available_condition1": 0,
"available_condition2": 1,
"price_condition1": "0.00",
"price_condition2": "13.00"
},
{
"title": "content2_title",
"original_text": "original text 2",
"full_text": "Sample full sized text 2",
"short_text": "Sample short text 2",
"children_ids": {
"item": [
1,
2
]
},
"children_uuids": {
"item": [
"item1_uuid",
"item2_uuid"
]
},
"parent_ids": {
"item": [
1
]
},
"parent_uuids": {
"item": [
"item1_uuid"
]
},
"aired_from": "1998-01-01",
"aired_to": "1998-01-31",
"tags": {
"item": [
""
]
},
"available_condition1": 0,
"available_condition2": 1,
"price_condition1": "0.00",
"price_condition2": "13.00"
}
]
},
"Success": true
}
The point is that I need to get a random "title" field that has at least one "children_uuids" out of the total number of units in the response.
So as I understand the steps:
1) get the total size of the "Contents.Contents" items. 2) get a random number between 0 and the total number of items. 3) use this number to select one item with the format "Contents.Contents. [N] .title" or similar.
Tried the following without success:
JsonPath jsonPath = new JsonPath(response.toString());
List<?> list = jsonPath.getList("Contents.Contents.title.flatten()");
System.out.println(list);
This gives me the following error:
io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document
For reference, in Ruby the code will be:
amount = @result['api_response']['Contents']['Contents'].count
amount = amount - 1
a = rand(0..amount)
while @result['api_response']['Contents']['Contents'][a]['children_uuids']['item'].nil? do
a = rand(0..amount)
#children_uuids = $result['api_response']['Contents']['Contents'][a]['children_uuids']
end
#price_hd = @result['api_response']['Contents']['Contents'][a]['price_hd']
content_title = @result['api_response']['Contents']['Contents'][a]['title']
UPDATE: It works for me partially ... I found a way to select one item from the list using this line:
String contentTitle = response.then().extract().path("Contents.Contents[0].title");
But can't find a way to use this jsonpath
String contentTitle = response.then().extract().path("Contents.Contents.[?(@.children_uuids)].uuid");
This second line gives me:
java.lang.IllegalArgumentException:
Invalid JSON expression:
Script1.groovy: 1: unexpected token: [ @ line 1, column 45.
nRootObject.Contents.Contents.[?(@.child
^
Thanks in advance.
source to share
UPDATE: I found a solution with the following code:
// get random content data
Integer contentAmmount = response.body().path("Contents.Contents.size()");
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(contentAmmount);
while (response.body().path(
String.join("", "Contents.Contents[", Integer.toString(randomInt),
"].children_uuids.item")).toString().isEmpty() ) {
randomInt = randomGenerator.nextInt(contentAmmount);
}
String contentTitle = response.then().extract()
.path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].title"));
String contentUuid = response.then().extract()
.path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].uuid"));
String contentChildrenUuid = response.body().path(
String.join("", "Contents.Contents[", Integer.toString(randomInt),
"].children_uuids.item"));
source to share
In my opinion this is very difficult to do in REST-certainties and maybe even in Java, for that matter.
May I suggest you take a look at Karate (disclaimer: am dev). So you can call a JavaScript function to generate a random number, assign that to a variable, and then form a path expression to do exactly what you want:
Feature:
Scenario:
* def data = read('data.json')
* def size = (data.Contents.Contents.length)
* def index = Math.floor(Math.random() * size)
* print 'selected index: ' + index
* def item = (data.Contents.Contents[index])
* def children_uuids = item.children_uuids
* match children_uuids == { item: ['item1_uuid', 'item2_uuid'] }
source to share