空手道-匹配两个动态反应



我必须将WebService响应与其下游服务进行比较。但是,我的响应和下游响应中的ID并不相同。我在下面给出一些回复样本。同样,一个是REST服务,另一个是SOAP服务,但是我可以进行类型转换(这不是问题(

MyWebService响应:

"myWebServiceResponse": {
"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"
}
]
}

现在,下面是下游服务响应。我不能使用'match-convest',因为myWebServiceResponse和DownstreamService中的ID不同,而且还有许多额外的参数。你可以看到下面。

下游服务响应:

"myDownstreamResponse": {
"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"
}
]
}

现在,我该如何匹配这两个回答?此外,您可以看到,很少有参数是随机的,无法通过逐行移动进行比较。只有分配给CD/指示灯的相同参数值。此外,我想知道如何基于一个主要值提取和匹配参数。例如,我想取"serviceCd":"ABCD",并将所有与ABCD相关的参数与下游服务的参数进行比较。

要获得一个更简单的示例,它可以让您更好地理解这个概念,尤其是karate.map(),它甚至可以用于嵌套的JSON结构,请参阅此处:https://stackoverflow.com/a/65036047/143475

还要阅读文档:https://github.com/intuit/karate#json-转换

* 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

最新更新