Skip to content

How to save generated ID , and use it for deletion

I’m testing API , using rest assured , programming language is JAVA, so my issue is , I’m trying to get generated ID from the response body , using this ID, trying to delete created product, but I’m having issues to save generated ID , for more information I have the following 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

I have the following Step Definition

 @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);


    }

I have following ouput

{
    "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

so the problem is , I’m creating private static id; in a class level , and getting this id’s value from the reponse body using

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

as u can see in the scenario i’m creating two same products for validation, first product is being created successfully , second product is not created, because they have the same dates, and it’s working as expected , but i need to delete first created product, and get the Id of this first product , but JAVA is getting Id from the second product , which is not created, so the question is how do i save the Id from first product which is successfully created , and using this Id delete the product. I hope I have described the issue good enough , if not pls comment bellow , thanks in advance

Answer

One simple solution on the top of my head is that instead of using private static String id, you can use private static List<String> ids = new ArraysList<>();

You will save each id to list of 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);
}

Then in the method that using it, you will get the id from ids

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