在REST保证的情况下使用groovy闭包在JSON上进行嵌套迭代 [英] Nested iteration over JSON using groovy closure in REST-assured

查看:100
本文介绍了在REST保证的情况下使用groovy闭包在JSON上进行嵌套迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的REST端点具有以下JSON响应:

I have the following JSON response for my REST endpoint:

{
  "response": {
    "status": 200,
    "startRow": 0,
    "endRow": 1,
    "totalRows": 1,
    "next": "",
    "data": {
      "id": "workflow-1",
      "name": "SampleWorkflow",
      "tasks": [
        {
          "id": "task-0",
          "name": "AWX",
          "triggered_by": ["task-5"]
        },
        {
          "id": "task-1",
          "name": "BrainStorming",
          "triggered_by": ["task-2", "task-5"]
        },
        {
          "id": "task-2",
          "name": "OnHold",
          "triggered_by": ["task-0", "task-4", "task-7", "task-8", "task9"]
        },
        {
          "id": "task-3",
          "name": "InvestigateSuggestions",
          "triggered_by": ["task-6"]
        },
        {
          "id": "task-4",
          "name": "Mistral",
          "triggered_by": ["task-3"]
        },
        {
          "id": "task-5",
          "name": "Ansible",
          "triggered_by": ["task-3"]
        },
        {
          "id": "task-6",
          "name": "Integration",
          "triggered_by": []
        },
        {
          "id": "task-7",
          "name": "Tower",
          "triggered_by": ["task-5"]
        },
        {
          "id": "task-8",
          "name": "Camunda",
          "triggered_by": ["task-3"]
        },
        {
          "id": "task-9",
          "name": "HungOnMistral",
          "triggered_by": ["task-0", "task-7"]
        },
        {
          "id": "task-10",
          "name": "MistralIsChosen",
          "triggered_by": ["task-1"]
        }
      ]
    }
  }
}

我正在使用带有groovy gpath表达式的放心方法进行提取,如下所示:

I am using rest-assured with a groovy gpath expression for an extraction as follows:

given()
.when()
.get("http://localhost:8080/workflow-1")
.then()
.extract()
.path("response.data.tasks.findAll{ it.triggered_by.contains('task-3') }.name")

正确地给了我[Mistral, Ansible, Camunda]

我要实现的目的是找到由InvestigateSuggestions任务触发的任务名称.但是我不确定我必须传递给contains()的taskId是task-3;我只知道它的名字,即InvestigateSuggestions.所以我尝试做:

What I am trying to achieve is to find the task names that are triggered by the InvestigateSuggestions task. But I don't know for sure that the taskId I have to pass in to contains() is task-3; I only know its name i.e. InvestigateSuggestions. So I attempt to do:

given()
.when()
.get("http://localhost:8080/workflow-1")
.then()
.extract()
.path("response.data.tasks.findAll{ 
    it.triggered_by.contains(response.data.tasks.find{
    it.name.equals('InvestigateSuggestions')}.id) }.name")

不起作用,并抱怨使用了参数"response",但未定义.

which does not work and complains that the parameter "response" was used but not defined.

如何从findAll闭包内部迭代外部集合,以找到正确的id并传递给contains()?

How do I iterate over the outer collection from inside the findAll closure to find the correct id to pass into contains() ??

推荐答案

您可以利用肮脏的机密restAssuredJsonRootObject.这是没有记录的(尽管据我所记得的REST保证7年以上的使用期限,它从未改变过,但可能会发生变化).

You can make use of a dirty secret, the restAssuredJsonRootObject. This is undocumented (and subject to change although it has never change as far as I can remember in the 7 year+ lifespan of REST Assured).

这将允许您编写:

given()
.when()
.get("http://localhost:8080/workflow-1")
.then()
.extract()
.path("response.data.tasks.findAll{ 
    it.triggered_by.contains(restAssuredJsonRootObject.response.data.tasks.find{
    it.name.equals('InvestigateSuggestions')}.id) }.name")

如果您不想使用此"hack",则需要执行与 Michael Easter 答案中提出.

If you don't want to use this "hack" then you need to do something similar to what Michael Easter proposed in his answer.

在根据响应主体生成匹配器时,故事会更好.参见文档

When it comes to generating matchers based on the response body the story is better. See docs here.

这篇关于在REST保证的情况下使用groovy闭包在JSON上进行嵌套迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆