复合体中的嵌入表达式未正确替换 json
embedded expression not replaced properly in the complex json
在空手道测试中 - 我们能够替换 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'是测试用例传过来的key。 'tenantID' 已正确替换为 json 中的 'description' 键。但是它没有被替换为 'serviceData' key
请提出一些解决方案。
提前致谢
编辑:在发布了很长的答案后(请参阅本答案的结尾/第二部分),我意识到使用空手道的 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 的组合太可怕了。嵌入表达式有一个规则,它们必须是 start with #(
的字符串
所以你需要将你的流程分成这样的东西。由于您的有效负载,它很复杂。不是因为空手道:)
* 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
在空手道测试中 - 我们能够替换 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'是测试用例传过来的key。 'tenantID' 已正确替换为 json 中的 'description' 键。但是它没有被替换为 'serviceData' key
请提出一些解决方案。 提前致谢
编辑:在发布了很长的答案后(请参阅本答案的结尾/第二部分),我意识到使用空手道的 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 的组合太可怕了。嵌入表达式有一个规则,它们必须是 start with #(
所以你需要将你的流程分成这样的东西。由于您的有效负载,它很复杂。不是因为空手道:)
* 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