i have response like { Data: {a: [{ date1: "2018-12-23", date2: "23-11-2018" },{ date1: "2018-12-20",date2: "23-11-2018" }] } }
and want to test if each date1 field is less than today's date and each date2 is less than input date field (i have a date field in input payload) How can i perform that.
In many ways, here is one. Refactoring this code to be cleaner is homework for you :P
* def toTime1 =
"""
function(s) {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(s).time
}
"""
* def toTime2 =
"""
function(s) {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat("dd-MM-yyyy");
return sdf.parse(s).time
}
"""
* def input = toTime2("24-11-2018")
* def today = new java.util.Date().time
* def isValid1 = function(x){ return toTime1(x) < today }
* def isValid2 = function(x){ return toTime2(x) < input }
* def response = [{ date1: "2018-12-23", date2: "23-11-2018" }, { date1: "2018-12-20", date2: "23-11-2018" }]
* match each response == { date1: '#? isValid1(_)', date2: '#? isValid2(_)' }
Related
I've been using something like this.
* def schema =
"""
{
eligible: #string,
Reason: ##string,
enrolled: '##regex ^\\d{4}-\\d{2}-\\d{2}$',
modifiable: ##string,
Date: '##regex ^\\d{4}-\\d{2}-\\d{2}$',
status: #string,
Id: #string,
email: #string,
serviceAddressDetails: ##[] firstSchema,
DeviceIds: #[] #string
}
"""
The expected response has two possible outcomes, I want to assert that if we get either of them, the test should pass.
First,
DeviceIds : ["abcderfg"]
Second
DeviceIds : [
{
id : "abcd"
}
],
If we get either of them in the response, the test/schema should pass. How can I assert both these scenarios in the same schema?
Any help is much appreciated. Thanks!
Just run a second check. I know, it may not feel like a "single reusable schema" but take my advice, it is not worth it. Here's a solution:
* def response1 = { deviceIds: ['abcd'] }
* def firstDevice = response1.deviceIds[0]
* def isTypeOne = karate.typeOf(firstDevice) == 'string'
* def expectedDevice = isTypeOne ? '#[] #string' : '#object'
* match response1 == { deviceIds: '#(expectedDevice)' }
* def response2 = { deviceIds: { id: 'abcd' } }
* def firstDevice = response2.deviceIds[0]
* def isTypeOne = karate.typeOf(firstDevice) == 'string'
* def expectedDevice = isTypeOne ? '#[] #string' : '#object'
* match response2 == { deviceIds: '#(expectedDevice)' }
Other ideas:
https://stackoverflow.com/a/62567262/143475
I have the below json:
{
"id": [
"1A",
"2B"
],
"name": [
"rs",
"mk"
]
}
I want to extract the id value when name is 'rs' or 'mk'. There will be no duplication of name values and the size of id and name keys will always match.
So i have created the following scenario where:
- I iterate through the name array using forEach.
- Find if the value of name matches rs or mk and if it does, retrieve the index.
- Then use this index to find the retrieve the value from id key.
When I run this, the name_rs_idx and name_mk_idx are not being set and are blank.
Scenario: Using forEach and karate.set
* def vals = { id: ['1A', '2B'], name: ['rs', 'mk'] }
* def name_rs_idx = ''
* def rsFun =
"""
function(x, i) {
if(x == 'rs') {
karate.set(name_rs_idx, i);
}
}
"""
* karate.forEach(vals.name, rsFun)
* print 'RS Index - ' + name_rs_idx
* def name_rs_id = vals.id[name_rs_idx]
* print 'RS id -' + name_rs_id
* def name_mk_idx = ''
* def mkFun =
"""
function(x, i) {
if(x == 'mk') {
karate.set(name_mk_idx, i);
}
}
"""
* karate.forEach(vals.name, mkFun)
* print 'MK Index - ' + name_mk_idx
* def name_mk_id = vals.id[name_mk_idx]
* print 'MK id -' + name_mk_id
Maybe i am not using the forEach function correctly or logic is incorrect.
I think you are over-complicating things :) Here is my solution, take time to read and understand it, it will improve your Karate fundamentals ! To simplify things, we first combine the data into an array of { name: '', id: '' } pairs. Then things become much easier.
* def vals = { id: ['1A', '2B'], name: ['rs', 'mk'] }
* def fun = function(x, i){ return { name: vals.name[i], id: vals.id[i] } }
* def pairs = karate.map(vals.name, fun)
* def fun = function(x){ return x.name == 'rs' || x.name == 'mk' }
* def filtered = karate.filter(pairs, fun)
my code for finding index as below
* def list = nestActual #this is API response value which is given at the end
* def searchFor = { category_name: 'books3'}
* def foundAt = []
* def fun = function(x, i){ if (karate.match(x, searchFor).pass) foundAt.add(i) }
* eval karate.forEach(list, fun)
* print "==========foundAt=======" +foundAt
i have tried the above code for finding index where im getting foundAt index as null.
Below is my response where i want to find index of "category_name":"books3"
[
{
"category_id":1, "parent_cat_id":0, "category_name":"books", "slug_name":"books_1", "popular":true,
}, {
"category_id":2, "parent_cat_id":1, "category_name":"books2", "slug_name":"books_2", "popular":false,
}, {
"category_id":3, "parent_cat_id":1, "category_name":"books3", "slug_name":"books3_2", "popular":false,
}, {
"category_id":4, "parent_cat_id":3, "category_name":"mp3", "slug_name":"mp_3", "popular":false, }, {
"category_id":5, "parent_cat_id":3, "category_name":"mp4", "slug_name":"humoristiska_deckare_mysi_deck_3", "popular":false, }, {
"category_id":6, "parent_cat_id":3, "category_name":"video", "slug_name":"video3", "popular":false,
} ]
Please let me know how to find index of "category_name":"books3" using karate
Guess what, there is a far simpler way, the trick is to convert your search target into an array of primitives. Then you can use the List.indexOf() Java method:
Scenario: using the java indexOf api (will change with graal)
* def response = [{ name: 'a' }, { name: 'b' }, { name: 'c' }]
* def names = $[*].name
* def index = names.indexOf('b')
* match index == 1
Given the following input:
* def response = [{ a: 1 }, { a: 2 }]
* def item = { a: 1 }
How to check that item is present only once in response?
There's no direct way to do this, since less common. You can do this in 2 steps by filtering the list and then using contains only.
* def response = [{ a: 1 }, { a: 2 }]
* def item = { a: 1 }
* match response contains item
* def fun = function(x){ return karate.match(x, item).pass }
* def filt = karate.filter(response, fun)
* match filt contains only item
How to merge to below arrays into an object in Karate API. I tried below code it is not working.
keys = ['foo', 'bar', 'qux']
values = ['1', '2', '3']
Feature: ArrayToObject
Scenario: ArrayToObject Coversion JS script
* def keys = ['foo', 'bar', 'qux']
* def values = ['1', '2', '3']
* def Arr2object =
"""
function (keys, vals) {
return keys.reduce(
function(prev, val, i) {
prev[val] = vals[i];
return prev;
}, {}
);
}
"""
* string text = Arr2object(keys, values)
* print text
Expected something like this
{
"foo": "1",
"bar": "2",
"qux": "3"
}
This might work,
* def Arr2object =
"""
function(keys,values){
var newObj = {};
if(keys.length == values.length){
for (var i = 0; i <= keys.length - 1; i++) {
newObj [keys[i]] = values[i];
}
return newObj;
}
return newObj;
}