如何保存生成的 ID ,并将其用于删除

How to save generated ID , and use it for deletion

我正在测试 API,请放心,编程语言是 JAVA,所以我的问题是,我正在尝试使用此 ID 从响应正文中获取生成的 ID,试图删除创建的产品,但我在保存生成的 ID 时遇到问题,有关详细信息,我有以下 Scenario

 @Validation
  Scenario: verifying the validation of productWorkingDates
    Given productWorkingDates is created with the following fields
      | productId | fromDate   | toDate     | name       | strictHours | maxUsedTicketsQuantity | errorCode |
      | ZOw7WVOx  | 2021-09-02 | 2022-11-02 | Validation | true        | 0                      | 0         |
    And timeSlots is created with the following fields
      | dayOfWeek | startTime | endTime  | duration | quantity | usedQuantity | active |
      | Monday    | 14:00:00  | 15:00:00 | 02:00:00 | 0        | 0            | true   |
      | Tuesday   | 14:00:00  | 15:00:00 | 02:00:00 | 0        | 0            | true   |
    Then verify status code is 200
    When productWorkingDates is created with the following fields
      | productId | fromDate   | toDate     | name       | strictHours | maxUsedTicketsQuantity | errorCode |
      | ZOw7WVOx  | 2021-09-02 | 2022-11-02 | Validation | true        | 0                      | 0         |
    And timeSlots is created with the following fields
      | dayOfWeek | startTime | endTime  | duration | quantity | usedQuantity | active |
      | Monday    | 14:00:00  | 15:00:00 | 02:00:00 | 0        | 0            | true   |
      | Tuesday   | 14:00:00  | 15:00:00 | 02:00:00 | 0        | 0            | true   |
    And verify that error is "Calendar already exist for this date"

    @Validation
    Scenario: delete created productWorkingDates
    And delete productWorkingDate
    Then verify status code is 200

我有以下步骤定义

 @Given("^productWorkingDates is created with the following fields$")
    public void productWorkingDatesIsCreatedWithTheFollowingFields(List<Map<String, String>> productWorkingDates) {

      
        productWorkingDate.setProductId(productWorkingDates.get(0).get("productId"));
        productWorkingDate.setFromDate(productWorkingDates.get(0).get("fromDate"));
        productWorkingDate.setToDate(productWorkingDates.get(0).get("toDate"));
        productWorkingDate.setName(productWorkingDates.get(0).get("name"));
        productWorkingDate.setStrictHours(Boolean.parseBoolean(productWorkingDates.get(0).get("strictHours")));
        productWorkingDate.setMaxUsedTicketsQuantity(Integer.parseInt(productWorkingDates.get(0).get("maxUsedTicketsQuantity")));
        productWorkingDate.setErrorCode(Integer.parseInt(productWorkingDates.get(0).get("errorCode")));


        root.setProductWorkingDate(productWorkingDate);


    }

    @And("^timeSlots is created with the following fields$")
    public void timeslotsIsCreatedWithTheFollowingFields(List<Map<String, String>> expectedTimeSlots) {

        List<TimeSlots> listTimeSlots = new ArrayList<>();

        for (Map<String, String> expectedTimeSlot : expectedTimeSlots) {
            TimeSlots timeSlots = new TimeSlots();
            timeSlots.setDayOfWeek(expectedTimeSlot.get("dayOfWeek"));
            timeSlots.setStartTime(expectedTimeSlot.get("startTime"));
            timeSlots.setEndTime((expectedTimeSlot.get("endTime")));
            timeSlots.setDuration(expectedTimeSlot.get("duration"));
            timeSlots.setQuantity(Integer.parseInt(expectedTimeSlot.get("quantity")));
            timeSlots.setUsedQuantity(Integer.parseInt(expectedTimeSlot.get("usedQuantity")));
            timeSlots.setActive(Boolean.parseBoolean(expectedTimeSlot.get("active")));

            listTimeSlots.add(timeSlots);
        }

        productWorkingDate.setTimeSlots(listTimeSlots);


        root.setProductWorkingDate(productWorkingDate);

        String newProd = ObjectConverter.convertObjectToJson(root);
        commonData.response = NewProductEndPoints.createProductWorkingDates(newProd, cookies);

        id = commonData.response.jsonPath().get("productWorkingDate.id");


    }



    @And("^delete productWorkingDate$")
    public  void deleteProductWorkingDate() {


      commonData.response = NewProductEndPoints.deleteProductWorkingDates(id);


    }

我有以下输出

{
    "productWorkingDate": {
        "id": "xl4W4jaj",
        "productId": "ZOw7WVOx",
        "fromDate": "2021-09-02",
        "toDate": "2022-11-02",
        "name": "Validation",
        "strictHours": true,
        "timeSlots": [
            {
                "id": "7OxWz2l6",
                "productWorkingDateId": "xl4W4jaj",
                "dayOfWeek": "Monday",
                "startTime": "14:00:00",
                "endTime": "15:00:00",
                "duration": "02:00:00",
                "quantity": 0,
                "usedQuantity": 0,
                "active": true,
                "deletedAt": null
            },
            {
                "id": "dl2rYVlX",
                "productWorkingDateId": "xl4W4jaj",
                "dayOfWeek": "Tuesday",
                "startTime": "14:00:00",
                "endTime": "15:00:00",
                "duration": "02:00:00",
                "quantity": 0,
                "usedQuantity": 0,
                "active": true,
                "deletedAt": null
            }
        ],
        "deletedAt": null,
        "maxUsedTicketsQuantity": 0,
        "errorCode": 0
    },
    "maxUsedTicketsQuantity": 0,
    "error": null,
    "errorCode": 0
}



{
    "productWorkingDate": null,
    "maxUsedTicketsQuantity": 0,
    "error": "Calendar already exist for this date",
    "errorCode": 0
}
{
    "productWorkingDate": null,
    "maxUsedTicketsQuantity": 0,
    "error": "Calendar already exist for this date",
    "errorCode": 0
}
 


{
    "type": "https://httpstatuses.com/400",
    "title": "Bad Request",
    "status": 400,
    "detail": "ProductWorkingDate with Id: 0 not exist",
    "traceId": "00-b8488f6a33bf384999f0ea4ebae376c1-ac88b69265568942-00"
}
{
    "type": "https://httpstatuses.com/400",
    "title": "Bad Request",
    "status": 400,
    "detail": "ProductWorkingDate with Id: 0 not exist",
    "traceId": "00-b8488f6a33bf384999f0ea4ebae376c1-ac88b69265568942-00"
}

org.junit.ComparisonFailure: 
Expected :200
Actual   :400

所以问题是,我正在 private static id; class 关卡中创建,并从响应正文使用

 id = commonData.response.jsonPath().get("productWorkingDate.id");

正如您在场景中看到的那样,我正在创建两个相同的产品进行验证,第一个产品已成功创建,第二个产品未创建,因为它们具有相同的日期,并且按预期工作,但我需要删除第一个创建的产品,并获取第一个产品的 Id,但是 JAVA 正在从第二个未创建的产品获取 Id,所以问题是如何保存第一个产品的 Id成功创建,并使用此 Id 删除产品。我希望我已经足够好地描述了这个问题,如果没有请在下面发表评论,在此先感谢

我想到的一个简单的解决方案是,您可以使用 private static List<String> ids = new ArraysList<>();

而不是 private static String id

您将每个 id 保存到 ids

的列表中
@And("^timeSlots is created with the following fields$")
    public void timeslotsIsCreatedWithTheFollowingFields(List<Map<String, String>> expectedTimeSlots) {
    ...
    id = commonData.response.jsonPath().get("productWorkingDate.id");
    ids.add(id);
}

然后在使用它的方法中,你将从ids

得到id
@And("^delete productWorkingDate$")
public  void deleteProductWorkingDate() {
    commonData.response = NewProductEndPoints.deleteProductWorkingDates(ids.get(0));
}