karate.repeat creates a malformed JSON that is unable to traverse - karate

I have a scenario where I need to call a secondary feature file that contains an API call where the response is a JSON object. However, I need to call this scenario multiple times, so I am using karate.repeat to achieve this. However, the resulting response is a malformed JSON that I cannot traverse.
This is what I am doing:
* def fun = function(i){ return karate.call('abc.feature#abc', value)}
* def loop = karate.repeat(2, fun)
* karate.log(loop)
The response I get is:
{
"Total_packages1": {
"package1": {
"tags": [
"kj21",
"j1",
"sj2",
"z1"
],
"expectedResponse": [
{
"firstName": "Name",
"lastName": "lastName",
"purchase": [
{
"title": "title",
"category": [
"a",
"b",
"c"
]
}
]
}
]
}
}
}
{
"Total_packages2": {
"package2": {
"tags": [
"kj212",
"j12",
"sj22",
"z12"
],
"expectedResponse": [
{
"firstName": "Name2",
"lastName": "lastName2",
"purchase": [
{
"title": "title2",
"category": [
"a2",
"b2",
"c2"
]
}
]
}
]
}
}
}
As you can see, Total_packages2 starts malformed. I need to grab the "tags" values from each package, however, I cannot simply do Total_packages1.package1.tags like I could with a single response in the JSON.
If I cannot achieve what I need by karate.repeat, is there another method that is recommended for looping like this? I haven't found anything in the documentation for this particular scenario.

Don't use karate.repeat() use call with a JSON array. Read this part of the docs: https://github.com/karatelabs/karate#data-driven-features

Related

Can't make PUT /raylight/v1/documents/id/parameter/id work properly

I need to update document parameters via REST API.
I've tried using the following:
PUT .../raylight/v1/documents/33903/parameters/3
with the following json payload
{
"parameters":{
"parameter": {
"id": 3,
"answer": {
"values": {
"value": [
"2019/9"
]
}
}
}
}
}
But the returned answer shows unmodified parameters:
{
"parameter": {
"#optional": "false",
"#type": "prompt",
...
"id": 3,
...
"answer": {
...
"info": {
...
"previous": {
"value": [
"2015\/12"
]
}
},
"values": {
"value": [
"2015\/12"
]
}
}
}
}
How can I properly set new prompt parameters?
Do:
PUT .../raylight/v1/documents/33903/parameters
instead of:
PUT .../raylight/v1/documents/33903/parameters/3
Adding a parameter ID at the end performs a different function: it returns the list of parameters that are dependent upon the one provided. You have only one in this case, and it's returning itself. Leave it off, to refresh the document.

How to check a particular value in karate on basis of condition when there are more than one key in response

There are multiple keys present in x.details[0].user so how to compare in such condition. It works fine where there is only one key while when there are more than one key it fails with error as there are multiple keys for user.
Please guide
* def array =
"""
{
"type": "1",
"array": 2,
"details": [
{
"path": "path",
"user": {
"school": {
"name": [
{
"value": "this is school",
"codeable": {
"details": [
{
"hello": "yty",
"condition": "check1"
}
],
"text": "123S"
}
}
]
},
"sample": "test1",
"id": "22222"
},
"data": {
"check": "abc"
}
},
{
"path": "path",
"user": {
"school": {
"name": [
{
"value": "this is school",
"codeable": {
"details": [
{
"hello": "def",
"condition": "check2"
}
],
"text": "123O"
}
}
]
},
"sample": "test",
"id": "11111"
},
"data": {
"check": "xyz"
}
}
]
}
"""
* def lookup = { 'check1': 'yty', 'check2': 'def' }
* match each array.details contains { user: { school: { name[0]: { codeable: { details[0]: { hello: '#(lookup[_$.user.school.name[0].codeable.details[0].condition])' } } } } } }
I have tried multiple ways but not sure how to make it work when there are more than one keys getting returned.
First let me say this. Please, please do not try to be "too clever" in your tests. Read this please: https://stackoverflow.com/a/54126724/143475
Just have simple tests that test for specific, predictable responses and stop there. The maximum complexity should be match contains, that's it. What you are trying to do is in my opinion a waste of time, and will just cause grief for anyone who tries to maintain these tests in future.
That said, I'm giving you a completely different approach here. There are a few different ways, this is just one. Here we attack the problem piece by piece instead of trying to write one massive match.
* def allXyz = array.details.filter(x => x.data.check == 'xyz')
* match each allXyz..details == [{ hello: 'def', condition: 'check2' }]
You should be able to extend this to do all the weird assertions that you want. If you are wondering what allXyz..details does, you can print it like this:
* def temp = $allXyz..details
* print temp

Karate: Match JSON Array responses where the order of array is different in each hit

I have a scenario where a portion of the response arrays is the response from a child API.
child API response looks like below, but there is no specific order. And I need to check whether the child API response is present in the parent API(irrespective of the order of the elements in the child API). I followed this Karate - Match two dynamic responses thread but its not working in my case.
* def response1 =
"""
{
"array1": [
{
"element": {
"id": "A1",
"array11": [
{
"uid": "u123",
"gid": [
"g1"
]
}
]
}
},
{
"element": {
"id": "A2",
"array11": [
{
"uid": "u124",
"gid": [
"g2"
]
}
]
}
}
]
}
"""
* def response2 =
"""
{
"array1": [
{
"element": {
"id": "A2",
"array11": [
{
"uid": "u124",
"gid": [
"g2"
]
}
]
}
},
{
"element": {
"id": "A1",
"array11": [
{
"uid": "u123",
"gid": [
"g1"
]
}
]
}
}
]
}
"""
This is a one liner :)
* match response2.array1 contains response1.array1
Guess what, you don't have to match pure JSON all the time, using child-sections is fine.
But also read this specific part of the docs: https://github.com/intuit/karate#contains-short-cuts
And this example: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/graphql/graphql.feature

Apache Nifi: UpdateRecord replace child values

I'm trying to use UpdateRecord 1.9.0 processor to modify a JSON but it does not replace the values as I want.
this is the source message
{
"type": "A",
"ids": [{
"id": "1",
"value": "abc"
}, {
"id": "2",
"value": "def"
}, {
"id": "3",
"value": "ghi"
}
]
}
and the wanted output
{
"ids": [{
"userId": "1",
}, {
"userId": "2",
}, {
"userId": "3",
}
]
}
I have configured the processor as follows
processor config
Reader:
reader
Schema registry:
schema
writer:
writer
And it works, the output is a JSON without the field 'type' and the ids have the field 'userId' instead 'id' and 'value'.
To fill the value of userId, I defined the replace strategy and the property to replace:
strategy
But the output is wrong. The userId is always filled with the id of the last element in the array:
{
"ids": [{
"userId": "3"
}, {
"userId": "3"
}, {
"userId": "3"
}
]
}
I think the value of the expression is ok because if I try to replace only one record it works fine (/ids[0]/userId, ..id)
Nifi docs has a really similar example (example 3):
https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.7.1/org.apache.nifi.processors.standard.UpdateRecord/additionalDetails.html
But it does not work for me.
What am I doing wrong?
thanks
Finally I have used JoltJSONTransform processor instead UpdateRecord
JoltJSONTransform
template:
[
{
"operation": "shift",
"spec": {
"ids":{
"*":{
"id": "ids[&1].userId"
}
}
}
}
]
Easier than UpdateRecord

Google sheets APIv4: Getting notes from cell

I can't find a way to retrieve notes from a cell. I looked here:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get
I can update the note by using the code here: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/cells
... but I can't find a way to retrieve the note in the first place.
This the result of a values.get call on a cell that has a note...
{u'range': u'Sheet1!D5', u'values': [[u'update CD data']], u'majorDimension': u'ROWS'}
as you can see, the notes to the cell are not there.
How about using sheets/data/rowData/values/note to the fields?
The endpoint is as follows.
GET https://sheets.googleapis.com/v4/spreadsheets/### Spreadsheet ID ###?fields=sheets%2Fdata%2FrowData%2Fvalues%2Fnote
From your profile, if you use Python, how about this?
response = service.spreadsheets().get(spreadsheetId=id, fields="sheets/data/rowData/values/note").execute()
Result:
When there are notes at "A1:B2", the result is as follows.
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"note": "sample note A1"
},
{
"note": "sample note B1"
}
]
},
{
"values": [
{
"note": "sample note A2"
},
{
"note": "sample note B2"
}
]
}
]
}
]
}
]
}
Reference:
CellData
If I misunderstand your question, please tell me. I would like to modify it.