2016-04-14 73 views
0

我有一个soapui的响应,其中包含几个参数有时参数名称重复并包含不同的值。 我们如何断言参数的存在? 我可以断言时间戳,数据,配置文件,完整,id,端点,因为它们是唯一的,但标签,分支,url,api-version,appname会重复多次。如何使用groovy断言参数名称没有值?

我知道如何断言一个参数的值,但我不知道如何断言标签,分支,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

你有什么这么远吗?你有什么尝试?你应该能够收集不同的值,并检查你有相同数量的密钥 –

+0

我试过但def endpoint = context.expand('$ {login#Endpoint}') def response = context.expand('$ {login#Response#declare namespace ns1 = \'https://au.io/ns/201 \'; // ns1:login_resp [1]/ns1:item [1]/ns1:response [1]}') def label1 endpoint.label [0] =(“label”) assert endpoint.contains(label1) –

+0

你可以把它放在问题中吗?代码是不可能的在评论 –

回答

1

我不知道,我真的很了解情况,似乎正在接收具有JSON中的一个节点属性的XML 响应 ...是不是?

尽管这样我的理解基本上是要检查您JSON所有endpoints条目包含了所有必需的属性:labelbranchappname;并且在每个端点中的所有branches都包含url,name,api_versionlabel

所以一种可能的方法是使用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' 
    } 
} 

UPDATE

不大可能XMLXSD,没有架构来验证您​​的的Json,但是你可以创建一个模板来验证你的回应反对使用JsonSlurper。由于您只想来检查name S中的元素不是它的值,你可以创建一个函数来递归比较name S:

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) 
} 

希望它能帮助,

+1

不错,只有一条评论。例如:当'appname'它自己丢失时,用户会收到错误信息。然而,当'appname'的值为空时,它会给出相同的断言错误,比如''appname“:”“',这可能不是我预期的。 – Rao

+0

@Rao ouch ...你是对的! ':)'...我想避免比较'null',但我不考虑空字符串是错误的。我更新我的答案。谢谢! – albciff

+0

很高兴,你很快。 :) – Rao