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

sn1*_*693 5 karate

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

我的网络服务响应:

"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"
 }
  ]
    }
Run Code Online (Sandbox Code Playgroud)

现在,下面是下游服务响应。我无法使用“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"
 }
  ]
    }
Run Code Online (Sandbox Code Playgroud)

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

Pet*_*mas 4

有关可以让您更好地理解该概念的更简单的示例,尤其karate.map()是甚至可以在嵌套 JSON 结构上使用的示例,请参阅此处: https: //stackoverflow.com/a/65036047/143475

另请阅读文档: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
Run Code Online (Sandbox Code Playgroud)