Karate Schema validation, does it work with nested arrays when using match contains assertion? - karate

I'm trying to use a match on a json object by only checking for some elements. The Json object contains a nested array which seem to be the root cause for the test failing. It seems like using the 'match contains' assertion on key:values within the nested array does not work as expected and throws an error.
I've tried to use 'match contains' on first level keys and that works fine but when attempting to match keys within a nested array I get the following error message: reason: actual value has 1 more key(s) than expected
* def json = { id: 1, priority: 1, compRound: { id: 1, comp_id: 89 } }
* match json contains
"""
{
id: '#number',
priority: '#number',
compRound: {
id: '#number'
}
}
"""
As I'm using the match contains assertion, I would expect for the test to pass but instead it looks like Karate expects all key:values within the nested array to be present.

Please read this section of the docs carefully: https://github.com/intuit/karate#contains-short-cuts
* def json = { id: 1, priority: 1, compRound: { id: 1, comp_id: 89 } }
* def compRound = { id: '#number' }
* match json == { id: '#number', priority: '#number', compRound: '#(^compRound)' }

Related

Karate - how to use param from response to test child endpoints

I have a parent URL that returns an array of objects with id and slug, this is dynamically generated because it depends of the user (test env). After getting this data, how can I dynamically call each element object from an array and make another request ?
Example :
Scenario: parent
Given url 'posts'
Then Status 200
* assert response.status == true
Example url/posts returns a data of array of objects :
[
{id: 6, slug: 'my-post-6'},
{id: 23, slug: 'example-test-23'},
{id: 133, slug: 'another-test-133'},
]
Then I want to make a get request to every object like so : url 'posts/' data.slug
How can I do this ?
GET url 'posts/my-post-6'
// Validate schema
GET url 'posts/example-test-23'
// Validate schema
GET url 'posts/another-test-133'
// Validate schema
Karate's call keyword auto-loops over a JSON array of objects. Refer the docs: https://github.com/karatelabs/karate#data-driven-features
You can try this simple example:
Feature:
Scenario:
* def data = [{ slug: 'one'}, { slug: 'two'}]
* call read('#called') data
#called #ignore
Scenario:
* url 'https://httpbin.org/anything'
* path slug
* method get
This is getting improved in future versions, refer: https://github.com/karatelabs/karate/issues/1905 (available now in 1.3.0.RC2)

Validate the json Array with Either one field required in Mule 4

Request Json Looks like the below :-
{
"eNumber": 8506493,
"details": [
{
"id":12345,
"name": xyz123
}
]
}
As part of requirement, I need to check the "details" array that either "id" or "name" field must present. if "id" field is present, "name" is non-mandatory. if "name" field is present, "id" is non-mandatory. Throws error if not met.
I tried few options using filter the details array and check the size of the filtered array in the validation component. It does not seems to be working well. if anyone has got better solutions.please share it here.
This example code will return true or false if it passes your condition
%dw 2.0
import * from dw::core::Arrays
output application/json
---
payload.details every ((item) -> item.id? or item.name?)
The function I'm using is every that checks that all elements in an array passes the given criteria.
Later you can use a choice and a use the raise error or you can use the fail dw function with an if else.
You can restrict it at RAML level.
Sample RAML -
#%RAML 1.0
title: api
types:
test1:
type: object
properties:
id:
type: string
example: 123a
test2:
type: object
properties:
name:
type: string
example: xyz124
test3:
type: object
properties:
enumber:
type: string
example: 8506493a
details :
type: [test1 | test2]
/test:
post:
body:
application/json:
type: test3

Trouble using #regex marker

I'm having trouble constructing a proper #regex marker.
I'm trying to match on a couple of date formats in my response JSON:
"created": "2017-03-23T14:16:25.854Z"
"modified": "2018-06-21T05:38:37.978Z"
I've attempted the following markers:
'#regex [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z'
'#regex [0-9]{4}-[0-9]{2}-[0-9]{2}.[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}.'
'#regex \d+-\d+-\d+.\d+:\d+:\d+.\d+.'
All 3 forms appear to be correct (according to rubular.com). I've also played around with escaping characters that might be problematic. The only one I been able to get tot work so far is:
[0-9-T:.Z]+
But this feels a little "loose" of a pattern match.
Basically I'm attempting this:
* def meta = { created: '#regex[[0-9]{4}-[0-9]{2}-[0-9]{2}.[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}.]', modified: '#regex[[0-9]{4}-[0-9]{2}-[0-9]{2}.[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}.]' }
And match each response ==
"""
{
id: '#regex [a-z0-9]+',
name: '#string',
type: '<Type>',
meta: #(meta),
integration_id: '#uuid'
}
"""
Get an error similar to this:
KarateException: objects-api.feature:40 - path: $[0].meta.created, actual: '2016-11-30T20:48:16.782Z', expected: '#regex [0-9]+-[0-9]+-[0-9]+[0-9]+:[0-9]+:[0-9]+[0-9]+', reason: regex match failed
Here is a suggestion, why don't you parse the dates into java Date objects and that will open up more possibilities such as being able to compare 2 dates.
Here are good examples:
https://stackoverflow.com/a/54133126/143475
https://stackoverflow.com/a/55938480/143475
That said, I think you missed having to escape the backslash-es, this is a Java thing and is mentioned in the docs. So this works:
* def date = '2017-03-23T14:16:25.854Z'
* match date == '#regex \\d+-\\d+-\\d+.\\d+:\\d+:\\d+.\\d+.'
* match date == '#regex [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z'
The Karate JS engine also supports the RegExp JS object, so see if this example gives you some solutions as well:
https://stackoverflow.com/a/54768838/143475
EDIT, this works for me as well:
* def meta = { created: '#regex \\d+-\\d+-\\d+.\\d+:\\d+:\\d+.\\d+.', modified: '#regex \\d+-\\d+-\\d+.\\d+:\\d+:\\d+.\\d+.' }
* def response = [{ meta: { created: '2017-03-23T14:16:25.854Z', modified: '2018-06-21T05:38:37.978Z' } }]
* match each response == { meta: '#(meta)' }

Karate contains and all key-values did not match error

I try to learn Karate but have some issue and I can't resolve it by myself.
So my Feature is looking rather simple:
Feature: Alerting get the list of all alerts
Background:
* url 'url'
Scenario: Retrieve all alerts
Given path '5c348c553a892c000bb1f2dd'
When method get
Then status 200
And match response contains {id: 5c348c553a892c000bb1f2dd}
The case here is to fetch a response and make sure that given ID is on the list. As far I understand this documentation keyword contains should lookup only for the given phrase but I get an error: reason: all key-values did not match
This is my console output:
allAlertsGet.feature:10 - path: $, actual: {data={name=Baelish of Harrenhal, user=griffin, id=5c348c553a892c000bb1f2dd, tags=["Gared"], triggers={prometheus=[{"js_id":"Qarth","labels":["Harry Potter and the Sorcerer's Stone"],"operator":"==","query":"up","value":"1"}]}, trigger_interval=398s, receivers={slack=[{"holdoffTime":"0s","id":"Stalls","message":"Dark and difficult times lie ahead. Soon we must all face the choice between what is right and what is easy.","revokeMessage":"Every flight begins with a fall.","token":"Buckbeak"}]}, hold_cap=4, max_cap=16, factor=2, createDate=1546947669, triggered_date=1546948867, mute_until=0, muted=false, status=3}}, expected: {id=5c348c553a892c000bb1f2dd}, reason: all key-values did not match
What I have missed? I use karate 0.9.0.
Pay attention to the nested structure of your JSON. You can paste this snippet into a Scenario and try it, this is a tip - you can experiment quickly without making HTTP requests like this:
* def response = { data: { name: 'Baelish of Harrenhal', user: 'griffin', id: '5c348c553a892c000bb1f2dd' } }
* match response.data contains { id: '5c348c553a892c000bb1f2dd' }
EDIT: just to show off a few other ways to do assertions:
* match response.data.id == '5c348c553a892c000bb1f2dd'
* match response..id contains '5c348c553a892c000bb1f2dd'
* def id = { id: '5c348c553a892c000bb1f2dd' }
* match response == { data: '#(^id)' }
* match response contains { data: '#(^id)' }

Matching values from response to values from DB

My response looks as follows :
{
"data":[
{
"foo":bar1
"user_email":"user#user.com",
"user_id":1
},
{
"foo":bar2
"user_email":"user#user.com",
"user_id":1
}
]
}
* def DBOutput = #fetching values from DB
* match response.data[*].foo contains [DBOutput1[0][0],DBOutput1[1][0]]
DBOutput1 has data as follows : [["bar1"],["bar2"]]
This match fails, for some reasons value passed into the expected list in match statement is DBOutput1[0][0]
This is the error I am getting actual: ["bar1","bar2"], expected: 'DBOutput1[0][0]',
You have some seriously malformed JSON in your example above. The below snippet works, just paste it into a new Scenario:
* def response =
"""
{
"data":[
{
"foo": "bar1",
"user_email":"user#user.com",
"user_id":1
},
{
"foo": "bar2",
"user_email":"user#user.com",
"user_id":1
}
]
}
"""
* match response.data[*].foo contains ['bar1', 'bar2']
Now it is up to you to fix your test, without knowing what your DBOutput is no one can help further.
I iterated over the DBoutput and stored them in a new array list. I then matched the response to the list and it worked.
match response.data[*].foo contains ListFromDB