2017-03-22 4 views
3

RubyからJavaに移行するには、複数のアイテムレスポンスから1つのフィールドをランダムに解析して取得する必要があります。複数のアイテムを持つ安心保証Jsonレスポンスからランダムフィールドを取得

これは私が私の応答を取得するために使用私のApiCall方法である:

 Response response = given(). 
      headers(this.headers). 
      params(this.body). 
     when(). 
      post(this.url). 
     then(). 
      contentType(ContentType.JSON). 
      statusCode(200). 
     extract(). 
      response(); 

そして、それは私にこのJSON構造を与える:

{ 
    "Contents": { 
    "Title": "Search results", 
    "Count": "10", 
    "Page": "1", 
    "TotalCount": "1", 
    "TotalPages": 2, 
    "Genres": [ 
     "Genre_1", 
     "Genre_2", 
     "Genre_3" 
    ], 
    "Contents": [ 
     { 
     "title": "content1_title", 
     "original_text": "original text 1", 
     "full_text": "Sample full sized text 1", 
     "short_text": "Sample short text 1", 
     "children_ids": { 
      "item": [ 
      1, 
      2 
      ] 
     }, 
     "children_uuids": { 
      "item": [ 
      "item1_uuid", 
      "item2_uuid" 
      ] 
     }, 
     "parent_ids": { 
      "item": [ 
      1 
      ] 
     }, 
     "parent_uuids": { 
      "item": [ 
      "item1_uuid" 
      ] 
     }, 
     "aired_from": "1994-01-01", 
     "aired_to": "1994-12-31", 
     "tags": { 
      "item": [ 
      "" 
      ] 
     }, 
     "available_condition1": 0, 
     "available_condition2": 1, 
     "price_condition1": "0.00", 
     "price_condition2": "13.00" 
     }, 
     { 
     "title": "content2_title", 
     "original_text": "original text 2", 
     "full_text": "Sample full sized text 2", 
     "short_text": "Sample short text 2", 
     "children_ids": { 
      "item": [ 
      1, 
      2 
      ] 
     }, 
     "children_uuids": { 
      "item": [ 
      "item1_uuid", 
      "item2_uuid" 
      ] 
     }, 
     "parent_ids": { 
      "item": [ 
      1 
      ] 
     }, 
     "parent_uuids": { 
      "item": [ 
      "item1_uuid" 
      ] 
     }, 
     "aired_from": "1998-01-01", 
     "aired_to": "1998-01-31", 
     "tags": { 
      "item": [ 
      "" 
      ] 
     }, 
     "available_condition1": 0, 
     "available_condition2": 1, 
     "price_condition1": "0.00", 
     "price_condition2": "13.00" 
     } 
    ] 
    }, 
    "Success": true 
} 

事は「私はランダムに取得する必要があるということですレスポンスに含まれるものの合計数から少なくとも1つの "children_uuids"を持つ "title"フィールドがあります。

ので、私は理解して、手順は次のとおりです。

1)「Contents.Contents」の項目の合計サイズを取得します。 2)0とアイテムの合計数の間の乱数を取得します。 3)その番号を使用して、「Contents.Contents。[n] .title」形式またはそれに類似する項目を1つ選択します。私に次のエラー与え

 JsonPath jsonPath = new JsonPath(response.toString()); 
     List<?> list = jsonPath.getList("Contents.Contents.title.flatten()"); 
     System.out.println(list); 

はありません成功し、次の試してみました参考

io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document 

を、Rubyでのコードは次のようになります。

  amount = @result['api_response']['Contents']['Contents'].count 
      amount = amount - 1 
      a = rand(0..amount) 
      while @result['api_response']['Contents']['Contents'][a]['children_uuids']['item'].nil? do 
       a = rand(0..amount) 
       #children_uuids = $result['api_response']['Contents']['Contents'][a]['children_uuids'] 
      end 
      #price_hd = @result['api_response']['Contents']['Contents'][a]['price_hd'] 
      content_title = @result['api_response']['Contents']['Contents'][a]['title'] 

UPDATE :私はそれが部分的に働いている...私はこの行でリストから1つの項目を選択する方法を見つけました:

String contentTitle = response.then().extract().path("Contents.Contents[0].title"); 

しかし、二行目は私を与えることは、このjsonpath

String contentTitle = response.then().extract().path("Contents.Contents.[?(@.children_uuids)].uuid"); 

を使用する方法を見つけることができません。事前に

java.lang.IllegalArgumentException: 
Invalid JSON expression: 
Script1.groovy: 1: unexpected token: [ @ line 1, column 45. 
    nRootObject.Contents.Contents.[?(@.child 
           ^

感謝を。

答えて

0

UPDATE:私は、次のと解決策を見つけたがコード:

  // get random content data 
      Integer contentAmmount = response.body().path("Contents.Contents.size()"); 
      Random randomGenerator = new Random(); 
      int randomInt = randomGenerator.nextInt(contentAmmount); 
      while (response.body().path(
        String.join("", "Contents.Contents[", Integer.toString(randomInt), 
          "].children_uuids.item")).toString().isEmpty()) { 
       randomInt = randomGenerator.nextInt(contentAmmount); 
      } 

      String contentTitle = response.then().extract() 
        .path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].title")); 
      String contentUuid = response.then().extract() 
        .path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].uuid")); 
      String contentChildrenUuid = response.body().path(
        String.join("", "Contents.Contents[", Integer.toString(randomInt), 
          "].children_uuids.item")); 
0

私の意見では、これはRESTで保証するのが非常に難しいことです。

Karate(免責事項:am dev)をご覧ください。だからあなたは、乱数を生成し、変数にそれを割り当て、正確に何をしたい行うには、パス式を形成するために、JavaScript関数を呼び出すことができます。

Feature: 

Scenario: 

* def data = read('data.json') 
* def size = (data.Contents.Contents.length) 
* def index = Math.floor(Math.random() * size) 
* print 'selected index: ' + index 
* def item = (data.Contents.Contents[index]) 
* def children_uuids = item.children_uuids 
* match children_uuids == { item: ['item1_uuid', 'item2_uuid'] } 
関連する問題