Asserting Against Large Dynamic Response - karate

I have a very large response array I want to assert against, but without knowing the order. I have a variable with the expected response values so I can do a single giant comparison, but I'm unable to load the entire response and compare it with the entire expected response variable at the same time.
* def obligationsQuery = Java.type("tests.account.sql.Obligations").getObligations(division, account)
* def getObligations = db.readRows(obligationsQuery)
Given path "account", "v1", "accounts", systemId, "obligations"
And header api-key = gatewayKey
When method GET
Then status 200
And match $.data != null
And match $.data[*].transactionType contains any "<transactionTypeResponse>"
And match $.data[*] contains only getObligations
Examples:
| description | transactionType | transactionTypeResponse |
| Invoice | 001 | invoice
The error I get is:
get_obligations_collection.feature:49 - path: $.data[*][*], actual: [{"object1"}, {"object2"}, {"etc"}], expected: {"object1"}, reason: actual value does not contain expected
I've also tried:
And match each $.data[*] contains only getObligations
But then I get:
get_obligations_collection.feature:49 - path: $[0], actual: [{"object1"}, expected: [{"object1"}, {"object2"}, {"etc"}, reason: actual value is not list-like

I assume $.data is a JSON array so no need to use json-path to again get the data into another array by calling as $.data[*].
so,
And match $.data contains only getObligations
should work.
If this still not working, please provide some proper response and getObligations values to investigate further.

I assumed that contains only would show the complete value of my variable, but smartly, it only shows the object that fails to match the api response object. Was able to verify that through a simpler assert, and then checking the error message saw that an ID was missing padding that the API adds, but the DB does not.
As usual, just looking more closely at the data returned provides a simple explanation.

Related

Rest API: Get Calls: Should return 404 or something else when request have query parameters which are not present in DB

Example: GET: {{url}}/path?query1=val1&query2=val2
Now, val1 & val2 are query parameters and there is no combination available in Database with these 2 parameters. So in that case what should API returns?
My Opinion:
If values passed in Query parameters don't find any matching record in DB it should return "204 - No Content" or "404 - Not Found".
If API is expected to always return data, then it should return 404 when no data matches the query parameters value.
If API is expected to return either Null/Data then it should return 204 (Typical Search Case).
Dev Opinion:
If the path parameters are not matching then only return 404 else if the Query parameters are not matching then it should return 204
Need your thoughts, please suggest....
DB
id
Frist Name
Last Name
Value
1
Mukesh
Suthar
Business User
2
Vijay
Kumar
Franchise User
API: {{url}}/find?FirstName=Mukesh&LastName=Kumar
Response: Returning 204 - No Content (Which is okay for a simple search when a record can be present or not)
Expected: Should Return "404 - Not Found" as the requested user is not found (Which is required to throw an error as on the basis of the query parameters it returns a specific value which is required for further processing.)

Karate match two json files(expected json and API response) irrespective of the order of array elements

Expected Response:
{"data":{
{"assignments":[{"locationId":"1186755","locationName":"X.11.11"},{"locationId":"1186756","locationName":"X.11.12"}]}}}
Response:
{"data":{
{"assignments":[{"locationId":"1186756","locationName":"X.11.12"},{"locationId":"1186755","locationName":"X.11.11"}]}}}
I saw a SO post stating to use karate.sort(response, x=>x.locationId), when i tried it's giving me empty response. Is there any simple way i can achieve do the comparison of whole response file irrespective of order?
Note: I even tried contains only, but it's failing the assertion.
Just use contains deep: https://stackoverflow.com/a/64373344/143475
* def response = {"assignments":[{"locationId":"1186755","locationName":"X.11.11"},{"locationId":"1186756","locationName":"X.11.12"}]}}}
* match response contains deep {"assignments":[{"locationId":"1186756","locationName":"X.11.12"},{"locationId":"1186755","locationName":"X.11.11"}]}}}

Karate- Good way to send custom headers with each request

I have a scenario where I am doing a GET call and on my table I have parameters defined. Now for the headers, I want to purposefully send incorrect values to make sure the test fails as expected. Not really sure how to do it though.
I have the following setup:
* table input
| machine | version | osSystem | status |
| machineName| version | windows | 401 |
My secondary file that the above calls on, looks like this:
Given url env
And path path
And header Content-Type = 'application/xml; charset=UTF-16'
And header Accept = '*/xml; charset=UTF-16'
And header key = key
When method GET
In above, I want to send bogus values for "key" header. Total of six bogus values (random alpha string, random number string, random alphanumerical value, random guid, etc). I have obviously tried entering the values as a json under "and header key = {}" but how do I make each request run EACH header per request, instead of running them all in one request?
Thank you in advance!
Try this example and observe how it works, it will answer all your questions:
Scenario Outline:
* url 'https://httpbin.org/anything'
* header foo = header
* method get
Examples:
| header |
| one |
| two |
And for an alternate way to "loop" refer to this answer: https://stackoverflow.com/a/69422451/143475

Can Karate generate multiple query parameters with the same name?

I need to pass multiple query parameters with the same name in a URL, but I am having problems getting it to work with Karate. In my case, the URL should look like this:
http://mytestapi.com/v1/orders?sort=order.orderNumber&sort=order.customer.name,DESC
Notice 2 query parameters named "sort". I attempted to create these query string parameters with Karate, but only the last "sort" parameter gets created in the query string. Here are the ways I tried to do this:
Given path 'v1/orders'
And param sort = 'order.orderNumber'
And param sort = 'order.customer.name,DESC'
And header Authorization = authInfo.token
And method get
Then status 200
And:
Given path 'v1/orders'
And params sort = { sort: 'order.orderNumber', sort: 'order.customer.name,DESC' }
And header Authorization = authInfo.token
And method get
Then status 200
And:
Given path 'v1/order?sort=order.orderNumber&sort=order.customer.name,DESC'
And header Authorization = authInfo.token
And method get
Then status 200
The first two ways provide the same query string result: ?sort=order.customer.name%2CDESC
The last example does not work because the ? get encoded, which was expected and explained in this post - Karate API Tests - Escaping '?' in the url in a feature file
It's clear that the second "sort" param is overriding the first and only one parameter is being added to the URL. I have gone through the Karate documentation, which is very good, but I have not found a way to add multiple parameters with the same name.
So, is there a way in Karate to set multiple URL query parameters with the same name?
Yes you can generate multiple query parameters with the same name in karate
All values of similar key should be provided in an array.
Given path 'v1/orders'
And params {"sort":["order.orderNumber","order.customer.name,DESC"]}
And header Authorization = authInfo.token
And method get
Then status 200
And for setting single parameter using param it will be like
And param sort = ["order.orderNumber","order.customer.name,DESC"]

Karate - unable to perform match operation - actual value not string

Scenario Outline: Verify retreiving endpoint for particular system
Given path 'kites/<id>'
When method get
Then status 200
And match response contains "<orgid>"
Examples:
| kites | orgid |
| 56338e32e4b0846c32c0732e | 56338e32e4b0846c32c0732e |
I am getting the following error:
com.intuit.karate.exception.KarateException: path: $, actual:
{
"orgId": "56338e32e4b0846c32c0732e",
"name": "Supplier Management"
}
expected: '56338e32e4b0846c32c0732e', reason: actual value is not a string
I tried with <orgid> and "<orgid>" but i am getting the same error.
Any help would be appreciated.
There are so many things I don't understand in your example. Where did <id> come from ? Where is the kites column used ?
The most serious problem is you are using orgid but the actual JSON has a key orgId. Please don't make basic mistakes and try again.
And shouldn't you be trying to do something like this:
And match response contains { orgId: "<orgid>" }
Edit: Since there are complaints about the above (which I find disappointing), I'm adding another option:
And match response.orgId == "<orgid>"