How to check if a field is null on payload from request body in Mulesoft? - anypoint-studio

I have 2 scenarios.
request body:
{
"id"="",
"name"="Jane"
}
Expected result:
"Success"
request body:
{
"id"="323",
"name"="Jane"
}
Expected result:
"id field should be emptied."

this following dataweave will help you achieve what you are looking for
%dw 1.0
%output application/json
---
{
output: 'Success' when
payload.id == null or payload.id == ""
otherwise 'id field should be emptied.'
}

Related

How to partially match a JSON key in Karate

I have a response that looks this
{
"metadata": {
"Customer_x0020_Folder": "Test"
}
}
I would like the assert key Customer_x0020_Folder is equal to 'Test'. However I don't care about the x0020 portion of they key and would like to ignore that part. I would like to do something like:
match response contains { metadata: {'Customer_' + '#ignore' + '_Folder': 'Test'} }
How can I perform this assertion in Karate?
Use karate.keysOf():
* def response = { "metadata": { "Customer_x0020_Folder": "Test" } }
* def keys = karate.keysOf(response.metadata)
* match keys[0] == '#regex Customer_.*_Folder'

Karate - Exception raises for invalid jsonpath

I have a Json response like below. The difference here is my Json body has a number as the parent node.
def response =
"""
{
"22388043":[
{
"firstName":"Romin",
"lastName":"Irani",
"phoneNumber":"408-1234567",
"emailAddress":"romin.k.irani#gmail.com"
}
]
}
"""
I want to return the mobileNumber attribute value from the response body. In this scenario I don't have that attribute in my response. So here I want to get a null value.
So when I use * def mobile = $.22388043[0].mobileNumber, I'm getting below error.
No results for path: $['22388043'][0]['mobileNumber']
Please advise on this.
Karate does give you a way to get the values of JSON keys.
Hopefully this example answers all your other questions as well:
* def response =
"""
{
"22388043":[
{
"firstName":"Romin",
"lastName":"Irani",
"phoneNumber":"408-1234567",
"emailAddress":"romin.k.irani#gmail.com"
}
]
}
"""
* def id = karate.keysOf(response)[0]
* match id == '22388043'
* def person = response[id][0]
* match person contains { firstName: 'Romin', lastName: 'Irani' }
* match person.mobileNumber == '#notpresent'

How to assert a json property which can be either null(Porp:null) or has a sub schema(Porp:{ denyAny: '#boolean', assertions: '#[]' }) In KARATE DSL?

I have a Json payload to validate. And It has a property which can be either null or a sub json object. But this property exists in the json.
I tried following methods:
01
And def dnyAssertionSchema = { denyAny: '#boolean', assertions: '##[]' }
And match each policyGResponse ==
"""
{
denyAssertions: '##(dnyAssertionSchema)'
}
"""
AND
And match each policyGResponse ==
"""
{
denyAssertions: '##null dnyAssertionSchema'
}
"""
AND
This does not work as the property is not an array so I tried above second method even I couldn't find an example as such.
And match each policyGResponse ==
"""
{
denyAssertions: '##[] dnyAssertionSchema'
}
"""
The Actual response can be either
{
denyAssertions=null
}
OR
{
denyAssertions={ denyAny: true, assertions: ["a","b"] }
}
I use Karate 0.9.1
Error message I get is 'reason: actual value has 1 more key(s) than expected: {denyAssertions=null}' in first try
In second try I get 'assertion failed: path: $[3].denyAssertions, actual: {denyAny=false, assertions=[]}, expected: '##null dnyAssertionSchema', reason: not equal'
Your JSON is still not valid but anyway. Here you go:
* def schema = { denyAny: '#boolean', assertions: '#[]' }
* def response1 = { denyAssertions: { denyAny: true, assertions: ["a","b"] } }
* match response1 == { denyAssertions: '##(schema)' }
* def response2 = { denyAssertions: null }
* match response1 == { denyAssertions: '##(schema)' }

'Self' Validation Expressions in JSON file (karate framework)

I have two questions concerning 'Self' Validation Expressions:
Question1
In my feature I have
* def isStatus = function(s) { return s ==='SUCCESS' || s ==='ERROR' }
And match response[0] contains { status: '#? isStatus(_)' }
I would like to do this for every item of response like (* instead of 0):
And match response[*] contains { status: '#? isStatus(_)' }
But it doesn't work? Is anything is wrong?
I can't do this without js function like in this example:
* def date = { month: 3 }
* match date == { month: '#? _ > 0 && _ < 13' }
This doesn't work > And match response[*] contains { status: '#? _ == 'SUCCESS' || _ == 'ERROR'' }
Question 2:
I have a json file where I would like to store response types. But when I use 'Self' Validation Expressions in my JSON file it doesn't work?
Thanks for any help.
Search the documentation for match each. try this:
And match each response contains { status: '#? isStatus(_)' }
not able to understand q 2, please ask separately.

How to check for two conditions using dataweave

<root>
<risk>
<Category>02</Category>
<NCD>
<CoverCode>B05</CoverCode>
<CI>
<CD>3</CD>
</CI>
</NCD>
</risk>
</root>
I need to check 2 conditions to get the required payload from the above. I am using transformation as below to check the 2 conditions. I want to get the value of CD only when Category is 02 and CoverCode is B05.
%dw 1.0
%output application/xml skipNullOn="everywhere"
%namespace soapenv http://schemas.xmlsoap.org/soap/envelope/
---
{
soapenv#Envelope: {
soapenv#Body: {
component:{
coverageClassCode:payload.root.risk.NCD.CI.CD when payload.root.risk.Category == '02' && payload.root.risk.NCD.CoverCode == 'B05' otherwise "" ,
blockedDr:"N"
}
}
}
}
Thanks for your help.
Just change && to and -
%dw 1.0
%output application/xml skipNullOn="everywhere"
%namespace soapenv http://schemas.xmlsoap.org/soap/envelope/
---
{
soapenv#Envelope:{
soapenv#body:{
component:{
coverageClassCode:payload.root.risk.NCD.CI.CD when payload.root.risk.Category == '02' and payload.root.risk.NCD.CoverCode == 'B05' otherwise "",
//payload.root.risk.NCD.CI.CD when payload.root.risk.Category == '02' && payload.root.risk.NCD.CoverCode == 'B05' otherwise "" ,
blockedDr:"N"
}
}
}
}