繁体   English   中英

使用 Java 随机更改每个“发布”请求正文的 JSON 值

[英]Randomly changing the JSON Values for every "Post" Request Body using Java

这可能是一个重复的问题,但我在任何地方都找不到我的解决方案。 因此,发布它。

我想简单地发布一个学生帐户创建方案的请求。 我确实有一个 JSON 文件,其中包含创建学生帐户所需的所有“键:值”。

这是文件 student_Profile.json 的样子:

{
   "FirstName":"APi1-Stud-FN",
   "MiddleInitial":"Q",
   "LastName":"APi1-Stud-LN",
   "UserAlternateEmail":"",
   "SecretQuestionId":12,
   "SecretQuestionAnswer":"Scot",
   "UserName":"APi1-stud@xyz.com",
   "VerifyUserName":"APi1-stud@xyz.com",
   "Password":"A123456",
   "VerifyPassword":"A123456",
   "YKey":"123xyz",
   "YId":6,
   "Status":false,
   "KeyCode":"",
   "SsoUserName":"APi1-stud@xyz.com",
   "SsoPassword":"",
   "BirthYear":2001
}

因此,从“放心”的角度发布请求的所有内容看起来都很好,只是我想使用JAVA更新上述 JSON 正文中的一些值,以便我每次运行我的函数时都可以创建一个新的学生配置文件和不必手动更改 Body。

对于每个 POST 学生帐户创建方案,我需要更新以下键的值,以便可以创建新的测试学生用户帐户:

  1. 姓氏和
  2. 用户名 // "VerifyUserName" 和 "SSO UserName" 将保持与用户名相同

我修改了答案以获取随机值并将它们传递给 json body。 随机值生成取自这个问题的公认答案。

public void testMethod() {

    List<String> randomValueList = new ArrayList<>();

    for (int i = 0; i < 3; i++) {
        String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuilder salt = new StringBuilder();
        Random rnd = new Random();
        while (salt.length() < 18) { // length of the random string.
            int index = (int) (rnd.nextFloat() * SALTCHARS.length());
            salt.append(SALTCHARS.charAt(index));
        }
        randomValueList.add(salt.toString());
    }

    String jsonBody = "{\n" +
            "   \"FirstName\":\"" + randomValueList.remove(0) + "\",\n" +
            "   \"MiddleInitial\":\"Q\",\n" +
            "   \"LastName\":\"" + randomValueList.remove(0) + "\",\n" +
            "   \"UserAlternateEmail\":\"\",\n" +
            "   \"SecretQuestionId\":12,\n" +
            "   \"SecretQuestionAnswer\":\"Scot\",\n" +
            "   \"UserName\":\"" + randomValueList.remove(0) + " \",\n" +
            "   \"VerifyUserName\":\"APi1-stud@xyz.com\",\n" +
            "   \"Password\":\"A123456\",\n" +
            "   \"VerifyPassword\":\"A123456\",\n" +
            "   \"YKey\":\"123xyz\",\n" +
            "   \"YId\":6,\n" +
            "   \"Status\":false,\n" +
            "   \"KeyCode\":\"\",\n" +
            "   \"SsoUserName\":\"APi1-stud@xyz.com\",\n" +
            "   \"SsoPassword\":\"\",\n" +
            "   \"BirthYear\":2001\n" +
            "}";

    Response response = RestAssured
            .given()
            .body(jsonBody)
            .when()
            .post("api_url")
            .then()
            .extract()
            .response();

    // Do what you need to do with the response body
}

我们可以使用基于 pojo 的方法很容易地做某些事情。 无论有效载荷有多复杂,序列化和柴油化都是最好的答案。 我为 api 自动化创建了一个框架模板,我们可以通过将所需的 POJO 放在 path 中来使用它:

https://github.com/tanuj-vishnoi/pojo_api_automation

为了创造 pojo,我也为你准备了食物:

https://github.com/tanuj-vishnoi/pojo_generator_using_jsonschema2pojo

对于上述问题,您可以参考 JsonPath lib https://github.com/json-path/JsonPath并使用以下代码:

String mypayload = "{\n" + 
                "   \"FirstName\":\"APi1-Stud-FN\",\n" + 
                "   \"MiddleInitial\":\"Q\",\n" + 
                "   \"LastName\":\"APi1-Stud-LN\"}";
    Map map = JsonPath.parse(mypayload).read("$",Map.class);
    System.out.println(list);

一旦有效负载转换为地图,您就可以根据要求仅更改所需的值

要生成随机字符串,您可以参考 lib org.apache.commons.lang3.RandomStringUtils;

public static String generateUniqueString(int lenghtOfString){
         return 
 RandomStringUtils.randomAlphabetic(lenghtOfString).toLowerCase();
 }

我建议将有效负载存储在一个单独的文件中并在运行时加载它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM