javacucumberrest-assuredapi-authorization

Data table in cucumber sending nested Json


hopping for your help and assistance I'm trying to send nested JSON through cucumber data table , but it's not being sent as expected, I have tried Scenario Outline too, didn't resolve the issue , pls help me to resolve it Thanks in advance

I have the following scenario;

Scenario: provider edits new productWorkingDate
    Given productWorkingDates is edited with following fields
      | id       | productId | fromDate   | toDate     | name   | strictHours | maxUsedTicketsQuantity | errorCode |
      | bpvjPBpJ | WaNX2QOd  | 2022-07-01 | 2022-12-01 | Test55 | false       | 0                      | 0         |
    And TimeSlots is edited with following fields
      | dayOfWeek | startTime | endTime  | duration | quantity | usedQuantity | active |
      | Sunday    | 14:00:00  | 15:00:00 | 02:00:00 | 0        | 0            | true   |
      | Monday    | 14:00:00  | 15:00:00 | 02:00:00 | 0        | 0            | true   |
      
    Then verify status code is 200

and I have the following step definition

 @And("^TimeSlots is edited with following fields$")
    public void timeslotsIsCreatedWithFollowingFields(List<Map<String, String>> expectedTimeSlots) {
        TimeSlots timeSlots = new TimeSlots();



              for(int i = 0; i < expectedTimeSlots.size(); i ++) {
                  timeSlots.setDayOfWeek(expectedTimeSlots.get(i).get("dayOfWeek"));
                  timeSlots.setStartTime(expectedTimeSlots.get(i).get("startTime"));
                  timeSlots.setEndTime((expectedTimeSlots.get(i).get("endTime")));
                  timeSlots.setDuration(expectedTimeSlots.get(i).get("duration"));
                  timeSlots.setQuantity(Integer.parseInt(expectedTimeSlots.get(i).get("quantity")));
                  timeSlots.setUsedQuantity(Integer.parseInt(expectedTimeSlots.get(i).get("usedQuantity")));
                  timeSlots.setActive(Boolean.parseBoolean(expectedTimeSlots.get(i).get("active")));

              }

Actual output is :

{
    "productWorkingDate": {
        "id": "bpvjPBpJ",
        "productId": "WaNX2QOd",
        "fromDate": "2022-07-01",
        "toDate": "2022-12-01",
        "name": "Test55",
        "strictHours": false,
        "timeSlots": [
            {
                "id": "Wlqb8XOb",
                "productWorkingDateId": "bpvjPBpJ",
                "dayOfWeek": "Monday",
                "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
}

Expected output is :

{
    "productWorkingDate": {
        "id": "bpvjPBpJ",
        "productId": "WaNX2QOd",
        "fromDate": "2022-07-01",
        "toDate": "2022-12-01",
        "name": "Test55",
        "strictHours": false,
        "timeSlots": [
            {
                "id": "4lrn8old",
                "productWorkingDateId": "bpvjPBpJ",
                "dayOfWeek": "Sunday",
                "startTime": "14:00:00",
                "endTime": "15:00:00",
                "duration": "02:00:00",
                "quantity": 0,
                "usedQuantity": 0,
                "active": true,
                "deletedAt": null
            },
            {
                "id": "dOnz85OV",
                "productWorkingDateId": "bpvjPBpJ",
                "dayOfWeek": "Monday",
                "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
}

POJO class for TimeSlots

I use in my POJO classes lombok library;

import lombok.Data;

@Data

public class TimeSlots {
    private String id;
    private String productWorkingDateId;
    private String startTime;
    private String endTime;
    private String duration;
    private Integer quantity;
    private Integer usedQuantity;
    private boolean active;
    private String deletedAt;
    private String dayOfWeek;

Solution

  • You seem to have redacted most / some of the information that was needed for me to replicate the error, I just constructed a sample from what you have given and have managed to get the desired output

    You are creating an object for TimeSlots outside the for loop however it should be within the loop

    Feature File :

    Feature: STACK
    
    Scenario: provider edits new productWorkingDate
    Given productWorkingDates is edited with following fields
    | id       | productId | fromDate   | toDate     | name   | strictHours | maxUsedTicketsQuantity | errorCode |
    | bpvjPBpJ | WaNX2QOd  | 2022-07-01 | 2022-12-01 | Test55 | false       |                      0 |         0 |
    And TimeSlots is edited with following fields
    | dayOfWeek | startTime | endTime  | duration | quantity | usedQuantity | active | productWorkingDateId | id       |
    | Sunday    | 14:00:00  | 15:00:00 | 02:00:00 |        0 |            0 | true   | bpvjPBpJ             | 4lrn8old |
    | Monday    | 14:00:00  | 15:00:00 | 02:00:00 |        0 |            0 | true   | bpvjPBpJ             | dOnz85OV |
    

    Step Definition :

    ProductWorkingDate pw = new ProductWorkingDate();
    Example ex = new Example();
    
    @Given("productWorkingDates is edited with following fields")
    public void product_working_dates_is_edited_with_following_fields(io.cucumber.datatable.DataTable dataTable) {
    
        pw.setId("bpvjPBpJ");
        pw.setProductId("WaNX2QOd");
        pw.setFromDate("2022-07-01");
        pw.setToDate("2022-12-01");
        pw.setName("Test55");
        pw.setStrictHours(false);
    
    }
    
    @Given("TimeSlots is edited with following fields")
    public void time_slots_is_edited_with_following_fields(List<Map<String, String>> expectedTimeSlots)
            throws JsonProcessingException {
    
        pw.setMaxUsedTicketsQuantity(0);
        pw.setDeletedAt("Test");
        pw.setErrorCode(0);
    
        List<TimeSlots> listTimeSlots = new ArrayList<TimeSlots>();
    
        for (int i = 0; i < expectedTimeSlots.size(); i++) {
    
            TimeSlots timeSlots = new TimeSlots();
    
            timeSlots.setId(expectedTimeSlots.get(i).get("id"));
            timeSlots.setProductWorkingDateId(expectedTimeSlots.get(i).get("productWorkingDateId"));
            timeSlots.setDayOfWeek(expectedTimeSlots.get(i).get("dayOfWeek"));
            timeSlots.setStartTime(expectedTimeSlots.get(i).get("startTime"));
            timeSlots.setEndTime((expectedTimeSlots.get(i).get("endTime")));
            timeSlots.setDuration(expectedTimeSlots.get(i).get("duration"));
            timeSlots.setQuantity(Integer.parseInt(expectedTimeSlots.get(i).get("quantity")));
            timeSlots.setUsedQuantity(Integer.parseInt(expectedTimeSlots.get(i).get("usedQuantity")));
            timeSlots.setActive(Boolean.parseBoolean(expectedTimeSlots.get(i).get("active")));
    
            listTimeSlots.add(timeSlots);
    
        }
    
        pw.setTimeSlots(listTimeSlots);
        ex.setProductWorkingDate(pw);
        ex.setMaxUsedTicketsQuantity(0);
        ex.setError("test");
        ex.setErrorCode(0);
    
        RestAssured.given().body(ex).when().post("http://localhost:8080/stack")...
    }