当前位置: 首页 > 知识库问答 >
问题:

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

金亦
2023-03-14

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

"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"
 }
  ]
    }

DownstreamServiceResponse:

"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/指示器。此外,我想知道如何提取和匹配参数的基础上一个主要的值。例如,我想取“servicecd”:“ABCD”并将所有与ABCD相关的参数与下游服务的参数进行比较。

共有1个答案

谭伟
2023-03-14

更简单的示例可以让您更好地理解这个概念,特别是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
 类似资料:
  • 我有以下两个响应,我需要检查(元素的顺序不同)。尝试了不同的事情,但没有任何结果。任何帮助都将不胜感激!

  • 我有以下API响应样本 根据上面的回答,我的测试表明,每当我点击API请求时,第11个ID是SMITH,第10个ID是JAMES 所以我想把它存储在一个表中,并根据实际的响应进行断言 现在我该如何一个接一个地匹配?与first类似,它解析API响应中的第一个ID和第一个名称,并与表first ID和表first name匹配 请分享从空手道做它的任何方便的方式

  • 我正在尝试验证具有可选键的复杂JSON 下面是完整的可执行特性文件 我想验证ValidJsonSchema,但无法绕过完整的模式匹配 当我尝试匹配可选字段时,我一直得到错误为 com.intuit.karate.exception.karateException:应在路径$中找到属性为[“MyArray”]的对象,但发现了“net.minidev.json.JsonArray”。根据JSONPro

  • 例如,我试图使用匹配包含来验证模式响应和数据类型,有时它返回null,有时返回字符串。我正在尝试下面的操作,但我得到的断言失败了,因为它没有计算为true。 例如,为“firstName”返回的数据为“firstName”:null, 在比赛之前,我向每个人发送以下信息: 我不是在定义模式,我还没有弄清楚如何做到这一点,所以我不确定这是否是我的问题。我知道我应该这样做,但我还在学习。

  • 下面是我在点击特定Web服务时收到的JSON响应: 我不知道哪个索引有我的期望值(我需要在确定哪个具有)后验证多个值),这是动态的。不希望使用硬编码值。并匹配,因为这将在下次更改。 对此我有两个问题: 如何将响应传递给java代码并获取具有的数组索引,以便使用此索引进行验证? 下面的代码不起作用。

  • 我尝试运行一些测试,如果测试成功,则需要在每个场景结束时调用“”文件,但如果测试失败,则不应调用“”文件。 我测试结果如下所示: 如果响应代码为,则应运行命令“”,如果为,则应跳过此命令。 有人能帮我一下吗?

  • 下面是用于匹配的json示例;匹配包含不起作用。 它的投掷低于错误,

  • 我在Twitter上注意到一个关于空手道Java API的有趣帖子:https://Twitter.com/ptrthomas/status/1344290316212342784 我没有一个推特账户无法回复--希望你不介意我在这里问。 我在一个工作项目中使用过空手道(与并行运行器、特性文件和所有),使用它是一种乐趣--一些工作场所的人对复杂的JSON断言印象深刻。我们正在为该项目实现一定程度的J