2016-04-14 8 views
0

私はいくつかのパラメータを含むsoapui応答を持っています。パラメータ名が繰り返され、異なる値を含むことがあります。 パラメータの存在をどのようにアサートすることができますか? ラベル、ブランチ、url、api-version、appnameが何回か繰り返されているので、タイムスタンプ、データ、プロファイル、完全、ID、エンドポイントをアサートできます。groovyを使用して値なしのパラメータ名をアサートする方法はありますか?

私はパラメータの値をアサートする方法を知っていますが、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

、それが役に立てば幸い?何を試しましたか?あなたは別個の値を集めて、キーと同じ番号を持っていることを確認できるはずです –

+0

私は試しましたが、def endpoint = context.expand( '$ {login#Endpoint}') def response = context.expand( '$ {ns1:login_resp [1]/ns1:item [1]/ns1:レスポンス[1]} ') def label1 endpoint.label [0] =( "label") assert endpoint.contains(label1) –

+0

これを質問に入れることはできますか?コードはコメントで読むことが不可能です –

答えて

1

をありがとう、それはあなたがノード属性にJSONを持ってXML応答を受信して​​いるようだ...それはないですか?私が理解これにもかかわらず

はあなたJSON内のすべてのendpointsエントリはすべての必須属性が含まれていることを確認したいという基本的である:labelbranchappnameを。各エンドポイント内のすべての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' 
    } 
} 

UPDATE

無理だろXMLXSD、あなたの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) 
} 

は、あなたがこれまでに何を持っています

+1

ニース、1つのコメント。たとえば:userは、 'appname'が自己完結していないときに、期待どおりにエラーメッセージを取得します。しかし、 'appname'の値が空であれば、同じアサーションエラーを出しています。' 'appname": "" 'は期待できないでしょう。 – Rao

+0

@Raoああ...あなたは正しい! ':)' ...私は比較のために 'null'を避けたいですが、私は空の文字列がfalseであることを考慮しません。私は自分の答えを更新する。ありがとう! – albciff

+0

喜んで、あなたはとても素早いです。 :) – Rao

関連する問題