【问题标题】:embedded expression not replaced properly in the complex json复杂 json 中的嵌入表达式未正确替换
【发布时间】:2018-05-16 10:25:20
【问题描述】:

在空手道测试中 - 我们能够替换 json 中单个键的嵌入式表达式。但是当尝试替换 json 的复杂键时它不起作用

输入json:

    {
    "integration": {
        "serviceData": {
            "integrationService": {
                "name": "#(integrationName)",
                "description": "#(tenantID)",
                "serviceData": "<xml xmlns=\"http://www.w3.org/1999/xhtml\">\n  <block type=\"start_block\" id=\"foOIiCF5aZGnie1GzBDB\" deletable=\"false\" x=\"150\" y=\"25\">\n    <field name=\"ORCH_NAME\">Add2NumDocInputs</field>\n    <field name=\"SVC_SIGNATURE\">{\"sig_in\":{\"rec_ref\":\"fld_[#(tenantID)]_stage00.documenttypes:sum2\",\"field_type\":\"recref\",\"node_type\":\"record\",\"field_dim\":\"0\"},\"sig_out\":{\"field_type\":\"record\",\"node_type\":\"record\",\"field_dim\":\"0\"}}</field>\n    <field name=\"AUDIT_SETTINGS\"></field>\n    <statement name=\"ADD_BLOCKS\">\n      <block type=\"service_block_math\" id=\"aUqb0MAozTHQFuHj5rma\">\n        <field name=\"SERVICE\">pub.math:addInts</field>\n        <field name=\"SERVICE_DESC_FIELD\"></field>\n        <field name=\"SERVICE_DETAILS\">{\"service\":\"pub.math:addInts\",\"map\":[{\"sourcePath\":\"/n2;1;0\",\"targetPath\":\"/num2;1;0\"},{\"sourcePath\":\"/n1;1;0\",\"targetPath\":\"/num1;1;0\"}]}</field>\n      </block>\n    </statement>\n  </block>\n</xml>"
            }
        }
    }
}

在上面的 json 中,'tenantID' 是从测试用例传递过来的键。 'tenantID' 被正确替换为 json 中的 'description' 键。但它不会被“serviceData”键替换

请提出一些解决方案。 提前致谢

【问题讨论】:

  • 如何替换? json["integration"]["serviceData"]["integrationService"]["serviceData"] = new_value,还是其他方法?也告诉我们替换应该是什么!
  • 在整个 json 中只有 2 个地方我需要替换变量 'tenantID' 。 1>对于'description'键“description”:“#(tenantID)”,(使用上面的代码2>在'serviceData'键内按预期工作。这是{\“rec_ref\”:\“fld_[ #(tenantID)]_stage00.documenttypes:sum2\"。这个替换不起作用

标签: karate


【解决方案1】:

编辑:在发布了很长的答案后(请参阅此答案的结尾/第二部分),我意识到使用空手道的 replace 语法为您提供了一个超级简单的解决方案:

* def integrationName = 'hello'
* def tenantID = 'world'
* def json =
"""
{
    "integration": {
        "serviceData": {
            "integrationService": {
                "name": "#(integrationName)",
                "description": "#(tenantID)",
                "serviceData": "<xml xmlns=\"http://www.w3.org/1999/xhtml\">\n  <block type=\"start_block\" id=\"foOIiCF5aZGnie1GzBDB\" deletable=\"false\" x=\"150\" y=\"25\">\n    <field name=\"ORCH_NAME\">Add2NumDocInputs</field>\n    <field name=\"SVC_SIGNATURE\">{\"sig_in\":{\"rec_ref\":\"fld_[<tenantID>]_stage00.documenttypes:sum2\",\"field_type\":\"recref\",\"node_type\":\"record\",\"field_dim\":\"0\"},\"sig_out\":{\"field_type\":\"record\",\"node_type\":\"record\",\"field_dim\":\"0\"}}</field>\n    <field name=\"AUDIT_SETTINGS\"></field>\n    <statement name=\"ADD_BLOCKS\">\n      <block type=\"service_block_math\" id=\"aUqb0MAozTHQFuHj5rma\">\n        <field name=\"SERVICE\">pub.math:addInts</field>\n        <field name=\"SERVICE_DESC_FIELD\"></field>\n        <field name=\"SERVICE_DETAILS\">{\"service\":\"pub.math:addInts\",\"map\":[{\"sourcePath\":\"/n2;1;0\",\"targetPath\":\"/num2;1;0\"},{\"sourcePath\":\"/n1;1;0\",\"targetPath\":\"/num1;1;0\"}]}</field>\n      </block>\n    </statement>\n  </block>\n</xml>"
            }
        }
    }
}
"""
* replace json.tenantID = tenantID
* match json == 
"""
{
    "integration": {
        "serviceData": {
            "integrationService": {
                "name": "hello",
                "description": "world",
                "serviceData": "<xml xmlns=\"http://www.w3.org/1999/xhtml\">\n  <block type=\"start_block\" id=\"foOIiCF5aZGnie1GzBDB\" deletable=\"false\" x=\"150\" y=\"25\">\n    <field name=\"ORCH_NAME\">Add2NumDocInputs</field>\n    <field name=\"SVC_SIGNATURE\">{\"sig_in\":{\"rec_ref\":\"fld_[world]_stage00.documenttypes:sum2\",\"field_type\":\"recref\",\"node_type\":\"record\",\"field_dim\":\"0\"},\"sig_out\":{\"field_type\":\"record\",\"node_type\":\"record\",\"field_dim\":\"0\"}}</field>\n    <field name=\"AUDIT_SETTINGS\"></field>\n    <statement name=\"ADD_BLOCKS\">\n      <block type=\"service_block_math\" id=\"aUqb0MAozTHQFuHj5rma\">\n        <field name=\"SERVICE\">pub.math:addInts</field>\n        <field name=\"SERVICE_DESC_FIELD\"></field>\n        <field name=\"SERVICE_DETAILS\">{\"service\":\"pub.math:addInts\",\"map\":[{\"sourcePath\":\"/n2;1;0\",\"targetPath\":\"/num2;1;0\"},{\"sourcePath\":\"/n1;1;0\",\"targetPath\":\"/num1;1;0\"}]}</field>\n      </block>\n    </statement>\n  </block>\n</xml>"
            }
        }
    }
}
"""

编辑:这是原始答案(如下),留在这里,因为它可能作为参考有用。

哇,我可以说这是 XML 和 JSON 的可怕组合。嵌入式表达式有一个规则,它们必须是以#(

开始的字符串

因此,您需要将流程拆分为类似的内容。由于您的有效负载,它很复杂。不是因为空手道:)

* def integrationName = 'hello'
* def tenantID = 'world'
* def recRef = 'fld_[' + tenantID + ']_stage00.documenttypes:sum2'
# by the way this is also possible as an embedded expression #('fld_[' + tenantID + ']_stage00.documenttypes:sum2')
* string signature = {"sig_in":{"rec_ref":"#(recRef)","field_type":"recref","node_type":"record","field_dim":"0"},"sig_out":{"field_type":"record","node_type":"record","field_dim":"0"}}
* def xml = 
"""
<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="start_block" id="foOIiCF5aZGnie1GzBDB" deletable="false" x="150" y="25">
    <field name="ORCH_NAME">Add2NumDocInputs</field>
    <field name="SVC_SIGNATURE">#(signature)</field>
    <field name="AUDIT_SETTINGS"></field>
    <statement name="ADD_BLOCKS">
      <block type="service_block_math" id="aUqb0MAozTHQFuHj5rma">
        <field name="SERVICE">pub.math:addInts</field>
        <field name="SERVICE_DESC_FIELD"></field>
        <field name="SERVICE_DETAILS">{"service":"pub.math:addInts","map":[{"sourcePath":"/n2;1;0","targetPath":"/num2;1;0"},{"sourcePath":"/n1;1;0","targetPath":"/num1;1;0"}]}</field>
      </block>
    </statement>
  </block>
</xml>
"""
* xmlstring xml = xml
* def json = 
"""
{
    "integration": {
        "serviceData": {
            "integrationService": {
                "name": "#(integrationName)",
                "description": "#(tenantID)",
                "serviceData": "#(xml)"
            }
        }
    }
}
"""
* match json ==
"""
{
    "integration": {
        "serviceData": {
            "integrationService": {
                "name": "hello",
                "description": "world",
                "serviceData": "#string"
            }
        }
    }
}
"""
* match json.integration.serviceData.integrationService.serviceData contains '"rec_ref":"fld_[world]_stage00.documenttypes:sum2"'

另请阅读关于类型转换的文档:https://github.com/intuit/karate#type-conversion

【讨论】:

  • 使用空手道替换语法的超级解决方案。谢谢彼得。它完全通用且简单的解决方案。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多