Karate - Match two dynamic responses
Solution 1:
For a simpler example that can give you a better understanding of the concept, especially karate.map()
which can even be used on nested JSON structures, see here: https://stackoverflow.com/a/65036047/143475
And also read the docs: https://github.com/intuit/karate#json-transforms
* def response =
"""
{
"webServiceSummary":{
"service":{
"serviceCd":"ABCD",
"serviceDescription":"Checking Main Service",
"hypotheticalInd":"100.0",
"realInd":"200.0"
},
"includeServicesList":[
{
"serviceCd":"XYZ",
"serviceDescription":"Checking AddOn Service",
"hypotheticalInd":"50.0",
"realInd":"60.0"
},
{
"serviceCd":"PQRS",
"serviceDescription":"Checking SecondAddOn Service",
"hypotheticalInd":"100.0",
"realInd":"200.0"
}
]
}
}
"""
* def source =
"""
{
"webServiceDetail":{
"feature":{
"featureCd":"ABCD",
"featureName":"Checking Main Service",
"imaginaryInd":"100.0",
"actualInd":"200.0",
"extraInd1":"someRandomValue1"
},
"includefeatureList":[
{
"featureCd":"PQRS",
"featureName":"Checking SecondAddOn Service",
"imaginaryInd":"100.0",
"actualInd":"200.0",
"extraInd1":"someRandomValue1",
"extraInd2":"someRandomValue1"
},
{
"featureCd":"XYZ",
"featureName":"Checking AddOn Service",
"imaginaryInd":"50.0",
"actualInd":"60.0",
"extraInd1":"someRandomValue1",
"extraInd2":"someRandomValue1"
}
]
}
}
"""
* def feature = source.webServiceDetail.feature
* set expected.webServiceSummary.service
| path | value |
| serviceCd | feature.featureCd |
| serviceDescription | feature.featureName |
| hypotheticalInd | feature.imaginaryInd |
| realInd | feature.actualInd |
* def mapper = function(x){ return { serviceCd: x.featureCd, serviceDescription: x.featureName, hypotheticalInd: x.imaginaryInd, realInd: x.actualInd } }
* def expectedList = karate.map(source.webServiceDetail.includefeatureList, mapper)
* set expected.webServiceSummary.includeServicesList = '#(^expectedList)'
* print expected
* match response == expected