JSON arrays with duplicate items with Hamcrest and RestAssured - hamcrest

As a follow up question for my question in Dealing arrays with hamcrest and rest assured
How can I use hamcrest with restassured so that I can test
{
"mobilenum": "+6519829340",
"firstname": "Allen",
"lastname": "Edwards",
"location": "Singapore"
"outbound": "YES"
"count" : 15
},
{
"mobilenum": "+6519829340",
"firstname": "Allen",
"lastname": "Edwards",
"location": "Singapore"
"outbound": "NO"
"count" : 9
}
That there exist two types of data, one containing mobilenum, firstname, etc having outbound equal to yes, and the other no.
It would be like having two objects having the same properties except the outbound property.
An answer by John, from the previous question is this:
.root("smsentries.find { it.mobilenum == '%s' }").
.body("firstname", withArgs("+6519829340"), equalTo("Allen")
.body("lastname", withArgs("+6519829340"), equalTo("Edwards").
.body("firstname", withArgs("+12345678"), equalTo("John")
.body("lastname", withArgs("+12345678"), equalTo("Doe").
I don't know how to add something like withArgs("Allen") and ("Edwards) .equalTo("outbound")
UPDATE
What I hope to happen is like this:
for (Map.Entry<String,JsonElement> entry : o.entrySet()) {
if (entry.getKey().equals("smsentries")) {
JsonArray array = entry.getValue().getAsJsonArray();
for (JsonElement elementJSON : array) {
SMSEntry smsEntry = mapper.readValue(elementJSON.toString(), SMSEntry.class);
if (smsEntry.getMobilenum().equals("+6519829340") &&
smsEntry.getOutbound().equals("YES")) {
assertThat(smsEntry.getLocation(), equalTo("Singapore"));
assertThat(smsEntry.getCount(), equalTo(15));
}
}
}
}
If I have a mobile number equal to +6519829340 and is outbound, assert that the location is in Singapore and has count of 15.

If I understand you correctly (and that the list of users(?) is called smsentries as it was in the previous question) you could do like this:
.root("smsentries.findAll { it.mobilenum == '%s' }").
.body("firstname", withArgs("+6519829340"), contains("Allen", "Allen"))
.body("lastname", withArgs("+6519829340"), contains("Edwards", "Edwards"))
.body("outbound", withArgs("+6519829340"), containsInAnyOrder("YES", "NO"))
// Additional body matchers
Update after clarification
If I have a mobile number equal to +6519829340 and is outbound, assert
that the location is in Singapore and has count of 15.
You can do like this:
.root("smsentries.find { it.mobilenum == '+6519829340' && it.outbound == 'YES'}").
.body("location", equalTo("Singapore"))
.body("count", equalTo(9))

Related

Restassured: How Can we compare each element in Json array to one particular Same value in Java using Hemcrest Matchers, not using Foreach loop

Restassured: How Can we compare each element in Json array to one particular Same value in Java using Hemcrest Matchers, not using Foreach loop.
{
"id": 52352,
"name": "Great Apartments",
"floorplans": [
{
"id": 5342622,
"name": "THE STUDIO",
"fpCustomAmenities": [
{
"displaySequence": 2,
"amenityPartnerId": "gadasd",
"display": true,
"leased": true
},
{
"displaySequence": 13,
"amenityPartnerId": "sdfsfd",
"display": true,
"leased": true
}
]
},
{
"id": 4321020,
"name": "THE First Bed",
"fpCustomAmenities": [
{
"displaySequence": 4,
"amenityPartnerId": "gadasd",
"display": true,
"leased": true
},
{
"displaySequence": 15,
"amenityPartnerId": "hsfdsdf",
"display": true,
"leased": true
}
]
}
]
}
I want to compare that Leased=true for all the leased nodes at all the levels in the json response...
I have working code...
List<List<Boolean>> displayedvaluesfpStandardAmenities =
when().get(baseUrl + restUrl).
then().statusCode(200).log().ifError().
extract().body().jsonPath().getList("floorplans.fpCustomAmenities.display");
for (List<Boolean> displayedStandardList : displayedvaluesfpStandardAmenities) {
for (Boolean isDisplayedTrue : displayedStandardList) {
softAssert.assertTrue(isDisplayedTrue);
}
}
But the issue is I need the code to be in simple format using either Hemcrest Matchers or Restaussred Matchers and try simplistic way like Below, ( which is not working)
when().get(baseUrl + restUrl).
then().assertThat().body("floorplans.fpCustomAmenities.display",equalTo("true"));
The error I am getting is
java.lang.AssertionError: 1 expectation failed.
JSON path floorplans.fpCustomAmenities.display doesn't match.
Expected: true
Actual: <[[true, true], [true, true]]>
So what I need is the that all thes 'display' nodes in the json response where ever it is need to compared with "true", so that my test can Pass.
I have an alternate solution like mentioned above, but All I need is working solution using matchers.
Assuming fpCustomAmenities arrays are not empty, you can use the following solution;
when().get(baseUrl + restUrl).then()
.body("floorplans.findAll { it }.fpCustomAmenities" + // 1st line
".findAll { it }.leased.each{ a -> println a }" + // 2nd line
".grep{ it.contains(false) }.size()", equalTo(0)); // 3rd line
Here from the 1st line, we return each object in fpCustomAmenities array.
From the 2nd line we get boolean value of leased in each fpCustomAmenities object to a boolean array ([true, true]).
Each boolean array is printed from .each{ a -> println a }. I added it only to explain the answer. It is not relevant to the solution.
From 3rd line we check whether, if there is a false in each boolean array. grep() will return only the arrays which has a false. And then we get the filtered array count. Then we check whether it is equal to 0.
Check groovy documentation for more details.
Or
This solution does not use any Matchers. But this works.
String responseBody = when().get(baseUrl + restUrl).
then().extract().response().getBody().asPrettyString();
Assert.assertFalse(responseBody.contains("\"leased\": false"));

OPA Rego function statement evaluation order

package play
exists(obj, a) {
obj[a]
}
hello {
exists(input, "department")
contains(input["location"], "London")
}
world {
contains(input["location"], "London")
exists(input, "department")
}
input = {
"department": "Eng",
"location": "London" }
Above code matches only hello. Why world does not match even when the conditions are same, but order reversed ?
The order of the statements does not matter. You've actually found a bug!
If you change the example slightly so that exists is not called with input as the first argument but instead something like exists(input.user, "department") and then you update the input document to reflect that:
{"user": {"department": "Eng", "location": "London"}}
You'll observe the correct behaviour (e.g., world { contains(input.user["location"], "London"); exists(input.user, "department") }).

Rest Assured Body handling ArrayList

I has a response body like this
enter code here
{
"applicationName": "Service MyService",
"someData": [
{
"name": "check1",
"props": [
"AAaa"
]
},
{
"name": "check2",
"props": [
"BBbb",
"CCcc"
]
}
]
}
Now I can use the following code and the test passes.
given().log().all()
.accept(JSON).expect().statusCode(SC_OK)
.when().log().all()
.get(contextPath + "/test")
.then().log().all()
.body("someData.name",
IsCollectionWithSize.hasSize(2))
.body("someData.name",
allOf(hasItems("check1", "check2")))
.body("someData.findAll {it.name == 'check1'}.props",
IsCollectionWithSize.hasSize(1))
.body("healthReports.findAll {it.name == 'check2'}.props",
IsCollectionWithSize.hasSize(2)));
However if I then attempt to check the values in the props field it fails I think because a ArrayList is returned and the matchers are checking on String.
given().log().all()
.accept(JSON).expect().statusCode(SC_OK)
.when().log().all()
.get(contextPath + "/test")
.then().log().all()
.body("someData.name",
IsCollectionWithSize.hasSize(2))
.body("healthReports.findAll {it.name == 'check1'}.props",
IsCollectionContaining.hasItems(startsWith("AA")));
I'm not sure how from the findAll ...props I can check the contents of the ArrayList.
The error displayed is:
JSON path someData.findAll {it.name == 'check1'}.props doesn't match.
Expected: (a collection containing a string starting with "AA")
Actual: [[AAaa]]
Any idea's ?
The findall return an Array of Array containing one element which is AA (which is why you have [[AAaa]] instead of [AAaa].
You have to flatten or extract one level of array to solve the problem I think.

Dealing arrays with hamcrest and rest assured

I can't figure out how to create the code using hamcrest to check an array inside array having these properties.
(Imagine this as it has multiple entries with different data)
{
"mobilenum": "+6519829340",
"firstname": "Allen",
"lastname": "Edwards",
"location": "Singapore"
}
If I use this:
.body("smsentries.mobilenum", contains(equalTo("+6519829340")));
it returns that it does exist but how can I put more checks that the object it has found also has the same firstname, lastname and location?
I also think that this is wrong:
.body("smsentries.mobilenum", contains(equalTo("+6519829340")))
.and()
.body("smsentries.firstname", contains(equalTo("Allen"));
As what I understand here is that it searches the array if the array contains mobilenum equal to what is provided and if the array contains the name "Allen"
What I needed to is to find the array having the mobilenum equal to "+6519829340" and having the firstname equalto "Allen".
Do you guys and gals have any idea how to go about this?
What I needed to is to find the array having the mobilenum equal to
"+6519829340" and having the firstname equalto "Allen".
You can make use of the "find" method:
.body("smsentries.find { it.mobilenum == '+6519829340' }.firstname", equalTo("Allen")
.body("smsentries.find { it.mobilenum == '+6519829340' }.lastname", equalTo("Edwards").
As you see you're essentially duplicating the path expression in the two cases so to improve this we can make use of root paths:
.root("smsentries.find { it.mobilenum == '+6519829340' }").
.body("firstname", equalTo("Allen")
.body("lastname", equalTo("Edwards").
You can also parameterize root paths:
.root("smsentries.find { it.mobilenum == '%s' }").
.body("firstname", withArgs("+6519829340"), equalTo("Allen")
.body("lastname", withArgs("+6519829340"), equalTo("Edwards").
.body("firstname", withArgs("+12345678"), equalTo("John")
.body("lastname", withArgs("+12345678"), equalTo("Doe").

ElasticSearch writing query for priority search

I am new to elastisearch and I just set it up and tried default search. I am using elasticsearch rails gem. I need to write custom query with priority search (some fields in table are more important then others, etc. title, updated_at in last 6 months...). I tried to find explanation or tutorial for how to do this but nothing seems understandable. Can anyone help me with this, soon better.
Never having used the ruby/elasticsearch integration, it doesn't seem too hard... The docs here show that you'd want to do something like this:
client.search index: 'my-index', body: { query: { match: { title: 'test' } } }
To do a basic search.
The ES documentation here shows how to do a field boosted query:
{
"multi_match" : {
"query" : "this is a test",
"fields" : [ "subject^3", "message" ]
}
}
Putting it all together, you'd do something like this:
client.search index: 'my-index', body: { query: { multi_match : {
query : "this is a test",
fields : [ "subject^3", "message" ]
} } }
That will allow you to search/boost on fields -- in the above case, the subject field is given 3 times the score of the message field.
There is a very good blog post about how to do advanced scoring. Part of it shows an example of adjusting the score based on a date:
...
"filter": {
"exists": {
"field": "date"
}
},
"script": "(0.08 / ((3.16*pow(10,-11)) * abs(now - doc['date'].date.getMillis()) + 0.05)) + 1.0"
...
I have done in php, Never used the gem from Ruby on rails. Here you can give the priority for the fields using the caret (^) notation.
Example:- Suppose if we have fields namely name, email, message and address in table and the priority should be given for the name and message then you can write as below
> { "multi_match" : {
> "query" : "this is a test",
> "fields" : [ "name^3", "message^2".... ] } }
Here name has 3 times higher priority than other fields and message has got 2 times higher priority than other fields.

Resources