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

Karate - Match two dynamic responses

我必须将我的 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 contains' 因为 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"
 }
  ]
    }

现在,我应该如何匹配这两个响应?此外,您可以看到很少有参数是随机的,无法通过逐行移动来进行比较。只有相同的参数值分配给 CDs/Indicators。而且,我想知道如何根据一个主要值来提取和匹配参数。例如,我想采用 "serviceCd" : "ABCD" 并将与 ABCD 相关的所有参数与下游服务的参数进行比较。

有关可以让您更好地理解概念的更简单的示例,尤其是 karate.map(),它甚至可以用于嵌套的 JSON 结构,请参见此处:

同时阅读文档: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