, bu size bir düğüm özelliğinde bir JSON olan bir XML yanıtını aldığınızı görünüyor size değil ederiz? Buna rağmen
benim anlamadığım size JSON tüm endpoints
girişleri tüm zorunlu özelliklerini içerdiğini kontrol etmek istiyorum temelde: label
, branch
, appname
; ve her son noktadaki tüm branches
, url
, name
, api_version
ve label
içerir.
Olası bir yaklaşım JsonSlurper
kullanmak ve öğenin null
olup olmadığını kontrol etmektir. Bir şey gibi:
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'
}
}
GÜNCELLEME
Muhtemel XML ve XSD, sizin Json karşı doğrulamak için hiçbir şema yoktur, ancak yanıtınızı doğrulamak için şablonu oluşturabilir JsonSlurper
kullanımına karşı. Yalnızca elemanların name
s için kontrol etmek istediğiniz beri değil, değerler yinelemeli name
s karşılaştırmak için bir işlevi oluşturabilirsiniz:
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)
}
Umarım yardımcı olur,
Ne kadar var? Ne denedin? Ayrı değerleri toplayabilmeniz ve aynı sayıda anahtarın –
anahtarını kullanıp kullanmadığınızı kontrol etmelisiniz, ancak son nokta = context.expand ('$ {login # Endpoint}') def yanıtı = context.expand ('$ {login # Response # declare namespace ns1 = \ 'https: //au.io/ns/201 \'; // ns1: login_resp [1]/ns1: öğe [1]/ns1: yanıt [1]} ') def etiketi1 endpoint.label [0] = ("etiket") assert endpoint.contains (label1) –
Bunu soruya koyabilir misiniz? –