我有一个soapui响应,其中包含多个参数,有时重复参数名称并包含不同的值。 我们如何断言参数的存在? 我可以断言时间戳,数据,配置文件,完整,ID,端点,因为它们是唯一的,但标签,分支,url,api-version,appname会重复多次。
我知道如何断言参数的值,但我不知道如何仅断言参数名称,如label,branches,url,api-version,appname。
响应:
<response>{
"timestamp": "2016-04-14T17:53:29Z",
"data": {
"profile": {
"full": "test"
},
"id": "544cc493-8f4a-4f14-b95b-2c127f54caac",
"endpoints": [
{
"label": "Gify",
"branches": [
{
"url": "/gify/v1.0/",
"name": "test",
"api_version": "1.0",
"label": "test"
}
],
"appname": "gify"
},
{
"label": "x1",
"branches": [
{
"url": "/x1/v1.0/",
"name": "test",
"api_version": "1.0",
"label": "test"
}
],
"appname": "gify2"
},
{
"label": "y1",
"branches": [
{
"url": "/y1/v1.0/",
"name": "test",
"api_version": "1.0",
"label": "test"
}
]}
<response>
你能帮帮我吗?
谢谢
答案 0 :(得分:1)
我不确定我是否真的了解这种情况,您似乎正在接收 XML 响应,该响应在节点中具有 JSON 属性......不是吗?
尽管如此,我理解的基本上是您要检查 JSON 中的所有endpoints
条目是否包含所有必需属性:label
,branch
, appname
;并且每个端点中的所有branches
都包含url
,name
,api_version
和label
。
所以可能的方法是使用JsonSlurper
并检查元素是否不是null
。类似的东西:
import groovy.json.JsonSlurper
def jsonTxt = '''{
"timestamp": "2016-04-14T17:53:29Z",
"id": "544cc493-8f4a-4f14-b95b-2c127f54caac",
"endpoints": [
{
"label": "Gify",
"branches": [
{
"url": "/gify/v1.0/",
"name": "test",
"api_version": "1.0",
"label": "test"
}
],
"appname": "gify"
},
{
"label": "x1",
"branches": [
{
"url": "/x1/v1.0/",
"name": "test",
"api_version": "1.0",
"label": "test"
}
],
"appname": "gify2"
}
]
}'''
// parse json
def json = new JsonSlurper().parseText(jsonTxt)
// for each endpoint
json.endpoints.each { endpoint ->
// check that label is not null
assert endpoint.label != null, 'ENDPOINT ENTRY NOT CONTAINS LABEL'
// check that appname is not null
assert endpoint.appname != null, 'ENDPOINT ENTRY NOT CONTAINS APPNAME'
// ...
assert endpoint.branches != null, 'ENDPOINT ENTRY NOT CONTAINS BRACHES'
// for each branch
assert endpoint.branches.each { branch ->
// and so on...
assert branch.url != null, 'BRANCH ENTRY NOT CONTAINS URL'
assert branch.name != null, 'BRANCH ENTRY NOT CONTAINS NAME'
assert branch.api_version != null, 'BRANCH ENTRY NOT CONTAINS API_VERSION'
assert branch.label != null, 'BRANCH ENTRY NOT CONTAINS LABEL'
}
}
<强>更新强>
不太可能 XML 和 XSD ,没有架构可以针对您的 Json 进行验证,但是您可以创建模板验证您对使用JsonSlurper
的回复。由于您只想检查元素的name
而不是其值,您可以创建一个函数来递归地比较name
:
import groovy.json.JsonSlurper
// compare json against the "schema"
def compareJsonNames(json,schema) {
if(json instanceof Map){
// it's a map... check all names
assert(json.keySet() == schema.keySet())
// for every element in a map...
json.eachWithIndex { it,index ->
def key = schema.keySet().getAt(index)
return compareJsonNames(it.value, schema.find{ e -> e.key == key}.value)
}
}else if(json instanceof List){
// it's a list, compare its elements
json.eachWithIndex { it, index ->
return compareJsonNames(it,schema[index])
}
}
// it's a simple value nothing to do
}
def jsonTxt = '''{
"timestamp": "2016-04-14T17:53:29Z",
"id": "544cc493-8f4a-4f14-b95b-2c127f54caac",
"endpoints": [
{
"label": "Gify",
"branches": [
{
"url": "/gify/v1.0/",
"name": "test",
"api_version": "1.0",
"label": "test"
}
],
"appname": "gify"
},
{
"label": "x1",
"branches": [
{
"url": "/x1/v1.0/",
"name": "test",
"api_version": "1.0",
"label": "test"
}
],
"appname": "gify2"
}
]
}'''
// template to validate the names
def template = '''{
"label": "",
"branches": [
{
"url": "",
"name": "",
"api_version": "",
"label": ""
}
],
"appname": ""
}'''
def slurper = new JsonSlurper()
// parse your response and the expected template
def json = slurper.parseText(jsonTxt)
def jsonTemplate = slurper.parseText(template)
// check if each endpoint are well formed against the template
json.endpoints.each {
compareJsonNames(it,jsonTemplate)
}
希望它有所帮助,