2016-04-14 75 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

相關問題