从API提取数据,并将其用作有效载荷以进行POST调用

时间:2019-07-11 11:21:26

标签: java json rest-assured web-api-testing rest-assured-jsonpath

我有需要的这种测试情况

  • 从API获取详细信息,
  • 将其写入文件
  • 对JSON数组进行更新并更新文件
  • 使用新文件作为有效负载。

我已经可以完成前两个步骤,但是由于我还没有使用Java中的JSON进行操作,因此无法继续进行下一步。

第1步的API结果:

{"id":123,"type":"employee","employee_id":"5162231","full_name":"Mark Hamil","email_id":"mark_ham@ham.test","created_at":"2014-03-26","updated_at":"2015-04-06","empno":9122,"branches":[{"name":"Assets","asset_id":"56","attr_typ":"Assets","asset_path":"12-34-56789","is_pr":false,"is_lead":false},{"name":"Retail Banking","asset_id":"8","attr_typ":"RB","asset_path":"8-12-11221","is_pr":false,"is_lead":true},{"name":"Corporate Banking","asset_id":"94","attr_typ":"CB","asset_path":"94-122-22123","is_pr":false,"is_lead":true},{"name":"Rural Banking","asset_id":"118","attr_typ":"RuB","asset_path":"97-1188","is_pr":false,"is_lead":true}],"email_freq":{"id":21,"immediately":"N","daily":"N","weekly":"N","monthly":"N","ind_id":1136},"responsible_for":[{"region":"AS","office_code":"TH","office_loc":"BNK","name":"Asia - Thailand - Bangkok"}]}

进行更新:

需要使用responsible_for键为负责的办公室添加新的json对象。 responsible_key的更新值必须类似于

"responsible_for":[{"region":"AS","office_code":"TH","office_loc":"BNK","name":"Asia - Thailand - Bangkok"},{"region":"AS","office_code":"ML","office_loc":"KLP","name":"Asia - Malaysia - Kualalumpur"}]}

到目前为止的代码

我制作了一种可以一次执行所有四个步骤的方法-

     public static void change_person_details_json()throws IOException {

     // get the person details after appending the id with base path/person          

 HelperRest.hitGetRequest(ConfigReader.getBaseUrl()+"person/"+HelperRest.get_person_id());
                 System.out.println(HelperRest.getResponse().body().asString());
                 HelperRest.logEverything();

    //write the API response to a json file
                 try(FileWriter writer = new FileWriter(ConfigReader.getRequestPayloadFilePath()+"update-json.json")){
                     writer.write(HelperRest.getResponse().body().asString());
                     writer.flush();
                 } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

1 个答案:

答案 0 :(得分:1)

我可以为您提供第三点帮助。将元素添加到JSON。

我使用了org.json

        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>

您可以将响应保存到.json文件,但是不必这样做。您可以使用给定的库将JSON作为String进行操作。因此,不要这样做:

//write the API response to a json file
                 try(FileWriter writer = new FileWriter(ConfigReader.getRequestPayloadFilePath()+"update-json.json")){
                     writer.write(HelperRest.getResponse().body().asString());
                     writer.flush();
                 } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

您可以使用

String jsonResponse = HelperRest.getResponse().body().asString();

JSONObject root = new JSONObject(jsonResponse);
JSONArray responsibleForArray = root.getJSONArray("responsible_for");

HashMap<String, String> newResponsibleForMap = new HashMap<>();
newResponsibleForMap.put("region", "AS");
newResponsibleForMap.put("office_code", "ML");
newResponsibleForMap.put("office_loc", "KLP");
newResponsibleForMap.put("name", "Asia - Malaysia - Kualalumpur");

responsibleForArray.put(newResponsibleForMap);

System.out.println(root);

希望它会有所帮助!